Form1.cs 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Threading;
  5. using System.Windows.Forms;
  6. namespace Welling_Motor_Debug_Tool
  7. {
  8. public partial class Form1 : Form
  9. {
  10. #region 变量定义
  11. //串口实例
  12. Serial_Process mySerialProcess = new Serial_Process();
  13. //MC运行信息超时计数
  14. bool MC_RunInfo_Refresh = false;
  15. ushort MC_RunInfo_Refresh_Cnt = 0;
  16. //MC故障码超时计数
  17. bool MC_ErrorCode_Refresh = false;
  18. ushort MC_ErrorCode_Refresh_Cnt = 0;
  19. //配置参数实例
  20. Params myParams = new Params();
  21. //记录文件名称
  22. string SaveFilename = "";
  23. //BMS运行信息超时计数
  24. bool BMS_RunInfo_Refresh = false;
  25. ushort BMS_RunInfo_Refresh_Cnt = 0;
  26. #endregion
  27. public Form1()
  28. {
  29. InitializeComponent();
  30. }
  31. private void Form1_Load(object sender, System.EventArgs e)
  32. {
  33. //页面初始化
  34. label_BuildTime.Text= "编译时间:"+ System.IO.File.GetLastWriteTime(this.GetType().Assembly.Location).ToString();
  35. //端口设置
  36. mySerialProcess.Init();
  37. toolStripComboBox_ComNum.Items.AddRange(mySerialProcess.refreshPort());
  38. toolStripComboBox_ComNum.SelectedIndex = toolStripComboBox_ComNum.Items.Count > 0 ? 0 : -1;
  39. toolStripComboBox_Baudrate.SelectedIndex = toolStripComboBox_Baudrate.Items.IndexOf("115200");
  40. //创建线程,定时解析数据
  41. Thread th = new Thread(Task_SerialProcess);
  42. th.IsBackground = true;
  43. th.Start();
  44. }
  45. #region 串口数据解析线程
  46. /// <summary>
  47. /// 串口数据解析线程
  48. /// </summary>
  49. private void Task_SerialProcess()
  50. {
  51. //创建定时器,定时读取串口数据
  52. System.Timers.Timer timer_ReadSerial = new System.Timers.Timer();
  53. timer_ReadSerial.Enabled = true;
  54. timer_ReadSerial.Interval = 20;
  55. timer_ReadSerial.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_ReadSerial_Tick);
  56. timer_ReadSerial.Start();
  57. while (true)
  58. {
  59. Thread.Sleep(1);
  60. };
  61. }
  62. /// <summary>
  63. /// 串口解析定时器
  64. /// </summary>
  65. /// <param name="sender"></param>
  66. /// <param name="e"></param>
  67. private void timer_ReadSerial_Tick(object sender, EventArgs e)
  68. {
  69. //串口解析
  70. Serial_process(mySerialProcess.buffer);
  71. }
  72. #endregion
  73. #region 数据解析
  74. public void Serial_process(List<byte> buffer)
  75. {
  76. byte DataNum = 0; //记录每条命令数据段的长度
  77. ushort PackageID, Cmd;
  78. byte[] Data = new byte[1024];
  79. while (buffer.Count >= 13)//当buffer中的数据的个数大于等于4个时,即每一帧的数据长度所在位(第4位)出现时
  80. {
  81. if (buffer[0] == 0x55 && buffer[1] == 0xAA)//判断帧头
  82. {
  83. PackageID = (ushort)(buffer[2] * 256 + buffer[3]);
  84. if ((PackageID == 0x0710) || (PackageID == 0x0715) || (PackageID == 0x07FF) || (PackageID == 0x0720) || (PackageID == 0x0713))
  85. {
  86. int CmdLen = buffer[5];
  87. if (buffer.Count < CmdLen + 11) break;//如果接收到的数据没有达到一帧数据的指定长度, 则执行下次循环
  88. buffer.RemoveRange(0, 6);//数据达到要求长度后删去帧头,ID,模式,和命令长度
  89. while (CmdLen > 0) //读取命令段
  90. {
  91. Cmd = (ushort)(buffer[0] * 256 + buffer[1]);
  92. DataNum = (byte)(buffer[1]);//命令字的第二个字节的表示数据位的长度
  93. if (DataNum <= (CmdLen - 2))
  94. {
  95. for (int i = 0; i < DataNum; i++)
  96. {
  97. Data[i] = buffer[i + 2];
  98. }
  99. DataCmdProcess(PackageID, Cmd, Data);//对于不同的命令段,做不同的处理
  100. buffer.RemoveRange(0, DataNum);//移除处理过的数据段
  101. CmdLen -= (DataNum + 2);
  102. if (CmdLen < 2)
  103. {
  104. buffer.RemoveRange(0, CmdLen + 2);
  105. return;
  106. }
  107. }
  108. else
  109. {
  110. buffer.RemoveRange(0, CmdLen + 11);
  111. }
  112. }
  113. }
  114. else
  115. {
  116. buffer.RemoveAt(0);//ID不对
  117. }
  118. }
  119. else
  120. {
  121. buffer.RemoveAt(0);//帧头不对,删除帧头高字节
  122. }
  123. }
  124. }
  125. private void DataCmdProcess(ushort ID, ushort CmdTemp, byte[] Data)
  126. {
  127. int DataTemp;
  128. ushort uData16;
  129. unchecked
  130. {
  131. this.Invoke((EventHandler)(delegate { label_RecCount.Text = Convert.ToString(Convert.ToInt32(label_RecCount.Text) + 1); }));
  132. }
  133. #region 解析发送给CDL的指令
  134. if (ID == 0x7FF)
  135. {
  136. switch (CmdTemp)
  137. {
  138. case 0x1100://
  139. {
  140. unchecked
  141. {
  142. this.Invoke((EventHandler)(delegate
  143. {
  144. //CDL连接成功
  145. mySerialProcess.CDL_Online_Flag = true;
  146. MessageBox.Show("连接成功!", "提示");
  147. }));
  148. }
  149. break;
  150. }
  151. default: break;
  152. }
  153. }
  154. #endregion
  155. #region 解析电机的命令
  156. else if ((ID == 0x715) || (ID == 0x710))
  157. {
  158. switch (CmdTemp)
  159. {
  160. case 0x1020://电机运行信息
  161. {
  162. unchecked
  163. {
  164. this.Invoke((EventHandler)(delegate
  165. {
  166. MC_RunInfo_Refresh = true;
  167. //车速
  168. DataTemp = (int)(Data[1] * 256 + Data[0]);
  169. textBox_RunInfo_BikeSpeed.Text = ((float)DataTemp / 10f).ToString("0.0") + " km/h";
  170. //输出转速
  171. DataTemp = (ushort)(Data[3] * 256 + Data[2]);
  172. textBox_RunInfo_MotorSpeed.Text = Convert.ToString(DataTemp) + " rpm";
  173. Class_MotorSpeed.String = textBox_RunInfo_MotorSpeed.Text;
  174. Class_MotorSpeed.Data = DataTemp;
  175. //电功率
  176. DataTemp = (ushort)(Data[5] * 256 + Data[4]);
  177. textBox_RunInfo_Power.Text = Convert.ToString(DataTemp * 2) + " W";
  178. //母线电压
  179. DataTemp = (ushort)(Data[7] * 256 + Data[6]);
  180. textBox_RunInfo_Vol.Text = Convert.ToString(DataTemp) + " mV";
  181. //母线电流
  182. DataTemp = (ushort)(Data[9] * 256 + Data[8]);
  183. textBox_RunInfo_Current.Text = Convert.ToString(DataTemp) + " mA";
  184. Class_CurrentData.String = textBox_RunInfo_Current.Text;
  185. Class_CurrentData.Data = DataTemp;
  186. //踏频
  187. DataTemp = (ushort)(Data[10]);
  188. textBox_RunInfo_Cadence.Text = Convert.ToString(DataTemp) + " rpm";
  189. //踩踏力矩
  190. DataTemp = (ushort)(Data[11]);
  191. textBox_RunInfo_Torque.Text = Convert.ToString(DataTemp) + " N.m";
  192. Class_TorqueData.String = textBox_RunInfo_Torque.Text;
  193. Class_TorqueData.Data = DataTemp;
  194. //踩踏方向
  195. DataTemp = (ushort)(Data[12]);
  196. if (DataTemp == 0)
  197. {
  198. textBox_RunInfo_Dir.Text = "正向";
  199. }
  200. else if (DataTemp == 1)
  201. {
  202. textBox_RunInfo_Dir.Text = "反向";
  203. }
  204. else if (DataTemp == 2)
  205. {
  206. textBox_RunInfo_Dir.Text = "停止";
  207. }
  208. //助力档位
  209. DataTemp = (ushort)(Data[13]);
  210. if (DataTemp == 0x22)
  211. {
  212. textBox_RunInfo_GearSt.Text = "Walk";
  213. comboBox_GearSt.SelectedIndex = 6;
  214. }
  215. else
  216. {
  217. textBox_RunInfo_GearSt.Text = Convert.ToString(DataTemp);
  218. comboBox_GearSt.SelectedIndex = DataTemp;
  219. }
  220. //大灯状态
  221. DataTemp = (ushort)(Data[14]);
  222. if (DataTemp == 0xF0)
  223. {
  224. textBox_RunInfo_LightSwitch.Text = "OFF";
  225. comboBox_LightSwitch.SelectedIndex = 1;
  226. }
  227. else if (DataTemp == 0xF1)
  228. {
  229. textBox_RunInfo_LightSwitch.Text = "ON";
  230. comboBox_LightSwitch.SelectedIndex = 0;
  231. }
  232. //剩余电量
  233. DataTemp = (ushort)(Data[15]);
  234. textBox_RunInfo_SOC.Text = Convert.ToString(DataTemp) + " %";
  235. //剩余续航里程
  236. DataTemp = (ushort)(Data[17] * 256 + Data[16]);
  237. if ((DataTemp == 0xEEEE) || (DataTemp == 0xFFFF))
  238. {
  239. textBox_RunInfo_Range.Text = "---";
  240. }
  241. else
  242. {
  243. textBox_RunInfo_Range.Text = Convert.ToString(DataTemp) + " km";
  244. }
  245. //预留,空2bytes
  246. //平均功耗
  247. DataTemp = (ushort)(Data[20]);
  248. textBox_RunInfo_AvgPower.Text = Convert.ToString(DataTemp * 10) + " mAh/km";
  249. //PCB温度
  250. DataTemp = (ushort)(Data[21]);
  251. textBox_RunInfo_T_PCB.Text = Convert.ToString((int)(DataTemp - 40));
  252. //绕组温度
  253. DataTemp = (ushort)(Data[22]);
  254. textBox_RunInfo_T_Coil.Text = Convert.ToString((int)(DataTemp - 40));
  255. //MCU温度
  256. DataTemp = (ushort)(Data[23]);
  257. textBox_RunInfo_T_MCU.Text = Convert.ToString((int)(DataTemp - 40));
  258. //单次里程
  259. DataTemp = (ushort)(Data[25] * 256 + Data[24]);
  260. textBox_RunInfo_Trip.Text = ((float)DataTemp / 10f).ToString("0.0") + " km";
  261. //单次时间
  262. DataTemp = (ushort)(Data[27] * 256 + Data[26]);
  263. textBox_RunInfo_Trip_Time.Text = Convert.ToString(DataTemp) + " s";
  264. //数据保存
  265. DataAutoSave();
  266. }));
  267. }
  268. break;
  269. }
  270. case 0x1104://故障码
  271. {
  272. unchecked
  273. {
  274. this.Invoke((EventHandler)(delegate
  275. {
  276. MC_ErrorCode_Refresh = true;
  277. textBox_ErrorCode.Text =
  278. Convert.ToString(Data[3], 16).PadLeft(2, '0').ToUpper() +
  279. Convert.ToString(Data[2], 16).PadLeft(2, '0').ToUpper() +
  280. Convert.ToString(Data[1], 16).PadLeft(2, '0').ToUpper() +
  281. Convert.ToString(Data[0], 16).PadLeft(2, '0').ToUpper() +
  282. "H";
  283. }));
  284. }
  285. break;
  286. }
  287. case 0xA903://反馈指令
  288. {
  289. unchecked
  290. {
  291. this.Invoke((EventHandler)(delegate
  292. {
  293. timer_1s.Enabled = false;
  294. MessageBox.Show("OK", "反馈指令", MessageBoxButtons.OK, MessageBoxIcon.Information);
  295. timer_1s.Enabled = true;
  296. }));
  297. }
  298. break;
  299. }
  300. case 0xB226://马达参数
  301. {
  302. unchecked
  303. {
  304. this.Invoke((EventHandler)(delegate
  305. {
  306. richTextBox_MotorParam.Clear();
  307. for (int i = 0; i < myParams.MotorParma.Count; i++)
  308. {
  309. richTextBox_MotorParam.AppendText(myParams.MotorParma[i] + "=" + Convert.ToString(Data[2 * i + 1] * 256 + Data[2 * i]) + ", ");
  310. }
  311. richTextBox_MotorParam.Text = richTextBox_MotorParam.Text.Substring(0, richTextBox_MotorParam.Text.Length - 2);
  312. }));
  313. }
  314. break;
  315. }
  316. case 0xB318://整车信息
  317. {
  318. unchecked
  319. {
  320. this.Invoke((EventHandler)(delegate
  321. {
  322. richTextBox_BikeParam.Clear();
  323. for (int i = 0; i < myParams.BikeParma.Count; i++)
  324. {
  325. richTextBox_BikeParam.AppendText(myParams.BikeParma[i] + "=" + Convert.ToString(Data[2 * i + 1] * 256 + Data[2 * i]) + ", ");
  326. }
  327. richTextBox_BikeParam.Text = richTextBox_BikeParam.Text.Substring(0, richTextBox_BikeParam.Text.Length - 2);
  328. }));
  329. }
  330. break;
  331. }
  332. case 0xB420://控制器参数
  333. {
  334. unchecked
  335. {
  336. this.Invoke((EventHandler)(delegate
  337. {
  338. richTextBox_ControlParam.Clear();
  339. for (int i = 0; i < myParams.ControlParma.Count; i++)
  340. {
  341. richTextBox_ControlParam.AppendText(myParams.ControlParma[i] + "=" + Convert.ToString(Data[2 * i + 1] * 256 + Data[2 * i]) + ", ");
  342. }
  343. richTextBox_ControlParam.Text = richTextBox_ControlParam.Text.Substring(0, richTextBox_ControlParam.Text.Length - 2);
  344. }));
  345. }
  346. break;
  347. }
  348. case 0xB528://传感器参数
  349. {
  350. unchecked
  351. {
  352. this.Invoke((EventHandler)(delegate
  353. {
  354. richTextBox_SensorParam.Clear();
  355. for (int i = 0; i < myParams.SensorParam.Count; i++)
  356. {
  357. richTextBox_SensorParam.AppendText(myParams.SensorParam[i] + "=" + Convert.ToString(Data[2 * i + 1] * 256 + Data[2 * i]) + ", ");
  358. }
  359. richTextBox_SensorParam.Text = richTextBox_SensorParam.Text.Substring(0, richTextBox_SensorParam.Text.Length - 2);
  360. }));
  361. }
  362. break;
  363. }
  364. case 0xB64C://助力参数
  365. {
  366. unchecked
  367. {
  368. this.Invoke((EventHandler)(delegate
  369. {
  370. richTextBox_AssistParam.Clear();
  371. int index_1 = myParams.AssistParam.IndexOf("转矩曲线.a");
  372. int index_2 = myParams.AssistParam.IndexOf("踏频曲线.d");
  373. for (int i = 0; i < myParams.AssistParam.Count; i++)
  374. {
  375. if (i < index_1) //数据占用2bytes
  376. {
  377. richTextBox_AssistParam.AppendText(myParams.AssistParam[i] + "=" + Convert.ToString(Data[2 * i + 1] * 256 + Data[2 * i]) + ", ");
  378. }
  379. else if (i <= index_2) //数据占用4bytes
  380. {
  381. richTextBox_AssistParam.AppendText(myParams.AssistParam[i] + "=" + Convert.ToString((Data[4 * i - 5] << 24) + (Data[4 * i - 6] << 16) + (Data[4 * i - 7] << 8) + Data[4 * i - 8]) + ", ");
  382. }
  383. else //数据占用2bytes
  384. {
  385. richTextBox_AssistParam.AppendText(myParams.AssistParam[i] + "=" + Convert.ToString(Data[2 * i + 17] * 256 + Data[2 * i + 16]) + ", ");
  386. }
  387. }
  388. richTextBox_AssistParam.Text = richTextBox_AssistParam.Text.Substring(0, richTextBox_AssistParam.Text.Length - 2);
  389. }));
  390. }
  391. break;
  392. }
  393. case 0xB74C://历史记录
  394. {
  395. unchecked
  396. {
  397. this.Invoke((EventHandler)(delegate
  398. {
  399. richTextBox_Record.Clear();
  400. for (int i = 0; i < myParams.Record.Count; i++)
  401. {
  402. richTextBox_Record.AppendText(myParams.Record[i] + "=" + Convert.ToString(Data[2 * i + 1] * 256 + Data[2 * i]) + ", ");
  403. }
  404. richTextBox_Record.Text = richTextBox_Record.Text.Substring(0, richTextBox_Record.Text.Length - 2);
  405. }));
  406. }
  407. break;
  408. }
  409. case 0xB832://调试参数
  410. {
  411. unchecked
  412. {
  413. this.Invoke((EventHandler)(delegate
  414. {
  415. richTextBox_DebugParam.Clear();
  416. for (int i = 0; i < myParams.DebugParam.Count; i++)
  417. {
  418. richTextBox_DebugParam.AppendText(myParams.DebugParam[i] + "=" + Convert.ToString(Data[2 * i + 1] * 256 + Data[2 * i]) + ", ");
  419. }
  420. richTextBox_DebugParam.Text = richTextBox_DebugParam.Text.Substring(0, richTextBox_DebugParam.Text.Length - 2);
  421. }));
  422. }
  423. break;
  424. }
  425. case 0x1240://电机版本信息
  426. {
  427. unchecked
  428. {
  429. this.Invoke((EventHandler)(delegate
  430. {
  431. //电机型号
  432. textBox_Model.Text = "";
  433. textBox_OBC_ReadModel.Text = "";
  434. for (ushort i = 0; i < 16; i++)
  435. {
  436. if (Data[i] == 0x2E)
  437. {
  438. break;
  439. }
  440. textBox_Model.Text += ((char)Data[i]).ToString();
  441. textBox_OBC_ReadModel.Text += ((char)Data[i]).ToString();
  442. }
  443. Class_Motor_Ver.Mode = textBox_Model.Text;
  444. //电机SN
  445. textBox_SN.Text = "";
  446. textBox_OBC_ReadSN.Text = "";
  447. for (ushort i = 0; i < 16; i++)
  448. {
  449. if (Data[16 + i] == 0x2E)
  450. {
  451. break;
  452. }
  453. textBox_SN.Text += ((char)Data[16 + i]).ToString();
  454. textBox_OBC_ReadSN.Text += ((char)Data[16 + i]).ToString();
  455. }
  456. Class_Motor_Ver.SN = textBox_SN.Text;
  457. //电机HW
  458. textBox_HW.Text = "";
  459. textBox_OBC_ReadHW.Text = "";
  460. for (ushort i = 0; i < 16; i++)
  461. {
  462. if (Data[32 + i] == 0x2E)
  463. {
  464. break;
  465. }
  466. textBox_HW.Text += ((char)Data[32 + i]).ToString();
  467. textBox_OBC_ReadHW.Text += ((char)Data[32 + i]).ToString();
  468. }
  469. Class_Motor_Ver.HW = textBox_HW.Text;
  470. //电机FW
  471. textBox_FW.Text = "";
  472. textBox_OBC_ReadFW.Text = "";
  473. for (ushort i = 0; i < 16; i++)
  474. {
  475. if (Data[48 + i] == 0x2E)
  476. {
  477. break;
  478. }
  479. if (Data[48 + i] == 'r')
  480. {
  481. Data[48 + i] = (byte)'.';
  482. }
  483. textBox_FW.Text += ((char)Data[48 + i]).ToString();
  484. textBox_OBC_ReadFW.Text += ((char)Data[48 + i]).ToString();
  485. }
  486. Class_Motor_Ver.FW = textBox_FW.Text;
  487. }));
  488. }
  489. break;
  490. }
  491. case 0xA610://自定义1
  492. {
  493. unchecked
  494. {
  495. this.Invoke((EventHandler)(delegate
  496. {
  497. textBox_User1.Text = "";
  498. for (ushort i = 0; i < 16; i++)
  499. {
  500. if (Data[i] == 0x2E)
  501. {
  502. break;
  503. }
  504. textBox_User1.Text += ((char)Data[i]).ToString();
  505. }
  506. }));
  507. }
  508. break;
  509. }
  510. case 0xA710://自定义2
  511. {
  512. unchecked
  513. {
  514. this.Invoke((EventHandler)(delegate
  515. {
  516. textBox_User2.Text = "";
  517. for (ushort i = 0; i < 16; i++)
  518. {
  519. if (Data[i] == 0x2E)
  520. {
  521. break;
  522. }
  523. textBox_User2.Text += ((char)Data[i]).ToString();
  524. }
  525. }));
  526. }
  527. break;
  528. }
  529. case 0xA810://自定义1
  530. {
  531. unchecked
  532. {
  533. this.Invoke((EventHandler)(delegate
  534. {
  535. textBox_User3.Text = "";
  536. for (ushort i = 0; i < 16; i++)
  537. {
  538. if (Data[i] == 0x2E)
  539. {
  540. break;
  541. }
  542. textBox_User3.Text += ((char)Data[i]).ToString();
  543. }
  544. }));
  545. }
  546. break;
  547. }
  548. case 0xA520://生产信息
  549. {
  550. unchecked
  551. {
  552. this.Invoke((EventHandler)(delegate
  553. {
  554. //生产商
  555. textBox_MAC.Text = "";
  556. for (ushort i = 0; i < 8; i++)
  557. {
  558. if (Data[i] == 0x2E)
  559. {
  560. break;
  561. }
  562. textBox_MAC.Text += ((char)Data[i]).ToString();
  563. }
  564. //生产地
  565. textBox_MACAdd.Text = "";
  566. for (ushort i = 0; i < 8; i++)
  567. {
  568. if (Data[8 + i] == 0x2E)
  569. {
  570. break;
  571. }
  572. textBox_MACAdd.Text += ((char)Data[8 + i]).ToString();
  573. }
  574. //生产日期
  575. textBox_MACDate.Text = "";
  576. for (ushort i = 0; i < 8; i++)
  577. {
  578. if (Data[16 + i] == 0x2E)
  579. {
  580. break;
  581. }
  582. textBox_MACDate.Text += ((char)Data[16 + i]).ToString();
  583. }
  584. //品牌信息
  585. textBox_PP.Text = "";
  586. for (ushort i = 0; i < 8; i++)
  587. {
  588. if (Data[24 + i] == 0x2E)
  589. {
  590. break;
  591. }
  592. textBox_PP.Text += ((char)Data[24 + i]).ToString();
  593. }
  594. }));
  595. }
  596. break;
  597. }
  598. case 0xA408://密钥
  599. {
  600. unchecked
  601. {
  602. this.Invoke((EventHandler)(delegate
  603. {
  604. textBox_Key.Text = "";
  605. for (ushort i = 0; i < 8; i++)
  606. {
  607. textBox_Key.Text += ((char)Data[i]).ToString();
  608. }
  609. }));
  610. }
  611. break;
  612. }
  613. case 0x1401://在线检测结果
  614. {
  615. unchecked
  616. {
  617. this.Invoke((EventHandler)(delegate
  618. {
  619. textBox_Online.Text = Convert.ToString(Data[3], 16).PadLeft(2, '0').ToUpper() + "H";
  620. }));
  621. }
  622. break;
  623. }
  624. case 0x1510://骑行历史信息
  625. {
  626. unchecked
  627. {
  628. this.Invoke((EventHandler)(delegate
  629. {
  630. long DataTemp32;
  631. //ODO里程
  632. DataTemp32 = (long)((Data[3] << 24) + (Data[2] << 16) + (Data[1] << 8) + Data[0]);
  633. textBox_OBC_ODO_KM.Text = ((float)DataTemp32 / 10f).ToString("0.0") + " km";
  634. //ODO时间
  635. DataTemp32 = (long)((Data[7] << 24) + (Data[6] << 16) + (Data[5] << 8) + Data[4]);
  636. if (DataTemp32 >= 60 * 100000)
  637. textBox_OBC_ODO_TIME.Text = Convert.ToString(DataTemp32 / 60) + " h";
  638. else
  639. textBox_OBC_ODO_TIME.Text = Convert.ToString(DataTemp32 / 60) + " h " + Convert.ToString(DataTemp32 % 60) + " m";
  640. //TRIP里程
  641. DataTemp32 = (long)((Data[11] << 24) + (Data[10] << 16) + (Data[9] << 8) + Data[8]);
  642. textBox_OBC_TRIP_KM.Text = ((float)DataTemp32 / 10f).ToString("0.0") + " km";
  643. //TRIP时间
  644. DataTemp32 = (long)((Data[15] << 24) + (Data[14] << 16) + (Data[13] << 8) + Data[12]);
  645. if (DataTemp32 >= 60 * 100000)
  646. textBox_OBC_TRIP_TIME.Text = Convert.ToString(DataTemp32 / 60) + " h";
  647. else
  648. textBox_OBC_TRIP_TIME.Text = Convert.ToString(DataTemp32 / 60) + " h " + Convert.ToString(DataTemp32 % 60) + " m";
  649. }));
  650. }
  651. break;
  652. }
  653. default: break;
  654. }
  655. }
  656. #endregion
  657. #region 解析BMS广播的命令
  658. else if (ID == 0x720)
  659. {
  660. switch (CmdTemp)
  661. {
  662. case 0x1010://BMS运行信息
  663. {
  664. unchecked
  665. {
  666. this.Invoke((EventHandler)(delegate
  667. {
  668. BMS_RunInfo_Refresh = true;
  669. richTextBox_OBC_BMS_RunInfo.Clear();
  670. //电压
  671. uData16 = (ushort)(Data[1] * 256 + Data[0]);
  672. richTextBox_OBC_BMS_RunInfo.AppendText("电压:" + Convert.ToString(uData16) + " mV" + "\r\n");
  673. //电流
  674. uData16 = (ushort)(Data[3] * 256 + Data[2]);
  675. richTextBox_OBC_BMS_RunInfo.AppendText("电流:" + Convert.ToString(uData16) + " mA" + "\r\n");
  676. //剩余容量
  677. uData16 = (ushort)(Data[5] * 256 + Data[4]);
  678. richTextBox_OBC_BMS_RunInfo.AppendText("剩余容量:" + Convert.ToString(uData16) + " mAh" + "\r\n");
  679. //满充容量
  680. uData16 = (ushort)(Data[7] * 256 + Data[6]);
  681. richTextBox_OBC_BMS_RunInfo.AppendText("满充容量:" + Convert.ToString(uData16) + " mV" + "\r\n");
  682. //电芯温度
  683. richTextBox_OBC_BMS_RunInfo.AppendText("电芯温度:" + Convert.ToString((int)(Data[8] - 40) + " ℃" + "\r\n"));
  684. //剩余电量
  685. richTextBox_OBC_BMS_RunInfo.AppendText("剩余电量:" + Convert.ToString(Data[9]) + " %" + "\r\n");
  686. //电池状态
  687. richTextBox_OBC_BMS_RunInfo.AppendText("电池状态:" + Convert.ToString(Data[10], 16).PadLeft(2, '0').ToUpper() + " H" + "\r\n");
  688. //SOH
  689. richTextBox_OBC_BMS_RunInfo.AppendText("电池寿命:" + Convert.ToString(Data[11]) + " %" + "\r\n");
  690. }));
  691. }
  692. break;
  693. }
  694. default: break;
  695. }
  696. }
  697. #endregion
  698. #region 解析电机发送OBC的命令
  699. else if (ID == 0x713)
  700. {
  701. switch (CmdTemp)
  702. {
  703. case 0x5408://电机用户参数
  704. {
  705. unchecked
  706. {
  707. this.Invoke((EventHandler)(delegate
  708. {
  709. richTextBox_OBC_ReadUserInfo.Clear();
  710. //默认周长
  711. richTextBox_OBC_ReadUserInfo.AppendText("默认周长:" + Convert.ToString((int)Data[0]) + " cm" + "\r\n");
  712. //启动模式
  713. richTextBox_OBC_ReadUserInfo.AppendText("启动模式:" + ((Data[1] == 0x01) ? "柔和" : (Data[1] == 0x02) ? "正常" : (Data[1] == 0x03) ? "强劲" : "无效") + "\r\n");
  714. comboBox_OBC_StartMode.SelectedIndex = Data[1] - 1;
  715. //限速
  716. richTextBox_OBC_ReadUserInfo.AppendText("限速:" + Convert.ToString(Data[2]) + " km/h" + "\r\n");
  717. //周长微调
  718. numericUpDown_OBC_WheelAdj.Value = (int)(Data[3] > 128 ? (Data[3] - 256) : Data[3]);
  719. richTextBox_OBC_ReadUserInfo.AppendText("周长微调:" + Convert.ToString(numericUpDown_OBC_WheelAdj.Value) + " cm" + "\r\n");
  720. //助力方案
  721. richTextBox_OBC_ReadUserInfo.AppendText("助力方案:" + Convert.ToString(Data[4]) + "\r\n");
  722. comboBox_OBC_AssistFunc.SelectedIndex = Data[4] - 1;
  723. }));
  724. }
  725. break;
  726. }
  727. case 0x5303://电机应答反馈
  728. {
  729. unchecked
  730. {
  731. this.Invoke((EventHandler)(delegate
  732. {
  733. timer_1s.Enabled = false;
  734. MessageBox.Show("OK", "反馈指令", MessageBoxButtons.OK, MessageBoxIcon.Information);
  735. timer_1s.Enabled = true;
  736. }));
  737. }
  738. break;
  739. }
  740. default: break;
  741. }
  742. }
  743. #endregion
  744. }
  745. #endregion
  746. /// <summary>
  747. /// 端口连接事件
  748. /// </summary>
  749. /// <param name="sender"></param>
  750. /// <param name="e"></param>
  751. private void 连接ToolStripMenuItem_Click(object sender, System.EventArgs e)
  752. {
  753. bool result = false;
  754. if (连接ToolStripMenuItem.Text == "连接")
  755. {
  756. result = mySerialProcess.SerialOpen(toolStripComboBox_ComNum.Text, toolStripComboBox_Baudrate.Text);
  757. if (result)
  758. {
  759. toolStripComboBox_ComNum.Enabled = false;
  760. toolStripComboBox_Baudrate.Enabled = false;
  761. 刷新ToolStripMenuItem.Enabled = false;
  762. 连接ToolStripMenuItem.Text = "断开";
  763. 连接ToolStripMenuItem.Checked = true;
  764. label_COM_Sta.Text = toolStripComboBox_ComNum.Text + "已连接," + toolStripComboBox_Baudrate.Text;
  765. }
  766. }
  767. else if (连接ToolStripMenuItem.Text == "断开")
  768. {
  769. result = mySerialProcess.SerialClose();
  770. if (result)
  771. {
  772. toolStripComboBox_ComNum.Enabled = true;
  773. toolStripComboBox_Baudrate.Enabled = true;
  774. 刷新ToolStripMenuItem.Enabled = true;
  775. 连接ToolStripMenuItem.Text = "连接";
  776. 连接ToolStripMenuItem.Checked = false;
  777. label_COM_Sta.Text = "连接状态:未连接";
  778. }
  779. }
  780. }
  781. /// <summary>
  782. /// 端口号刷新事件
  783. /// </summary>
  784. /// <param name="sender"></param>
  785. /// <param name="e"></param>
  786. private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
  787. {
  788. toolStripComboBox_ComNum.Items.Clear();
  789. toolStripComboBox_ComNum.Items.AddRange(mySerialProcess.refreshPort());
  790. toolStripComboBox_ComNum.SelectedIndex = toolStripComboBox_ComNum.Items.Count > 0 ? 0 : -1;
  791. }
  792. /// <summary>
  793. /// 转速调节事件
  794. /// </summary>
  795. /// <param name="sender"></param>
  796. /// <param name="e"></param>
  797. private void numericUpDown_SpeedAdj_ValueChanged(object sender, EventArgs e)
  798. {
  799. var Data = new byte[1];
  800. Data[0] = (byte)numericUpDown_SpeedAdj.Value;
  801. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2C01, Data);
  802. }
  803. /// <summary>
  804. /// 存储标志修改事件
  805. /// </summary>
  806. /// <param name="sender"></param>
  807. /// <param name="e"></param>
  808. private void 写入存储ToolStripMenuItem_Click(object sender, EventArgs e)
  809. {
  810. 写入存储ToolStripMenuItem.Checked = !写入存储ToolStripMenuItem.Checked;
  811. }
  812. /// <summary>
  813. /// 1s定时器
  814. /// </summary>
  815. /// <param name="sender"></param>
  816. /// <param name="e"></param>
  817. private void timer_1s_Tick(object sender, EventArgs e)
  818. {
  819. //时间更新
  820. label_SystemTime.Text = "系统时间:" + DateTime.Now.ToString();
  821. //MC_Runinfo超时清除
  822. if (MC_RunInfo_Refresh == false)
  823. {
  824. MC_RunInfo_Refresh_Cnt++;
  825. if (MC_RunInfo_Refresh_Cnt > 5)//5s
  826. {
  827. MC_RunInfo_Clear();
  828. MC_RunInfo_Refresh_Cnt = 0;
  829. }
  830. }
  831. else
  832. {
  833. MC_RunInfo_Refresh_Cnt = 0;
  834. }
  835. MC_RunInfo_Refresh = false;
  836. //BMS_RunInfo超时清除
  837. if (BMS_RunInfo_Refresh == false)
  838. {
  839. BMS_RunInfo_Refresh_Cnt++;
  840. if (BMS_RunInfo_Refresh_Cnt > 5)//5s
  841. {
  842. richTextBox_OBC_BMS_RunInfo.Clear();
  843. BMS_RunInfo_Refresh_Cnt = 0;
  844. }
  845. }
  846. else
  847. {
  848. BMS_RunInfo_Refresh_Cnt = 0;
  849. }
  850. BMS_RunInfo_Refresh = false;
  851. //故障码超时清除
  852. if (MC_ErrorCode_Refresh == false)
  853. {
  854. MC_ErrorCode_Refresh_Cnt++;
  855. if (MC_ErrorCode_Refresh_Cnt > 5)//5s
  856. {
  857. textBox_ErrorCode.Text = "---";
  858. MC_ErrorCode_Refresh_Cnt = 0;
  859. }
  860. }
  861. else
  862. {
  863. MC_ErrorCode_Refresh_Cnt = 0;
  864. }
  865. MC_ErrorCode_Refresh = false;
  866. //CDL连接超时
  867. #if false
  868. if (mySerialProcess.CDL_Online_Flag == false)
  869. {
  870. mySerialProcess.CDL_OnlineCheck_Cnt++;
  871. if (mySerialProcess.CDL_OnlineCheck_Cnt > 3)//3s
  872. {
  873. timer_1s.Enabled = false;
  874. MessageBox.Show("连接失败!", "提示");
  875. timer_1s.Enabled = true;
  876. //关闭串口
  877. mySerialProcess.SerialClose();
  878. toolStripComboBox_ComNum.Enabled = true;
  879. toolStripComboBox_Baudrate.Enabled = true;
  880. 刷新ToolStripMenuItem.Enabled = true;
  881. 连接ToolStripMenuItem.Text = "连接";
  882. 连接ToolStripMenuItem.Checked = false;
  883. label_COM_Sta.Text = "连接状态:未连接";
  884. }
  885. }
  886. #endif
  887. //OBC定时发送设置档位
  888. if (checkBox_OBC_StartSetGearSt.Checked == true)
  889. {
  890. var CtrlCode = new byte[2];
  891. if (comboBox_OBC_SetGearST.SelectedIndex == 6)//档位
  892. CtrlCode[0] = 0x22;
  893. else
  894. CtrlCode[0] = (byte)comboBox_OBC_SetGearST.SelectedIndex;
  895. if (comboBox_OBC_LightSw.SelectedIndex == 0)//车灯
  896. CtrlCode[1] = 0xF1;
  897. else
  898. CtrlCode[1] = 0xF0;
  899. mySerialProcess.SendCmd((ushort)0x731, (byte)0x0C, (ushort)0x3002, CtrlCode);
  900. }
  901. //OBC定时发送查询电池
  902. if (checkBox_OBC_StartReadBMS.Checked == true)
  903. {
  904. mySerialProcess.SendCmd((ushort)0x732, (byte)0x11, (ushort)0x5000, null);
  905. }
  906. }
  907. /// <summary>
  908. /// 运行信息清除
  909. /// </summary>
  910. private void MC_RunInfo_Clear()
  911. {
  912. foreach (Control c in groupBox2.Controls)
  913. {
  914. if ((c is TextBox))
  915. {
  916. if(c.Name != textBox_ErrorCode.ToString())
  917. c.Text = "---";
  918. }
  919. }
  920. }
  921. /// <summary>
  922. /// 读取马达参数
  923. /// </summary>
  924. /// <param name="sender"></param>
  925. /// <param name="e"></param>
  926. private void button_Read_MotorParam_Click(object sender, EventArgs e)
  927. {
  928. richTextBox_MotorParam.Clear();
  929. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3A00, null);
  930. }
  931. /// <summary>
  932. /// 写入马达参数
  933. /// </summary>
  934. /// <param name="sender"></param>
  935. /// <param name="e"></param>
  936. private void button_Write_Click(object sender, EventArgs e)
  937. {
  938. var ConfigParam = new byte[40];
  939. for (int i = 0; i < 40; i++)
  940. ConfigParam[i] = 0;
  941. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  942. ushort uwDataTemp = 0;
  943. string[] strDataTemp = richTextBox_MotorParam.Text.Split(new string[] { ", "},StringSplitOptions.None);
  944. try
  945. {
  946. for (int i = 0; i < strDataTemp.Length; i++)
  947. {
  948. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  949. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  950. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  951. }
  952. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3B28, ConfigParam);
  953. }
  954. catch (Exception)
  955. {
  956. timer_1s.Enabled = false;
  957. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  958. timer_1s.Enabled = true;
  959. }
  960. }
  961. /// <summary>
  962. /// 读取整车信息
  963. /// </summary>
  964. /// <param name="sender"></param>
  965. /// <param name="e"></param>
  966. private void button_ReadBikeParam_Click(object sender, EventArgs e)
  967. {
  968. richTextBox_BikeParam.Clear();
  969. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3C00, null);
  970. }
  971. /// <summary>
  972. /// 写入整车信息
  973. /// </summary>
  974. /// <param name="sender"></param>
  975. /// <param name="e"></param>
  976. private void button_WriteBikeParam_Click(object sender, EventArgs e)
  977. {
  978. var ConfigParam = new byte[26];
  979. for (int i = 0; i < 26; i++)
  980. ConfigParam[i] = 0;
  981. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  982. ushort uwDataTemp = 0;
  983. string[] strDataTemp = richTextBox_BikeParam.Text.Split(new string[] { ", " }, StringSplitOptions.None);
  984. try
  985. {
  986. for (int i = 0; i < strDataTemp.Length; i++)
  987. {
  988. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  989. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  990. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  991. }
  992. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3D1A, ConfigParam);
  993. }
  994. catch (Exception)
  995. {
  996. timer_1s.Enabled = false;
  997. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  998. timer_1s.Enabled = true;
  999. }
  1000. }
  1001. /// <summary>
  1002. /// 读取控制器参数
  1003. /// </summary>
  1004. /// <param name="sender"></param>
  1005. /// <param name="e"></param>
  1006. private void button_ReadControlParam_Click(object sender, EventArgs e)
  1007. {
  1008. richTextBox_ControlParam.Clear();
  1009. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3E00, null);
  1010. }
  1011. /// <summary>
  1012. /// 写入控制器参数
  1013. /// </summary>
  1014. /// <param name="sender"></param>
  1015. /// <param name="e"></param>
  1016. private void button_WriteControlParam_Click(object sender, EventArgs e)
  1017. {
  1018. var ConfigParam = new byte[34];
  1019. for (int i = 0; i < 34; i++)
  1020. ConfigParam[i] = 0;
  1021. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  1022. ushort uwDataTemp = 0;
  1023. string[] strDataTemp = richTextBox_ControlParam.Text.Split(new string[] { ", " }, StringSplitOptions.None);
  1024. try
  1025. {
  1026. for (int i = 0; i < strDataTemp.Length; i++)
  1027. {
  1028. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  1029. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  1030. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  1031. }
  1032. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3F22, ConfigParam);
  1033. }
  1034. catch (Exception)
  1035. {
  1036. timer_1s.Enabled = false;
  1037. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1038. timer_1s.Enabled = true;
  1039. }
  1040. }
  1041. /// <summary>
  1042. /// 读取助力参数
  1043. /// </summary>
  1044. /// <param name="sender"></param>
  1045. /// <param name="e"></param>
  1046. private void button_ReadAssistParam_Click(object sender, EventArgs e)
  1047. {
  1048. var ConfigParam = new byte[4];
  1049. int AssistNum = 0;
  1050. try
  1051. {
  1052. AssistNum = Convert.ToInt32(comboBox_AssistTorque.Text);
  1053. ConfigParam[0] = (byte)(AssistNum & 0xFF);
  1054. ConfigParam[1] = (byte)(AssistNum >> 8);
  1055. AssistNum = Convert.ToInt32(comboBox_AssistCadence.Text);
  1056. ConfigParam[2] = (byte)(AssistNum & 0xFF);
  1057. ConfigParam[3] = (byte)(AssistNum >> 8);
  1058. richTextBox_AssistParam.Clear();
  1059. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4304, ConfigParam);
  1060. }
  1061. catch (Exception)
  1062. {
  1063. timer_1s.Enabled = false;
  1064. MessageBox.Show("请选择助力参数编号!","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
  1065. timer_1s.Enabled = true;
  1066. }
  1067. }
  1068. /// <summary>
  1069. /// 写入助力参数
  1070. /// </summary>
  1071. /// <param name="sender"></param>
  1072. /// <param name="e"></param>
  1073. private void button_WriteAssistParam_Click(object sender, EventArgs e)
  1074. {
  1075. var ConfigParam = new byte[80];
  1076. for (int i = 0; i < 80; i++)
  1077. ConfigParam[i] = 0;
  1078. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  1079. ulong ulDataTemp = 0;
  1080. string[] strDataTemp = richTextBox_AssistParam.Text.Split(new string[] { ", " }, StringSplitOptions.None);
  1081. int index_1 = 0, index_2 = 0;
  1082. try
  1083. {
  1084. for (int i = 0; i < strDataTemp.Length; i++)
  1085. {
  1086. if (strDataTemp[i].Split('=')[0] == "转矩曲线.a")
  1087. {
  1088. index_1 = i;
  1089. break;
  1090. }
  1091. }
  1092. for (int i = 0; i < strDataTemp.Length; i++)
  1093. {
  1094. if (strDataTemp[i].Split('=')[0] == "踏频曲线.d")
  1095. {
  1096. index_2 = i;
  1097. break;
  1098. }
  1099. }
  1100. for (int i = 0; i < strDataTemp.Length; i++)
  1101. {
  1102. ulDataTemp = Convert.ToUInt32(strDataTemp[i].Split('=')[1]);
  1103. if (i < index_1)
  1104. {
  1105. ConfigParam[2 * i + 4] = (byte)ulDataTemp;
  1106. ConfigParam[2 * i + 5] = (byte)(ulDataTemp >> 8);
  1107. }
  1108. else if (i <= index_2)
  1109. {
  1110. ConfigParam[4 * i - 4] = (byte)ulDataTemp;
  1111. ConfigParam[4 * i - 3] = (byte)(ulDataTemp >> 8);
  1112. ConfigParam[4 * i - 2] = (byte)(ulDataTemp >> 16);
  1113. ConfigParam[4 * i - 1] = (byte)(ulDataTemp >> 24);
  1114. }
  1115. else
  1116. {
  1117. ConfigParam[2 * i + 20] = (byte)ulDataTemp;
  1118. ConfigParam[2 * i + 21] = (byte)(ulDataTemp >> 8);
  1119. }
  1120. }
  1121. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x4450, ConfigParam);
  1122. }
  1123. catch (Exception)
  1124. {
  1125. timer_1s.Enabled = false;
  1126. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1127. timer_1s.Enabled = true;
  1128. }
  1129. }
  1130. /// <summary>
  1131. /// 读取调试参数
  1132. /// </summary>
  1133. /// <param name="sender"></param>
  1134. /// <param name="e"></param>
  1135. private void button_ReadDebugParam_Click(object sender, EventArgs e)
  1136. {
  1137. richTextBox_DebugParam.Clear();
  1138. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4500, null);
  1139. }
  1140. /// <summary>
  1141. /// 写入调试参数
  1142. /// </summary>
  1143. /// <param name="sender"></param>
  1144. /// <param name="e"></param>
  1145. private void button_WriteDebugParam_Click(object sender, EventArgs e)
  1146. {
  1147. var ConfigParam = new byte[52];
  1148. for (int i = 0; i < 52; i++)
  1149. ConfigParam[i] = 0;
  1150. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  1151. ushort uwDataTemp = 0;
  1152. string[] strDataTemp = richTextBox_DebugParam.Text.Split(new string[] { ", " }, StringSplitOptions.None);
  1153. try
  1154. {
  1155. for (int i = 0; i < strDataTemp.Length; i++)
  1156. {
  1157. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  1158. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  1159. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  1160. }
  1161. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x4634, ConfigParam);
  1162. }
  1163. catch (Exception)
  1164. {
  1165. timer_1s.Enabled = false;
  1166. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1167. timer_1s.Enabled = true;
  1168. }
  1169. }
  1170. /// <summary>
  1171. /// 读取历史
  1172. /// </summary>
  1173. /// <param name="sender"></param>
  1174. /// <param name="e"></param>
  1175. private void button_ReadRecord_Click(object sender, EventArgs e)
  1176. {
  1177. richTextBox_Record.Clear();
  1178. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1E00, null);
  1179. }
  1180. /// <summary>
  1181. /// 读取传感器参数
  1182. /// </summary>
  1183. /// <param name="sender"></param>
  1184. /// <param name="e"></param>
  1185. private void button_ReadSensorParam_Click(object sender, EventArgs e)
  1186. {
  1187. richTextBox_SensorParam.Clear();
  1188. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4000, null);
  1189. }
  1190. /// <summary>
  1191. /// 写入力矩传感器标定值
  1192. /// </summary>
  1193. /// <param name="sender"></param>
  1194. /// <param name="e"></param>
  1195. private void button_Write_Cal_Click(object sender, EventArgs e)
  1196. {
  1197. var ConfigParam = new byte[4];
  1198. for (int i = 0; i < 4; i++)
  1199. ConfigParam[i] = 0;
  1200. ushort uwDataTemp = 0;
  1201. try
  1202. {
  1203. uwDataTemp = Convert.ToUInt16(comboBox_TorqueSet.Text);//负载序号
  1204. ConfigParam[0] = (byte)(uwDataTemp & 0xFF);
  1205. ConfigParam[1] = (byte)(uwDataTemp >> 8);
  1206. uwDataTemp = (ushort)(decimal.Parse(textBox_Load.Text) * 10); ;//负载值0.1Nm
  1207. ConfigParam[2] = (byte)(uwDataTemp & 0xFF);
  1208. ConfigParam[3] = (byte)(uwDataTemp >> 8);
  1209. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x4104, ConfigParam);
  1210. }
  1211. catch (Exception)
  1212. {
  1213. timer_1s.Enabled = false;
  1214. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1215. timer_1s.Enabled = true;
  1216. }
  1217. }
  1218. /// <summary>
  1219. /// 写入传感器参数
  1220. /// </summary>
  1221. /// <param name="sender"></param>
  1222. /// <param name="e"></param>
  1223. private void button_WriteSensorParam_Click(object sender, EventArgs e)
  1224. {
  1225. var ConfigParam = new byte[14];
  1226. for (int i = 0; i < 14; i++)
  1227. ConfigParam[i] = 0;
  1228. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  1229. ushort uwDataTemp = 0;
  1230. //传感器参数只写入最后2项
  1231. richTextBox_SensorParam.Text = richTextBox_SensorParam.Text.Substring(richTextBox_SensorParam.Text.LastIndexOf(", ", richTextBox_SensorParam.Text.LastIndexOf(", ") - 1) + 2);
  1232. string[] strDataTemp = richTextBox_SensorParam.Text.Split(new string[] { ", " }, StringSplitOptions.None);
  1233. try
  1234. {
  1235. for (int i = 0; i < strDataTemp.Length; i++)
  1236. {
  1237. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  1238. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  1239. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  1240. }
  1241. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x420E, ConfigParam);
  1242. }
  1243. catch (Exception)
  1244. {
  1245. timer_1s.Enabled = false;
  1246. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1247. timer_1s.Enabled = true;
  1248. }
  1249. }
  1250. /// <summary>
  1251. /// 写入型号
  1252. /// </summary>
  1253. /// <param name="sender"></param>
  1254. /// <param name="e"></param>
  1255. private void button_WriteModel_Click(object sender, EventArgs e)
  1256. {
  1257. string Mode = textBox_Model.Text;
  1258. var ModeArray = new byte[16];
  1259. for (ushort i = 0; i < Mode.Length; i++)
  1260. {
  1261. ModeArray[i] = (byte)Mode[i];
  1262. }
  1263. if (Mode.Length < 16)
  1264. {
  1265. ModeArray[Mode.Length] = (byte)'.';
  1266. for (ushort i = 0; i < 16 - Mode.Length - 1; i++)
  1267. {
  1268. ModeArray[Mode.Length + 1 + i] = 0x20;
  1269. }
  1270. }
  1271. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2210, ModeArray);
  1272. }
  1273. /// <summary>
  1274. /// 写入SN
  1275. /// </summary>
  1276. /// <param name="sender"></param>
  1277. /// <param name="e"></param>
  1278. private void button_WriteSN_Click(object sender, EventArgs e)
  1279. {
  1280. string SN = textBox_SN.Text;
  1281. var SNArray = new byte[16];
  1282. for (ushort i = 0; i < SN.Length; i++)
  1283. {
  1284. SNArray[i] = (byte)SN[i];
  1285. }
  1286. if (SN.Length < 16)
  1287. {
  1288. SNArray[SN.Length] = (byte)'.';
  1289. for (ushort i = 0; i < 16 - SN.Length - 1; i++)
  1290. {
  1291. SNArray[SN.Length + 1 + i] = 0x20;
  1292. }
  1293. }
  1294. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2310, SNArray);
  1295. }
  1296. /// <summary>
  1297. /// 查询版本信息
  1298. /// </summary>
  1299. /// <param name="sender"></param>
  1300. /// <param name="e"></param>
  1301. private void button_ReadVersion_Click(object sender, EventArgs e)
  1302. {
  1303. textBox_Model.Text = "---";
  1304. textBox_SN.Text = "---";
  1305. textBox_HW.Text = "---";
  1306. textBox_FW.Text = "---";
  1307. textBox_OBC_ReadModel.Text = "---";
  1308. textBox_OBC_ReadSN.Text = "---";
  1309. textBox_OBC_ReadHW.Text = "---";
  1310. textBox_OBC_ReadFW.Text = "---";
  1311. Class_Motor_Ver.Mode = "---";
  1312. Class_Motor_Ver.SN = "---";
  1313. Class_Motor_Ver.HW = "---";
  1314. Class_Motor_Ver.FW = "---";
  1315. Class_Motor_Ver.Special = "---";
  1316. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
  1317. }
  1318. /// <summary>
  1319. /// 查询自定义1
  1320. /// </summary>
  1321. /// <param name="sender"></param>
  1322. /// <param name="e"></param>
  1323. private void button_ReadUser1_Click(object sender, EventArgs e)
  1324. {
  1325. textBox_User1.Text = "---";
  1326. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1300, null);
  1327. }
  1328. /// <summary>
  1329. /// 查询自定义2
  1330. /// </summary>
  1331. /// <param name="sender"></param>
  1332. /// <param name="e"></param>
  1333. private void button_ReadUser2_Click(object sender, EventArgs e)
  1334. {
  1335. textBox_User2.Text = "---";
  1336. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1500, null);
  1337. }
  1338. /// <summary>
  1339. /// 查询自定义3
  1340. /// </summary>
  1341. /// <param name="sender"></param>
  1342. /// <param name="e"></param>
  1343. private void button_ReadUser3_Click(object sender, EventArgs e)
  1344. {
  1345. textBox_User3.Text = "---";
  1346. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1700, null);
  1347. }
  1348. /// <summary>
  1349. /// 写入自定义字符串1
  1350. /// </summary>
  1351. /// <param name="sender"></param>
  1352. /// <param name="e"></param>
  1353. private void button_WriteUser1_Click(object sender, EventArgs e)
  1354. {
  1355. string User = textBox_User1.Text;
  1356. var UserArray = new byte[16];
  1357. for (ushort i = 0; i < User.Length; i++)
  1358. {
  1359. UserArray[i] = (byte)User[i];
  1360. }
  1361. if (User.Length < 16)
  1362. {
  1363. UserArray[User.Length] = (byte)'.';
  1364. for (ushort i = 0; i < 16 - User.Length - 1; i++)
  1365. {
  1366. UserArray[User.Length + 1 + i] = 0x20;
  1367. }
  1368. }
  1369. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1410, UserArray);
  1370. }
  1371. /// <summary>
  1372. /// 写入自定义字符串2
  1373. /// </summary>
  1374. /// <param name="sender"></param>
  1375. /// <param name="e"></param>
  1376. private void button_WriteUser2_Click(object sender, EventArgs e)
  1377. {
  1378. string User = textBox_User2.Text;
  1379. var UserArray = new byte[16];
  1380. for (ushort i = 0; i < User.Length; i++)
  1381. {
  1382. UserArray[i] = (byte)User[i];
  1383. }
  1384. if (User.Length < 16)
  1385. {
  1386. UserArray[User.Length] = (byte)'.';
  1387. for (ushort i = 0; i < 16 - User.Length - 1; i++)
  1388. {
  1389. UserArray[User.Length + 1 + i] = 0x20;
  1390. }
  1391. }
  1392. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1610, UserArray);
  1393. }
  1394. /// <summary>
  1395. /// 写入自定义字符串3
  1396. /// </summary>
  1397. /// <param name="sender"></param>
  1398. /// <param name="e"></param>
  1399. private void button_WriteUser3_Click(object sender, EventArgs e)
  1400. {
  1401. string User = textBox_User3.Text;
  1402. var UserArray = new byte[16];
  1403. for (ushort i = 0; i < User.Length; i++)
  1404. {
  1405. UserArray[i] = (byte)User[i];
  1406. }
  1407. if (User.Length < 16)
  1408. {
  1409. UserArray[User.Length] = (byte)'.';
  1410. for (ushort i = 0; i < 16 - User.Length - 1; i++)
  1411. {
  1412. UserArray[User.Length + 1 + i] = 0x20;
  1413. }
  1414. }
  1415. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1810, UserArray);
  1416. }
  1417. /// <summary>
  1418. /// 查询生产信息
  1419. /// </summary>
  1420. /// <param name="sender"></param>
  1421. /// <param name="e"></param>
  1422. private void button_ReadMAC_Click(object sender, EventArgs e)
  1423. {
  1424. textBox_MAC.Text = "---";
  1425. textBox_MACAdd.Text = "---";
  1426. textBox_MACDate.Text = "---";
  1427. textBox_PP.Text = "---";
  1428. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1F00, null);
  1429. }
  1430. /// <summary>
  1431. /// 写入生产信息
  1432. /// </summary>
  1433. /// <param name="sender"></param>
  1434. /// <param name="e"></param>
  1435. private void button_WriteMac_Click(object sender, EventArgs e)
  1436. {
  1437. var MACArray = new byte[32];
  1438. //填入生产商
  1439. for (ushort i = 0; i < textBox_MAC.Text.Length; i++)
  1440. {
  1441. MACArray[i] = (byte)textBox_MAC.Text[i];
  1442. }
  1443. if (textBox_MAC.Text.Length < 8)
  1444. {
  1445. MACArray[textBox_MAC.Text.Length] = (byte)'.';
  1446. for (ushort i = 0; i < 8 - textBox_MAC.Text.Length - 1; i++)
  1447. {
  1448. MACArray[textBox_MAC.Text.Length + 1 + i] = 0x20;
  1449. }
  1450. }
  1451. //填入生产地
  1452. for (ushort i = 0; i < textBox_MACAdd.Text.Length; i++)
  1453. {
  1454. MACArray[8 + i] = (byte)textBox_MACAdd.Text[i];
  1455. }
  1456. if (textBox_MACAdd.Text.Length < 8)
  1457. {
  1458. MACArray[textBox_MACAdd.Text.Length + 8] = (byte)'.';
  1459. for (ushort i = 0; i < 8 - textBox_MACAdd.Text.Length - 1; i++)
  1460. {
  1461. MACArray[textBox_MACAdd.Text.Length + 1 + i + 8] = 0x20;
  1462. }
  1463. }
  1464. //填入生产日期
  1465. for (ushort i = 0; i < textBox_MACDate.Text.Length; i++)
  1466. {
  1467. MACArray[16 + i] = (byte)textBox_MACDate.Text[i];
  1468. }
  1469. if (textBox_MACDate.Text.Length < 8)
  1470. {
  1471. MACArray[textBox_MACDate.Text.Length + 16] = (byte)'.';
  1472. for (ushort i = 0; i < 8 - textBox_MACDate.Text.Length - 1; i++)
  1473. {
  1474. MACArray[textBox_MACDate.Text.Length + 1 + i + 16] = 0x20;
  1475. }
  1476. }
  1477. //填入品牌
  1478. for (ushort i = 0; i < textBox_PP.Text.Length; i++)
  1479. {
  1480. MACArray[24 + i] = (byte)textBox_PP.Text[i];
  1481. }
  1482. if (textBox_PP.Text.Length < 8)
  1483. {
  1484. MACArray[textBox_PP.Text.Length + 24] = (byte)'.';
  1485. for (ushort i = 0; i < 8 - textBox_PP.Text.Length - 1; i++)
  1486. {
  1487. MACArray[textBox_PP.Text.Length + 1 + i + 24] = 0x20;
  1488. }
  1489. }
  1490. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2420, MACArray);
  1491. }
  1492. /// <summary>
  1493. /// 查询密钥
  1494. /// </summary>
  1495. /// <param name="sender"></param>
  1496. /// <param name="e"></param>
  1497. private void button_ReadKey_Click(object sender, EventArgs e)
  1498. {
  1499. textBox_Key.Text = "---";
  1500. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1000, null);
  1501. }
  1502. /// <summary>
  1503. /// 写入校验密钥
  1504. /// </summary>
  1505. /// <param name="sender"></param>
  1506. /// <param name="e"></param>
  1507. private void button_WriteKey_Click(object sender, EventArgs e)
  1508. {
  1509. string Key = textBox_Key.Text;
  1510. var KeyArray = new byte[8];
  1511. if (Key.Length != 8)
  1512. {
  1513. timer_1s.Enabled = false;
  1514. MessageBox.Show("请检查字符长度是否为8", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1515. timer_1s.Enabled = true;
  1516. return;
  1517. }
  1518. for (ushort i = 0; i < Key.Length; i++)
  1519. {
  1520. KeyArray[i] = (byte)Key[i];
  1521. }
  1522. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1108, KeyArray);
  1523. }
  1524. /// <summary>
  1525. /// 查询在线检测结果
  1526. /// </summary>
  1527. /// <param name="sender"></param>
  1528. /// <param name="e"></param>
  1529. private void button_ReadOnLine_Click(object sender, EventArgs e)
  1530. {
  1531. textBox_Online.Text = "---";
  1532. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2100, null);
  1533. }
  1534. /// <summary>
  1535. /// 系统清除
  1536. /// </summary>
  1537. /// <param name="sender"></param>
  1538. /// <param name="e"></param>
  1539. private void button_SystemClear_Click(object sender, EventArgs e)
  1540. {
  1541. var Code = new byte[5];
  1542. Code[0] = (byte)'C';
  1543. Code[1] = (byte)'L';
  1544. Code[2] = (byte)'E';
  1545. Code[3] = (byte)'A';
  1546. Code[4] = (byte)'R';
  1547. timer_1s.Enabled = false;
  1548. if (MessageBox.Show("系统清除后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1549. {
  1550. if (MessageBox.Show("请再次确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1551. {
  1552. if (MessageBox.Show("请最后确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1553. {
  1554. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2605, Code);
  1555. }
  1556. }
  1557. }
  1558. timer_1s.Enabled = true;
  1559. }
  1560. /// <summary>
  1561. /// 参数还原
  1562. /// </summary>
  1563. /// <param name="sender"></param>
  1564. /// <param name="e"></param>
  1565. private void button_Recovery_Click(object sender, EventArgs e)
  1566. {
  1567. var Code = new byte[8];
  1568. Code[0] = (byte)'R';
  1569. Code[1] = (byte)'E';
  1570. Code[2] = (byte)'C';
  1571. Code[3] = (byte)'O';
  1572. Code[4] = (byte)'V';
  1573. Code[5] = (byte)'E';
  1574. Code[6] = (byte)'R';
  1575. Code[7] = (byte)'Y';
  1576. timer_1s.Enabled = false;
  1577. if (MessageBox.Show("参数还原后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1578. {
  1579. if (MessageBox.Show("请再次确认", "确认还原?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1580. {
  1581. if (MessageBox.Show("请最后确认", "确认还原?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1582. {
  1583. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2708, Code);
  1584. }
  1585. }
  1586. }
  1587. timer_1s.Enabled = true;
  1588. }
  1589. /// <summary>
  1590. /// 系统复位
  1591. /// </summary>
  1592. /// <param name="sender"></param>
  1593. /// <param name="e"></param>
  1594. private void button_Reset_Click(object sender, EventArgs e)
  1595. {
  1596. var Code = new byte[5];
  1597. Code[0] = (byte)'R';
  1598. Code[1] = (byte)'E';
  1599. Code[2] = (byte)'S';
  1600. Code[3] = (byte)'E';
  1601. Code[4] = (byte)'T';
  1602. timer_1s.Enabled = false;
  1603. if (MessageBox.Show("系统将重启", "确认重启?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1604. {
  1605. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2505, Code);
  1606. }
  1607. timer_1s.Enabled = true;
  1608. }
  1609. /// <summary>
  1610. /// 记录清除
  1611. /// </summary>
  1612. /// <param name="sender"></param>
  1613. /// <param name="e"></param>
  1614. private void button_ClearLog_Click(object sender, EventArgs e)
  1615. {
  1616. var Code = new byte[9];
  1617. Code[0] = (byte)'L';
  1618. Code[1] = (byte)'O';
  1619. Code[2] = (byte)'G';
  1620. Code[3] = (byte)' ';
  1621. Code[4] = (byte)'C';
  1622. Code[5] = (byte)'L';
  1623. Code[6] = (byte)'E';
  1624. Code[7] = (byte)'A';
  1625. Code[8] = (byte)'R';
  1626. timer_1s.Enabled = false;
  1627. if (MessageBox.Show("历史记录信息清除后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1628. {
  1629. if (MessageBox.Show("请再次确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1630. {
  1631. if (MessageBox.Show("请最后确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1632. {
  1633. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3909, Code);
  1634. }
  1635. }
  1636. }
  1637. timer_1s.Enabled = true;
  1638. }
  1639. /// <summary>
  1640. /// 打开系统计算器
  1641. /// </summary>
  1642. /// <param name="sender"></param>
  1643. /// <param name="e"></param>
  1644. private void 计算器ToolStripMenuItem_Click(object sender, EventArgs e)
  1645. {
  1646. System.Diagnostics.Process.Start("calc.exe");
  1647. }
  1648. /// <summary>
  1649. ///故障转换和显示
  1650. /// </summary>
  1651. List<string> ErrorInfo = new List<string>{ "过流保护", "低压保护", "过压保护", "堵转保护","过热保护","速度传感器故障","力矩传感器故障","霍尔故障",
  1652. "马达缺相","NTC故障","BMS校验失败","","OBC校验失败","MCU故障","踏频传感器故障","指拨故障","MOS短路","电压异常","","","","","","","","","","","","",
  1653. "",""};
  1654. private string ErrorInfoDisplay(UInt32 Code)
  1655. {
  1656. string Result = "";
  1657. if (Code == 0)
  1658. {
  1659. Result = "无故障";
  1660. }
  1661. else
  1662. {
  1663. for (int i = 0; i < 32; i++)
  1664. {
  1665. if ((Code & 0x01) == 0x01)
  1666. {
  1667. Result += ErrorInfo[i] + " ";
  1668. }
  1669. Code >>= 1;
  1670. }
  1671. }
  1672. return Result;
  1673. }
  1674. /// <summary>
  1675. /// 鼠标滑过故障码显示具体内容
  1676. /// </summary>
  1677. /// <param name="sender"></param>
  1678. /// <param name="e"></param>
  1679. private void textBox_ErrorCode_MouseHover(object sender, EventArgs e)
  1680. {
  1681. // 创建the ToolTip
  1682. ToolTip toolTip1 = new ToolTip();
  1683. // 设置显示样式
  1684. toolTip1.AutoPopDelay = 30000;//提示信息的可见时间
  1685. toolTip1.InitialDelay = 50;//事件触发多久后出现提示
  1686. toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  1687. toolTip1.ShowAlways = true;//是否显示提示框
  1688. // 设置伴随的对象.
  1689. string ErrorCode = this.textBox_ErrorCode.Text;
  1690. UInt32 Code = ErrorCode.Contains("H") ? Convert.ToUInt32(ErrorCode.Split('H')[0], 16) : 0;
  1691. toolTip1.SetToolTip(this.textBox_ErrorCode, ErrorInfoDisplay(Code));//设置故障内容显示
  1692. }
  1693. /// <summary>
  1694. /// 数据记录
  1695. /// </summary>
  1696. private void DataAutoSave()
  1697. {
  1698. if (记录数据ToolStripMenuItem.Checked == true)
  1699. {
  1700. if (SaveFilename == string.Empty)
  1701. {
  1702. return;
  1703. }
  1704. string SaveData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace(" ", "_") + " ";
  1705. foreach (Control c in groupBox3.Controls)
  1706. {
  1707. if (c is TextBox)
  1708. {
  1709. SaveData += c.Text + " , ";
  1710. }
  1711. }
  1712. SaveData += "\r\n";
  1713. System.IO.File.AppendAllText(SaveFilename, SaveData);//sb.ToString());
  1714. }
  1715. }
  1716. /// <summary>
  1717. /// 打开或关闭数据记录
  1718. /// </summary>
  1719. /// <param name="sender"></param>
  1720. /// <param name="e"></param>
  1721. private void 记录数据ToolStripMenuItem_Click(object sender, EventArgs e)
  1722. {
  1723. if (记录数据ToolStripMenuItem.Checked == false)
  1724. {
  1725. if (mySerialProcess.mySerial.IsOpen == true)
  1726. {
  1727. SaveFilename = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_运行信息" + ".txt";
  1728. timer_1s.Enabled = false;
  1729. MessageBox.Show("开始记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  1730. timer_1s.Enabled = true;
  1731. 记录数据ToolStripMenuItem.Checked = true;
  1732. }
  1733. else
  1734. {
  1735. timer_1s.Enabled = false;
  1736. MessageBox.Show("请连接电机", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  1737. timer_1s.Enabled = true;
  1738. }
  1739. }
  1740. else
  1741. {
  1742. 记录数据ToolStripMenuItem.Checked = false;
  1743. timer_1s.Enabled = false;
  1744. MessageBox.Show("停止记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  1745. timer_1s.Enabled = true;
  1746. }
  1747. }
  1748. /// <summary>
  1749. /// 修改写入的助力力矩曲线编号
  1750. /// </summary>
  1751. /// <param name="sender"></param>
  1752. /// <param name="e"></param>
  1753. private void comboBox_AssistTorque_SelectionChangeCommitted(object sender, EventArgs e)
  1754. {
  1755. }
  1756. /// <summary>
  1757. /// 修改写入的助力踏频曲线编号
  1758. /// </summary>
  1759. /// <param name="sender"></param>
  1760. /// <param name="e"></param>
  1761. private void comboBox_AssistCadence_SelectionChangeCommitted(object sender, EventArgs e)
  1762. {
  1763. }
  1764. /// <summary>
  1765. /// 工作模式选择事件
  1766. /// </summary>
  1767. /// <param name="sender"></param>
  1768. /// <param name="e"></param>
  1769. private void comboBox_WorkMode_SelectionChangeCommitted(object sender, EventArgs e)
  1770. {
  1771. var RunMode = new byte[1];
  1772. if (comboBox_WorkMode.Text == "配置模式")
  1773. RunMode[0] = 0x01;
  1774. else
  1775. RunMode[0] = 0x00;
  1776. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, RunMode);
  1777. }
  1778. /// <summary>
  1779. /// 助力档位选择事件
  1780. /// </summary>
  1781. /// <param name="sender"></param>
  1782. /// <param name="e"></param>
  1783. private void comboBox_GearSt_SelectionChangeCommitted(object sender, EventArgs e)
  1784. {
  1785. var CtrlCode = new byte[2];
  1786. if (comboBox_GearSt.SelectedIndex == 6)//档位
  1787. CtrlCode[0] = 0x22;
  1788. else
  1789. CtrlCode[0] = (byte)comboBox_GearSt.SelectedIndex;
  1790. if (comboBox_LightSwitch.SelectedIndex == 0)//车灯
  1791. CtrlCode[1] = 0xF1;
  1792. else
  1793. CtrlCode[1] = 0xF0;
  1794. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  1795. }
  1796. /// <summary>
  1797. /// 仪表启动控制档位开关事件
  1798. /// </summary>
  1799. /// <param name="sender"></param>
  1800. /// <param name="e"></param>
  1801. private void checkBox_OBC_StartSetGearSt_CheckedChanged(object sender, EventArgs e)
  1802. {
  1803. if (checkBox_OBC_StartSetGearSt.Checked == true)
  1804. {
  1805. //检查串口是否打开
  1806. if (!mySerialProcess.mySerial.IsOpen)
  1807. {
  1808. timer_1s.Enabled = false;
  1809. MessageBox.Show("请先连接串口!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  1810. timer_1s.Enabled = true;
  1811. checkBox_OBC_StartSetGearSt.Checked = false;
  1812. return;
  1813. }
  1814. //检查是否选择档位和打开车灯
  1815. if ((comboBox_OBC_SetGearST.SelectedIndex == -1) || (comboBox_OBC_LightSw.SelectedIndex == -1))
  1816. {
  1817. timer_1s.Enabled = false;
  1818. MessageBox.Show("请检查档位设置和大灯开关!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  1819. timer_1s.Enabled = true;
  1820. checkBox_OBC_StartSetGearSt.Checked = false;
  1821. return;
  1822. }
  1823. //检查串口是否打开
  1824. if (!mySerialProcess.mySerial.IsOpen)
  1825. {
  1826. timer_1s.Enabled = false;
  1827. MessageBox.Show("请检查是否打开串口!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  1828. timer_1s.Enabled = true;
  1829. checkBox_OBC_StartSetGearSt.Checked = false;
  1830. return;
  1831. }
  1832. }
  1833. }
  1834. /// <summary>
  1835. /// OBC查询电机用户参数
  1836. /// </summary>
  1837. /// <param name="sender"></param>
  1838. /// <param name="e"></param>
  1839. private void button_OBC_ReadUserInfo_Click(object sender, EventArgs e)
  1840. {
  1841. richTextBox_OBC_ReadUserInfo.Clear();
  1842. mySerialProcess.SendCmd((ushort)0x731, (byte)0x11, (ushort)0x3300, null);
  1843. }
  1844. /// <summary>
  1845. /// OBC设置电机用户参数
  1846. /// </summary>
  1847. /// <param name="sender"></param>
  1848. /// <param name="e"></param>
  1849. private void button_OBC_WriteUserInfo_Click(object sender, EventArgs e)
  1850. {
  1851. var UserInfoCode = new byte[8];
  1852. for (int i = 0; i < UserInfoCode.Length; i++)
  1853. UserInfoCode[i] = 0;
  1854. if (comboBox_OBC_StartMode.SelectedIndex == -1)//检查数据有效性
  1855. {
  1856. timer_1s.Enabled = false;
  1857. MessageBox.Show("请选择启动模式!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1858. timer_1s.Enabled = true;
  1859. return;
  1860. }
  1861. if (comboBox_OBC_AssistFunc.SelectedIndex == -1)//检查数据有效性
  1862. {
  1863. timer_1s.Enabled = false;
  1864. MessageBox.Show("请选择助力方案!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1865. timer_1s.Enabled = true;
  1866. return;
  1867. }
  1868. UserInfoCode[0] = (byte)(int)numericUpDown_OBC_WheelAdj.Value;
  1869. UserInfoCode[1] = (byte)(comboBox_OBC_StartMode.SelectedIndex + 1);
  1870. UserInfoCode[2] = (byte)(comboBox_OBC_AssistFunc.SelectedIndex + 1);
  1871. mySerialProcess.SendCmd((ushort)0x731, (byte)0x16, (ushort)0x3408, UserInfoCode);
  1872. }
  1873. /// <summary>
  1874. /// OBC查询电机版本信息
  1875. /// </summary>
  1876. /// <param name="sender"></param>
  1877. /// <param name="e"></param>
  1878. private void button_OBC_ReadVerInfo_Click(object sender, EventArgs e)
  1879. {
  1880. textBox_Model.Text = "---";
  1881. textBox_SN.Text = "---";
  1882. textBox_HW.Text = "---";
  1883. textBox_FW.Text = "---";
  1884. textBox_OBC_ReadModel.Text = "---";
  1885. textBox_OBC_ReadSN.Text = "---";
  1886. textBox_OBC_ReadHW.Text = "---";
  1887. textBox_OBC_ReadFW.Text = "---";
  1888. Class_Motor_Ver.Mode = "---";
  1889. Class_Motor_Ver.SN = "---";
  1890. Class_Motor_Ver.HW = "---";
  1891. Class_Motor_Ver.FW = "---";
  1892. Class_Motor_Ver.Special = "---";
  1893. mySerialProcess.SendCmd((ushort)0x731, (byte)0x11, (ushort)0x3900, null);
  1894. }
  1895. /// <summary>
  1896. /// OBC读取骑行历史
  1897. /// </summary>
  1898. /// <param name="sender"></param>
  1899. /// <param name="e"></param>
  1900. private void button_OBC_ReadRideInfo_Click(object sender, EventArgs e)
  1901. {
  1902. textBox_OBC_TRIP_KM.Text = "";
  1903. textBox_OBC_TRIP_TIME.Text = "";
  1904. textBox_OBC_ODO_KM.Text = "";
  1905. textBox_OBC_ODO_TIME.Text = "";
  1906. mySerialProcess.SendCmd((ushort)0x731, (byte)0x11, (ushort)0x3500, null);
  1907. }
  1908. /// <summary>
  1909. /// OBC清除TRIP里程
  1910. /// </summary>
  1911. /// <param name="sender"></param>
  1912. /// <param name="e"></param>
  1913. private void button_OBC_ClearTrip_Click(object sender, EventArgs e)
  1914. {
  1915. var Code = new byte[5];
  1916. Code[0] = (byte)'C';
  1917. Code[1] = (byte)'L';
  1918. Code[2] = (byte)'E';
  1919. Code[3] = (byte)'A';
  1920. Code[4] = (byte)'R';
  1921. timer_1s.Enabled = false;
  1922. if (MessageBox.Show("TRIP清除后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1923. mySerialProcess.SendCmd((ushort)0x731, (byte)0x16, (ushort)0x3605, Code);
  1924. timer_1s.Enabled = true;
  1925. }
  1926. private void checkBox_OBC_StartReadBMS_CheckedChanged(object sender, EventArgs e)
  1927. {
  1928. if (checkBox_OBC_StartReadBMS.Checked == true)
  1929. {
  1930. //检查串口是否打开
  1931. if (!mySerialProcess.mySerial.IsOpen)
  1932. {
  1933. timer_1s.Enabled = false;
  1934. MessageBox.Show("请检查是否打开串口!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  1935. timer_1s.Enabled = true;
  1936. checkBox_OBC_StartReadBMS.Checked = false;
  1937. return;
  1938. }
  1939. }
  1940. }
  1941. }
  1942. }