using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Windows.Forms; using System.Xml.Serialization; namespace MOTINOVA_Motor_Factory_Set { public partial class ExportForm : Form { //参数按照字典类型存储 Dictionary Params1 = new Dictionary(); Dictionary Params2 = new Dictionary(); Dictionary MotorParam = new Dictionary(); Dictionary Otherinfo = new Dictionary(); //文件路径 string FilePath = "cfg\\"; public ExportForm() { InitializeComponent(); } private void ExportForm_Load(object sender, EventArgs e) { } private void SetDefault() { //参数1 Params1.Clear(); Params1.Add("启动模式", 2); Params1.Add("停机时间", 100); Params1.Add("限速值", 25); Params1.Add("下降速度", 3); Params1.Add("前飞", 14); Params1.Add("后飞", 9); Params1.Add("限流", 15); Params1.Add("温度预警", 95); Params1.Add("温度保护", 125); Params1.Add("无码表支持", 0x55); Params1.Add("轮胎周长", 219); Params1.Add("系列号", 0); Params1.Add("ECO增益", 100);//放大100倍 Params1.Add("ECO加速", 100); Params1.Add("NORM增益", 100); Params1.Add("NORM加速", 100); Params1.Add("SPORT增益", 100); Params1.Add("SPORT加速", 100); Params1.Add("TURBO增益", 100); Params1.Add("TURBO加速", 100); Params1.Add("SMART增益", 100); Params1.Add("SMART加速", 100); Params1.Add("车速级数", 1); Params1.Add("踏频启动", 2); Params1.Add("指拨模式", 0x55); Params1.Add("速度信号来源", 0x55); Params1.Add("周长微调", 0); Params1.Add("低压保护", 3100);//mV Params1.Add("推行限速", 60);//放大10倍 Params1.Add("推行转速", 135); //参数2 Params2.Clear(); Params2.Add("姿态传感器", 0x55); Params2.Add("俯仰角零偏", 0); Params2.Add("横滚角零偏", 0); Params2.Add("尾灯模式", 2); //马达参数 MotorParam.Clear(); MotorParam.Add("额定功率", 250); MotorParam.Add("额定转速", 1500); MotorParam.Add("定子电阻", 10); MotorParam.Add("定子Lq", 10); MotorParam.Add("定子Ld", 10); MotorParam.Add("反电动势", 36000); MotorParam.Add("额定电压", 36); //其它信息 Otherinfo.Clear(); Otherinfo.Add("生产商", "TTIUM"); Otherinfo.Add("生产地", "WUHAN"); Otherinfo.Add("生产日期", "20200901"); Otherinfo.Add("校验密钥", "AVONITOM"); Otherinfo.Add("信息1", ""); Otherinfo.Add("信息2", ""); Otherinfo.Add("信息3", ""); } private void ParamsDisplayUpdate() { //参数1 if (Params1["启动模式"] == 1) { comboBox_StartMode.Text = "柔和"; } else if (Params1["启动模式"] == 2) { comboBox_StartMode.Text = "正常"; } else if (Params1["启动模式"] == 3) { comboBox_StartMode.Text = "强劲"; } textBox_StopTime.Text = Params1["停机时间"].ToString(); textBox_SpeedLimit.Text = Params1["限速值"].ToString(); textBox_Dec.Text = Params1["下降速度"].ToString(); textBox_T_Front.Text = Params1["前飞"].ToString(); textBox_T_Tail.Text = Params1["后飞"].ToString(); textBox_MaxCurrent.Text = Params1["限流"].ToString(); textBox_NTC_Alarm.Text = Params1["温度预警"].ToString(); textBox_NTC_Ptotect.Text = Params1["温度保护"].ToString(); if (Params1["无码表支持"] == 0x55) { comboBox_NoPBU.Text = "不支持"; } else if (Params1["无码表支持"] == 0xAA) { comboBox_NoPBU.Text = "支持"; } textBox_WheelSize.Text = Params1["轮胎周长"].ToString(); textBox_Serial.Text = Params1["系列号"].ToString(); textBox_ECO_Gain.Text = ((float)(Params1["ECO增益"]) / 100.0f).ToString("#0.00"); textBox_ECO_Acc.Text = ((float)(Params1["ECO加速"]) / 100.0f).ToString("#0.00"); textBox_NORM_Gain.Text = ((float)(Params1["NORM增益"]) / 100.0f).ToString("#0.00"); textBox_NORM_Acc.Text = ((float)(Params1["NORM加速"]) / 100.0f).ToString("#0.00"); textBox_SPORT_Gain.Text = ((float)(Params1["SPORT增益"]) / 100.0f).ToString("#0.00"); textBox_SPORT_Acc.Text = ((float)(Params1["SPORT加速"]) / 100.0f).ToString("#0.00"); textBox_TURBO_Gain.Text = ((float)(Params1["TURBO增益"]) / 100.0f).ToString("#0.00"); textBox_TURBO_Acc.Text = ((float)(Params1["TURBO加速"]) / 100.0f).ToString("#0.00"); textBox_SMART_Gain.Text = ((float)(Params1["SMART增益"]) / 100.0f).ToString("#0.00"); textBox_SMART_Acc.Text = ((float)(Params1["SMART加速"]) / 100.0f).ToString("#0.00"); textBox_SpeedSensor.Text = Params1["车速级数"].ToString(); textBox_CadenceStart.Text = Params1["踏频启动"].ToString(); if (Params1["指拨模式"] == 0x55) { comboBox_GasMode.Text = "不支持"; } else if (Params1["指拨模式"] == 0xAA) { comboBox_GasMode.Text = "支持"; } if (Params1["速度信号来源"] == 0x55) { comboBox_SpeedSensor.Text = "传感器"; } else if (Params1["速度信号来源"] == 0xAA) { comboBox_SpeedSensor.Text = "踏频"; } else if (Params1["速度信号来源"] == 0xEE) { comboBox_SpeedSensor.Text = "通信"; } textBox_WheelSizeAdj.Text = Params1["周长微调"].ToString(); textBox_UV_Protect.Text = Params1["低压保护"].ToString(); textBox_Walk_SpeedLimit.Text = ((float)(Params1["推行限速"]) / 10.0f).ToString("#0.0"); textBox_WalkSpeed.Text = Params1["推行转速"].ToString(); //参数2 if (Params2["姿态传感器"] == 0x55) { comboBox_AstSensor.Text = "不支持"; } else if (Params1["姿态传感器"] == 0xAA) { comboBox_AstSensor.Text = "支持"; } textBox_Angle_P.Text = ((float)(Params2["俯仰角零偏"]) / 10.0f).ToString("#0.0"); textBox_Angle_R.Text = ((float)(Params2["横滚角零偏"]) / 10.0f).ToString("#0.0"); if (Params2["尾灯模式"] == 1) { comboBox_LightMode.Text = "模式 1"; } else if (Params2["尾灯模式"] == 2) { comboBox_LightMode.Text = "模式 2"; } else if (Params2["尾灯模式"] == 3) { comboBox_LightMode.Text = "模式 3"; } else if (Params2["尾灯模式"] == 4) { comboBox_LightMode.Text = "模式 4"; } //马达参数 textBox_RatePower.Text = MotorParam["额定功率"].ToString(); textBox_RateSpeed.Text = MotorParam["额定转速"].ToString(); textBox_RotorR.Text = MotorParam["定子电阻"].ToString(); textBox_Rotor_Lq.Text = MotorParam["定子Lq"].ToString(); textBox_Rotor_Ld.Text = MotorParam["定子Ld"].ToString(); textBox_E.Text = MotorParam["反电动势"].ToString(); textBox_Rate_Vol.Text = MotorParam["额定电压"].ToString(); //其它信息 textBox_Mac.Text = Otherinfo["生产商"].ToString(); textBox_Mac_Addr.Text = Otherinfo["生产地"].ToString(); textBox_Mac_Date.Text = Otherinfo["生产日期"].ToString(); textBox_Secrect.Text = Otherinfo["校验密钥"].ToString(); textBox_Info1.Text = Otherinfo["信息1"].ToString(); textBox_Info2.Text = Otherinfo["信息2"].ToString(); textBox_Info3.Text = Otherinfo["信息3"].ToString(); } private bool DisplaySetToParams() { try { //参数1 Params1.Clear(); if (comboBox_StartMode.Text == "柔和") { Params1.Add("启动模式", 1); } else if (comboBox_StartMode.Text == "正常") { Params1.Add("启动模式", 2); } else if (comboBox_StartMode.Text == "强劲") { Params1.Add("启动模式", 3); } else { MessageBox.Show("请检查启动模式设置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } Params1.Add("停机时间", Convert.ToUInt16(textBox_StopTime.Text)); Params1.Add("限速值", Convert.ToUInt16(textBox_SpeedLimit.Text)); Params1.Add("下降速度", Convert.ToUInt16(textBox_Dec.Text)); Params1.Add("前飞", Convert.ToUInt16(textBox_T_Front.Text)); Params1.Add("后飞", Convert.ToUInt16(textBox_T_Tail.Text)); Params1.Add("限流", Convert.ToUInt16(textBox_MaxCurrent.Text)); Params1.Add("温度预警", Convert.ToUInt16(textBox_NTC_Alarm.Text)); Params1.Add("温度保护", Convert.ToUInt16(textBox_NTC_Ptotect.Text)); if (comboBox_NoPBU.Text == "不支持") { Params1.Add("无码表支持", 0x55); } else if (comboBox_NoPBU.Text == "支持") { Params1.Add("无码表支持", 0xAA); } else { MessageBox.Show("请检查无码表设置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } Params1.Add("轮胎周长", Convert.ToUInt16(textBox_WheelSize.Text)); Params1.Add("系列号", Convert.ToUInt16(textBox_Serial.Text)); Params1.Add("ECO增益", Convert.ToUInt16(float.Parse(textBox_ECO_Gain.Text) * 100));//放大100倍 Params1.Add("ECO加速", Convert.ToUInt16(float.Parse(textBox_ECO_Acc.Text) * 100)); Params1.Add("NORM增益", Convert.ToUInt16(float.Parse(textBox_NORM_Gain.Text) * 100)); Params1.Add("NORM加速", Convert.ToUInt16(float.Parse(textBox_NORM_Acc.Text) * 100)); Params1.Add("SPORT增益", Convert.ToUInt16(float.Parse(textBox_SPORT_Gain.Text) * 100)); Params1.Add("SPORT加速", Convert.ToUInt16(float.Parse(textBox_SPORT_Acc.Text) * 100)); Params1.Add("TURBO增益", Convert.ToUInt16(float.Parse(textBox_TURBO_Gain.Text) * 100)); Params1.Add("TURBO加速", Convert.ToUInt16(float.Parse(textBox_TURBO_Acc.Text) * 100)); Params1.Add("SMART增益", Convert.ToUInt16(float.Parse(textBox_SMART_Gain.Text) * 100)); Params1.Add("SMART加速", Convert.ToUInt16(float.Parse(textBox_SMART_Acc.Text) * 100)); Params1.Add("车速级数", Convert.ToUInt16(textBox_SpeedSensor.Text)); Params1.Add("踏频启动", Convert.ToUInt16(textBox_CadenceStart.Text)); if (comboBox_GasMode.Text == "不支持") { Params1.Add("指拨模式", 0x55); } else if (comboBox_GasMode.Text == "支持") { Params1.Add("指拨模式", 0xAA); } else { MessageBox.Show("请检查指拨模式设置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } if (comboBox_SpeedSensor.Text == "传感器") { Params1.Add("速度信号来源", 0x55); } else if (comboBox_GasMode.Text == "踏频") { Params1.Add("速度信号来源", 0xAA); } else if (comboBox_GasMode.Text == "通信") { Params1.Add("速度信号来源", 0xEE); } else { MessageBox.Show("请检查速度信号来源设置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } Params1.Add("周长微调", Convert.ToUInt16(textBox_WheelSizeAdj.Text)); Params1.Add("低压保护", Convert.ToUInt16(textBox_UV_Protect.Text));//mV Params1.Add("推行限速", Convert.ToUInt16(float.Parse(textBox_Walk_SpeedLimit.Text) * 10));//放大10倍 Params1.Add("推行转速", Convert.ToUInt16(textBox_WalkSpeed.Text)); //参数2 Params2.Clear(); if (comboBox_AstSensor.Text == "不支持") { Params2.Add("姿态传感器", 0x55); } else if (comboBox_AstSensor.Text == "支持") { Params2.Add("姿态传感器", 0xAA); } else { MessageBox.Show("请检查姿态传感器设置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } Params2.Add("俯仰角零偏", Convert.ToUInt16(float.Parse(textBox_Angle_P.Text) * 10)); Params2.Add("横滚角零偏", Convert.ToUInt16(float.Parse(textBox_Angle_R.Text) * 10)); if (comboBox_LightMode.Text == "模式 1") { Params2.Add("尾灯模式", 1); } else if(comboBox_LightMode.Text == "模式 2") { Params2.Add("尾灯模式", 2); } else if (comboBox_LightMode.Text == "模式 3") { Params2.Add("尾灯模式", 3); } else if (comboBox_LightMode.Text == "模式 4") { Params2.Add("尾灯模式", 4); } //马达参数 MotorParam.Clear(); MotorParam.Add("额定功率", Convert.ToUInt16(textBox_RatePower.Text)); MotorParam.Add("额定转速", Convert.ToUInt16(textBox_RateSpeed.Text)); MotorParam.Add("定子电阻", Convert.ToUInt16(textBox_RotorR.Text)); MotorParam.Add("定子Lq", Convert.ToUInt16(textBox_Rotor_Lq.Text)); MotorParam.Add("定子Ld", Convert.ToUInt16(textBox_Rotor_Ld.Text)); MotorParam.Add("反电动势", Convert.ToUInt16(textBox_E.Text)); MotorParam.Add("额定电压", Convert.ToUInt16(textBox_Rate_Vol.Text)); //其它信息 Otherinfo.Clear(); Otherinfo.Add("生产商", textBox_Mac.Text); Otherinfo.Add("生产地", textBox_Mac_Addr.Text); Otherinfo.Add("生产日期", textBox_Mac_Date.Text); Otherinfo.Add("校验密钥", textBox_Secrect.Text); Otherinfo.Add("信息1", textBox_Info1.Text); Otherinfo.Add("信息2", textBox_Info2.Text); Otherinfo.Add("信息3", textBox_Info3.Text); return true; } catch(Exception ex) { MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } } private void button_Default_Click(object sender, EventArgs e) { SetDefault(); ParamsDisplayUpdate(); } private void button_Generate_Click(object sender, EventArgs e) { //空间内容更新到字典 if (DisplaySetToParams() == false) { MessageBox.Show("参数生成失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //保存为文件 if (textBox_Model.Text == string.Empty) { MessageBox.Show("请输入电机型号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (textBox_Client.Text == string.Empty) { textBox_Client.Text = "STD"; } if (System.IO.Directory.Exists(FilePath) == false) System.IO.Directory.CreateDirectory(FilePath); using (StreamWriter file = new StreamWriter(FilePath + textBox_Model.Text + "_" + textBox_Client.Text + "_" + textBox_Remark.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd-HHmmss") + ".ttcfg")) { //用户参数1 file.WriteLine("[{0}]", "用户参数1"); foreach (var entry in Params1) file.WriteLine("{0},{1}", entry.Key, entry.Value); //用户参数2 file.WriteLine(""); file.WriteLine("[{0}]", "用户参数2"); foreach (var entry in Params2) file.WriteLine("{0},{1}", entry.Key, entry.Value); //马达参数 file.WriteLine(""); file.WriteLine("[{0}]", "马达参数"); foreach (var entry in MotorParam) file.WriteLine("{0},{1}", entry.Key, entry.Value); //其它信息 file.WriteLine(""); file.WriteLine("[{0}]", "其它信息"); foreach (var entry in Otherinfo) file.WriteLine("{0},{1}", entry.Key, entry.Value); } MessageBox.Show("参数已生成,进入文件管理页面查看!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }