Form1.cs 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193
  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 0xB83A://调试参数
  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 0xB904://存储标志
  436. {
  437. unchecked
  438. {
  439. this.Invoke((EventHandler)(delegate
  440. {
  441. ushort Flag = 0;
  442. Flag = (ushort)(Data[1] * 256 + Data[0]);//EEPROM存储标志
  443. if (Flag == 0)
  444. {
  445. radioButton_EE_SaveNo.Checked = true;
  446. radioButton_EE_SaveYes.Checked = false;
  447. }
  448. else
  449. {
  450. radioButton_EE_SaveNo.Checked = false;
  451. radioButton_EE_SaveYes.Checked = true;
  452. }
  453. Flag = (ushort)(Data[3] * 256 + Data[2]);//SIP偏移校准存储标志
  454. if (Flag == 0)
  455. {
  456. radioButton_SIP_SaveNo.Checked = true;
  457. radioButton_SIP_SaveYes.Checked = false;
  458. }
  459. else
  460. {
  461. radioButton_SIP_SaveNo.Checked = false;
  462. radioButton_SIP_SaveYes.Checked = true;
  463. }
  464. }));
  465. }
  466. break;
  467. }
  468. case 0x1240://电机版本信息
  469. {
  470. unchecked
  471. {
  472. this.Invoke((EventHandler)(delegate
  473. {
  474. //电机型号
  475. textBox_Model.Text = "";
  476. textBox_OBC_ReadModel.Text = "";
  477. for (ushort i = 0; i < 16; i++)
  478. {
  479. if (Data[i] == 0x2E)
  480. {
  481. break;
  482. }
  483. textBox_Model.Text += ((char)Data[i]).ToString();
  484. textBox_OBC_ReadModel.Text += ((char)Data[i]).ToString();
  485. }
  486. Class_Motor_Ver.Mode = textBox_Model.Text;
  487. //电机SN
  488. textBox_SN.Text = "";
  489. textBox_OBC_ReadSN.Text = "";
  490. for (ushort i = 0; i < 16; i++)
  491. {
  492. if (Data[16 + i] == 0x2E)
  493. {
  494. break;
  495. }
  496. textBox_SN.Text += ((char)Data[16 + i]).ToString();
  497. textBox_OBC_ReadSN.Text += ((char)Data[16 + i]).ToString();
  498. }
  499. Class_Motor_Ver.SN = textBox_SN.Text;
  500. //电机HW
  501. textBox_HW.Text = "";
  502. textBox_OBC_ReadHW.Text = "";
  503. for (ushort i = 0; i < 16; i++)
  504. {
  505. if (Data[32 + i] == 0x2E)
  506. {
  507. break;
  508. }
  509. textBox_HW.Text += ((char)Data[32 + i]).ToString();
  510. textBox_OBC_ReadHW.Text += ((char)Data[32 + i]).ToString();
  511. }
  512. Class_Motor_Ver.HW = textBox_HW.Text;
  513. //电机FW
  514. textBox_FW.Text = "";
  515. textBox_OBC_ReadFW.Text = "";
  516. for (ushort i = 0; i < 16; i++)
  517. {
  518. if (Data[48 + i] == 0x2E)
  519. {
  520. break;
  521. }
  522. if (Data[48 + i] == 'r')
  523. {
  524. Data[48 + i] = (byte)'.';
  525. }
  526. textBox_FW.Text += ((char)Data[48 + i]).ToString();
  527. textBox_OBC_ReadFW.Text += ((char)Data[48 + i]).ToString();
  528. }
  529. Class_Motor_Ver.FW = textBox_FW.Text;
  530. }));
  531. }
  532. break;
  533. }
  534. case 0xA610://自定义1
  535. {
  536. unchecked
  537. {
  538. this.Invoke((EventHandler)(delegate
  539. {
  540. textBox_User1.Text = "";
  541. for (ushort i = 0; i < 16; i++)
  542. {
  543. if (Data[i] == 0x2E)
  544. {
  545. break;
  546. }
  547. textBox_User1.Text += ((char)Data[i]).ToString();
  548. }
  549. }));
  550. }
  551. break;
  552. }
  553. case 0xA710://自定义2
  554. {
  555. unchecked
  556. {
  557. this.Invoke((EventHandler)(delegate
  558. {
  559. textBox_User2.Text = "";
  560. for (ushort i = 0; i < 16; i++)
  561. {
  562. if (Data[i] == 0x2E)
  563. {
  564. break;
  565. }
  566. textBox_User2.Text += ((char)Data[i]).ToString();
  567. }
  568. }));
  569. }
  570. break;
  571. }
  572. case 0xA810://自定义1
  573. {
  574. unchecked
  575. {
  576. this.Invoke((EventHandler)(delegate
  577. {
  578. textBox_User3.Text = "";
  579. for (ushort i = 0; i < 16; i++)
  580. {
  581. if (Data[i] == 0x2E)
  582. {
  583. break;
  584. }
  585. textBox_User3.Text += ((char)Data[i]).ToString();
  586. }
  587. }));
  588. }
  589. break;
  590. }
  591. case 0xA520://生产信息
  592. {
  593. unchecked
  594. {
  595. this.Invoke((EventHandler)(delegate
  596. {
  597. //生产商
  598. textBox_MAC.Text = "";
  599. for (ushort i = 0; i < 8; i++)
  600. {
  601. if (Data[i] == 0x2E)
  602. {
  603. break;
  604. }
  605. textBox_MAC.Text += ((char)Data[i]).ToString();
  606. }
  607. //生产地
  608. textBox_MACAdd.Text = "";
  609. for (ushort i = 0; i < 8; i++)
  610. {
  611. if (Data[8 + i] == 0x2E)
  612. {
  613. break;
  614. }
  615. textBox_MACAdd.Text += ((char)Data[8 + i]).ToString();
  616. }
  617. //生产日期
  618. textBox_MACDate.Text = "";
  619. for (ushort i = 0; i < 8; i++)
  620. {
  621. if (Data[16 + i] == 0x2E)
  622. {
  623. break;
  624. }
  625. textBox_MACDate.Text += ((char)Data[16 + i]).ToString();
  626. }
  627. //品牌信息
  628. textBox_PP.Text = "";
  629. for (ushort i = 0; i < 8; i++)
  630. {
  631. if (Data[24 + i] == 0x2E)
  632. {
  633. break;
  634. }
  635. textBox_PP.Text += ((char)Data[24 + i]).ToString();
  636. }
  637. }));
  638. }
  639. break;
  640. }
  641. case 0xA408://密钥
  642. {
  643. unchecked
  644. {
  645. this.Invoke((EventHandler)(delegate
  646. {
  647. textBox_Key.Text = "";
  648. for (ushort i = 0; i < 8; i++)
  649. {
  650. textBox_Key.Text += ((char)Data[i]).ToString();
  651. }
  652. }));
  653. }
  654. break;
  655. }
  656. case 0x1401://在线检测结果
  657. {
  658. unchecked
  659. {
  660. this.Invoke((EventHandler)(delegate
  661. {
  662. textBox_Online.Text = Convert.ToString(Data[3], 16).PadLeft(2, '0').ToUpper() + "H";
  663. }));
  664. }
  665. break;
  666. }
  667. case 0x1510://骑行历史信息
  668. {
  669. unchecked
  670. {
  671. this.Invoke((EventHandler)(delegate
  672. {
  673. long DataTemp32;
  674. //ODO里程
  675. DataTemp32 = (long)((Data[3] << 24) + (Data[2] << 16) + (Data[1] << 8) + Data[0]);
  676. textBox_OBC_ODO_KM.Text = ((float)DataTemp32 / 10f).ToString("0.0") + " km";
  677. //ODO时间
  678. DataTemp32 = (long)((Data[7] << 24) + (Data[6] << 16) + (Data[5] << 8) + Data[4]);
  679. if (DataTemp32 >= 60 * 100000)
  680. textBox_OBC_ODO_TIME.Text = Convert.ToString(DataTemp32 / 60) + " h";
  681. else
  682. textBox_OBC_ODO_TIME.Text = Convert.ToString(DataTemp32 / 60) + " h " + Convert.ToString(DataTemp32 % 60) + " m";
  683. //TRIP里程
  684. DataTemp32 = (long)((Data[11] << 24) + (Data[10] << 16) + (Data[9] << 8) + Data[8]);
  685. textBox_OBC_TRIP_KM.Text = ((float)DataTemp32 / 10f).ToString("0.0") + " km";
  686. //TRIP时间
  687. DataTemp32 = (long)((Data[15] << 24) + (Data[14] << 16) + (Data[13] << 8) + Data[12]);
  688. if (DataTemp32 >= 60 * 100000)
  689. textBox_OBC_TRIP_TIME.Text = Convert.ToString(DataTemp32 / 60) + " h";
  690. else
  691. textBox_OBC_TRIP_TIME.Text = Convert.ToString(DataTemp32 / 60) + " h " + Convert.ToString(DataTemp32 % 60) + " m";
  692. }));
  693. }
  694. break;
  695. }
  696. default: break;
  697. }
  698. }
  699. #endregion
  700. #region 解析BMS广播的命令
  701. else if (ID == 0x720)
  702. {
  703. switch (CmdTemp)
  704. {
  705. case 0x1010://BMS运行信息
  706. {
  707. unchecked
  708. {
  709. this.Invoke((EventHandler)(delegate
  710. {
  711. BMS_RunInfo_Refresh = true;
  712. richTextBox_OBC_BMS_RunInfo.Clear();
  713. //电压
  714. uData16 = (ushort)(Data[1] * 256 + Data[0]);
  715. richTextBox_OBC_BMS_RunInfo.AppendText("电压:" + Convert.ToString(uData16) + " mV" + "\r\n");
  716. //电流
  717. uData16 = (ushort)(Data[3] * 256 + Data[2]);
  718. richTextBox_OBC_BMS_RunInfo.AppendText("电流:" + Convert.ToString(uData16) + " mA" + "\r\n");
  719. //剩余容量
  720. uData16 = (ushort)(Data[5] * 256 + Data[4]);
  721. richTextBox_OBC_BMS_RunInfo.AppendText("剩余容量:" + Convert.ToString(uData16) + " mAh" + "\r\n");
  722. //满充容量
  723. uData16 = (ushort)(Data[7] * 256 + Data[6]);
  724. richTextBox_OBC_BMS_RunInfo.AppendText("满充容量:" + Convert.ToString(uData16) + " mV" + "\r\n");
  725. //电芯温度
  726. richTextBox_OBC_BMS_RunInfo.AppendText("电芯温度:" + Convert.ToString((int)(Data[8] - 40) + " ℃" + "\r\n"));
  727. //剩余电量
  728. richTextBox_OBC_BMS_RunInfo.AppendText("剩余电量:" + Convert.ToString(Data[9]) + " %" + "\r\n");
  729. //电池状态
  730. richTextBox_OBC_BMS_RunInfo.AppendText("电池状态:" + Convert.ToString(Data[10], 16).PadLeft(2, '0').ToUpper() + " H" + "\r\n");
  731. //SOH
  732. richTextBox_OBC_BMS_RunInfo.AppendText("电池寿命:" + Convert.ToString(Data[11]) + " %" + "\r\n");
  733. }));
  734. }
  735. break;
  736. }
  737. default: break;
  738. }
  739. }
  740. #endregion
  741. #region 解析电机发送OBC的命令
  742. else if (ID == 0x713)
  743. {
  744. switch (CmdTemp)
  745. {
  746. case 0x5408://电机用户参数
  747. {
  748. unchecked
  749. {
  750. this.Invoke((EventHandler)(delegate
  751. {
  752. richTextBox_OBC_ReadUserInfo.Clear();
  753. //默认周长
  754. richTextBox_OBC_ReadUserInfo.AppendText("默认周长:" + Convert.ToString((int)Data[0]) + " cm" + "\r\n");
  755. //启动模式
  756. richTextBox_OBC_ReadUserInfo.AppendText("启动模式:" + ((Data[1] == 0x01) ? "柔和" : (Data[1] == 0x02) ? "正常" : (Data[1] == 0x03) ? "强劲" : "无效") + "\r\n");
  757. comboBox_OBC_StartMode.SelectedIndex = Data[1] - 1;
  758. //限速
  759. richTextBox_OBC_ReadUserInfo.AppendText("限速:" + Convert.ToString(Data[2]) + " km/h" + "\r\n");
  760. //周长微调
  761. numericUpDown_OBC_WheelAdj.Value = (int)(Data[3] > 128 ? (Data[3] - 256) : Data[3]);
  762. richTextBox_OBC_ReadUserInfo.AppendText("周长微调:" + Convert.ToString(numericUpDown_OBC_WheelAdj.Value) + " cm" + "\r\n");
  763. //助力方案
  764. richTextBox_OBC_ReadUserInfo.AppendText("助力方案:" + Convert.ToString(Data[4]) + "\r\n");
  765. comboBox_OBC_AssistFunc.SelectedIndex = Data[4] - 1;
  766. }));
  767. }
  768. break;
  769. }
  770. case 0x5303://电机应答反馈
  771. {
  772. unchecked
  773. {
  774. this.Invoke((EventHandler)(delegate
  775. {
  776. timer_1s.Enabled = false;
  777. MessageBox.Show("OK", "反馈指令", MessageBoxButtons.OK, MessageBoxIcon.Information);
  778. timer_1s.Enabled = true;
  779. }));
  780. }
  781. break;
  782. }
  783. default: break;
  784. }
  785. }
  786. #endregion
  787. }
  788. #endregion
  789. /// <summary>
  790. /// 端口连接事件
  791. /// </summary>
  792. /// <param name="sender"></param>
  793. /// <param name="e"></param>
  794. private void 连接ToolStripMenuItem_Click(object sender, System.EventArgs e)
  795. {
  796. bool result = false;
  797. if (连接ToolStripMenuItem.Text == "连接")
  798. {
  799. result = mySerialProcess.SerialOpen(toolStripComboBox_ComNum.Text, toolStripComboBox_Baudrate.Text);
  800. if (result)
  801. {
  802. toolStripComboBox_ComNum.Enabled = false;
  803. toolStripComboBox_Baudrate.Enabled = false;
  804. 刷新ToolStripMenuItem.Enabled = false;
  805. 连接ToolStripMenuItem.Text = "断开";
  806. 连接ToolStripMenuItem.Checked = true;
  807. label_COM_Sta.Text = toolStripComboBox_ComNum.Text + "已连接," + toolStripComboBox_Baudrate.Text;
  808. }
  809. }
  810. else if (连接ToolStripMenuItem.Text == "断开")
  811. {
  812. result = mySerialProcess.SerialClose();
  813. if (result)
  814. {
  815. toolStripComboBox_ComNum.Enabled = true;
  816. toolStripComboBox_Baudrate.Enabled = true;
  817. 刷新ToolStripMenuItem.Enabled = true;
  818. 连接ToolStripMenuItem.Text = "连接";
  819. 连接ToolStripMenuItem.Checked = false;
  820. label_COM_Sta.Text = "连接状态:未连接";
  821. }
  822. }
  823. }
  824. /// <summary>
  825. /// 端口号刷新事件
  826. /// </summary>
  827. /// <param name="sender"></param>
  828. /// <param name="e"></param>
  829. private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
  830. {
  831. toolStripComboBox_ComNum.Items.Clear();
  832. toolStripComboBox_ComNum.Items.AddRange(mySerialProcess.refreshPort());
  833. toolStripComboBox_ComNum.SelectedIndex = toolStripComboBox_ComNum.Items.Count > 0 ? 0 : -1;
  834. }
  835. /// <summary>
  836. /// 转速调节事件
  837. /// </summary>
  838. /// <param name="sender"></param>
  839. /// <param name="e"></param>
  840. private void numericUpDown_SpeedAdj_ValueChanged(object sender, EventArgs e)
  841. {
  842. var Data = new byte[1];
  843. Data[0] = (byte)numericUpDown_SpeedAdj.Value;
  844. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2C01, Data);
  845. }
  846. /// <summary>
  847. /// 存储标志修改事件
  848. /// </summary>
  849. /// <param name="sender"></param>
  850. /// <param name="e"></param>
  851. private void 写入存储ToolStripMenuItem_Click(object sender, EventArgs e)
  852. {
  853. 写入存储ToolStripMenuItem.Checked = !写入存储ToolStripMenuItem.Checked;
  854. }
  855. /// <summary>
  856. /// 1s定时器
  857. /// </summary>
  858. /// <param name="sender"></param>
  859. /// <param name="e"></param>
  860. private void timer_1s_Tick(object sender, EventArgs e)
  861. {
  862. //时间更新
  863. label_SystemTime.Text = "系统时间:" + DateTime.Now.ToString();
  864. //MC_Runinfo超时清除
  865. if (MC_RunInfo_Refresh == false)
  866. {
  867. MC_RunInfo_Refresh_Cnt++;
  868. if (MC_RunInfo_Refresh_Cnt > 5)//5s
  869. {
  870. MC_RunInfo_Clear();
  871. MC_RunInfo_Refresh_Cnt = 0;
  872. }
  873. }
  874. else
  875. {
  876. MC_RunInfo_Refresh_Cnt = 0;
  877. }
  878. MC_RunInfo_Refresh = false;
  879. //BMS_RunInfo超时清除
  880. if (BMS_RunInfo_Refresh == false)
  881. {
  882. BMS_RunInfo_Refresh_Cnt++;
  883. if (BMS_RunInfo_Refresh_Cnt > 5)//5s
  884. {
  885. richTextBox_OBC_BMS_RunInfo.Clear();
  886. BMS_RunInfo_Refresh_Cnt = 0;
  887. }
  888. }
  889. else
  890. {
  891. BMS_RunInfo_Refresh_Cnt = 0;
  892. }
  893. BMS_RunInfo_Refresh = false;
  894. //故障码超时清除
  895. if (MC_ErrorCode_Refresh == false)
  896. {
  897. MC_ErrorCode_Refresh_Cnt++;
  898. if (MC_ErrorCode_Refresh_Cnt > 5)//5s
  899. {
  900. textBox_ErrorCode.Text = "---";
  901. MC_ErrorCode_Refresh_Cnt = 0;
  902. }
  903. }
  904. else
  905. {
  906. MC_ErrorCode_Refresh_Cnt = 0;
  907. }
  908. MC_ErrorCode_Refresh = false;
  909. //CDL连接超时
  910. #if false
  911. if (mySerialProcess.CDL_Online_Flag == false)
  912. {
  913. mySerialProcess.CDL_OnlineCheck_Cnt++;
  914. if (mySerialProcess.CDL_OnlineCheck_Cnt > 3)//3s
  915. {
  916. timer_1s.Enabled = false;
  917. MessageBox.Show("连接失败!", "提示");
  918. timer_1s.Enabled = true;
  919. //关闭串口
  920. mySerialProcess.SerialClose();
  921. toolStripComboBox_ComNum.Enabled = true;
  922. toolStripComboBox_Baudrate.Enabled = true;
  923. 刷新ToolStripMenuItem.Enabled = true;
  924. 连接ToolStripMenuItem.Text = "连接";
  925. 连接ToolStripMenuItem.Checked = false;
  926. label_COM_Sta.Text = "连接状态:未连接";
  927. }
  928. }
  929. #endif
  930. //OBC定时发送设置档位
  931. if (checkBox_OBC_StartSetGearSt.Checked == true)
  932. {
  933. var CtrlCode = new byte[2];
  934. if (comboBox_OBC_SetGearST.SelectedIndex == 6)//档位
  935. CtrlCode[0] = 0x22;
  936. else
  937. CtrlCode[0] = (byte)comboBox_OBC_SetGearST.SelectedIndex;
  938. if (comboBox_OBC_LightSw.SelectedIndex == 0)//车灯
  939. CtrlCode[1] = 0xF1;
  940. else
  941. CtrlCode[1] = 0xF0;
  942. mySerialProcess.SendCmd((ushort)0x731, (byte)0x0C, (ushort)0x3002, CtrlCode);
  943. }
  944. //OBC定时发送查询电池
  945. if (checkBox_OBC_StartReadBMS.Checked == true)
  946. {
  947. mySerialProcess.SendCmd((ushort)0x732, (byte)0x11, (ushort)0x5000, null);
  948. }
  949. }
  950. /// <summary>
  951. /// 运行信息清除
  952. /// </summary>
  953. private void MC_RunInfo_Clear()
  954. {
  955. foreach (Control c in groupBox2.Controls)
  956. {
  957. if ((c is TextBox))
  958. {
  959. if(c.Name != textBox_ErrorCode.ToString())
  960. c.Text = "---";
  961. }
  962. }
  963. }
  964. /// <summary>
  965. /// 读取马达参数
  966. /// </summary>
  967. /// <param name="sender"></param>
  968. /// <param name="e"></param>
  969. private void button_Read_MotorParam_Click(object sender, EventArgs e)
  970. {
  971. richTextBox_MotorParam.Clear();
  972. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3A00, null);
  973. }
  974. /// <summary>
  975. /// 写入马达参数
  976. /// </summary>
  977. /// <param name="sender"></param>
  978. /// <param name="e"></param>
  979. private void button_Write_Click(object sender, EventArgs e)
  980. {
  981. var ConfigParam = new byte[40];
  982. for (int i = 0; i < 40; i++)
  983. ConfigParam[i] = 0;
  984. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  985. ushort uwDataTemp = 0;
  986. string[] strDataTemp = richTextBox_MotorParam.Text.Split(new string[] { ", "},StringSplitOptions.None);
  987. try
  988. {
  989. for (int i = 0; i < strDataTemp.Length; i++)
  990. {
  991. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  992. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  993. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  994. }
  995. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3B28, ConfigParam);
  996. }
  997. catch (Exception)
  998. {
  999. timer_1s.Enabled = false;
  1000. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1001. timer_1s.Enabled = true;
  1002. }
  1003. }
  1004. /// <summary>
  1005. /// 读取整车信息
  1006. /// </summary>
  1007. /// <param name="sender"></param>
  1008. /// <param name="e"></param>
  1009. private void button_ReadBikeParam_Click(object sender, EventArgs e)
  1010. {
  1011. richTextBox_BikeParam.Clear();
  1012. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3C00, null);
  1013. }
  1014. /// <summary>
  1015. /// 写入整车信息
  1016. /// </summary>
  1017. /// <param name="sender"></param>
  1018. /// <param name="e"></param>
  1019. private void button_WriteBikeParam_Click(object sender, EventArgs e)
  1020. {
  1021. var ConfigParam = new byte[26];
  1022. for (int i = 0; i < 26; i++)
  1023. ConfigParam[i] = 0;
  1024. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  1025. ushort uwDataTemp = 0;
  1026. string[] strDataTemp = richTextBox_BikeParam.Text.Split(new string[] { ", " }, StringSplitOptions.None);
  1027. try
  1028. {
  1029. for (int i = 0; i < strDataTemp.Length; i++)
  1030. {
  1031. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  1032. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  1033. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  1034. }
  1035. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3D1A, ConfigParam);
  1036. }
  1037. catch (Exception)
  1038. {
  1039. timer_1s.Enabled = false;
  1040. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1041. timer_1s.Enabled = true;
  1042. }
  1043. }
  1044. /// <summary>
  1045. /// 读取控制器参数
  1046. /// </summary>
  1047. /// <param name="sender"></param>
  1048. /// <param name="e"></param>
  1049. private void button_ReadControlParam_Click(object sender, EventArgs e)
  1050. {
  1051. richTextBox_ControlParam.Clear();
  1052. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3E00, null);
  1053. }
  1054. /// <summary>
  1055. /// 写入控制器参数
  1056. /// </summary>
  1057. /// <param name="sender"></param>
  1058. /// <param name="e"></param>
  1059. private void button_WriteControlParam_Click(object sender, EventArgs e)
  1060. {
  1061. var ConfigParam = new byte[34];
  1062. for (int i = 0; i < 34; i++)
  1063. ConfigParam[i] = 0;
  1064. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  1065. ushort uwDataTemp = 0;
  1066. string[] strDataTemp = richTextBox_ControlParam.Text.Split(new string[] { ", " }, StringSplitOptions.None);
  1067. try
  1068. {
  1069. for (int i = 0; i < strDataTemp.Length; i++)
  1070. {
  1071. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  1072. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  1073. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  1074. }
  1075. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3F22, ConfigParam);
  1076. }
  1077. catch (Exception)
  1078. {
  1079. timer_1s.Enabled = false;
  1080. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1081. timer_1s.Enabled = true;
  1082. }
  1083. }
  1084. /// <summary>
  1085. /// 读取助力参数
  1086. /// </summary>
  1087. /// <param name="sender"></param>
  1088. /// <param name="e"></param>
  1089. private void button_ReadAssistParam_Click(object sender, EventArgs e)
  1090. {
  1091. var ConfigParam = new byte[4];
  1092. int AssistNum = 0;
  1093. try
  1094. {
  1095. AssistNum = Convert.ToInt32(comboBox_AssistTorque.Text);
  1096. ConfigParam[0] = (byte)(AssistNum & 0xFF);
  1097. ConfigParam[1] = (byte)(AssistNum >> 8);
  1098. AssistNum = Convert.ToInt32(comboBox_AssistCadence.Text);
  1099. ConfigParam[2] = (byte)(AssistNum & 0xFF);
  1100. ConfigParam[3] = (byte)(AssistNum >> 8);
  1101. richTextBox_AssistParam.Clear();
  1102. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4304, ConfigParam);
  1103. }
  1104. catch (Exception)
  1105. {
  1106. timer_1s.Enabled = false;
  1107. MessageBox.Show("请选择助力参数编号!","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
  1108. timer_1s.Enabled = true;
  1109. }
  1110. }
  1111. /// <summary>
  1112. /// 写入助力参数
  1113. /// </summary>
  1114. /// <param name="sender"></param>
  1115. /// <param name="e"></param>
  1116. private void button_WriteAssistParam_Click(object sender, EventArgs e)
  1117. {
  1118. var ConfigParam = new byte[80];
  1119. for (int i = 0; i < 80; i++)
  1120. ConfigParam[i] = 0;
  1121. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  1122. ulong ulDataTemp = 0;
  1123. string[] strDataTemp = richTextBox_AssistParam.Text.Split(new string[] { ", " }, StringSplitOptions.None);
  1124. int index_1 = 0, index_2 = 0;
  1125. try
  1126. {
  1127. for (int i = 0; i < strDataTemp.Length; i++)
  1128. {
  1129. if (strDataTemp[i].Split('=')[0] == "转矩曲线.a")
  1130. {
  1131. index_1 = i;
  1132. break;
  1133. }
  1134. }
  1135. for (int i = 0; i < strDataTemp.Length; i++)
  1136. {
  1137. if (strDataTemp[i].Split('=')[0] == "踏频曲线.d")
  1138. {
  1139. index_2 = i;
  1140. break;
  1141. }
  1142. }
  1143. for (int i = 0; i < strDataTemp.Length; i++)
  1144. {
  1145. ulDataTemp = Convert.ToUInt32(strDataTemp[i].Split('=')[1]);
  1146. if (i < index_1)
  1147. {
  1148. ConfigParam[2 * i + 4] = (byte)ulDataTemp;
  1149. ConfigParam[2 * i + 5] = (byte)(ulDataTemp >> 8);
  1150. }
  1151. else if (i <= index_2)
  1152. {
  1153. ConfigParam[4 * i - 4] = (byte)ulDataTemp;
  1154. ConfigParam[4 * i - 3] = (byte)(ulDataTemp >> 8);
  1155. ConfigParam[4 * i - 2] = (byte)(ulDataTemp >> 16);
  1156. ConfigParam[4 * i - 1] = (byte)(ulDataTemp >> 24);
  1157. }
  1158. else
  1159. {
  1160. ConfigParam[2 * i + 20] = (byte)ulDataTemp;
  1161. ConfigParam[2 * i + 21] = (byte)(ulDataTemp >> 8);
  1162. }
  1163. }
  1164. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x4450, ConfigParam);
  1165. }
  1166. catch (Exception)
  1167. {
  1168. timer_1s.Enabled = false;
  1169. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1170. timer_1s.Enabled = true;
  1171. }
  1172. }
  1173. /// <summary>
  1174. /// 读取调试参数
  1175. /// </summary>
  1176. /// <param name="sender"></param>
  1177. /// <param name="e"></param>
  1178. private void button_ReadDebugParam_Click(object sender, EventArgs e)
  1179. {
  1180. richTextBox_DebugParam.Clear();
  1181. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4500, null);
  1182. }
  1183. /// <summary>
  1184. /// 写入调试参数
  1185. /// </summary>
  1186. /// <param name="sender"></param>
  1187. /// <param name="e"></param>
  1188. private void button_WriteDebugParam_Click(object sender, EventArgs e)
  1189. {
  1190. var ConfigParam = new byte[60];
  1191. for (int i = 0; i < 60; i++)
  1192. ConfigParam[i] = 0;
  1193. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  1194. ushort uwDataTemp = 0;
  1195. string[] strDataTemp = richTextBox_DebugParam.Text.Split(new string[] { ", " }, StringSplitOptions.None);
  1196. try
  1197. {
  1198. for (int i = 0; i < strDataTemp.Length; i++)
  1199. {
  1200. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  1201. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  1202. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  1203. }
  1204. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x463C, ConfigParam);
  1205. }
  1206. catch (Exception)
  1207. {
  1208. timer_1s.Enabled = false;
  1209. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1210. timer_1s.Enabled = true;
  1211. }
  1212. }
  1213. /// <summary>
  1214. /// 读取历史
  1215. /// </summary>
  1216. /// <param name="sender"></param>
  1217. /// <param name="e"></param>
  1218. private void button_ReadRecord_Click(object sender, EventArgs e)
  1219. {
  1220. richTextBox_Record.Clear();
  1221. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1E00, null);
  1222. }
  1223. /// <summary>
  1224. /// 读取传感器参数
  1225. /// </summary>
  1226. /// <param name="sender"></param>
  1227. /// <param name="e"></param>
  1228. private void button_ReadSensorParam_Click(object sender, EventArgs e)
  1229. {
  1230. richTextBox_SensorParam.Clear();
  1231. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4000, null);
  1232. }
  1233. /// <summary>
  1234. /// 写入力矩传感器标定值
  1235. /// </summary>
  1236. /// <param name="sender"></param>
  1237. /// <param name="e"></param>
  1238. private void button_Write_Cal_Click(object sender, EventArgs e)
  1239. {
  1240. var ConfigParam = new byte[4];
  1241. for (int i = 0; i < 4; i++)
  1242. ConfigParam[i] = 0;
  1243. ushort uwDataTemp = 0;
  1244. try
  1245. {
  1246. uwDataTemp = Convert.ToUInt16(comboBox_TorqueSet.Text);//负载序号
  1247. ConfigParam[0] = (byte)(uwDataTemp & 0xFF);
  1248. ConfigParam[1] = (byte)(uwDataTemp >> 8);
  1249. uwDataTemp = (ushort)(decimal.Parse(textBox_Load.Text) * 10); ;//负载值0.1Nm
  1250. ConfigParam[2] = (byte)(uwDataTemp & 0xFF);
  1251. ConfigParam[3] = (byte)(uwDataTemp >> 8);
  1252. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x4104, ConfigParam);
  1253. }
  1254. catch (Exception)
  1255. {
  1256. timer_1s.Enabled = false;
  1257. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1258. timer_1s.Enabled = true;
  1259. }
  1260. }
  1261. /// <summary>
  1262. /// 写入传感器参数
  1263. /// </summary>
  1264. /// <param name="sender"></param>
  1265. /// <param name="e"></param>
  1266. private void button_WriteSensorParam_Click(object sender, EventArgs e)
  1267. {
  1268. var ConfigParam = new byte[14];
  1269. for (int i = 0; i < 14; i++)
  1270. ConfigParam[i] = 0;
  1271. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  1272. ushort uwDataTemp = 0;
  1273. //力矩传感器参数只写入前面14项与力矩传感器相关的参数
  1274. string[] strDataTemp = richTextBox_SensorParam.Text.Split(new string[] { ", " }, StringSplitOptions.None);
  1275. int index = 0;
  1276. try
  1277. {
  1278. for (int i = 0; i < strDataTemp.Length; i++)
  1279. {
  1280. if (strDataTemp[i].Split('=')[0] == "踏频传感器一圈脉冲数")
  1281. {
  1282. index = i;
  1283. break;
  1284. }
  1285. }
  1286. for (int i = 0; i < (strDataTemp.Length - index); i++)
  1287. {
  1288. uwDataTemp = Convert.ToUInt16(strDataTemp[i + index].Split('=')[1]);
  1289. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  1290. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  1291. }
  1292. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x420E, ConfigParam);
  1293. }
  1294. catch (Exception)
  1295. {
  1296. timer_1s.Enabled = false;
  1297. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1298. timer_1s.Enabled = true;
  1299. }
  1300. }
  1301. /// <summary>
  1302. /// 写入型号
  1303. /// </summary>
  1304. /// <param name="sender"></param>
  1305. /// <param name="e"></param>
  1306. private void button_WriteModel_Click(object sender, EventArgs e)
  1307. {
  1308. string Mode = textBox_Model.Text;
  1309. var ModeArray = new byte[16];
  1310. for (ushort i = 0; i < Mode.Length; i++)
  1311. {
  1312. ModeArray[i] = (byte)Mode[i];
  1313. }
  1314. if (Mode.Length < 16)
  1315. {
  1316. ModeArray[Mode.Length] = (byte)'.';
  1317. for (ushort i = 0; i < 16 - Mode.Length - 1; i++)
  1318. {
  1319. ModeArray[Mode.Length + 1 + i] = 0x20;
  1320. }
  1321. }
  1322. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2210, ModeArray);
  1323. }
  1324. /// <summary>
  1325. /// 写入SN
  1326. /// </summary>
  1327. /// <param name="sender"></param>
  1328. /// <param name="e"></param>
  1329. private void button_WriteSN_Click(object sender, EventArgs e)
  1330. {
  1331. string SN = textBox_SN.Text;
  1332. var SNArray = new byte[16];
  1333. for (ushort i = 0; i < SN.Length; i++)
  1334. {
  1335. SNArray[i] = (byte)SN[i];
  1336. }
  1337. if (SN.Length < 16)
  1338. {
  1339. SNArray[SN.Length] = (byte)'.';
  1340. for (ushort i = 0; i < 16 - SN.Length - 1; i++)
  1341. {
  1342. SNArray[SN.Length + 1 + i] = 0x20;
  1343. }
  1344. }
  1345. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2310, SNArray);
  1346. }
  1347. /// <summary>
  1348. /// 查询版本信息
  1349. /// </summary>
  1350. /// <param name="sender"></param>
  1351. /// <param name="e"></param>
  1352. private void button_ReadVersion_Click(object sender, EventArgs e)
  1353. {
  1354. textBox_Model.Text = "---";
  1355. textBox_SN.Text = "---";
  1356. textBox_HW.Text = "---";
  1357. textBox_FW.Text = "---";
  1358. textBox_OBC_ReadModel.Text = "---";
  1359. textBox_OBC_ReadSN.Text = "---";
  1360. textBox_OBC_ReadHW.Text = "---";
  1361. textBox_OBC_ReadFW.Text = "---";
  1362. Class_Motor_Ver.Mode = "---";
  1363. Class_Motor_Ver.SN = "---";
  1364. Class_Motor_Ver.HW = "---";
  1365. Class_Motor_Ver.FW = "---";
  1366. Class_Motor_Ver.Special = "---";
  1367. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
  1368. }
  1369. /// <summary>
  1370. /// 查询自定义1
  1371. /// </summary>
  1372. /// <param name="sender"></param>
  1373. /// <param name="e"></param>
  1374. private void button_ReadUser1_Click(object sender, EventArgs e)
  1375. {
  1376. textBox_User1.Text = "---";
  1377. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1300, null);
  1378. }
  1379. /// <summary>
  1380. /// 查询自定义2
  1381. /// </summary>
  1382. /// <param name="sender"></param>
  1383. /// <param name="e"></param>
  1384. private void button_ReadUser2_Click(object sender, EventArgs e)
  1385. {
  1386. textBox_User2.Text = "---";
  1387. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1500, null);
  1388. }
  1389. /// <summary>
  1390. /// 查询自定义3
  1391. /// </summary>
  1392. /// <param name="sender"></param>
  1393. /// <param name="e"></param>
  1394. private void button_ReadUser3_Click(object sender, EventArgs e)
  1395. {
  1396. textBox_User3.Text = "---";
  1397. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1700, null);
  1398. }
  1399. /// <summary>
  1400. /// 写入自定义字符串1
  1401. /// </summary>
  1402. /// <param name="sender"></param>
  1403. /// <param name="e"></param>
  1404. private void button_WriteUser1_Click(object sender, EventArgs e)
  1405. {
  1406. string User = textBox_User1.Text;
  1407. var UserArray = new byte[16];
  1408. for (ushort i = 0; i < User.Length; i++)
  1409. {
  1410. UserArray[i] = (byte)User[i];
  1411. }
  1412. if (User.Length < 16)
  1413. {
  1414. UserArray[User.Length] = (byte)'.';
  1415. for (ushort i = 0; i < 16 - User.Length - 1; i++)
  1416. {
  1417. UserArray[User.Length + 1 + i] = 0x20;
  1418. }
  1419. }
  1420. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1410, UserArray);
  1421. }
  1422. /// <summary>
  1423. /// 写入自定义字符串2
  1424. /// </summary>
  1425. /// <param name="sender"></param>
  1426. /// <param name="e"></param>
  1427. private void button_WriteUser2_Click(object sender, EventArgs e)
  1428. {
  1429. string User = textBox_User2.Text;
  1430. var UserArray = new byte[16];
  1431. for (ushort i = 0; i < User.Length; i++)
  1432. {
  1433. UserArray[i] = (byte)User[i];
  1434. }
  1435. if (User.Length < 16)
  1436. {
  1437. UserArray[User.Length] = (byte)'.';
  1438. for (ushort i = 0; i < 16 - User.Length - 1; i++)
  1439. {
  1440. UserArray[User.Length + 1 + i] = 0x20;
  1441. }
  1442. }
  1443. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1610, UserArray);
  1444. }
  1445. /// <summary>
  1446. /// 写入自定义字符串3
  1447. /// </summary>
  1448. /// <param name="sender"></param>
  1449. /// <param name="e"></param>
  1450. private void button_WriteUser3_Click(object sender, EventArgs e)
  1451. {
  1452. string User = textBox_User3.Text;
  1453. var UserArray = new byte[16];
  1454. for (ushort i = 0; i < User.Length; i++)
  1455. {
  1456. UserArray[i] = (byte)User[i];
  1457. }
  1458. if (User.Length < 16)
  1459. {
  1460. UserArray[User.Length] = (byte)'.';
  1461. for (ushort i = 0; i < 16 - User.Length - 1; i++)
  1462. {
  1463. UserArray[User.Length + 1 + i] = 0x20;
  1464. }
  1465. }
  1466. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1810, UserArray);
  1467. }
  1468. /// <summary>
  1469. /// 查询生产信息
  1470. /// </summary>
  1471. /// <param name="sender"></param>
  1472. /// <param name="e"></param>
  1473. private void button_ReadMAC_Click(object sender, EventArgs e)
  1474. {
  1475. textBox_MAC.Text = "---";
  1476. textBox_MACAdd.Text = "---";
  1477. textBox_MACDate.Text = "---";
  1478. textBox_PP.Text = "---";
  1479. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1F00, null);
  1480. }
  1481. /// <summary>
  1482. /// 写入生产信息
  1483. /// </summary>
  1484. /// <param name="sender"></param>
  1485. /// <param name="e"></param>
  1486. private void button_WriteMac_Click(object sender, EventArgs e)
  1487. {
  1488. var MACArray = new byte[32];
  1489. //填入生产商
  1490. for (ushort i = 0; i < textBox_MAC.Text.Length; i++)
  1491. {
  1492. MACArray[i] = (byte)textBox_MAC.Text[i];
  1493. }
  1494. if (textBox_MAC.Text.Length < 8)
  1495. {
  1496. MACArray[textBox_MAC.Text.Length] = (byte)'.';
  1497. for (ushort i = 0; i < 8 - textBox_MAC.Text.Length - 1; i++)
  1498. {
  1499. MACArray[textBox_MAC.Text.Length + 1 + i] = 0x20;
  1500. }
  1501. }
  1502. //填入生产地
  1503. for (ushort i = 0; i < textBox_MACAdd.Text.Length; i++)
  1504. {
  1505. MACArray[8 + i] = (byte)textBox_MACAdd.Text[i];
  1506. }
  1507. if (textBox_MACAdd.Text.Length < 8)
  1508. {
  1509. MACArray[textBox_MACAdd.Text.Length + 8] = (byte)'.';
  1510. for (ushort i = 0; i < 8 - textBox_MACAdd.Text.Length - 1; i++)
  1511. {
  1512. MACArray[textBox_MACAdd.Text.Length + 1 + i + 8] = 0x20;
  1513. }
  1514. }
  1515. //填入生产日期
  1516. for (ushort i = 0; i < textBox_MACDate.Text.Length; i++)
  1517. {
  1518. MACArray[16 + i] = (byte)textBox_MACDate.Text[i];
  1519. }
  1520. if (textBox_MACDate.Text.Length < 8)
  1521. {
  1522. MACArray[textBox_MACDate.Text.Length + 16] = (byte)'.';
  1523. for (ushort i = 0; i < 8 - textBox_MACDate.Text.Length - 1; i++)
  1524. {
  1525. MACArray[textBox_MACDate.Text.Length + 1 + i + 16] = 0x20;
  1526. }
  1527. }
  1528. //填入品牌
  1529. for (ushort i = 0; i < textBox_PP.Text.Length; i++)
  1530. {
  1531. MACArray[24 + i] = (byte)textBox_PP.Text[i];
  1532. }
  1533. if (textBox_PP.Text.Length < 8)
  1534. {
  1535. MACArray[textBox_PP.Text.Length + 24] = (byte)'.';
  1536. for (ushort i = 0; i < 8 - textBox_PP.Text.Length - 1; i++)
  1537. {
  1538. MACArray[textBox_PP.Text.Length + 1 + i + 24] = 0x20;
  1539. }
  1540. }
  1541. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2420, MACArray);
  1542. }
  1543. /// <summary>
  1544. /// 查询密钥
  1545. /// </summary>
  1546. /// <param name="sender"></param>
  1547. /// <param name="e"></param>
  1548. private void button_ReadKey_Click(object sender, EventArgs e)
  1549. {
  1550. textBox_Key.Text = "---";
  1551. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1000, null);
  1552. }
  1553. /// <summary>
  1554. /// 写入校验密钥
  1555. /// </summary>
  1556. /// <param name="sender"></param>
  1557. /// <param name="e"></param>
  1558. private void button_WriteKey_Click(object sender, EventArgs e)
  1559. {
  1560. string Key = textBox_Key.Text;
  1561. var KeyArray = new byte[8];
  1562. if (Key.Length != 8)
  1563. {
  1564. timer_1s.Enabled = false;
  1565. MessageBox.Show("请检查字符长度是否为8", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1566. timer_1s.Enabled = true;
  1567. return;
  1568. }
  1569. for (ushort i = 0; i < Key.Length; i++)
  1570. {
  1571. KeyArray[i] = (byte)Key[i];
  1572. }
  1573. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1108, KeyArray);
  1574. }
  1575. /// <summary>
  1576. /// 查询在线检测结果
  1577. /// </summary>
  1578. /// <param name="sender"></param>
  1579. /// <param name="e"></param>
  1580. private void button_ReadOnLine_Click(object sender, EventArgs e)
  1581. {
  1582. textBox_Online.Text = "---";
  1583. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2100, null);
  1584. }
  1585. /// <summary>
  1586. /// 系统清除
  1587. /// </summary>
  1588. /// <param name="sender"></param>
  1589. /// <param name="e"></param>
  1590. private void button_SystemClear_Click(object sender, EventArgs e)
  1591. {
  1592. var Code = new byte[5];
  1593. Code[0] = (byte)'C';
  1594. Code[1] = (byte)'L';
  1595. Code[2] = (byte)'E';
  1596. Code[3] = (byte)'A';
  1597. Code[4] = (byte)'R';
  1598. timer_1s.Enabled = false;
  1599. if (MessageBox.Show("系统清除后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1600. {
  1601. if (MessageBox.Show("请再次确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1602. {
  1603. if (MessageBox.Show("请最后确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1604. {
  1605. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2605, Code);
  1606. }
  1607. }
  1608. }
  1609. timer_1s.Enabled = true;
  1610. }
  1611. /// <summary>
  1612. /// 参数还原
  1613. /// </summary>
  1614. /// <param name="sender"></param>
  1615. /// <param name="e"></param>
  1616. private void button_Recovery_Click(object sender, EventArgs e)
  1617. {
  1618. var Code = new byte[8];
  1619. Code[0] = (byte)'R';
  1620. Code[1] = (byte)'E';
  1621. Code[2] = (byte)'C';
  1622. Code[3] = (byte)'O';
  1623. Code[4] = (byte)'V';
  1624. Code[5] = (byte)'E';
  1625. Code[6] = (byte)'R';
  1626. Code[7] = (byte)'Y';
  1627. timer_1s.Enabled = false;
  1628. if (MessageBox.Show("参数还原后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1629. {
  1630. if (MessageBox.Show("请再次确认", "确认还原?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1631. {
  1632. if (MessageBox.Show("请最后确认", "确认还原?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1633. {
  1634. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2708, Code);
  1635. }
  1636. }
  1637. }
  1638. timer_1s.Enabled = true;
  1639. }
  1640. /// <summary>
  1641. /// 系统复位
  1642. /// </summary>
  1643. /// <param name="sender"></param>
  1644. /// <param name="e"></param>
  1645. private void button_Reset_Click(object sender, EventArgs e)
  1646. {
  1647. var Code = new byte[5];
  1648. Code[0] = (byte)'R';
  1649. Code[1] = (byte)'E';
  1650. Code[2] = (byte)'S';
  1651. Code[3] = (byte)'E';
  1652. Code[4] = (byte)'T';
  1653. timer_1s.Enabled = false;
  1654. if (MessageBox.Show("系统将重启", "确认重启?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1655. {
  1656. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2505, Code);
  1657. }
  1658. timer_1s.Enabled = true;
  1659. }
  1660. /// <summary>
  1661. /// 记录清除
  1662. /// </summary>
  1663. /// <param name="sender"></param>
  1664. /// <param name="e"></param>
  1665. private void button_ClearLog_Click(object sender, EventArgs e)
  1666. {
  1667. var Code = new byte[9];
  1668. Code[0] = (byte)'L';
  1669. Code[1] = (byte)'O';
  1670. Code[2] = (byte)'G';
  1671. Code[3] = (byte)' ';
  1672. Code[4] = (byte)'C';
  1673. Code[5] = (byte)'L';
  1674. Code[6] = (byte)'E';
  1675. Code[7] = (byte)'A';
  1676. Code[8] = (byte)'R';
  1677. timer_1s.Enabled = false;
  1678. if (MessageBox.Show("历史记录信息清除后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1679. {
  1680. if (MessageBox.Show("请再次确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1681. {
  1682. if (MessageBox.Show("请最后确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1683. {
  1684. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3909, Code);
  1685. }
  1686. }
  1687. }
  1688. timer_1s.Enabled = true;
  1689. }
  1690. /// <summary>
  1691. /// 打开系统计算器
  1692. /// </summary>
  1693. /// <param name="sender"></param>
  1694. /// <param name="e"></param>
  1695. private void 计算器ToolStripMenuItem_Click(object sender, EventArgs e)
  1696. {
  1697. System.Diagnostics.Process.Start("calc.exe");
  1698. }
  1699. /// <summary>
  1700. ///故障转换和显示
  1701. /// </summary>
  1702. List<string> ErrorInfo = new List<string>{ "过流保护", "低压保护", "过压保护", "堵转保护","过热保护","速度传感器故障","力矩传感器故障","霍尔故障",
  1703. "马达缺相","NTC故障","BMS校验失败","","OBC校验失败","MCU故障","踏频传感器故障","指拨故障","MOS短路","电压异常","","","","","","","","","","","","",
  1704. "",""};
  1705. private string ErrorInfoDisplay(UInt32 Code)
  1706. {
  1707. string Result = "";
  1708. if (Code == 0)
  1709. {
  1710. Result = "无故障";
  1711. }
  1712. else
  1713. {
  1714. for (int i = 0; i < 32; i++)
  1715. {
  1716. if ((Code & 0x01) == 0x01)
  1717. {
  1718. Result += ErrorInfo[i] + " ";
  1719. }
  1720. Code >>= 1;
  1721. }
  1722. }
  1723. return Result;
  1724. }
  1725. /// <summary>
  1726. /// 鼠标滑过故障码显示具体内容
  1727. /// </summary>
  1728. /// <param name="sender"></param>
  1729. /// <param name="e"></param>
  1730. private void textBox_ErrorCode_MouseHover(object sender, EventArgs e)
  1731. {
  1732. // 创建the ToolTip
  1733. ToolTip toolTip1 = new ToolTip();
  1734. // 设置显示样式
  1735. toolTip1.AutoPopDelay = 30000;//提示信息的可见时间
  1736. toolTip1.InitialDelay = 50;//事件触发多久后出现提示
  1737. toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  1738. toolTip1.ShowAlways = true;//是否显示提示框
  1739. // 设置伴随的对象.
  1740. string ErrorCode = this.textBox_ErrorCode.Text;
  1741. UInt32 Code = ErrorCode.Contains("H") ? Convert.ToUInt32(ErrorCode.Split('H')[0], 16) : 0;
  1742. toolTip1.SetToolTip(this.textBox_ErrorCode, ErrorInfoDisplay(Code));//设置故障内容显示
  1743. }
  1744. /// <summary>
  1745. /// 数据记录
  1746. /// </summary>
  1747. private void DataAutoSave()
  1748. {
  1749. if (记录数据ToolStripMenuItem.Checked == true)
  1750. {
  1751. if (SaveFilename == string.Empty)
  1752. {
  1753. return;
  1754. }
  1755. string SaveData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace(" ", "_") + " ";
  1756. foreach (Control c in groupBox3.Controls)
  1757. {
  1758. if (c is TextBox)
  1759. {
  1760. SaveData += c.Text + " , ";
  1761. }
  1762. }
  1763. SaveData += "\r\n";
  1764. System.IO.File.AppendAllText(SaveFilename, SaveData);//sb.ToString());
  1765. }
  1766. }
  1767. /// <summary>
  1768. /// 打开或关闭数据记录
  1769. /// </summary>
  1770. /// <param name="sender"></param>
  1771. /// <param name="e"></param>
  1772. private void 记录数据ToolStripMenuItem_Click(object sender, EventArgs e)
  1773. {
  1774. if (记录数据ToolStripMenuItem.Checked == false)
  1775. {
  1776. if (mySerialProcess.mySerial.IsOpen == true)
  1777. {
  1778. SaveFilename = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_运行信息" + ".txt";
  1779. timer_1s.Enabled = false;
  1780. MessageBox.Show("开始记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  1781. timer_1s.Enabled = true;
  1782. 记录数据ToolStripMenuItem.Checked = true;
  1783. }
  1784. else
  1785. {
  1786. timer_1s.Enabled = false;
  1787. MessageBox.Show("请连接电机", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  1788. timer_1s.Enabled = true;
  1789. }
  1790. }
  1791. else
  1792. {
  1793. 记录数据ToolStripMenuItem.Checked = false;
  1794. timer_1s.Enabled = false;
  1795. MessageBox.Show("停止记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  1796. timer_1s.Enabled = true;
  1797. }
  1798. }
  1799. /// <summary>
  1800. /// 修改写入的助力力矩曲线编号
  1801. /// </summary>
  1802. /// <param name="sender"></param>
  1803. /// <param name="e"></param>
  1804. private void comboBox_AssistTorque_SelectionChangeCommitted(object sender, EventArgs e)
  1805. {
  1806. }
  1807. /// <summary>
  1808. /// 修改写入的助力踏频曲线编号
  1809. /// </summary>
  1810. /// <param name="sender"></param>
  1811. /// <param name="e"></param>
  1812. private void comboBox_AssistCadence_SelectionChangeCommitted(object sender, EventArgs e)
  1813. {
  1814. }
  1815. /// <summary>
  1816. /// 工作模式选择事件
  1817. /// </summary>
  1818. /// <param name="sender"></param>
  1819. /// <param name="e"></param>
  1820. private void comboBox_WorkMode_SelectionChangeCommitted(object sender, EventArgs e)
  1821. {
  1822. var RunMode = new byte[1];
  1823. if (comboBox_WorkMode.Text == "配置模式")
  1824. RunMode[0] = 0x01;
  1825. else
  1826. RunMode[0] = 0x00;
  1827. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, RunMode);
  1828. }
  1829. /// <summary>
  1830. /// 助力档位选择事件
  1831. /// </summary>
  1832. /// <param name="sender"></param>
  1833. /// <param name="e"></param>
  1834. private void comboBox_GearSt_SelectionChangeCommitted(object sender, EventArgs e)
  1835. {
  1836. var CtrlCode = new byte[2];
  1837. if (comboBox_GearSt.SelectedIndex == 6)//档位
  1838. CtrlCode[0] = 0x22;
  1839. else
  1840. CtrlCode[0] = (byte)comboBox_GearSt.SelectedIndex;
  1841. if (comboBox_LightSwitch.SelectedIndex == 0)//车灯
  1842. CtrlCode[1] = 0xF1;
  1843. else
  1844. CtrlCode[1] = 0xF0;
  1845. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  1846. }
  1847. /// <summary>
  1848. /// 仪表启动控制档位开关事件
  1849. /// </summary>
  1850. /// <param name="sender"></param>
  1851. /// <param name="e"></param>
  1852. private void checkBox_OBC_StartSetGearSt_CheckedChanged(object sender, EventArgs e)
  1853. {
  1854. if (checkBox_OBC_StartSetGearSt.Checked == true)
  1855. {
  1856. //检查串口是否打开
  1857. if (!mySerialProcess.mySerial.IsOpen)
  1858. {
  1859. timer_1s.Enabled = false;
  1860. MessageBox.Show("串口未连接!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  1861. timer_1s.Enabled = true;
  1862. checkBox_OBC_StartSetGearSt.Checked = false;
  1863. return;
  1864. }
  1865. //检查是否选择档位和打开车灯
  1866. if ((comboBox_OBC_SetGearST.SelectedIndex == -1) || (comboBox_OBC_LightSw.SelectedIndex == -1))
  1867. {
  1868. timer_1s.Enabled = false;
  1869. MessageBox.Show("请检查档位设置和大灯开关!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  1870. timer_1s.Enabled = true;
  1871. checkBox_OBC_StartSetGearSt.Checked = false;
  1872. return;
  1873. }
  1874. }
  1875. }
  1876. /// <summary>
  1877. /// OBC查询电机用户参数
  1878. /// </summary>
  1879. /// <param name="sender"></param>
  1880. /// <param name="e"></param>
  1881. private void button_OBC_ReadUserInfo_Click(object sender, EventArgs e)
  1882. {
  1883. richTextBox_OBC_ReadUserInfo.Clear();
  1884. mySerialProcess.SendCmd((ushort)0x731, (byte)0x11, (ushort)0x3300, null);
  1885. }
  1886. /// <summary>
  1887. /// OBC设置电机用户参数
  1888. /// </summary>
  1889. /// <param name="sender"></param>
  1890. /// <param name="e"></param>
  1891. private void button_OBC_WriteUserInfo_Click(object sender, EventArgs e)
  1892. {
  1893. var UserInfoCode = new byte[8];
  1894. for (int i = 0; i < UserInfoCode.Length; i++)
  1895. UserInfoCode[i] = 0;
  1896. if (comboBox_OBC_StartMode.SelectedIndex == -1)//检查数据有效性
  1897. {
  1898. timer_1s.Enabled = false;
  1899. MessageBox.Show("请选择启动模式!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1900. timer_1s.Enabled = true;
  1901. return;
  1902. }
  1903. if (comboBox_OBC_AssistFunc.SelectedIndex == -1)//检查数据有效性
  1904. {
  1905. timer_1s.Enabled = false;
  1906. MessageBox.Show("请选择助力方案!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1907. timer_1s.Enabled = true;
  1908. return;
  1909. }
  1910. UserInfoCode[0] = (byte)(int)numericUpDown_OBC_WheelAdj.Value;
  1911. UserInfoCode[1] = (byte)(comboBox_OBC_StartMode.SelectedIndex + 1);
  1912. UserInfoCode[2] = (byte)(comboBox_OBC_AssistFunc.SelectedIndex + 1);
  1913. mySerialProcess.SendCmd((ushort)0x731, (byte)0x16, (ushort)0x3408, UserInfoCode);
  1914. }
  1915. /// <summary>
  1916. /// OBC查询电机版本信息
  1917. /// </summary>
  1918. /// <param name="sender"></param>
  1919. /// <param name="e"></param>
  1920. private void button_OBC_ReadVerInfo_Click(object sender, EventArgs e)
  1921. {
  1922. textBox_Model.Text = "---";
  1923. textBox_SN.Text = "---";
  1924. textBox_HW.Text = "---";
  1925. textBox_FW.Text = "---";
  1926. textBox_OBC_ReadModel.Text = "---";
  1927. textBox_OBC_ReadSN.Text = "---";
  1928. textBox_OBC_ReadHW.Text = "---";
  1929. textBox_OBC_ReadFW.Text = "---";
  1930. Class_Motor_Ver.Mode = "---";
  1931. Class_Motor_Ver.SN = "---";
  1932. Class_Motor_Ver.HW = "---";
  1933. Class_Motor_Ver.FW = "---";
  1934. Class_Motor_Ver.Special = "---";
  1935. mySerialProcess.SendCmd((ushort)0x731, (byte)0x11, (ushort)0x3900, null);
  1936. }
  1937. /// <summary>
  1938. /// OBC读取骑行历史
  1939. /// </summary>
  1940. /// <param name="sender"></param>
  1941. /// <param name="e"></param>
  1942. private void button_OBC_ReadRideInfo_Click(object sender, EventArgs e)
  1943. {
  1944. textBox_OBC_TRIP_KM.Text = "";
  1945. textBox_OBC_TRIP_TIME.Text = "";
  1946. textBox_OBC_ODO_KM.Text = "";
  1947. textBox_OBC_ODO_TIME.Text = "";
  1948. mySerialProcess.SendCmd((ushort)0x731, (byte)0x11, (ushort)0x3500, null);
  1949. }
  1950. /// <summary>
  1951. /// OBC清除TRIP里程
  1952. /// </summary>
  1953. /// <param name="sender"></param>
  1954. /// <param name="e"></param>
  1955. private void button_OBC_ClearTrip_Click(object sender, EventArgs e)
  1956. {
  1957. var Code = new byte[5];
  1958. Code[0] = (byte)'C';
  1959. Code[1] = (byte)'L';
  1960. Code[2] = (byte)'E';
  1961. Code[3] = (byte)'A';
  1962. Code[4] = (byte)'R';
  1963. timer_1s.Enabled = false;
  1964. if (MessageBox.Show("TRIP清除后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1965. mySerialProcess.SendCmd((ushort)0x731, (byte)0x16, (ushort)0x3605, Code);
  1966. timer_1s.Enabled = true;
  1967. }
  1968. private void checkBox_OBC_StartReadBMS_CheckedChanged(object sender, EventArgs e)
  1969. {
  1970. if (checkBox_OBC_StartReadBMS.Checked == true)
  1971. {
  1972. //检查串口是否打开
  1973. if (!mySerialProcess.mySerial.IsOpen)
  1974. {
  1975. timer_1s.Enabled = false;
  1976. MessageBox.Show("串口未连接!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  1977. timer_1s.Enabled = true;
  1978. checkBox_OBC_StartReadBMS.Checked = false;
  1979. return;
  1980. }
  1981. }
  1982. }
  1983. /// <summary>
  1984. /// 写入力矩传感器参数
  1985. /// </summary>
  1986. /// <param name="sender"></param>
  1987. /// <param name="e"></param>
  1988. private void button_WriteTqSensorParam_Click(object sender, EventArgs e)
  1989. {
  1990. var ConfigParam = new byte[46];
  1991. for (int i = 0; i < 14; i++)
  1992. ConfigParam[i] = 0;
  1993. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  1994. ushort uwDataTemp = 0;
  1995. //力矩传感器参数只写入前面14项与力矩传感器相关的参数
  1996. string[] strDataTemp = richTextBox_SensorParam.Text.Split(new string[] { ", " }, StringSplitOptions.None);
  1997. int index = 0;
  1998. try
  1999. {
  2000. for (int i = 0; i < strDataTemp.Length; i++)
  2001. {
  2002. if (strDataTemp[i].Split('=')[0] == "第四阶段转矩点采集值")
  2003. {
  2004. index = i;
  2005. break;
  2006. }
  2007. }
  2008. for (int i = 0; i < (index + 1); i++)
  2009. {
  2010. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  2011. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  2012. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  2013. }
  2014. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x472E, ConfigParam);
  2015. }
  2016. catch (Exception)
  2017. {
  2018. timer_1s.Enabled = false;
  2019. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2020. timer_1s.Enabled = true;
  2021. }
  2022. }
  2023. /// <summary>
  2024. /// 查询存储标志
  2025. /// </summary>
  2026. /// <param name="sender"></param>
  2027. /// <param name="e"></param>
  2028. private void button_ReadSaveFlag_Click(object sender, EventArgs e)
  2029. {
  2030. radioButton_EE_SaveYes.Checked = false;
  2031. radioButton_EE_SaveNo.Checked = false;
  2032. radioButton_SIP_SaveYes.Checked = false;
  2033. radioButton_SIP_SaveNo.Checked = false;
  2034. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4800, null);
  2035. }
  2036. /// <summary>
  2037. /// 写入存储标志
  2038. /// </summary>
  2039. /// <param name="sender"></param>
  2040. /// <param name="e"></param>
  2041. private void button_WriteSaveFlag_Click(object sender, EventArgs e)
  2042. {
  2043. var SaveFlag = new byte[6];
  2044. for (int i = 0; i < 6; i++)
  2045. SaveFlag[i] = 0;
  2046. if ((radioButton_EE_SaveYes.Checked == false) && (radioButton_EE_SaveNo.Checked == false))
  2047. {
  2048. timer_1s.Enabled = false;
  2049. MessageBox.Show("请选择EEPROM存储标志", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2050. timer_1s.Enabled = true;
  2051. return;
  2052. }
  2053. if ((radioButton_SIP_SaveYes.Checked == false) && (radioButton_SIP_SaveNo.Checked == false))
  2054. {
  2055. timer_1s.Enabled = false;
  2056. MessageBox.Show("请选择SIP校验存储标志", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2057. timer_1s.Enabled = true;
  2058. return;
  2059. }
  2060. SaveFlag[0] = (byte)((checkBox_SaveYes.Checked) ? 0x01 : 0x00);
  2061. SaveFlag[2] = (byte)(radioButton_EE_SaveYes.Checked ? 0x01 : 0x00);
  2062. SaveFlag[3] = 0;
  2063. SaveFlag[4] = (byte)(radioButton_SIP_SaveYes.Checked ? 0x01 : 0x00);
  2064. SaveFlag[5] = 0;
  2065. timer_1s.Enabled = false;
  2066. if (MessageBox.Show("确认修改存储标志?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2067. {
  2068. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x4906, SaveFlag);
  2069. }
  2070. timer_1s.Enabled = true;
  2071. }
  2072. }
  2073. }