Form1.cs 88 KB

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