ExportForm.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Runtime.Serialization.Formatters.Binary;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using System.Xml.Serialization;
  11. namespace MOTINOVA_Motor_Factory_Set
  12. {
  13. public partial class ExportForm : Form
  14. {
  15. //参数按照字典类型存储
  16. Dictionary<string, UInt16> Params1 = new Dictionary<string, UInt16>();
  17. Dictionary<string, UInt16> Params2 = new Dictionary<string, UInt16>();
  18. Dictionary<string, UInt16> MotorParam = new Dictionary<string, UInt16>();
  19. Dictionary<string, string> Otherinfo = new Dictionary<string, string>();
  20. //文件路径
  21. string FilePath = "cfg\\";
  22. public ExportForm()
  23. {
  24. InitializeComponent();
  25. }
  26. private void ExportForm_Load(object sender, EventArgs e)
  27. {
  28. }
  29. private void SetDefault()
  30. {
  31. //参数1
  32. Params1.Clear();
  33. Params1.Add("启动模式", 2);
  34. Params1.Add("停机时间", 100);
  35. Params1.Add("限速值", 25);
  36. Params1.Add("下降速度", 3);
  37. Params1.Add("前飞", 14);
  38. Params1.Add("后飞", 9);
  39. Params1.Add("限流", 15);
  40. Params1.Add("温度预警", 95);
  41. Params1.Add("温度保护", 125);
  42. Params1.Add("无码表支持", 0x55);
  43. Params1.Add("轮胎周长", 219);
  44. Params1.Add("系列号", 0);
  45. Params1.Add("ECO增益", 100);//放大100倍
  46. Params1.Add("ECO加速", 100);
  47. Params1.Add("NORM增益", 100);
  48. Params1.Add("NORM加速", 100);
  49. Params1.Add("SPORT增益", 100);
  50. Params1.Add("SPORT加速", 100);
  51. Params1.Add("TURBO增益", 100);
  52. Params1.Add("TURBO加速", 100);
  53. Params1.Add("SMART增益", 100);
  54. Params1.Add("SMART加速", 100);
  55. Params1.Add("车速级数", 1);
  56. Params1.Add("踏频启动", 2);
  57. Params1.Add("指拨模式", 0x55);
  58. Params1.Add("速度信号来源", 0x55);
  59. Params1.Add("周长微调", 0);
  60. Params1.Add("低压保护", 3100);//mV
  61. Params1.Add("推行限速", 60);//放大10倍
  62. Params1.Add("推行转速", 135);
  63. //参数2
  64. Params2.Clear();
  65. Params2.Add("姿态传感器", 0x55);
  66. Params2.Add("俯仰角零偏", 0);
  67. Params2.Add("横滚角零偏", 0);
  68. Params2.Add("尾灯模式", 2);
  69. //马达参数
  70. MotorParam.Clear();
  71. MotorParam.Add("额定功率", 250);
  72. MotorParam.Add("额定转速", 1500);
  73. MotorParam.Add("定子电阻", 10);
  74. MotorParam.Add("定子Lq", 10);
  75. MotorParam.Add("定子Ld", 10);
  76. MotorParam.Add("反电动势", 36000);
  77. MotorParam.Add("额定电压", 36);
  78. //其它信息
  79. Otherinfo.Clear();
  80. Otherinfo.Add("生产商", "TTIUM");
  81. Otherinfo.Add("生产地", "WUHAN");
  82. Otherinfo.Add("生产日期", "20200901");
  83. Otherinfo.Add("校验密钥", "AVONITOM");
  84. Otherinfo.Add("信息1", "");
  85. Otherinfo.Add("信息2", "");
  86. Otherinfo.Add("信息3", "");
  87. }
  88. private void ParamsDisplayUpdate()
  89. {
  90. //参数1
  91. if (Params1["启动模式"] == 1)
  92. {
  93. comboBox_StartMode.Text = "柔和";
  94. }
  95. else if (Params1["启动模式"] == 2)
  96. {
  97. comboBox_StartMode.Text = "正常";
  98. }
  99. else if (Params1["启动模式"] == 3)
  100. {
  101. comboBox_StartMode.Text = "强劲";
  102. }
  103. textBox_StopTime.Text = Params1["停机时间"].ToString();
  104. textBox_SpeedLimit.Text = Params1["限速值"].ToString();
  105. textBox_Dec.Text = Params1["下降速度"].ToString();
  106. textBox_T_Front.Text = Params1["前飞"].ToString();
  107. textBox_T_Tail.Text = Params1["后飞"].ToString();
  108. textBox_MaxCurrent.Text = Params1["限流"].ToString();
  109. textBox_NTC_Alarm.Text = Params1["温度预警"].ToString();
  110. textBox_NTC_Ptotect.Text = Params1["温度保护"].ToString();
  111. if (Params1["无码表支持"] == 0x55)
  112. {
  113. comboBox_NoPBU.Text = "不支持";
  114. }
  115. else if (Params1["无码表支持"] == 0xAA)
  116. {
  117. comboBox_NoPBU.Text = "支持";
  118. }
  119. textBox_WheelSize.Text = Params1["轮胎周长"].ToString();
  120. textBox_Serial.Text = Params1["系列号"].ToString();
  121. textBox_ECO_Gain.Text = ((float)(Params1["ECO增益"]) / 100.0f).ToString("#0.00");
  122. textBox_ECO_Acc.Text = ((float)(Params1["ECO加速"]) / 100.0f).ToString("#0.00");
  123. textBox_NORM_Gain.Text = ((float)(Params1["NORM增益"]) / 100.0f).ToString("#0.00");
  124. textBox_NORM_Acc.Text = ((float)(Params1["NORM加速"]) / 100.0f).ToString("#0.00");
  125. textBox_SPORT_Gain.Text = ((float)(Params1["SPORT增益"]) / 100.0f).ToString("#0.00");
  126. textBox_SPORT_Acc.Text = ((float)(Params1["SPORT加速"]) / 100.0f).ToString("#0.00");
  127. textBox_TURBO_Gain.Text = ((float)(Params1["TURBO增益"]) / 100.0f).ToString("#0.00");
  128. textBox_TURBO_Acc.Text = ((float)(Params1["TURBO加速"]) / 100.0f).ToString("#0.00");
  129. textBox_SMART_Gain.Text = ((float)(Params1["SMART增益"]) / 100.0f).ToString("#0.00");
  130. textBox_SMART_Acc.Text = ((float)(Params1["SMART加速"]) / 100.0f).ToString("#0.00");
  131. textBox_SpeedSensor.Text = Params1["车速级数"].ToString();
  132. textBox_CadenceStart.Text = Params1["踏频启动"].ToString();
  133. if (Params1["指拨模式"] == 0x55)
  134. {
  135. comboBox_GasMode.Text = "不支持";
  136. }
  137. else if (Params1["指拨模式"] == 0xAA)
  138. {
  139. comboBox_GasMode.Text = "支持";
  140. }
  141. if (Params1["速度信号来源"] == 0x55)
  142. {
  143. comboBox_SpeedSensor.Text = "传感器";
  144. }
  145. else if (Params1["速度信号来源"] == 0xAA)
  146. {
  147. comboBox_SpeedSensor.Text = "踏频";
  148. }
  149. else if (Params1["速度信号来源"] == 0xEE)
  150. {
  151. comboBox_SpeedSensor.Text = "通信";
  152. }
  153. textBox_WheelSizeAdj.Text = Params1["周长微调"].ToString();
  154. textBox_UV_Protect.Text = Params1["低压保护"].ToString();
  155. textBox_Walk_SpeedLimit.Text = ((float)(Params1["推行限速"]) / 10.0f).ToString("#0.0");
  156. textBox_WalkSpeed.Text = Params1["推行转速"].ToString();
  157. //参数2
  158. if (Params2["姿态传感器"] == 0x55)
  159. {
  160. comboBox_AstSensor.Text = "不支持";
  161. }
  162. else if (Params1["姿态传感器"] == 0xAA)
  163. {
  164. comboBox_AstSensor.Text = "支持";
  165. }
  166. textBox_Angle_P.Text = ((float)(Params2["俯仰角零偏"]) / 10.0f).ToString("#0.0");
  167. textBox_Angle_R.Text = ((float)(Params2["横滚角零偏"]) / 10.0f).ToString("#0.0");
  168. if (Params2["尾灯模式"] == 1)
  169. {
  170. comboBox_LightMode.Text = "模式 1";
  171. }
  172. else if (Params2["尾灯模式"] == 2)
  173. {
  174. comboBox_LightMode.Text = "模式 2";
  175. }
  176. else if (Params2["尾灯模式"] == 3)
  177. {
  178. comboBox_LightMode.Text = "模式 3";
  179. }
  180. else if (Params2["尾灯模式"] == 4)
  181. {
  182. comboBox_LightMode.Text = "模式 4";
  183. }
  184. //马达参数
  185. textBox_RatePower.Text = MotorParam["额定功率"].ToString();
  186. textBox_RateSpeed.Text = MotorParam["额定转速"].ToString();
  187. textBox_RotorR.Text = MotorParam["定子电阻"].ToString();
  188. textBox_Rotor_Lq.Text = MotorParam["定子Lq"].ToString();
  189. textBox_Rotor_Ld.Text = MotorParam["定子Ld"].ToString();
  190. textBox_E.Text = MotorParam["反电动势"].ToString();
  191. textBox_Rate_Vol.Text = MotorParam["额定电压"].ToString();
  192. //其它信息
  193. textBox_Mac.Text = Otherinfo["生产商"].ToString();
  194. textBox_Mac_Addr.Text = Otherinfo["生产地"].ToString();
  195. textBox_Mac_Date.Text = Otherinfo["生产日期"].ToString();
  196. textBox_Secrect.Text = Otherinfo["校验密钥"].ToString();
  197. textBox_Info1.Text = Otherinfo["信息1"].ToString();
  198. textBox_Info2.Text = Otherinfo["信息2"].ToString();
  199. textBox_Info3.Text = Otherinfo["信息3"].ToString();
  200. }
  201. private bool DisplaySetToParams()
  202. {
  203. try
  204. {
  205. //参数1
  206. Params1.Clear();
  207. if (comboBox_StartMode.Text == "柔和")
  208. {
  209. Params1.Add("启动模式", 1);
  210. }
  211. else if (comboBox_StartMode.Text == "正常")
  212. {
  213. Params1.Add("启动模式", 2);
  214. }
  215. else if (comboBox_StartMode.Text == "强劲")
  216. {
  217. Params1.Add("启动模式", 3);
  218. }
  219. else
  220. {
  221. MessageBox.Show("请检查启动模式设置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  222. }
  223. Params1.Add("停机时间", Convert.ToUInt16(textBox_StopTime.Text));
  224. Params1.Add("限速值", Convert.ToUInt16(textBox_SpeedLimit.Text));
  225. Params1.Add("下降速度", Convert.ToUInt16(textBox_Dec.Text));
  226. Params1.Add("前飞", Convert.ToUInt16(textBox_T_Front.Text));
  227. Params1.Add("后飞", Convert.ToUInt16(textBox_T_Tail.Text));
  228. Params1.Add("限流", Convert.ToUInt16(textBox_MaxCurrent.Text));
  229. Params1.Add("温度预警", Convert.ToUInt16(textBox_NTC_Alarm.Text));
  230. Params1.Add("温度保护", Convert.ToUInt16(textBox_NTC_Ptotect.Text));
  231. if (comboBox_NoPBU.Text == "不支持")
  232. {
  233. Params1.Add("无码表支持", 0x55);
  234. }
  235. else if (comboBox_NoPBU.Text == "支持")
  236. {
  237. Params1.Add("无码表支持", 0xAA);
  238. }
  239. else
  240. {
  241. MessageBox.Show("请检查无码表设置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  242. }
  243. Params1.Add("轮胎周长", Convert.ToUInt16(textBox_WheelSize.Text));
  244. Params1.Add("系列号", Convert.ToUInt16(textBox_Serial.Text));
  245. Params1.Add("ECO增益", Convert.ToUInt16(float.Parse(textBox_ECO_Gain.Text) * 100));//放大100倍
  246. Params1.Add("ECO加速", Convert.ToUInt16(float.Parse(textBox_ECO_Acc.Text) * 100));
  247. Params1.Add("NORM增益", Convert.ToUInt16(float.Parse(textBox_NORM_Gain.Text) * 100));
  248. Params1.Add("NORM加速", Convert.ToUInt16(float.Parse(textBox_NORM_Acc.Text) * 100));
  249. Params1.Add("SPORT增益", Convert.ToUInt16(float.Parse(textBox_SPORT_Gain.Text) * 100));
  250. Params1.Add("SPORT加速", Convert.ToUInt16(float.Parse(textBox_SPORT_Acc.Text) * 100));
  251. Params1.Add("TURBO增益", Convert.ToUInt16(float.Parse(textBox_TURBO_Gain.Text) * 100));
  252. Params1.Add("TURBO加速", Convert.ToUInt16(float.Parse(textBox_TURBO_Acc.Text) * 100));
  253. Params1.Add("SMART增益", Convert.ToUInt16(float.Parse(textBox_SMART_Gain.Text) * 100));
  254. Params1.Add("SMART加速", Convert.ToUInt16(float.Parse(textBox_SMART_Acc.Text) * 100));
  255. Params1.Add("车速级数", Convert.ToUInt16(textBox_SpeedSensor.Text));
  256. Params1.Add("踏频启动", Convert.ToUInt16(textBox_CadenceStart.Text));
  257. if (comboBox_GasMode.Text == "不支持")
  258. {
  259. Params1.Add("指拨模式", 0x55);
  260. }
  261. else if (comboBox_GasMode.Text == "支持")
  262. {
  263. Params1.Add("指拨模式", 0xAA);
  264. }
  265. else
  266. {
  267. MessageBox.Show("请检查指拨模式设置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  268. }
  269. if (comboBox_SpeedSensor.Text == "传感器")
  270. {
  271. Params1.Add("速度信号来源", 0x55);
  272. }
  273. else if (comboBox_GasMode.Text == "踏频")
  274. {
  275. Params1.Add("速度信号来源", 0xAA);
  276. }
  277. else if (comboBox_GasMode.Text == "通信")
  278. {
  279. Params1.Add("速度信号来源", 0xEE);
  280. }
  281. else
  282. {
  283. MessageBox.Show("请检查速度信号来源设置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  284. }
  285. Params1.Add("周长微调", Convert.ToUInt16(textBox_WheelSizeAdj.Text));
  286. Params1.Add("低压保护", Convert.ToUInt16(textBox_UV_Protect.Text));//mV
  287. Params1.Add("推行限速", Convert.ToUInt16(float.Parse(textBox_Walk_SpeedLimit.Text) * 10));//放大10倍
  288. Params1.Add("推行转速", Convert.ToUInt16(textBox_WalkSpeed.Text));
  289. //参数2
  290. Params2.Clear();
  291. if (comboBox_AstSensor.Text == "不支持")
  292. {
  293. Params2.Add("姿态传感器", 0x55);
  294. }
  295. else if (comboBox_AstSensor.Text == "支持")
  296. {
  297. Params2.Add("姿态传感器", 0xAA);
  298. }
  299. else
  300. {
  301. MessageBox.Show("请检查姿态传感器设置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  302. }
  303. Params2.Add("俯仰角零偏", Convert.ToUInt16(float.Parse(textBox_Angle_P.Text) * 10));
  304. Params2.Add("横滚角零偏", Convert.ToUInt16(float.Parse(textBox_Angle_R.Text) * 10));
  305. if (comboBox_LightMode.Text == "模式 1")
  306. {
  307. Params2.Add("尾灯模式", 1);
  308. }
  309. else if(comboBox_LightMode.Text == "模式 2")
  310. {
  311. Params2.Add("尾灯模式", 2);
  312. }
  313. else if (comboBox_LightMode.Text == "模式 3")
  314. {
  315. Params2.Add("尾灯模式", 3);
  316. }
  317. else if (comboBox_LightMode.Text == "模式 4")
  318. {
  319. Params2.Add("尾灯模式", 4);
  320. }
  321. //马达参数
  322. MotorParam.Clear();
  323. MotorParam.Add("额定功率", Convert.ToUInt16(textBox_RatePower.Text));
  324. MotorParam.Add("额定转速", Convert.ToUInt16(textBox_RateSpeed.Text));
  325. MotorParam.Add("定子电阻", Convert.ToUInt16(textBox_RotorR.Text));
  326. MotorParam.Add("定子Lq", Convert.ToUInt16(textBox_Rotor_Lq.Text));
  327. MotorParam.Add("定子Ld", Convert.ToUInt16(textBox_Rotor_Ld.Text));
  328. MotorParam.Add("反电动势", Convert.ToUInt16(textBox_E.Text));
  329. MotorParam.Add("额定电压", Convert.ToUInt16(textBox_Rate_Vol.Text));
  330. //其它信息
  331. Otherinfo.Clear();
  332. Otherinfo.Add("生产商", textBox_Mac.Text);
  333. Otherinfo.Add("生产地", textBox_Mac_Addr.Text);
  334. Otherinfo.Add("生产日期", textBox_Mac_Date.Text);
  335. Otherinfo.Add("校验密钥", textBox_Secrect.Text);
  336. Otherinfo.Add("信息1", textBox_Info1.Text);
  337. Otherinfo.Add("信息2", textBox_Info2.Text);
  338. Otherinfo.Add("信息3", textBox_Info3.Text);
  339. return true;
  340. }
  341. catch(Exception ex)
  342. {
  343. MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  344. return false;
  345. }
  346. }
  347. private void button_Default_Click(object sender, EventArgs e)
  348. {
  349. SetDefault();
  350. ParamsDisplayUpdate();
  351. }
  352. private void button_Generate_Click(object sender, EventArgs e)
  353. {
  354. //空间内容更新到字典
  355. if (DisplaySetToParams() == false)
  356. {
  357. MessageBox.Show("参数生成失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  358. return;
  359. }
  360. //保存为文件
  361. if (textBox_Model.Text == string.Empty)
  362. {
  363. MessageBox.Show("请输入电机型号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  364. return;
  365. }
  366. if (textBox_Client.Text == string.Empty)
  367. {
  368. textBox_Client.Text = "STD";
  369. }
  370. if (System.IO.Directory.Exists(FilePath) == false)
  371. System.IO.Directory.CreateDirectory(FilePath);
  372. using (StreamWriter file = new StreamWriter(FilePath + textBox_Model.Text + "_" + textBox_Client.Text + "_" + textBox_Remark.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd-HHmmss") + ".ttcfg"))
  373. {
  374. //用户参数1
  375. file.WriteLine("[{0}]", "用户参数1");
  376. foreach (var entry in Params1)
  377. file.WriteLine("{0},{1}", entry.Key, entry.Value);
  378. //用户参数2
  379. file.WriteLine("");
  380. file.WriteLine("[{0}]", "用户参数2");
  381. foreach (var entry in Params2)
  382. file.WriteLine("{0},{1}", entry.Key, entry.Value);
  383. //马达参数
  384. file.WriteLine("");
  385. file.WriteLine("[{0}]", "马达参数");
  386. foreach (var entry in MotorParam)
  387. file.WriteLine("{0},{1}", entry.Key, entry.Value);
  388. //其它信息
  389. file.WriteLine("");
  390. file.WriteLine("[{0}]", "其它信息");
  391. foreach (var entry in Otherinfo)
  392. file.WriteLine("{0},{1}", entry.Key, entry.Value);
  393. }
  394. MessageBox.Show("参数已生成,进入文件管理页面查看!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  395. }
  396. }
  397. }