Form1.cs 146 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366
  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. //生产模式关机指令反馈装
  27. bool PowerOffAckStatus = false;
  28. #endregion
  29. public Form1()
  30. {
  31. InitializeComponent();
  32. }
  33. #region 非独占性延时函数
  34. public static void Delay_ms(int milliSecond)
  35. {
  36. int start = Environment.TickCount;
  37. while (Math.Abs(Environment.TickCount - start) < milliSecond)//毫秒
  38. {
  39. Application.DoEvents();
  40. }
  41. }
  42. #endregion
  43. private void Form1_Load(object sender, System.EventArgs e)
  44. {
  45. //页面初始化
  46. label_BuildTime.Text= "编译时间:"+ System.IO.File.GetLastWriteTime(this.GetType().Assembly.Location).ToString();
  47. //下拉控件初始值设定
  48. comboBox_WorkMode.SelectedIndex = 0;
  49. comboBox_GearSt.SelectedIndex = 0;
  50. comboBox_LightSwitch.SelectedIndex = 1;
  51. comboBox_OBC_SetGearST.SelectedIndex = 0;
  52. comboBox_OBC_LightSw.SelectedIndex = 1;
  53. comboBox_AssistTorque.SelectedIndex = 0;
  54. comboBox_AssistCadence.SelectedIndex = 0;
  55. //combobox事件定义
  56. this.comboBox_WorkMode.SelectedIndexChanged += new System.EventHandler(this.comboBox_WorkMode_SelectedIndexChanged);
  57. this.comboBox_GearSt.SelectedIndexChanged += new System.EventHandler(this.comboBox_GearSt_SelectedIndexChanged);
  58. this.comboBox_LightSwitch.SelectedIndexChanged += new System.EventHandler(this.comboBox_GearSt_SelectedIndexChanged);
  59. this.comboBox_OBC_SetGearST.SelectedIndexChanged += new System.EventHandler(this.comboBox_OBC_SetGearST_SelectedIndexChanged);
  60. this.comboBox_OBC_LightSw.SelectedIndexChanged += new System.EventHandler(this.comboBox_OBC_SetGearST_SelectedIndexChanged);
  61. //端口设置
  62. mySerialProcess.Init();
  63. toolStripComboBox_ComNum.Items.AddRange(mySerialProcess.refreshPort());
  64. toolStripComboBox_ComNum.SelectedIndex = toolStripComboBox_ComNum.Items.Count > 0 ? 0 : -1;
  65. toolStripComboBox_Baudrate.SelectedIndex = toolStripComboBox_Baudrate.Items.IndexOf("115200");
  66. //创建线程,定时解析数据
  67. Thread th = new Thread(Task_SerialProcess);
  68. th.IsBackground = true;
  69. th.Start();
  70. }
  71. #region 串口数据解析线程
  72. /// <summary>
  73. /// 串口数据解析线程
  74. /// </summary>
  75. private void Task_SerialProcess()
  76. {
  77. //创建定时器,定时读取串口数据
  78. System.Timers.Timer timer_ReadSerial = new System.Timers.Timer();
  79. timer_ReadSerial.Enabled = true;
  80. timer_ReadSerial.Interval = 20;
  81. timer_ReadSerial.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_ReadSerial_Tick);
  82. timer_ReadSerial.Start();
  83. while (true)
  84. {
  85. Thread.Sleep(1);
  86. };
  87. }
  88. /// <summary>
  89. /// 串口解析定时器
  90. /// </summary>
  91. /// <param name="sender"></param>
  92. /// <param name="e"></param>
  93. private void timer_ReadSerial_Tick(object sender, EventArgs e)
  94. {
  95. //串口解析
  96. Serial_process(mySerialProcess.buffer);
  97. }
  98. #endregion
  99. #region 数据解析
  100. public void Serial_process(List<byte> buffer)
  101. {
  102. byte DataNum = 0; //记录每条命令数据段的长度
  103. ushort PackageID, Cmd;
  104. byte[] Data = new byte[1024];
  105. while (buffer.Count >= 13)//当buffer中的数据的个数大于等于4个时,即每一帧的数据长度所在位(第4位)出现时
  106. {
  107. if (buffer[0] == 0x55 && buffer[1] == 0xAA)//判断帧头
  108. {
  109. PackageID = (ushort)(buffer[2] * 256 + buffer[3]);
  110. if ((PackageID == 0x0710) || (PackageID == 0x0715) || (PackageID == 0x07FF) || (PackageID == 0x0720) || (PackageID == 0x0713))
  111. {
  112. int CmdLen = buffer[5];
  113. if (buffer.Count < CmdLen + 11) break;//如果接收到的数据没有达到一帧数据的指定长度, 则执行下次循环
  114. buffer.RemoveRange(0, 6);//数据达到要求长度后删去帧头,ID,模式,和命令长度
  115. while (CmdLen > 0) //读取命令段
  116. {
  117. Cmd = (ushort)(buffer[0] * 256 + buffer[1]);
  118. DataNum = (byte)(buffer[1]);//命令字的第二个字节的表示数据位的长度
  119. if (DataNum <= (CmdLen - 2))
  120. {
  121. for (int i = 0; i < DataNum; i++)
  122. {
  123. Data[i] = buffer[i + 2];
  124. }
  125. DataCmdProcess(PackageID, Cmd, Data);//对于不同的命令段,做不同的处理
  126. buffer.RemoveRange(0, DataNum);//移除处理过的数据段
  127. CmdLen -= (DataNum + 2);
  128. if (CmdLen < 2)
  129. {
  130. buffer.RemoveRange(0, CmdLen + 2);
  131. return;
  132. }
  133. }
  134. else
  135. {
  136. buffer.RemoveRange(0, CmdLen + 11);
  137. }
  138. }
  139. }
  140. else
  141. {
  142. buffer.RemoveAt(0);//ID不对
  143. }
  144. }
  145. else
  146. {
  147. buffer.RemoveAt(0);//帧头不对,删除帧头高字节
  148. }
  149. }
  150. }
  151. private void DataCmdProcess(ushort ID, ushort CmdTemp, byte[] Data)
  152. {
  153. int DataTemp;
  154. ushort uData16;
  155. unchecked
  156. {
  157. this.Invoke((EventHandler)(delegate { label_RecCount.Text = Convert.ToString(Convert.ToInt32(label_RecCount.Text) + 1); }));
  158. }
  159. #region 解析发送给CDL的指令
  160. if (ID == 0x7FF)
  161. {
  162. switch (CmdTemp)
  163. {
  164. case 0x1100://
  165. {
  166. unchecked
  167. {
  168. this.Invoke((EventHandler)(delegate
  169. {
  170. //CDL连接成功
  171. mySerialProcess.CDL_Online_Flag = true;
  172. MessageBox.Show("连接成功!", "提示");
  173. }));
  174. }
  175. break;
  176. }
  177. default: break;
  178. }
  179. }
  180. #endregion
  181. #region 解析电机的命令
  182. else if ((ID == 0x715) || (ID == 0x710))
  183. {
  184. switch (CmdTemp)
  185. {
  186. case 0x1020://电机运行信息
  187. {
  188. unchecked
  189. {
  190. this.Invoke((EventHandler)(delegate
  191. {
  192. MC_RunInfo_Refresh = true;
  193. //车速
  194. DataTemp = (int)(Data[1] * 256 + Data[0]);
  195. textBox_RunInfo_BikeSpeed.Text = ((float)DataTemp / 10f).ToString("0.0") + " km/h";
  196. //输出转速
  197. DataTemp = (ushort)(Data[3] * 256 + Data[2]);
  198. textBox_RunInfo_MotorSpeed.Text = Convert.ToString(DataTemp) + " rpm";
  199. Class_MotorSpeed.String = textBox_RunInfo_MotorSpeed.Text;
  200. Class_MotorSpeed.Data = DataTemp;
  201. //电功率
  202. DataTemp = (ushort)(Data[5] * 256 + Data[4]);
  203. textBox_RunInfo_Power.Text = Convert.ToString(DataTemp) + " W";
  204. //母线电压
  205. DataTemp = (ushort)(Data[7] * 256 + Data[6]);
  206. textBox_RunInfo_Vol.Text = (DataTemp / 1000f).ToString("0.0") + " V";
  207. //母线电流
  208. DataTemp = (short)(Data[9] * 256 + Data[8]);
  209. textBox_RunInfo_Current.Text = (DataTemp / 1000f).ToString("0.0") + " A";
  210. Class_CurrentData.String = textBox_RunInfo_Current.Text;
  211. Class_CurrentData.Data = DataTemp;
  212. //踏频
  213. DataTemp = (ushort)(Data[10]);
  214. textBox_RunInfo_Cadence.Text = Convert.ToString(DataTemp) + " rpm";
  215. Class_CadenceData.String = textBox_RunInfo_Cadence.Text;
  216. Class_CadenceData.Data = DataTemp;
  217. //踩踏力矩
  218. DataTemp = (ushort)(Data[11]);
  219. textBox_RunInfo_Torque.Text = Convert.ToString(DataTemp) + " N.m";
  220. Class_TorqueData.String = textBox_RunInfo_Torque.Text;
  221. Class_TorqueData.Data = DataTemp;
  222. //踩踏方向
  223. DataTemp = (ushort)(Data[12]);
  224. if (DataTemp == 0)
  225. {
  226. textBox_RunInfo_Dir.Text = "正向";
  227. }
  228. else if (DataTemp == 1)
  229. {
  230. textBox_RunInfo_Dir.Text = "反向";
  231. }
  232. else if (DataTemp == 2)
  233. {
  234. textBox_RunInfo_Dir.Text = "停止";
  235. }
  236. //助力档位
  237. DataTemp = (ushort)(Data[13]);
  238. if (DataTemp == 0x33)
  239. {
  240. textBox_RunInfo_GearSt.Text = "SMART";
  241. //comboBox_GearSt.SelectedIndex = 5;
  242. //comboBox_OBC_SetGearST.SelectedIndex = 5;
  243. }
  244. else if (DataTemp == 0x22)
  245. {
  246. textBox_RunInfo_GearSt.Text = "WALK";
  247. //comboBox_GearSt.SelectedIndex = 6;
  248. //comboBox_OBC_SetGearST.SelectedIndex = 6;
  249. }
  250. else
  251. {
  252. string[] Gears = new string[5] { "OFF", "ECO", "NORM", "SPORT", "TURBO" };
  253. textBox_RunInfo_GearSt.Text = Gears[DataTemp];
  254. //comboBox_GearSt.SelectedIndex = DataTemp;
  255. //comboBox_OBC_SetGearST.SelectedIndex = DataTemp;
  256. }
  257. //大灯状态
  258. DataTemp = (ushort)(Data[14]);
  259. if (DataTemp == 0xF0)
  260. {
  261. textBox_RunInfo_LightSwitch.Text = "OFF";
  262. //comboBox_LightSwitch.SelectedIndex = 1;
  263. //comboBox_OBC_LightSw.SelectedIndex = 1;
  264. }
  265. else if (DataTemp == 0xF1)
  266. {
  267. textBox_RunInfo_LightSwitch.Text = "ON";
  268. //comboBox_LightSwitch.SelectedIndex = 0;
  269. //comboBox_OBC_LightSw.SelectedIndex = 0;
  270. }
  271. //剩余电量
  272. DataTemp = (ushort)(Data[15]);
  273. textBox_RunInfo_SOC.Text = Convert.ToString(DataTemp) + " %";
  274. //剩余续航里程
  275. DataTemp = (ushort)(Data[17] * 256 + Data[16]);
  276. if ((DataTemp == 0xEEEE) || (DataTemp == 0xFFFF))
  277. {
  278. textBox_RunInfo_Range.Text = "---";
  279. }
  280. else
  281. {
  282. textBox_RunInfo_Range.Text = Convert.ToString(DataTemp) + " km";
  283. }
  284. //预留,空2bytes
  285. //平均功耗
  286. DataTemp = (ushort)(Data[20]);
  287. textBox_RunInfo_AvgPower.Text = Convert.ToString(DataTemp * 10) + " mAh/km";
  288. //PCB温度
  289. DataTemp = (ushort)(Data[21]);
  290. textBox_RunInfo_T_PCB.Text = Convert.ToString((int)(DataTemp - 40))+" ℃";
  291. Class_MotorTemp.String = textBox_RunInfo_T_PCB.Text;
  292. Class_MotorTemp.Data = DataTemp - 40;
  293. //绕组温度
  294. DataTemp = (ushort)(Data[22]);
  295. textBox_RunInfo_T_Coil.Text = Convert.ToString((int)(DataTemp - 40)) + " ℃";
  296. //MCU温度
  297. DataTemp = (ushort)(Data[23]);
  298. textBox_RunInfo_T_MCU.Text = Convert.ToString((int)(DataTemp - 40)) + " ℃";
  299. //单次里程
  300. DataTemp = (ushort)(Data[25] * 256 + Data[24]);
  301. textBox_RunInfo_Trip.Text = ((float)DataTemp / 10f).ToString("0.0") + " km";
  302. //单次时间
  303. DataTemp = (ushort)(Data[27] * 256 + Data[26]);
  304. textBox_RunInfo_Trip_Time.Text = Convert.ToString(DataTemp) + " s";
  305. //数据保存
  306. DataAutoSave();
  307. }));
  308. }
  309. break;
  310. }
  311. case 0x1104://故障码
  312. {
  313. unchecked
  314. {
  315. this.Invoke((EventHandler)(delegate
  316. {
  317. MC_ErrorCode_Refresh = true;
  318. textBox_ErrorCode.Text =
  319. Convert.ToString(Data[3], 16).PadLeft(2, '0').ToUpper() +
  320. Convert.ToString(Data[2], 16).PadLeft(2, '0').ToUpper() +
  321. Convert.ToString(Data[1], 16).PadLeft(2, '0').ToUpper() +
  322. Convert.ToString(Data[0], 16).PadLeft(2, '0').ToUpper() +
  323. "H";
  324. }));
  325. }
  326. break;
  327. }
  328. case 0xA903://反馈指令
  329. {
  330. unchecked
  331. {
  332. this.Invoke((EventHandler)(delegate
  333. {
  334. timer_1s.Enabled = false;
  335. MessageBox.Show("OK", "反馈指令", MessageBoxButtons.OK, MessageBoxIcon.Information);
  336. timer_1s.Enabled = true;
  337. //关机反馈
  338. PowerOffAckStatus = true;
  339. }));
  340. }
  341. break;
  342. }
  343. case 0xB226://马达参数
  344. {
  345. unchecked
  346. {
  347. this.Invoke((EventHandler)(delegate
  348. {
  349. richTextBox_MotorParam.Clear();
  350. for (int i = 0; i < myParams.MotorParma.Count; i++)
  351. {
  352. richTextBox_MotorParam.AppendText(myParams.MotorParma[i] + "=" + Convert.ToString(Data[2 * i + 1] * 256 + Data[2 * i]) + ", ");
  353. }
  354. richTextBox_MotorParam.Text = richTextBox_MotorParam.Text.Substring(0, richTextBox_MotorParam.Text.Length - 2);
  355. }));
  356. }
  357. break;
  358. }
  359. case 0xB31A://整车信息
  360. {
  361. unchecked
  362. {
  363. this.Invoke((EventHandler)(delegate
  364. {
  365. richTextBox_BikeParam.Clear();
  366. for (int i = 0; i < myParams.BikeParma.Count; i++)
  367. {
  368. richTextBox_BikeParam.AppendText(myParams.BikeParma[i] + "=" + Convert.ToString(Data[2 * i + 1] * 256 + Data[2 * i]) + ", ");
  369. }
  370. richTextBox_BikeParam.Text = richTextBox_BikeParam.Text.Substring(0, richTextBox_BikeParam.Text.Length - 2);
  371. //更新生产模式中整车参数
  372. textBox_FacModeWheelSize.Text = (Data[1] * 256 + Data[0]).ToString();//周长
  373. textBox_FacModeSpeedLimit.Text= (Data[5] * 256 + Data[4]).ToString();//限速
  374. DataTemp = (int)(Data[13] * 256 + Data[12]);//助力方案1
  375. if (DataTemp == 0) comboBox_FacModeAssist1.SelectedIndex = 0;
  376. else if (DataTemp == 341) comboBox_FacModeAssist1.SelectedIndex = 1;
  377. else if (DataTemp == 682) comboBox_FacModeAssist1.SelectedIndex = 2;
  378. else comboBox_FacModeAssist1.SelectedIndex = 3;
  379. DataTemp = (int)(Data[15] * 256 + Data[14]);//助力方案2
  380. if (DataTemp == 0) comboBox_FacModeAssist2.SelectedIndex = 0;
  381. else if (DataTemp == 341) comboBox_FacModeAssist2.SelectedIndex = 1;
  382. else if (DataTemp == 682) comboBox_FacModeAssist2.SelectedIndex = 2;
  383. else comboBox_FacModeAssist2.SelectedIndex = 3;
  384. DataTemp = (int)(Data[17] * 256 + Data[16]);//灯压
  385. if (DataTemp == 6) comboBox_FacModeLightVol.SelectedIndex = 0;
  386. else if (DataTemp == 12) comboBox_FacModeLightVol.SelectedIndex = 1;
  387. else comboBox_FacModeLightVol.SelectedIndex = 2;
  388. DataTemp = (int)(Data[21] * 256 + Data[20]);//启动模式
  389. if (DataTemp == 1) comboBox_FacModeStartMode.SelectedIndex = 0;
  390. else if (DataTemp == 3) comboBox_FacModeStartMode.SelectedIndex = 2;
  391. else comboBox_FacModeStartMode.SelectedIndex = 1;
  392. }));
  393. }
  394. break;
  395. }
  396. case 0xB420://控制器参数
  397. {
  398. unchecked
  399. {
  400. this.Invoke((EventHandler)(delegate
  401. {
  402. richTextBox_ControlParam.Clear();
  403. for (int i = 0; i < myParams.ControlParma.Count; i++)
  404. {
  405. richTextBox_ControlParam.AppendText(myParams.ControlParma[i] + "=" + Convert.ToString(Data[2 * i + 1] * 256 + Data[2 * i]) + ", ");
  406. }
  407. richTextBox_ControlParam.Text = richTextBox_ControlParam.Text.Substring(0, richTextBox_ControlParam.Text.Length - 2);
  408. }));
  409. }
  410. break;
  411. }
  412. case 0xB528://传感器参数
  413. {
  414. unchecked
  415. {
  416. this.Invoke((EventHandler)(delegate
  417. {
  418. richTextBox_SensorParam.Clear();
  419. for (int i = 0; i < myParams.SensorParam.Count; i++)
  420. {
  421. richTextBox_SensorParam.AppendText(myParams.SensorParam[i] + "=" + Convert.ToString(Data[2 * i + 1] * 256 + Data[2 * i]) + ", ");
  422. }
  423. richTextBox_SensorParam.Text = richTextBox_SensorParam.Text.Substring(0, richTextBox_SensorParam.Text.Length - 2);
  424. //更新生产模式页面传感器零点和标定值
  425. textBox_FacModeSensorADC0.Text = (Data[1] * 256 + Data[0]).ToString();
  426. textBox_FacModeSensorADC1.Text = (Data[15] * 256 + Data[14]).ToString();
  427. textBox_FacModeSensorADC2.Text = (Data[19] * 256 + Data[18]).ToString();
  428. textBox_FacModeSensorADC3.Text = (Data[23] * 256 + Data[22]).ToString();
  429. textBox_FacModeSensorADC4.Text = (Data[27] * 256 + Data[26]).ToString();
  430. }));
  431. }
  432. break;
  433. }
  434. case 0xB64C://助力参数
  435. {
  436. unchecked
  437. {
  438. this.Invoke((EventHandler)(delegate
  439. {
  440. richTextBox_AssistParam.Clear();
  441. int index_1 = myParams.AssistParam.IndexOf("转矩曲线.a");
  442. int index_2 = myParams.AssistParam.IndexOf("踏频曲线.d");
  443. for (int i = 0; i < myParams.AssistParam.Count; i++)
  444. {
  445. if (i < index_1) //数据占用2bytes
  446. {
  447. richTextBox_AssistParam.AppendText(myParams.AssistParam[i] + "=" + Convert.ToString(Data[2 * i + 1] * 256 + Data[2 * i]) + ", ");
  448. }
  449. else if (i <= index_2) //数据占用4bytes
  450. {
  451. 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]) + ", ");
  452. }
  453. else //数据占用2bytes
  454. {
  455. richTextBox_AssistParam.AppendText(myParams.AssistParam[i] + "=" + Convert.ToString(Data[2 * i + 17] * 256 + Data[2 * i + 16]) + ", ");
  456. }
  457. }
  458. richTextBox_AssistParam.Text = richTextBox_AssistParam.Text.Substring(0, richTextBox_AssistParam.Text.Length - 2);
  459. //更新生产模式中助力参数
  460. textBox_FacModeStartGain.Text = (Data[1] * 256 + Data[0]).ToString();//零速增益
  461. textBox_FacModeCircuitK.Text = (Data[45] * 256 + Data[44]).ToString();//电流阶梯
  462. textBox_FacModeFltCounter.Text = (Data[49] * 256 + Data[48]).ToString();//滤波脉冲
  463. textBox_FacModeSpeedLimitTh.Text = (Data[55] * 256 + Data[54]).ToString();//限速启动
  464. textBox_FacModeSpeedLimitEnd.Text = (Data[57] * 256 + Data[56]).ToString();//限速停止
  465. textBox_FacModeCadencePer.Text = (Data[59] * 256 + Data[58]).ToString();//踏频占比
  466. }));
  467. }
  468. break;
  469. }
  470. case 0xB74C://历史记录
  471. {
  472. unchecked
  473. {
  474. this.Invoke((EventHandler)(delegate
  475. {
  476. richTextBox_Record.Clear();
  477. for (int i = 0; i < myParams.Record.Count; i++)
  478. {
  479. richTextBox_Record.AppendText(myParams.Record[i] + "=" + Convert.ToString(Data[2 * i + 1] * 256 + Data[2 * i]) + ", ");
  480. }
  481. richTextBox_Record.Text = richTextBox_Record.Text.Substring(0, richTextBox_Record.Text.Length - 2);
  482. }));
  483. }
  484. break;
  485. }
  486. case 0xB83A://调试参数
  487. {
  488. unchecked
  489. {
  490. this.Invoke((EventHandler)(delegate
  491. {
  492. richTextBox_DebugParam.Clear();
  493. for (int i = 0; i < myParams.DebugParam.Count; i++)
  494. {
  495. richTextBox_DebugParam.AppendText(myParams.DebugParam[i] + "=" + Convert.ToString(Data[2 * i + 1] * 256 + Data[2 * i]) + ", ");
  496. }
  497. richTextBox_DebugParam.Text = richTextBox_DebugParam.Text.Substring(0, richTextBox_DebugParam.Text.Length - 2);
  498. }));
  499. }
  500. break;
  501. }
  502. case 0xB904://存储标志
  503. {
  504. unchecked
  505. {
  506. this.Invoke((EventHandler)(delegate
  507. {
  508. ushort Flag = 0;
  509. Flag = (ushort)(Data[1] * 256 + Data[0]);//EEPROM存储标志
  510. if (Flag == 0)
  511. {
  512. radioButton_EE_SaveNo.Checked = true;
  513. radioButton_EE_SaveYes.Checked = false;
  514. }
  515. else
  516. {
  517. radioButton_EE_SaveNo.Checked = false;
  518. radioButton_EE_SaveYes.Checked = true;
  519. }
  520. Flag = (ushort)(Data[3] * 256 + Data[2]);//SIP偏移校准存储标志
  521. if (Flag == 0)
  522. {
  523. radioButton_SIP_SaveNo.Checked = true;
  524. radioButton_SIP_SaveYes.Checked = false;
  525. }
  526. else
  527. {
  528. radioButton_SIP_SaveNo.Checked = false;
  529. radioButton_SIP_SaveYes.Checked = true;
  530. }
  531. }));
  532. }
  533. break;
  534. }
  535. case 0x1240://电机版本信息
  536. {
  537. unchecked
  538. {
  539. this.Invoke((EventHandler)(delegate
  540. {
  541. //电机型号
  542. textBox_Model.Text = "";
  543. textBox_OBC_ReadModel.Text = "";
  544. textBox_FacModeName.Text = "";
  545. for (ushort i = 0; i < 16; i++)
  546. {
  547. if (Data[i] == 0x2E)
  548. {
  549. break;
  550. }
  551. textBox_Model.Text += ((char)Data[i]).ToString();
  552. textBox_OBC_ReadModel.Text += ((char)Data[i]).ToString();
  553. textBox_FacModeName.Text += ((char)Data[i]).ToString(); ;
  554. }
  555. Class_Motor_Ver.Mode = textBox_Model.Text;
  556. //电机SN
  557. textBox_SN.Text = "";
  558. textBox_OBC_ReadSN.Text = "";
  559. textBox_FacModeNum.Text = "";
  560. for (ushort i = 0; i < 16; i++)
  561. {
  562. if (Data[16 + i] == 0x2E)
  563. {
  564. break;
  565. }
  566. textBox_SN.Text += ((char)Data[16 + i]).ToString();
  567. textBox_OBC_ReadSN.Text += ((char)Data[16 + i]).ToString();
  568. textBox_FacModeNum.Text += ((char)Data[16 + i]).ToString();
  569. }
  570. Class_Motor_Ver.SN = textBox_SN.Text;
  571. //电机HW
  572. textBox_HW.Text = "";
  573. textBox_OBC_ReadHW.Text = "";
  574. textBox_FacModeHW.Text = "";
  575. for (ushort i = 0; i < 16; i++)
  576. {
  577. if (Data[32 + i] == 0x2E)
  578. {
  579. break;
  580. }
  581. textBox_HW.Text += ((char)Data[32 + i]).ToString();
  582. textBox_OBC_ReadHW.Text += ((char)Data[32 + i]).ToString();
  583. textBox_FacModeHW.Text += ((char)Data[32 + i]).ToString();
  584. }
  585. Class_Motor_Ver.HW = textBox_HW.Text;
  586. //电机FW
  587. textBox_FW.Text = "";
  588. textBox_OBC_ReadFW.Text = "";
  589. textBox_FacModeFW.Text = "";
  590. for (ushort i = 0; i < 16; i++)
  591. {
  592. if (Data[48 + i] == 0x2E)
  593. {
  594. break;
  595. }
  596. if (Data[48 + i] == 'r')
  597. {
  598. Data[48 + i] = (byte)'.';
  599. }
  600. textBox_FW.Text += ((char)Data[48 + i]).ToString();
  601. textBox_OBC_ReadFW.Text += ((char)Data[48 + i]).ToString();
  602. textBox_FacModeFW.Text += ((char)Data[48 + i]).ToString();
  603. }
  604. Class_Motor_Ver.FW = textBox_FW.Text;
  605. }));
  606. }
  607. break;
  608. }
  609. case 0xA610://自定义1
  610. {
  611. unchecked
  612. {
  613. this.Invoke((EventHandler)(delegate
  614. {
  615. textBox_User1.Text = "";
  616. for (ushort i = 0; i < 16; i++)
  617. {
  618. if (Data[i] == 0x2E)
  619. {
  620. break;
  621. }
  622. textBox_User1.Text += ((char)Data[i]).ToString();
  623. }
  624. }));
  625. }
  626. break;
  627. }
  628. case 0xA710://自定义2
  629. {
  630. unchecked
  631. {
  632. this.Invoke((EventHandler)(delegate
  633. {
  634. textBox_User2.Text = "";
  635. for (ushort i = 0; i < 16; i++)
  636. {
  637. if (Data[i] == 0x2E)
  638. {
  639. break;
  640. }
  641. textBox_User2.Text += ((char)Data[i]).ToString();
  642. }
  643. }));
  644. }
  645. break;
  646. }
  647. case 0xA810://自定义1
  648. {
  649. unchecked
  650. {
  651. this.Invoke((EventHandler)(delegate
  652. {
  653. textBox_User3.Text = "";
  654. for (ushort i = 0; i < 16; i++)
  655. {
  656. if (Data[i] == 0x2E)
  657. {
  658. break;
  659. }
  660. textBox_User3.Text += ((char)Data[i]).ToString();
  661. }
  662. }));
  663. }
  664. break;
  665. }
  666. case 0xA520://生产信息
  667. {
  668. unchecked
  669. {
  670. this.Invoke((EventHandler)(delegate
  671. {
  672. //生产商
  673. textBox_MAC.Text = "";
  674. for (ushort i = 0; i < 8; i++)
  675. {
  676. if (Data[i] == 0x2E)
  677. {
  678. break;
  679. }
  680. textBox_MAC.Text += ((char)Data[i]).ToString();
  681. }
  682. //生产地
  683. textBox_MACAdd.Text = "";
  684. for (ushort i = 0; i < 8; i++)
  685. {
  686. if (Data[8 + i] == 0x2E)
  687. {
  688. break;
  689. }
  690. textBox_MACAdd.Text += ((char)Data[8 + i]).ToString();
  691. }
  692. //生产日期
  693. textBox_MACDate.Text = "";
  694. for (ushort i = 0; i < 8; i++)
  695. {
  696. if (Data[16 + i] == 0x2E)
  697. {
  698. break;
  699. }
  700. textBox_MACDate.Text += ((char)Data[16 + i]).ToString();
  701. }
  702. //品牌信息
  703. textBox_PP.Text = "";
  704. for (ushort i = 0; i < 8; i++)
  705. {
  706. if (Data[24 + i] == 0x2E)
  707. {
  708. break;
  709. }
  710. textBox_PP.Text += ((char)Data[24 + i]).ToString();
  711. }
  712. }));
  713. }
  714. break;
  715. }
  716. case 0xA408://密钥
  717. {
  718. unchecked
  719. {
  720. this.Invoke((EventHandler)(delegate
  721. {
  722. textBox_Key.Text = "";
  723. for (ushort i = 0; i < 8; i++)
  724. {
  725. textBox_Key.Text += ((char)Data[i]).ToString();
  726. }
  727. }));
  728. }
  729. break;
  730. }
  731. case 0x1401://在线检测结果
  732. {
  733. unchecked
  734. {
  735. this.Invoke((EventHandler)(delegate
  736. {
  737. textBox_Online.Text = Convert.ToString(Data[3], 16).PadLeft(2, '0').ToUpper() + "H";
  738. }));
  739. }
  740. break;
  741. }
  742. case 0x1510://骑行历史信息
  743. {
  744. unchecked
  745. {
  746. this.Invoke((EventHandler)(delegate
  747. {
  748. long DataTemp32;
  749. //ODO里程
  750. DataTemp32 = (long)((Data[3] << 24) + (Data[2] << 16) + (Data[1] << 8) + Data[0]);
  751. textBox_OBC_ODO_KM.Text = ((float)DataTemp32 / 10f).ToString("0.0") + " km";
  752. //ODO时间
  753. DataTemp32 = (long)((Data[7] << 24) + (Data[6] << 16) + (Data[5] << 8) + Data[4]);
  754. if (DataTemp32 >= 60 * 100000)
  755. textBox_OBC_ODO_TIME.Text = Convert.ToString(DataTemp32 / 60) + " h";
  756. else
  757. textBox_OBC_ODO_TIME.Text = Convert.ToString(DataTemp32 / 60) + " h " + Convert.ToString(DataTemp32 % 60) + " m";
  758. //TRIP里程
  759. DataTemp32 = (long)((Data[11] << 24) + (Data[10] << 16) + (Data[9] << 8) + Data[8]);
  760. textBox_OBC_TRIP_KM.Text = ((float)DataTemp32 / 10f).ToString("0.0") + " km";
  761. //TRIP时间
  762. DataTemp32 = (long)((Data[15] << 24) + (Data[14] << 16) + (Data[13] << 8) + Data[12]);
  763. if (DataTemp32 >= 60 * 100000)
  764. textBox_OBC_TRIP_TIME.Text = Convert.ToString(DataTemp32 / 60) + " h";
  765. else
  766. textBox_OBC_TRIP_TIME.Text = Convert.ToString(DataTemp32 / 60) + " h " + Convert.ToString(DataTemp32 % 60) + " m";
  767. }));
  768. }
  769. break;
  770. }
  771. case 0x1720://客户信息
  772. {
  773. unchecked
  774. {
  775. this.Invoke((EventHandler)(delegate
  776. {
  777. //程序特性
  778. textBox_SP.Text = "";
  779. textBox_OBC_ReadSP.Text = "";
  780. textBox_FacModeSP.Text = "";
  781. for (ushort i = 0; i < 32; i++)
  782. {
  783. if (Data[i] == 0x2E)
  784. {
  785. break;
  786. }
  787. textBox_SP.Text += ((char)Data[i]).ToString();
  788. textBox_OBC_ReadSP.Text += ((char)Data[i]).ToString();
  789. textBox_FacModeSP.Text += ((char)Data[i]).ToString(); ;
  790. }
  791. Class_Motor_Ver.Special = textBox_SP.Text;
  792. }));
  793. }
  794. break;
  795. }
  796. default:
  797. {
  798. unchecked
  799. {
  800. if ((CmdTemp & 0xFF00) == 0xAB00) //返回指定地址存储器数据
  801. {
  802. this.Invoke((EventHandler)(delegate
  803. {
  804. long ByteNum = 0;
  805. ByteNum = ((Data[4] << 24) + (Data[5] << 16) + (Data[6] << 8) + Data[7]) - ((Data[0] << 24) + (Data[1] << 16) + (Data[2] << 8) + Data[3]) + 1;
  806. for (int i = 0; i < ByteNum; i++)
  807. {
  808. richTextBox_RamFlasgData.AppendText(Convert.ToString(Data[8 + i], 16).PadLeft(2, '0').ToUpper() + " ");
  809. }
  810. }));
  811. }
  812. }
  813. break;
  814. }
  815. }
  816. }
  817. #endregion
  818. #region 解析BMS广播的命令
  819. else if (ID == 0x720)
  820. {
  821. switch (CmdTemp)
  822. {
  823. case 0x1010://BMS运行信息
  824. {
  825. unchecked
  826. {
  827. this.Invoke((EventHandler)(delegate
  828. {
  829. BMS_RunInfo_Refresh = true;
  830. richTextBox_OBC_BMS_RunInfo.Clear();
  831. //电压
  832. uData16 = (ushort)(Data[1] * 256 + Data[0]);
  833. richTextBox_OBC_BMS_RunInfo.AppendText("电压:" + Convert.ToString(uData16) + " mV" + "\r\n");
  834. //电流
  835. uData16 = (ushort)(Data[3] * 256 + Data[2]);
  836. richTextBox_OBC_BMS_RunInfo.AppendText("电流:" + Convert.ToString(uData16) + " mA" + "\r\n");
  837. //剩余容量
  838. uData16 = (ushort)(Data[5] * 256 + Data[4]);
  839. richTextBox_OBC_BMS_RunInfo.AppendText("剩余容量:" + Convert.ToString(uData16) + " mAh" + "\r\n");
  840. //满充容量
  841. uData16 = (ushort)(Data[7] * 256 + Data[6]);
  842. richTextBox_OBC_BMS_RunInfo.AppendText("满充容量:" + Convert.ToString(uData16) + " mV" + "\r\n");
  843. //电芯温度
  844. richTextBox_OBC_BMS_RunInfo.AppendText("电芯温度:" + Convert.ToString((int)(Data[8] - 40) + " ℃" + "\r\n"));
  845. //剩余电量
  846. richTextBox_OBC_BMS_RunInfo.AppendText("剩余电量:" + Convert.ToString(Data[9]) + " %" + "\r\n");
  847. //电池状态
  848. richTextBox_OBC_BMS_RunInfo.AppendText("电池状态:" + Convert.ToString(Data[10], 16).PadLeft(2, '0').ToUpper() + " H" + "\r\n");
  849. //SOH
  850. richTextBox_OBC_BMS_RunInfo.AppendText("电池寿命:" + Convert.ToString(Data[11]) + " %" + "\r\n");
  851. }));
  852. }
  853. break;
  854. }
  855. default: break;
  856. }
  857. }
  858. #endregion
  859. #region 解析电机发送OBC的命令
  860. else if (ID == 0x713)
  861. {
  862. switch (CmdTemp)
  863. {
  864. case 0x5408://电机用户参数
  865. {
  866. unchecked
  867. {
  868. this.Invoke((EventHandler)(delegate
  869. {
  870. richTextBox_OBC_ReadUserInfo.Clear();
  871. //默认周长
  872. richTextBox_OBC_ReadUserInfo.AppendText("默认周长:" + Convert.ToString((int)Data[0]) + " cm" + "\r\n");
  873. //启动模式
  874. richTextBox_OBC_ReadUserInfo.AppendText("启动模式:" + ((Data[1] == 0x01) ? "柔和" : (Data[1] == 0x02) ? "正常" : (Data[1] == 0x03) ? "强劲" : "无效") + "\r\n");
  875. comboBox_OBC_StartMode.SelectedIndex = Data[1] - 1;
  876. //限速
  877. richTextBox_OBC_ReadUserInfo.AppendText("限速:" + Convert.ToString(Data[2]) + " km/h" + "\r\n");
  878. //周长微调
  879. numericUpDown_OBC_WheelAdj.Value = (int)(Data[3] > 128 ? (Data[3] - 256) : Data[3]);
  880. richTextBox_OBC_ReadUserInfo.AppendText("周长微调:" + Convert.ToString(numericUpDown_OBC_WheelAdj.Value) + " cm" + "\r\n");
  881. //助力方案
  882. richTextBox_OBC_ReadUserInfo.AppendText("助力方案:" + Convert.ToString(Data[4]) + "\r\n");
  883. comboBox_OBC_AssistFunc.SelectedIndex = Data[4] - 1;
  884. //关机时间
  885. richTextBox_OBC_ReadUserInfo.AppendText("关机时间:" + Convert.ToString(Data[5]) + " min" + "\r\n");
  886. numericUpDown_OBC_OffTime.Value = (int)Data[5];
  887. }));
  888. }
  889. break;
  890. }
  891. case 0x5303://电机应答反馈
  892. {
  893. unchecked
  894. {
  895. this.Invoke((EventHandler)(delegate
  896. {
  897. timer_1s.Enabled = false;
  898. MessageBox.Show("OK", "反馈指令", MessageBoxButtons.OK, MessageBoxIcon.Information);
  899. timer_1s.Enabled = true;
  900. }));
  901. }
  902. break;
  903. }
  904. default: break;
  905. }
  906. }
  907. #endregion
  908. }
  909. #endregion
  910. /// <summary>
  911. /// 端口连接事件
  912. /// </summary>
  913. /// <param name="sender"></param>
  914. /// <param name="e"></param>
  915. private void 连接ToolStripMenuItem_Click(object sender, System.EventArgs e)
  916. {
  917. bool result = false;
  918. if (连接ToolStripMenuItem.Text == "连接")
  919. {
  920. result = mySerialProcess.SerialOpen(toolStripComboBox_ComNum.Text, toolStripComboBox_Baudrate.Text);
  921. if (result)
  922. {
  923. toolStripComboBox_ComNum.Enabled = false;
  924. toolStripComboBox_Baudrate.Enabled = false;
  925. 刷新ToolStripMenuItem.Enabled = false;
  926. 连接ToolStripMenuItem.Text = "断开";
  927. 连接ToolStripMenuItem.Checked = true;
  928. label_COM_Sta.Text = toolStripComboBox_ComNum.Text + "已连接," + toolStripComboBox_Baudrate.Text;
  929. }
  930. }
  931. else if (连接ToolStripMenuItem.Text == "断开")
  932. {
  933. checkBox_OBC_StartSetGearSt.Checked = false;
  934. mySerialProcess.SerialClose();
  935. toolStripComboBox_ComNum.Enabled = true;
  936. toolStripComboBox_Baudrate.Enabled = true;
  937. 刷新ToolStripMenuItem.Enabled = true;
  938. 连接ToolStripMenuItem.Text = "连接";
  939. 连接ToolStripMenuItem.Checked = false;
  940. label_COM_Sta.Text = "连接状态:未连接";
  941. }
  942. }
  943. /// <summary>
  944. /// 端口号刷新事件
  945. /// </summary>
  946. /// <param name="sender"></param>
  947. /// <param name="e"></param>
  948. private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
  949. {
  950. toolStripComboBox_ComNum.Items.Clear();
  951. toolStripComboBox_ComNum.Items.AddRange(mySerialProcess.refreshPort());
  952. toolStripComboBox_ComNum.SelectedIndex = toolStripComboBox_ComNum.Items.Count > 0 ? 0 : -1;
  953. }
  954. /// <summary>
  955. /// 转速调节事件
  956. /// </summary>
  957. /// <param name="sender"></param>
  958. /// <param name="e"></param>
  959. private void numericUpDown_SpeedAdj_ValueChanged(object sender, EventArgs e)
  960. {
  961. //生产模式转速设置值更新
  962. trackBar_FacModeMotorSpeedAdj.Value = (int)numericUpDown_SpeedAdj.Value;
  963. label_FacModeMotorSpeedSet.Text = trackBar_FacModeMotorSpeedAdj.Value.ToString() + "%";
  964. //设置转速百分比
  965. var Data = new byte[1];
  966. Data[0] = (byte)numericUpDown_SpeedAdj.Value;
  967. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2C01, Data);
  968. }
  969. /// <summary>
  970. /// 存储标志修改事件
  971. /// </summary>
  972. /// <param name="sender"></param>
  973. /// <param name="e"></param>
  974. private void 写入存储ToolStripMenuItem_Click(object sender, EventArgs e)
  975. {
  976. 写入存储ToolStripMenuItem.Checked = !写入存储ToolStripMenuItem.Checked;
  977. }
  978. /// <summary>
  979. /// 1s定时器
  980. /// </summary>
  981. /// <param name="sender"></param>
  982. /// <param name="e"></param>
  983. private void timer_1s_Tick(object sender, EventArgs e)
  984. {
  985. //时间更新
  986. label_SystemTime.Text = "系统时间:" + DateTime.Now.ToString();
  987. //MC_Runinfo超时清除
  988. if (MC_RunInfo_Refresh == false)
  989. {
  990. MC_RunInfo_Refresh_Cnt++;
  991. if (MC_RunInfo_Refresh_Cnt > 10)//5s
  992. {
  993. MC_RunInfo_Clear();
  994. MC_RunInfo_Refresh_Cnt = 0;
  995. }
  996. }
  997. else
  998. {
  999. MC_RunInfo_Refresh_Cnt = 0;
  1000. }
  1001. MC_RunInfo_Refresh = false;
  1002. //BMS_RunInfo超时清除
  1003. if (BMS_RunInfo_Refresh == false)
  1004. {
  1005. BMS_RunInfo_Refresh_Cnt++;
  1006. if (BMS_RunInfo_Refresh_Cnt > 10)//5s
  1007. {
  1008. richTextBox_OBC_BMS_RunInfo.Clear();
  1009. BMS_RunInfo_Refresh_Cnt = 0;
  1010. }
  1011. }
  1012. else
  1013. {
  1014. BMS_RunInfo_Refresh_Cnt = 0;
  1015. }
  1016. BMS_RunInfo_Refresh = false;
  1017. //故障码超时清除
  1018. if (MC_ErrorCode_Refresh == false)
  1019. {
  1020. MC_ErrorCode_Refresh_Cnt++;
  1021. if (MC_ErrorCode_Refresh_Cnt > 10)//5s
  1022. {
  1023. textBox_ErrorCode.Text = "---";
  1024. MC_ErrorCode_Refresh_Cnt = 0;
  1025. }
  1026. }
  1027. else
  1028. {
  1029. MC_ErrorCode_Refresh_Cnt = 0;
  1030. }
  1031. MC_ErrorCode_Refresh = false;
  1032. //CDL连接超时
  1033. #if false
  1034. if (mySerialProcess.CDL_Online_Flag == false)
  1035. {
  1036. mySerialProcess.CDL_OnlineCheck_Cnt++;
  1037. if (mySerialProcess.CDL_OnlineCheck_Cnt > 3)//3s
  1038. {
  1039. timer_1s.Enabled = false;
  1040. MessageBox.Show("连接失败!", "提示");
  1041. timer_1s.Enabled = true;
  1042. //关闭串口
  1043. mySerialProcess.SerialClose();
  1044. toolStripComboBox_ComNum.Enabled = true;
  1045. toolStripComboBox_Baudrate.Enabled = true;
  1046. 刷新ToolStripMenuItem.Enabled = true;
  1047. 连接ToolStripMenuItem.Text = "连接";
  1048. 连接ToolStripMenuItem.Checked = false;
  1049. label_COM_Sta.Text = "连接状态:未连接";
  1050. }
  1051. }
  1052. #endif
  1053. //OBC定时发送设置档位
  1054. if (checkBox_OBC_StartSetGearSt.Checked == true)
  1055. {
  1056. var CtrlCode = new byte[2];
  1057. if (comboBox_OBC_SetGearST.SelectedIndex == 5)//档位
  1058. CtrlCode[0] = 0x33;
  1059. else if (comboBox_OBC_SetGearST.SelectedIndex == 6)//档位
  1060. CtrlCode[0] = 0x22;
  1061. else
  1062. CtrlCode[0] = (byte)comboBox_OBC_SetGearST.SelectedIndex;
  1063. if (comboBox_OBC_LightSw.SelectedIndex == 0)//车灯
  1064. CtrlCode[1] = 0xF1;
  1065. else
  1066. CtrlCode[1] = 0xF0;
  1067. if (!mySerialProcess.SendCmdWithoutNotice((ushort)0x731, (byte)0x0C, (ushort)0x3002, CtrlCode))
  1068. {
  1069. checkBox_OBC_StartSetGearSt.Checked = false;
  1070. }
  1071. }
  1072. //OBC定时发送查询电池
  1073. if (checkBox_OBC_StartReadBMS.Checked == true)
  1074. {
  1075. if (!mySerialProcess.SendCmd((ushort)0x732, (byte)0x11, (ushort)0x5000, null))
  1076. {
  1077. checkBox_OBC_StartReadBMS.Checked = false;
  1078. }
  1079. }
  1080. }
  1081. /// <summary>
  1082. /// 运行信息清除
  1083. /// </summary>
  1084. private void MC_RunInfo_Clear()
  1085. {
  1086. foreach (Control c in groupBox2.Controls)
  1087. {
  1088. if ((c is TextBox))
  1089. {
  1090. if(c.Name != textBox_ErrorCode.ToString())
  1091. c.Text = "---";
  1092. }
  1093. }
  1094. }
  1095. /// <summary>
  1096. /// 读取马达参数
  1097. /// </summary>
  1098. /// <param name="sender"></param>
  1099. /// <param name="e"></param>
  1100. private void button_Read_MotorParam_Click(object sender, EventArgs e)
  1101. {
  1102. richTextBox_MotorParam.Clear();
  1103. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3A00, null);
  1104. }
  1105. /// <summary>
  1106. /// 写入马达参数
  1107. /// </summary>
  1108. /// <param name="sender"></param>
  1109. /// <param name="e"></param>
  1110. private void button_Write_Click(object sender, EventArgs e)
  1111. {
  1112. var ConfigParam = new byte[40];
  1113. for (int i = 0; i < 40; i++)
  1114. ConfigParam[i] = 0;
  1115. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  1116. ushort uwDataTemp = 0;
  1117. string[] strDataTemp = richTextBox_MotorParam.Text.Split(new string[] { ", "},StringSplitOptions.None);
  1118. try
  1119. {
  1120. for (int i = 0; i < strDataTemp.Length; i++)
  1121. {
  1122. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  1123. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  1124. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  1125. }
  1126. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3B28, ConfigParam);
  1127. }
  1128. catch (Exception)
  1129. {
  1130. timer_1s.Enabled = false;
  1131. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1132. timer_1s.Enabled = true;
  1133. }
  1134. }
  1135. /// <summary>
  1136. /// 读取整车信息
  1137. /// </summary>
  1138. /// <param name="sender"></param>
  1139. /// <param name="e"></param>
  1140. private void button_ReadBikeParam_Click(object sender, EventArgs e)
  1141. {
  1142. richTextBox_BikeParam.Clear();
  1143. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3C00, null);
  1144. }
  1145. /// <summary>
  1146. /// 写入整车信息
  1147. /// </summary>
  1148. /// <param name="sender"></param>
  1149. /// <param name="e"></param>
  1150. private void button_WriteBikeParam_Click(object sender, EventArgs e)
  1151. {
  1152. var ConfigParam = new byte[28];
  1153. for (int i = 0; i < 28; i++)
  1154. ConfigParam[i] = 0;
  1155. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  1156. ushort uwDataTemp = 0;
  1157. string[] strDataTemp = richTextBox_BikeParam.Text.Split(new string[] { ", " }, StringSplitOptions.None);
  1158. try
  1159. {
  1160. for (int i = 0; i < strDataTemp.Length; i++)
  1161. {
  1162. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  1163. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  1164. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  1165. }
  1166. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3D1C, ConfigParam);
  1167. }
  1168. catch (Exception)
  1169. {
  1170. timer_1s.Enabled = false;
  1171. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1172. timer_1s.Enabled = true;
  1173. }
  1174. }
  1175. /// <summary>
  1176. /// 读取控制器参数
  1177. /// </summary>
  1178. /// <param name="sender"></param>
  1179. /// <param name="e"></param>
  1180. private void button_ReadControlParam_Click(object sender, EventArgs e)
  1181. {
  1182. richTextBox_ControlParam.Clear();
  1183. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3E00, null);
  1184. }
  1185. /// <summary>
  1186. /// 写入控制器参数
  1187. /// </summary>
  1188. /// <param name="sender"></param>
  1189. /// <param name="e"></param>
  1190. private void button_WriteControlParam_Click(object sender, EventArgs e)
  1191. {
  1192. var ConfigParam = new byte[34];
  1193. for (int i = 0; i < 34; i++)
  1194. ConfigParam[i] = 0;
  1195. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  1196. ushort uwDataTemp = 0;
  1197. string[] strDataTemp = richTextBox_ControlParam.Text.Split(new string[] { ", " }, StringSplitOptions.None);
  1198. try
  1199. {
  1200. for (int i = 0; i < strDataTemp.Length; i++)
  1201. {
  1202. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  1203. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  1204. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  1205. }
  1206. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3F22, ConfigParam);
  1207. }
  1208. catch (Exception)
  1209. {
  1210. timer_1s.Enabled = false;
  1211. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1212. timer_1s.Enabled = true;
  1213. }
  1214. }
  1215. /// <summary>
  1216. /// 读取助力参数
  1217. /// </summary>
  1218. /// <param name="sender"></param>
  1219. /// <param name="e"></param>
  1220. private void button_ReadAssistParam_Click(object sender, EventArgs e)
  1221. {
  1222. var ConfigParam = new byte[4];
  1223. int AssistNum = 0;
  1224. try
  1225. {
  1226. AssistNum = Convert.ToInt32(comboBox_AssistTorque.Text);
  1227. ConfigParam[0] = (byte)(AssistNum & 0xFF);
  1228. ConfigParam[1] = (byte)(AssistNum >> 8);
  1229. AssistNum = Convert.ToInt32(comboBox_AssistCadence.Text);
  1230. ConfigParam[2] = (byte)(AssistNum & 0xFF);
  1231. ConfigParam[3] = (byte)(AssistNum >> 8);
  1232. richTextBox_AssistParam.Clear();
  1233. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4304, ConfigParam);
  1234. }
  1235. catch (Exception)
  1236. {
  1237. timer_1s.Enabled = false;
  1238. MessageBox.Show("请选择助力参数编号!","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
  1239. timer_1s.Enabled = true;
  1240. }
  1241. }
  1242. /// <summary>
  1243. /// 写入助力参数
  1244. /// </summary>
  1245. /// <param name="sender"></param>
  1246. /// <param name="e"></param>
  1247. private void button_WriteAssistParam_Click(object sender, EventArgs e)
  1248. {
  1249. var ConfigParam = new byte[80];
  1250. for (int i = 0; i < 80; i++)
  1251. ConfigParam[i] = 0;
  1252. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  1253. ulong ulDataTemp = 0;
  1254. string[] strDataTemp = richTextBox_AssistParam.Text.Split(new string[] { ", " }, StringSplitOptions.None);
  1255. int index_1 = 0, index_2 = 0;
  1256. try
  1257. {
  1258. for (int i = 0; i < strDataTemp.Length; i++)
  1259. {
  1260. if (strDataTemp[i].Split('=')[0] == "转矩曲线.a")
  1261. {
  1262. index_1 = i;
  1263. break;
  1264. }
  1265. }
  1266. for (int i = 0; i < strDataTemp.Length; i++)
  1267. {
  1268. if (strDataTemp[i].Split('=')[0] == "踏频曲线.d")
  1269. {
  1270. index_2 = i;
  1271. break;
  1272. }
  1273. }
  1274. for (int i = 0; i < strDataTemp.Length; i++)
  1275. {
  1276. ulDataTemp = Convert.ToUInt32(strDataTemp[i].Split('=')[1]);
  1277. if (i < index_1)
  1278. {
  1279. ConfigParam[2 * i + 4] = (byte)ulDataTemp;
  1280. ConfigParam[2 * i + 5] = (byte)(ulDataTemp >> 8);
  1281. }
  1282. else if (i <= index_2)
  1283. {
  1284. ConfigParam[4 * i - 4] = (byte)ulDataTemp;
  1285. ConfigParam[4 * i - 3] = (byte)(ulDataTemp >> 8);
  1286. ConfigParam[4 * i - 2] = (byte)(ulDataTemp >> 16);
  1287. ConfigParam[4 * i - 1] = (byte)(ulDataTemp >> 24);
  1288. }
  1289. else
  1290. {
  1291. ConfigParam[2 * i + 20] = (byte)ulDataTemp;
  1292. ConfigParam[2 * i + 21] = (byte)(ulDataTemp >> 8);
  1293. }
  1294. }
  1295. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x4450, ConfigParam);
  1296. }
  1297. catch (Exception)
  1298. {
  1299. timer_1s.Enabled = false;
  1300. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1301. timer_1s.Enabled = true;
  1302. }
  1303. }
  1304. /// <summary>
  1305. /// 读取调试参数
  1306. /// </summary>
  1307. /// <param name="sender"></param>
  1308. /// <param name="e"></param>
  1309. private void button_ReadDebugParam_Click(object sender, EventArgs e)
  1310. {
  1311. richTextBox_DebugParam.Clear();
  1312. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4500, null);
  1313. }
  1314. /// <summary>
  1315. /// 写入调试参数
  1316. /// </summary>
  1317. /// <param name="sender"></param>
  1318. /// <param name="e"></param>
  1319. private void button_WriteDebugParam_Click(object sender, EventArgs e)
  1320. {
  1321. var ConfigParam = new byte[60];
  1322. for (int i = 0; i < 60; i++)
  1323. ConfigParam[i] = 0;
  1324. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  1325. ushort uwDataTemp = 0;
  1326. string[] strDataTemp = richTextBox_DebugParam.Text.Split(new string[] { ", " }, StringSplitOptions.None);
  1327. try
  1328. {
  1329. for (int i = 0; i < strDataTemp.Length; i++)
  1330. {
  1331. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  1332. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  1333. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  1334. }
  1335. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x463C, ConfigParam);
  1336. }
  1337. catch (Exception)
  1338. {
  1339. timer_1s.Enabled = false;
  1340. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1341. timer_1s.Enabled = true;
  1342. }
  1343. }
  1344. /// <summary>
  1345. /// 读取历史
  1346. /// </summary>
  1347. /// <param name="sender"></param>
  1348. /// <param name="e"></param>
  1349. private void button_ReadRecord_Click(object sender, EventArgs e)
  1350. {
  1351. richTextBox_Record.Clear();
  1352. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1E00, null);
  1353. }
  1354. /// <summary>
  1355. /// 读取传感器参数
  1356. /// </summary>
  1357. /// <param name="sender"></param>
  1358. /// <param name="e"></param>
  1359. private void button_ReadSensorParam_Click(object sender, EventArgs e)
  1360. {
  1361. richTextBox_SensorParam.Clear();
  1362. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4000, null);
  1363. }
  1364. /// <summary>
  1365. /// 写入力矩传感器标定值
  1366. /// </summary>
  1367. /// <param name="sender"></param>
  1368. /// <param name="e"></param>
  1369. private void button_Write_Cal_Click(object sender, EventArgs e)
  1370. {
  1371. var ConfigParam = new byte[4];
  1372. for (int i = 0; i < 4; i++)
  1373. ConfigParam[i] = 0;
  1374. ushort uwDataTemp = 0;
  1375. try
  1376. {
  1377. uwDataTemp = Convert.ToUInt16(comboBox_TorqueSet.Text);//负载序号
  1378. ConfigParam[0] = (byte)(uwDataTemp & 0xFF);
  1379. ConfigParam[1] = (byte)(uwDataTemp >> 8);
  1380. uwDataTemp = (ushort)(decimal.Parse(textBox_Load.Text) * 10); ;//负载值0.1Nm
  1381. ConfigParam[2] = (byte)(uwDataTemp & 0xFF);
  1382. ConfigParam[3] = (byte)(uwDataTemp >> 8);
  1383. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x4104, ConfigParam);
  1384. }
  1385. catch (Exception)
  1386. {
  1387. timer_1s.Enabled = false;
  1388. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1389. timer_1s.Enabled = true;
  1390. }
  1391. }
  1392. /// <summary>
  1393. /// 写入传感器参数
  1394. /// </summary>
  1395. /// <param name="sender"></param>
  1396. /// <param name="e"></param>
  1397. private void button_WriteSensorParam_Click(object sender, EventArgs e)
  1398. {
  1399. var ConfigParam = new byte[14];
  1400. for (int i = 0; i < 14; i++)
  1401. ConfigParam[i] = 0;
  1402. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  1403. ushort uwDataTemp = 0;
  1404. //力矩传感器参数只写入前面14项与力矩传感器相关的参数
  1405. string[] strDataTemp = richTextBox_SensorParam.Text.Split(new string[] { ", " }, StringSplitOptions.None);
  1406. int index = 0;
  1407. try
  1408. {
  1409. for (int i = 0; i < strDataTemp.Length; i++)
  1410. {
  1411. if (strDataTemp[i].Split('=')[0] == "踏频传感器一圈脉冲数")
  1412. {
  1413. index = i;
  1414. break;
  1415. }
  1416. }
  1417. for (int i = 0; i < (strDataTemp.Length - index); i++)
  1418. {
  1419. uwDataTemp = Convert.ToUInt16(strDataTemp[i + index].Split('=')[1]);
  1420. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  1421. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  1422. }
  1423. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x420E, ConfigParam);
  1424. }
  1425. catch (Exception)
  1426. {
  1427. timer_1s.Enabled = false;
  1428. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1429. timer_1s.Enabled = true;
  1430. }
  1431. }
  1432. /// <summary>
  1433. /// 写入型号
  1434. /// </summary>
  1435. /// <param name="sender"></param>
  1436. /// <param name="e"></param>
  1437. private void button_WriteModel_Click(object sender, EventArgs e)
  1438. {
  1439. string Mode = textBox_Model.Text;
  1440. var ModeArray = new byte[16];
  1441. for (ushort i = 0; i < Mode.Length; i++)
  1442. {
  1443. ModeArray[i] = (byte)Mode[i];
  1444. }
  1445. if (Mode.Length < 16)
  1446. {
  1447. ModeArray[Mode.Length] = (byte)'.';
  1448. for (ushort i = 0; i < 16 - Mode.Length - 1; i++)
  1449. {
  1450. ModeArray[Mode.Length + 1 + i] = 0x20;
  1451. }
  1452. }
  1453. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2210, ModeArray);
  1454. }
  1455. /// <summary>
  1456. /// 写入SN
  1457. /// </summary>
  1458. /// <param name="sender"></param>
  1459. /// <param name="e"></param>
  1460. private void button_WriteSN_Click(object sender, EventArgs e)
  1461. {
  1462. string SN = textBox_SN.Text;
  1463. var SNArray = new byte[16];
  1464. for (ushort i = 0; i < SN.Length; i++)
  1465. {
  1466. SNArray[i] = (byte)SN[i];
  1467. }
  1468. if (SN.Length < 16)
  1469. {
  1470. SNArray[SN.Length] = (byte)'.';
  1471. for (ushort i = 0; i < 16 - SN.Length - 1; i++)
  1472. {
  1473. SNArray[SN.Length + 1 + i] = 0x20;
  1474. }
  1475. }
  1476. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2310, SNArray);
  1477. }
  1478. /// <summary>
  1479. /// 查询版本信息
  1480. /// </summary>
  1481. /// <param name="sender"></param>
  1482. /// <param name="e"></param>
  1483. private void button_ReadVersion_Click(object sender, EventArgs e)
  1484. {
  1485. textBox_Model.Text = "---";
  1486. textBox_SN.Text = "---";
  1487. textBox_HW.Text = "---";
  1488. textBox_FW.Text = "---";
  1489. textBox_SP.Text = "---";
  1490. textBox_OBC_ReadModel.Text = "---";
  1491. textBox_OBC_ReadSN.Text = "---";
  1492. textBox_OBC_ReadHW.Text = "---";
  1493. textBox_OBC_ReadFW.Text = "---";
  1494. textBox_OBC_ReadSP.Text = "---";
  1495. textBox_FacModeName.Text = "---";
  1496. textBox_FacModeNum.Text = "---";
  1497. textBox_FacModeHW.Text = "---";
  1498. textBox_FacModeFW.Text = "---";
  1499. textBox_FacModeSP.Text = "---";
  1500. Class_Motor_Ver.Mode = "---";
  1501. Class_Motor_Ver.SN = "---";
  1502. Class_Motor_Ver.HW = "---";
  1503. Class_Motor_Ver.FW = "---";
  1504. Class_Motor_Ver.Special = "---";
  1505. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
  1506. }
  1507. /// <summary>
  1508. /// 查询自定义1
  1509. /// </summary>
  1510. /// <param name="sender"></param>
  1511. /// <param name="e"></param>
  1512. private void button_ReadUser1_Click(object sender, EventArgs e)
  1513. {
  1514. textBox_User1.Text = "---";
  1515. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1300, null);
  1516. }
  1517. /// <summary>
  1518. /// 查询自定义2
  1519. /// </summary>
  1520. /// <param name="sender"></param>
  1521. /// <param name="e"></param>
  1522. private void button_ReadUser2_Click(object sender, EventArgs e)
  1523. {
  1524. textBox_User2.Text = "---";
  1525. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1500, null);
  1526. }
  1527. /// <summary>
  1528. /// 查询自定义3
  1529. /// </summary>
  1530. /// <param name="sender"></param>
  1531. /// <param name="e"></param>
  1532. private void button_ReadUser3_Click(object sender, EventArgs e)
  1533. {
  1534. textBox_User3.Text = "---";
  1535. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1700, null);
  1536. }
  1537. /// <summary>
  1538. /// 写入自定义字符串1
  1539. /// </summary>
  1540. /// <param name="sender"></param>
  1541. /// <param name="e"></param>
  1542. private void button_WriteUser1_Click(object sender, EventArgs e)
  1543. {
  1544. string User = textBox_User1.Text;
  1545. var UserArray = new byte[16];
  1546. for (ushort i = 0; i < User.Length; i++)
  1547. {
  1548. UserArray[i] = (byte)User[i];
  1549. }
  1550. if (User.Length < 16)
  1551. {
  1552. UserArray[User.Length] = (byte)'.';
  1553. for (ushort i = 0; i < 16 - User.Length - 1; i++)
  1554. {
  1555. UserArray[User.Length + 1 + i] = 0x20;
  1556. }
  1557. }
  1558. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1410, UserArray);
  1559. }
  1560. /// <summary>
  1561. /// 写入自定义字符串2
  1562. /// </summary>
  1563. /// <param name="sender"></param>
  1564. /// <param name="e"></param>
  1565. private void button_WriteUser2_Click(object sender, EventArgs e)
  1566. {
  1567. string User = textBox_User2.Text;
  1568. var UserArray = new byte[16];
  1569. for (ushort i = 0; i < User.Length; i++)
  1570. {
  1571. UserArray[i] = (byte)User[i];
  1572. }
  1573. if (User.Length < 16)
  1574. {
  1575. UserArray[User.Length] = (byte)'.';
  1576. for (ushort i = 0; i < 16 - User.Length - 1; i++)
  1577. {
  1578. UserArray[User.Length + 1 + i] = 0x20;
  1579. }
  1580. }
  1581. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1610, UserArray);
  1582. }
  1583. /// <summary>
  1584. /// 写入自定义字符串3
  1585. /// </summary>
  1586. /// <param name="sender"></param>
  1587. /// <param name="e"></param>
  1588. private void button_WriteUser3_Click(object sender, EventArgs e)
  1589. {
  1590. string User = textBox_User3.Text;
  1591. var UserArray = new byte[16];
  1592. for (ushort i = 0; i < User.Length; i++)
  1593. {
  1594. UserArray[i] = (byte)User[i];
  1595. }
  1596. if (User.Length < 16)
  1597. {
  1598. UserArray[User.Length] = (byte)'.';
  1599. for (ushort i = 0; i < 16 - User.Length - 1; i++)
  1600. {
  1601. UserArray[User.Length + 1 + i] = 0x20;
  1602. }
  1603. }
  1604. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1810, UserArray);
  1605. }
  1606. /// <summary>
  1607. /// 查询生产信息
  1608. /// </summary>
  1609. /// <param name="sender"></param>
  1610. /// <param name="e"></param>
  1611. private void button_ReadMAC_Click(object sender, EventArgs e)
  1612. {
  1613. textBox_MAC.Text = "---";
  1614. textBox_MACAdd.Text = "---";
  1615. textBox_MACDate.Text = "---";
  1616. textBox_PP.Text = "---";
  1617. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1F00, null);
  1618. }
  1619. /// <summary>
  1620. /// 写入生产信息
  1621. /// </summary>
  1622. /// <param name="sender"></param>
  1623. /// <param name="e"></param>
  1624. private void button_WriteMac_Click(object sender, EventArgs e)
  1625. {
  1626. var MACArray = new byte[32];
  1627. //填入生产商
  1628. for (ushort i = 0; i < textBox_MAC.Text.Length; i++)
  1629. {
  1630. MACArray[i] = (byte)textBox_MAC.Text[i];
  1631. }
  1632. if (textBox_MAC.Text.Length < 8)
  1633. {
  1634. MACArray[textBox_MAC.Text.Length] = (byte)'.';
  1635. for (ushort i = 0; i < 8 - textBox_MAC.Text.Length - 1; i++)
  1636. {
  1637. MACArray[textBox_MAC.Text.Length + 1 + i] = 0x20;
  1638. }
  1639. }
  1640. //填入生产地
  1641. for (ushort i = 0; i < textBox_MACAdd.Text.Length; i++)
  1642. {
  1643. MACArray[8 + i] = (byte)textBox_MACAdd.Text[i];
  1644. }
  1645. if (textBox_MACAdd.Text.Length < 8)
  1646. {
  1647. MACArray[textBox_MACAdd.Text.Length + 8] = (byte)'.';
  1648. for (ushort i = 0; i < 8 - textBox_MACAdd.Text.Length - 1; i++)
  1649. {
  1650. MACArray[textBox_MACAdd.Text.Length + 1 + i + 8] = 0x20;
  1651. }
  1652. }
  1653. //填入生产日期
  1654. for (ushort i = 0; i < textBox_MACDate.Text.Length; i++)
  1655. {
  1656. MACArray[16 + i] = (byte)textBox_MACDate.Text[i];
  1657. }
  1658. if (textBox_MACDate.Text.Length < 8)
  1659. {
  1660. MACArray[textBox_MACDate.Text.Length + 16] = (byte)'.';
  1661. for (ushort i = 0; i < 8 - textBox_MACDate.Text.Length - 1; i++)
  1662. {
  1663. MACArray[textBox_MACDate.Text.Length + 1 + i + 16] = 0x20;
  1664. }
  1665. }
  1666. //填入品牌
  1667. for (ushort i = 0; i < textBox_PP.Text.Length; i++)
  1668. {
  1669. MACArray[24 + i] = (byte)textBox_PP.Text[i];
  1670. }
  1671. if (textBox_PP.Text.Length < 8)
  1672. {
  1673. MACArray[textBox_PP.Text.Length + 24] = (byte)'.';
  1674. for (ushort i = 0; i < 8 - textBox_PP.Text.Length - 1; i++)
  1675. {
  1676. MACArray[textBox_PP.Text.Length + 1 + i + 24] = 0x20;
  1677. }
  1678. }
  1679. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2420, MACArray);
  1680. }
  1681. /// <summary>
  1682. /// 查询密钥
  1683. /// </summary>
  1684. /// <param name="sender"></param>
  1685. /// <param name="e"></param>
  1686. private void button_ReadKey_Click(object sender, EventArgs e)
  1687. {
  1688. textBox_Key.Text = "---";
  1689. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1000, null);
  1690. }
  1691. /// <summary>
  1692. /// 写入校验密钥
  1693. /// </summary>
  1694. /// <param name="sender"></param>
  1695. /// <param name="e"></param>
  1696. private void button_WriteKey_Click(object sender, EventArgs e)
  1697. {
  1698. string Key = textBox_Key.Text;
  1699. var KeyArray = new byte[8];
  1700. if (Key.Length != 8)
  1701. {
  1702. timer_1s.Enabled = false;
  1703. MessageBox.Show("请检查字符长度是否为8", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1704. timer_1s.Enabled = true;
  1705. return;
  1706. }
  1707. for (ushort i = 0; i < Key.Length; i++)
  1708. {
  1709. KeyArray[i] = (byte)Key[i];
  1710. }
  1711. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1108, KeyArray);
  1712. }
  1713. /// <summary>
  1714. /// 查询在线检测结果
  1715. /// </summary>
  1716. /// <param name="sender"></param>
  1717. /// <param name="e"></param>
  1718. private void button_ReadOnLine_Click(object sender, EventArgs e)
  1719. {
  1720. textBox_Online.Text = "---";
  1721. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2100, null);
  1722. }
  1723. /// <summary>
  1724. /// 系统清除
  1725. /// </summary>
  1726. /// <param name="sender"></param>
  1727. /// <param name="e"></param>
  1728. private void button_SystemClear_Click(object sender, EventArgs e)
  1729. {
  1730. var Code = new byte[5];
  1731. Code[0] = (byte)'C';
  1732. Code[1] = (byte)'L';
  1733. Code[2] = (byte)'E';
  1734. Code[3] = (byte)'A';
  1735. Code[4] = (byte)'R';
  1736. timer_1s.Enabled = false;
  1737. if (MessageBox.Show("系统清除后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1738. {
  1739. if (MessageBox.Show("请再次确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1740. {
  1741. if (MessageBox.Show("请最后确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1742. {
  1743. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2605, Code);
  1744. MessageBox.Show("系统已关机,请重启!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  1745. }
  1746. }
  1747. }
  1748. timer_1s.Enabled = true;
  1749. }
  1750. /// <summary>
  1751. /// 参数还原
  1752. /// </summary>
  1753. /// <param name="sender"></param>
  1754. /// <param name="e"></param>
  1755. private void button_Recovery_Click(object sender, EventArgs e)
  1756. {
  1757. var Code = new byte[8];
  1758. Code[0] = (byte)'R';
  1759. Code[1] = (byte)'E';
  1760. Code[2] = (byte)'C';
  1761. Code[3] = (byte)'O';
  1762. Code[4] = (byte)'V';
  1763. Code[5] = (byte)'E';
  1764. Code[6] = (byte)'R';
  1765. Code[7] = (byte)'Y';
  1766. timer_1s.Enabled = false;
  1767. if (MessageBox.Show("参数还原后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1768. {
  1769. if (MessageBox.Show("请再次确认", "确认还原?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1770. {
  1771. if (MessageBox.Show("请最后确认", "确认还原?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1772. {
  1773. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2708, Code);
  1774. MessageBox.Show("系统已关机,请重启!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  1775. }
  1776. }
  1777. }
  1778. timer_1s.Enabled = true;
  1779. }
  1780. /// <summary>
  1781. /// 系统复位
  1782. /// </summary>
  1783. /// <param name="sender"></param>
  1784. /// <param name="e"></param>
  1785. private void button_Reset_Click(object sender, EventArgs e)
  1786. {
  1787. var Code = new byte[5];
  1788. Code[0] = (byte)'O';
  1789. Code[1] = (byte)'F';
  1790. Code[2] = (byte)'F';
  1791. Code[3] = (byte)'.';
  1792. Code[4] = (byte)'.';
  1793. timer_1s.Enabled = false;
  1794. if (MessageBox.Show("系统将关机", "确认关机?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1795. {
  1796. timer_1s.Enabled = true;
  1797. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2505, Code);
  1798. PowerOffAckStatus = false;
  1799. Delay_ms(250);
  1800. int TryCnt = 3;
  1801. while ((TryCnt--) >= 0)
  1802. {
  1803. if (!PowerOffAckStatus)
  1804. {
  1805. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2505, Code);
  1806. PowerOffAckStatus = false;
  1807. Delay_ms(250);
  1808. }
  1809. else break;
  1810. }
  1811. if (PowerOffAckStatus)
  1812. {
  1813. timer_1s.Enabled = false;
  1814. MessageBox.Show("已关机!", "提示", MessageBoxButtons.OK, MessageBoxIcon.None);
  1815. timer_1s.Enabled = true;
  1816. }
  1817. else
  1818. {
  1819. timer_1s.Enabled = false;
  1820. MessageBox.Show("关机失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1821. timer_1s.Enabled = true;
  1822. }
  1823. }
  1824. timer_1s.Enabled = true;
  1825. }
  1826. /// <summary>
  1827. /// 记录清除
  1828. /// </summary>
  1829. /// <param name="sender"></param>
  1830. /// <param name="e"></param>
  1831. private void button_ClearLog_Click(object sender, EventArgs e)
  1832. {
  1833. var Code = new byte[9];
  1834. Code[0] = (byte)'L';
  1835. Code[1] = (byte)'O';
  1836. Code[2] = (byte)'G';
  1837. Code[3] = (byte)' ';
  1838. Code[4] = (byte)'C';
  1839. Code[5] = (byte)'L';
  1840. Code[6] = (byte)'E';
  1841. Code[7] = (byte)'A';
  1842. Code[8] = (byte)'R';
  1843. timer_1s.Enabled = false;
  1844. if (MessageBox.Show("历史记录信息清除后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1845. {
  1846. if (MessageBox.Show("请再次确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1847. {
  1848. if (MessageBox.Show("请最后确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1849. {
  1850. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3909, Code);
  1851. MessageBox.Show("系统已关机,请重启!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  1852. }
  1853. }
  1854. }
  1855. timer_1s.Enabled = true;
  1856. }
  1857. /// <summary>
  1858. /// 打开系统计算器
  1859. /// </summary>
  1860. /// <param name="sender"></param>
  1861. /// <param name="e"></param>
  1862. private void 计算器ToolStripMenuItem_Click(object sender, EventArgs e)
  1863. {
  1864. System.Diagnostics.Process.Start("calc.exe");
  1865. }
  1866. /// <summary>
  1867. ///故障转换和显示
  1868. /// </summary>
  1869. List<string> ErrorInfo = new List<string>{ "过流保护", "低压保护", "过压保护", "堵转保护","过热保护","速度传感器故障","力矩传感器故障","霍尔故障",
  1870. "马达缺相","NTC故障","BMS校验失败","","OBC校验失败","MCU故障","踏频传感器故障","指拨故障","MOS短路","电压异常","","","","","","","","","","","","",
  1871. "",""};
  1872. private string ErrorInfoDisplay(UInt32 Code)
  1873. {
  1874. string Result = "";
  1875. if (Code == 0)
  1876. {
  1877. Result = "无故障";
  1878. }
  1879. else
  1880. {
  1881. for (int i = 0; i < 32; i++)
  1882. {
  1883. if ((Code & 0x01) == 0x01)
  1884. {
  1885. Result += ErrorInfo[i] + " ";
  1886. }
  1887. Code >>= 1;
  1888. }
  1889. }
  1890. return Result;
  1891. }
  1892. /// <summary>
  1893. /// 鼠标滑过故障码显示具体内容
  1894. /// </summary>
  1895. /// <param name="sender"></param>
  1896. /// <param name="e"></param>
  1897. private void textBox_ErrorCode_MouseHover(object sender, EventArgs e)
  1898. {
  1899. // 创建the ToolTip
  1900. ToolTip toolTip1 = new ToolTip();
  1901. // 设置显示样式
  1902. toolTip1.AutoPopDelay = 30000;//提示信息的可见时间
  1903. toolTip1.InitialDelay = 50;//事件触发多久后出现提示
  1904. toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  1905. toolTip1.ShowAlways = true;//是否显示提示框
  1906. // 设置伴随的对象.
  1907. string ErrorCode = this.textBox_ErrorCode.Text;
  1908. UInt32 Code = ErrorCode.Contains("H") ? Convert.ToUInt32(ErrorCode.Split('H')[0], 16) : 0;
  1909. toolTip1.SetToolTip(this.textBox_ErrorCode, ErrorInfoDisplay(Code));//设置故障内容显示
  1910. }
  1911. /// <summary>
  1912. /// 数据记录
  1913. /// </summary>
  1914. private void DataAutoSave()
  1915. {
  1916. if (记录数据ToolStripMenuItem.Checked == true)
  1917. {
  1918. if (SaveFilename == string.Empty)
  1919. {
  1920. return;
  1921. }
  1922. string SaveData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace(" ", "_") + " ";
  1923. foreach (Control c in groupBox3.Controls)
  1924. {
  1925. if (c is TextBox)
  1926. {
  1927. SaveData += c.Text + " , ";
  1928. }
  1929. }
  1930. SaveData += "\r\n";
  1931. System.IO.File.AppendAllText(SaveFilename, SaveData);//sb.ToString());
  1932. }
  1933. }
  1934. /// <summary>
  1935. /// 打开或关闭数据记录
  1936. /// </summary>
  1937. /// <param name="sender"></param>
  1938. /// <param name="e"></param>
  1939. private void 记录数据ToolStripMenuItem_Click(object sender, EventArgs e)
  1940. {
  1941. if (记录数据ToolStripMenuItem.Checked == false)
  1942. {
  1943. if (mySerialProcess.mySerial.IsOpen == true)
  1944. {
  1945. SaveFilename = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_运行信息" + ".txt";
  1946. timer_1s.Enabled = false;
  1947. MessageBox.Show("开始记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  1948. timer_1s.Enabled = true;
  1949. 记录数据ToolStripMenuItem.Checked = true;
  1950. }
  1951. else
  1952. {
  1953. timer_1s.Enabled = false;
  1954. MessageBox.Show("请连接电机", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  1955. timer_1s.Enabled = true;
  1956. }
  1957. }
  1958. else
  1959. {
  1960. 记录数据ToolStripMenuItem.Checked = false;
  1961. timer_1s.Enabled = false;
  1962. MessageBox.Show("停止记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  1963. timer_1s.Enabled = true;
  1964. }
  1965. }
  1966. /// <summary>
  1967. /// 仪表启动控制档位开关事件
  1968. /// </summary>
  1969. /// <param name="sender"></param>
  1970. /// <param name="e"></param>
  1971. private void checkBox_OBC_StartSetGearSt_CheckedChanged(object sender, EventArgs e)
  1972. {
  1973. if (checkBox_OBC_StartSetGearSt.Checked == true)
  1974. {
  1975. //检查串口是否打开
  1976. if (!mySerialProcess.mySerial.IsOpen)
  1977. {
  1978. timer_1s.Enabled = false;
  1979. MessageBox.Show("串口未连接!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  1980. timer_1s.Enabled = true;
  1981. checkBox_OBC_StartSetGearSt.Checked = false;
  1982. return;
  1983. }
  1984. //检查是否选择档位和打开车灯
  1985. if ((comboBox_OBC_SetGearST.SelectedIndex == -1) || (comboBox_OBC_LightSw.SelectedIndex == -1))
  1986. {
  1987. timer_1s.Enabled = false;
  1988. MessageBox.Show("请检查档位设置和大灯开关!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  1989. timer_1s.Enabled = true;
  1990. checkBox_OBC_StartSetGearSt.Checked = false;
  1991. return;
  1992. }
  1993. }
  1994. }
  1995. /// <summary>
  1996. /// OBC查询电机用户参数
  1997. /// </summary>
  1998. /// <param name="sender"></param>
  1999. /// <param name="e"></param>
  2000. private void button_OBC_ReadUserInfo_Click(object sender, EventArgs e)
  2001. {
  2002. richTextBox_OBC_ReadUserInfo.Clear();
  2003. mySerialProcess.SendCmd((ushort)0x731, (byte)0x11, (ushort)0x3300, null);
  2004. }
  2005. /// <summary>
  2006. /// OBC设置电机用户参数
  2007. /// </summary>
  2008. /// <param name="sender"></param>
  2009. /// <param name="e"></param>
  2010. private void button_OBC_WriteUserInfo_Click(object sender, EventArgs e)
  2011. {
  2012. var UserInfoCode = new byte[8];
  2013. for (int i = 0; i < UserInfoCode.Length; i++)
  2014. UserInfoCode[i] = 0;
  2015. if (comboBox_OBC_StartMode.SelectedIndex == -1)//检查数据有效性
  2016. {
  2017. timer_1s.Enabled = false;
  2018. MessageBox.Show("请选择启动模式!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2019. timer_1s.Enabled = true;
  2020. return;
  2021. }
  2022. if (comboBox_OBC_AssistFunc.SelectedIndex == -1)//检查数据有效性
  2023. {
  2024. timer_1s.Enabled = false;
  2025. MessageBox.Show("请选择助力方案!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2026. timer_1s.Enabled = true;
  2027. return;
  2028. }
  2029. UserInfoCode[0] = (byte)(int)numericUpDown_OBC_WheelAdj.Value;
  2030. UserInfoCode[1] = (byte)(comboBox_OBC_StartMode.SelectedIndex + 1);
  2031. UserInfoCode[2] = (byte)(comboBox_OBC_AssistFunc.SelectedIndex + 1);
  2032. UserInfoCode[3] = (byte)(uint)numericUpDown_OBC_OffTime.Value;
  2033. mySerialProcess.SendCmd((ushort)0x731, (byte)0x16, (ushort)0x3408, UserInfoCode);
  2034. }
  2035. /// <summary>
  2036. /// OBC查询电机版本信息
  2037. /// </summary>
  2038. /// <param name="sender"></param>
  2039. /// <param name="e"></param>
  2040. private void button_OBC_ReadVerInfo_Click(object sender, EventArgs e)
  2041. {
  2042. textBox_Model.Text = "---";
  2043. textBox_SN.Text = "---";
  2044. textBox_HW.Text = "---";
  2045. textBox_FW.Text = "---";
  2046. textBox_SP.Text = "---";
  2047. textBox_OBC_ReadModel.Text = "---";
  2048. textBox_OBC_ReadSN.Text = "---";
  2049. textBox_OBC_ReadHW.Text = "---";
  2050. textBox_OBC_ReadFW.Text = "---";
  2051. textBox_OBC_ReadSP.Text = "---";
  2052. textBox_FacModeName.Text = "---";
  2053. textBox_FacModeNum.Text = "---";
  2054. textBox_FacModeHW.Text = "---";
  2055. textBox_FacModeFW.Text = "---";
  2056. textBox_FacModeSP.Text = "---";
  2057. Class_Motor_Ver.Mode = "---";
  2058. Class_Motor_Ver.SN = "---";
  2059. Class_Motor_Ver.HW = "---";
  2060. Class_Motor_Ver.FW = "---";
  2061. Class_Motor_Ver.Special = "---";
  2062. mySerialProcess.SendCmd((ushort)0x731, (byte)0x11, (ushort)0x3900, null);
  2063. }
  2064. /// <summary>
  2065. /// OBC读取骑行历史
  2066. /// </summary>
  2067. /// <param name="sender"></param>
  2068. /// <param name="e"></param>
  2069. private void button_OBC_ReadRideInfo_Click(object sender, EventArgs e)
  2070. {
  2071. textBox_OBC_TRIP_KM.Text = "";
  2072. textBox_OBC_TRIP_TIME.Text = "";
  2073. textBox_OBC_ODO_KM.Text = "";
  2074. textBox_OBC_ODO_TIME.Text = "";
  2075. mySerialProcess.SendCmd((ushort)0x731, (byte)0x11, (ushort)0x3500, null);
  2076. }
  2077. /// <summary>
  2078. /// OBC清除TRIP里程
  2079. /// </summary>
  2080. /// <param name="sender"></param>
  2081. /// <param name="e"></param>
  2082. private void button_OBC_ClearTrip_Click(object sender, EventArgs e)
  2083. {
  2084. var Code = new byte[5];
  2085. Code[0] = (byte)'C';
  2086. Code[1] = (byte)'L';
  2087. Code[2] = (byte)'E';
  2088. Code[3] = (byte)'A';
  2089. Code[4] = (byte)'R';
  2090. timer_1s.Enabled = false;
  2091. if (MessageBox.Show("TRIP清除后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2092. mySerialProcess.SendCmd((ushort)0x731, (byte)0x16, (ushort)0x3605, Code);
  2093. timer_1s.Enabled = true;
  2094. }
  2095. private void checkBox_OBC_StartReadBMS_CheckedChanged(object sender, EventArgs e)
  2096. {
  2097. if (checkBox_OBC_StartReadBMS.Checked == true)
  2098. {
  2099. //检查串口是否打开
  2100. if (!mySerialProcess.mySerial.IsOpen)
  2101. {
  2102. timer_1s.Enabled = false;
  2103. MessageBox.Show("串口未连接!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2104. timer_1s.Enabled = true;
  2105. checkBox_OBC_StartReadBMS.Checked = false;
  2106. return;
  2107. }
  2108. }
  2109. }
  2110. /// <summary>
  2111. /// 写入力矩传感器参数
  2112. /// </summary>
  2113. /// <param name="sender"></param>
  2114. /// <param name="e"></param>
  2115. private void button_WriteTqSensorParam_Click(object sender, EventArgs e)
  2116. {
  2117. var ConfigParam = new byte[46];
  2118. for (int i = 0; i < 14; i++)
  2119. ConfigParam[i] = 0;
  2120. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  2121. ushort uwDataTemp = 0;
  2122. //力矩传感器参数只写入前面14项与力矩传感器相关的参数
  2123. string[] strDataTemp = richTextBox_SensorParam.Text.Split(new string[] { ", " }, StringSplitOptions.None);
  2124. int index = 0;
  2125. try
  2126. {
  2127. for (int i = 0; i < strDataTemp.Length; i++)
  2128. {
  2129. if (strDataTemp[i].Split('=')[0] == "第四阶段转矩点采集值")
  2130. {
  2131. index = i;
  2132. break;
  2133. }
  2134. }
  2135. for (int i = 0; i < (index + 1); i++)
  2136. {
  2137. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  2138. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  2139. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  2140. }
  2141. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x472E, ConfigParam);
  2142. }
  2143. catch (Exception)
  2144. {
  2145. timer_1s.Enabled = false;
  2146. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2147. timer_1s.Enabled = true;
  2148. }
  2149. }
  2150. /// <summary>
  2151. /// 查询存储标志
  2152. /// </summary>
  2153. /// <param name="sender"></param>
  2154. /// <param name="e"></param>
  2155. private void button_ReadSaveFlag_Click(object sender, EventArgs e)
  2156. {
  2157. radioButton_EE_SaveYes.Checked = false;
  2158. radioButton_EE_SaveNo.Checked = false;
  2159. radioButton_SIP_SaveYes.Checked = false;
  2160. radioButton_SIP_SaveNo.Checked = false;
  2161. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4800, null);
  2162. }
  2163. /// <summary>
  2164. /// 写入存储标志
  2165. /// </summary>
  2166. /// <param name="sender"></param>
  2167. /// <param name="e"></param>
  2168. private void button_WriteSaveFlag_Click(object sender, EventArgs e)
  2169. {
  2170. var SaveFlag = new byte[6];
  2171. for (int i = 0; i < 6; i++)
  2172. SaveFlag[i] = 0;
  2173. if ((radioButton_EE_SaveYes.Checked == false) && (radioButton_EE_SaveNo.Checked == false))
  2174. {
  2175. timer_1s.Enabled = false;
  2176. MessageBox.Show("请选择EEPROM存储标志", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2177. timer_1s.Enabled = true;
  2178. return;
  2179. }
  2180. if ((radioButton_SIP_SaveYes.Checked == false) && (radioButton_SIP_SaveNo.Checked == false))
  2181. {
  2182. timer_1s.Enabled = false;
  2183. MessageBox.Show("请选择SIP校验存储标志", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2184. timer_1s.Enabled = true;
  2185. return;
  2186. }
  2187. SaveFlag[0] = (byte)((checkBox_SaveYes.Checked) ? 0x01 : 0x00);
  2188. SaveFlag[2] = (byte)(radioButton_EE_SaveYes.Checked ? 0x01 : 0x00);
  2189. SaveFlag[3] = 0;
  2190. SaveFlag[4] = (byte)(radioButton_SIP_SaveYes.Checked ? 0x01 : 0x00);
  2191. SaveFlag[5] = 0;
  2192. timer_1s.Enabled = false;
  2193. if (MessageBox.Show("确认修改存储标志?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2194. {
  2195. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x4906, SaveFlag);
  2196. }
  2197. timer_1s.Enabled = true;
  2198. }
  2199. /// <summary>
  2200. /// 读取指定地址数据
  2201. /// </summary>
  2202. /// <param name="sender"></param>
  2203. /// <param name="e"></param>
  2204. private void button_ReadRamFlash_Click(object sender, EventArgs e)
  2205. {
  2206. var Address = new byte[4];
  2207. UInt32 Address_Begin;
  2208. UInt32 Addres_End;
  2209. richTextBox_RamFlasgData.Clear();
  2210. if ((textBox_Address_Begin.Text == string.Empty) || (textBox_Address_End.Text == string.Empty))
  2211. {
  2212. timer_1s.Enabled = false;
  2213. MessageBox.Show("请输入起始和结束地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2214. timer_1s.Enabled = true;
  2215. return;
  2216. }
  2217. else if ((textBox_Address_Begin.Text.Length != 8) || (textBox_Address_End.Text.Length != 8))
  2218. {
  2219. timer_1s.Enabled = false;
  2220. MessageBox.Show("请检查地址长度!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2221. timer_1s.Enabled = true;
  2222. return;
  2223. }
  2224. else
  2225. {
  2226. Address = mySerialProcess.HexStringToBytes(textBox_Address_Begin.Text, false);
  2227. Address_Begin = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
  2228. Address = mySerialProcess.HexStringToBytes(textBox_Address_End.Text, false);
  2229. Addres_End = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
  2230. if (Addres_End < Address_Begin)
  2231. {
  2232. timer_1s.Enabled = false;
  2233. MessageBox.Show("结束地址必须大于或等于起始地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2234. timer_1s.Enabled = true;
  2235. return;
  2236. }
  2237. else
  2238. {
  2239. var SendByte = new byte[8];
  2240. SendByte[0] = (byte)(Address_Begin >> 24);
  2241. SendByte[1] = (byte)(Address_Begin >> 16);
  2242. SendByte[2] = (byte)(Address_Begin >> 8);
  2243. SendByte[3] = (byte)(Address_Begin);
  2244. SendByte[4] = (byte)(Addres_End >> 24);
  2245. SendByte[5] = (byte)(Addres_End >> 16);
  2246. SendByte[6] = (byte)(Addres_End >> 8);
  2247. SendByte[7] = (byte)(Addres_End);
  2248. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2D08, SendByte);
  2249. }
  2250. }
  2251. }
  2252. /// <summary>
  2253. /// 工作模式选择
  2254. /// </summary>
  2255. /// <param name="sender"></param>
  2256. /// <param name="e"></param>
  2257. private void comboBox_WorkMode_SelectedIndexChanged(object sender, EventArgs e)
  2258. {
  2259. var RunMode = new byte[1];
  2260. if (comboBox_WorkMode.Text == "配置模式")
  2261. RunMode[0] = 0x01;
  2262. else
  2263. RunMode[0] = 0x00;
  2264. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, RunMode);
  2265. }
  2266. /// <summary>
  2267. /// 助力档位选择
  2268. /// </summary>
  2269. /// <param name="sender"></param>
  2270. /// <param name="e"></param>
  2271. private void comboBox_GearSt_SelectedIndexChanged(object sender, EventArgs e)
  2272. {
  2273. var CtrlCode = new byte[2];
  2274. if (comboBox_GearSt.SelectedIndex == 5)//档位
  2275. CtrlCode[0] = 0x33;
  2276. else if (comboBox_GearSt.SelectedIndex == 6)//档位
  2277. CtrlCode[0] = 0x22;
  2278. else
  2279. CtrlCode[0] = (byte)comboBox_GearSt.SelectedIndex;
  2280. if (comboBox_LightSwitch.SelectedIndex == 0)//车灯
  2281. CtrlCode[1] = 0xF1;
  2282. else
  2283. CtrlCode[1] = 0xF0;
  2284. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  2285. comboBox_OBC_SetGearST.SelectedIndex = comboBox_GearSt.SelectedIndex;
  2286. comboBox_OBC_LightSw.SelectedIndex = comboBox_LightSwitch.SelectedIndex;
  2287. }
  2288. /// <summary>
  2289. /// 仪表档位选择
  2290. /// </summary>
  2291. /// <param name="sender"></param>
  2292. /// <param name="e"></param>
  2293. private void comboBox_OBC_SetGearST_SelectedIndexChanged(object sender, EventArgs e)
  2294. {
  2295. comboBox_GearSt.SelectedIndex = comboBox_OBC_SetGearST.SelectedIndex;
  2296. comboBox_LightSwitch.SelectedIndex = comboBox_OBC_LightSw.SelectedIndex;
  2297. }
  2298. /// <summary>
  2299. /// 生产模式初始化
  2300. /// </summary>
  2301. /// <param name="sender"></param>
  2302. /// <param name="e"></param>
  2303. private void button_FacModeInit_Click(object sender, EventArgs e)
  2304. {
  2305. var Code = new byte[5];
  2306. Code[0] = (byte)'C';
  2307. Code[1] = (byte)'L';
  2308. Code[2] = (byte)'E';
  2309. Code[3] = (byte)'A';
  2310. Code[4] = (byte)'R';
  2311. timer_1s.Enabled = false;
  2312. if (MessageBox.Show("电机即将初始化!", "确认?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2313. {
  2314. if (MessageBox.Show("请再次确认", "确认?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2315. {
  2316. timer_1s.Enabled = true;
  2317. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2605, Code)) return;
  2318. PowerOffAckStatus = false;
  2319. Delay_ms(250);
  2320. int TryCnt = 3;
  2321. while ((TryCnt--) >= 0)
  2322. {
  2323. if (!PowerOffAckStatus)
  2324. {
  2325. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2605, Code)) return;
  2326. PowerOffAckStatus = false;
  2327. Delay_ms(250);
  2328. }
  2329. else break;
  2330. }
  2331. if (PowerOffAckStatus)
  2332. {
  2333. timer_1s.Enabled = false;
  2334. MessageBox.Show("系统已关机,请保持无负载重新开机!", "提示", MessageBoxButtons.OK, MessageBoxIcon.None);
  2335. timer_1s.Enabled = true;
  2336. }
  2337. else
  2338. {
  2339. timer_1s.Enabled = false;
  2340. MessageBox.Show("初始化失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2341. timer_1s.Enabled = true;
  2342. }
  2343. timer_1s.Enabled = true;
  2344. }
  2345. }
  2346. timer_1s.Enabled = true;
  2347. //清除历史测试记录
  2348. textBox_FacModeSensorADC0.Text = "";
  2349. textBox_FacModeSensorADC1.Text = "";
  2350. textBox_FacModeSensorADC2.Text = "";
  2351. textBox_FacModeSensorADC3.Text = "";
  2352. textBox_FacModeSensorADC4.Text = "";
  2353. checkBox_FacModeCheckBikeSpeed.Checked = false;
  2354. checkBox_FacModeCheckCadence.Checked = false;
  2355. checkBox_FacModeCheckNTC.Checked = false;
  2356. checkBox_FacModeCheckVol.Checked = false;
  2357. LightSwitchStatus = false;
  2358. MotorRunStatus = false;
  2359. trackBar_FacModeMotorSpeedAdj.Value = 5;
  2360. textBox_FacModeWheelSize.Text = "";
  2361. textBox_FacModeSpeedLimit.Text = "";
  2362. comboBox_FacModeAssist1.SelectedIndex = -1;
  2363. comboBox_FacModeAssist2.SelectedIndex = -1;
  2364. comboBox_FacModeLightVol.SelectedIndex = -1;
  2365. comboBox_FacModeStartMode.SelectedIndex = -1;
  2366. textBox_FacModeStartGain.Text = "";
  2367. textBox_FacModeCircuitK.Text = "";
  2368. textBox_FacModeFltCounter.Text = "";
  2369. textBox_FacModeSpeedLimitTh.Text = "";
  2370. textBox_FacModeSpeedLimitEnd.Text = "";
  2371. textBox_FacModeCadencePer.Text = "";
  2372. textBox_FacModeName.Text = "";
  2373. textBox_FacModeNum.Text = "";
  2374. textBox_FacModeHW.Text = "";
  2375. textBox_FacModeFW.Text = "";
  2376. }
  2377. /// <summary>
  2378. /// 生产模式读取传感器参数
  2379. /// </summary>
  2380. /// <param name="sender"></param>
  2381. /// <param name="e"></param>
  2382. private void button_FacModeSensorRead_Click(object sender, EventArgs e)
  2383. {
  2384. richTextBox_SensorParam.Clear();
  2385. textBox_FacModeSensorADC0.Text = "";
  2386. textBox_FacModeSensorADC1.Text = "";
  2387. textBox_FacModeSensorADC2.Text = "";
  2388. textBox_FacModeSensorADC3.Text = "";
  2389. textBox_FacModeSensorADC4.Text = "";
  2390. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4000, null);
  2391. }
  2392. /// <summary>
  2393. /// 生产模式传感器标定1
  2394. /// </summary>
  2395. /// <param name="sender"></param>
  2396. /// <param name="e"></param>
  2397. private void buttonFacModeSet1_Click(object sender, EventArgs e)
  2398. {
  2399. var ConfigParam = new byte[4];
  2400. for (int i = 0; i < 4; i++)
  2401. ConfigParam[i] = 0;
  2402. ushort uwDataTemp = 0;
  2403. try
  2404. {
  2405. uwDataTemp = 1;//负载序号
  2406. ConfigParam[0] = (byte)(uwDataTemp & 0xFF);
  2407. ConfigParam[1] = (byte)(uwDataTemp >> 8);
  2408. uwDataTemp = (ushort)(decimal.Parse(textBox_FacModeSensorCal1.Text) * 10); ;//负载值0.1Nm
  2409. ConfigParam[2] = (byte)(uwDataTemp & 0xFF);
  2410. ConfigParam[3] = (byte)(uwDataTemp >> 8);
  2411. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x4104, ConfigParam);
  2412. Delay_ms(200);
  2413. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4000, null);
  2414. }
  2415. catch (Exception)
  2416. {
  2417. timer_1s.Enabled = false;
  2418. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2419. timer_1s.Enabled = true;
  2420. }
  2421. }
  2422. /// <summary>
  2423. /// 生产模式传感器标定2
  2424. /// </summary>
  2425. /// <param name="sender"></param>
  2426. /// <param name="e"></param>
  2427. private void buttonFacModeSet2_Click(object sender, EventArgs e)
  2428. {
  2429. var ConfigParam = new byte[4];
  2430. for (int i = 0; i < 4; i++)
  2431. ConfigParam[i] = 0;
  2432. ushort uwDataTemp = 0;
  2433. try
  2434. {
  2435. uwDataTemp = 2;//负载序号
  2436. ConfigParam[0] = (byte)(uwDataTemp & 0xFF);
  2437. ConfigParam[1] = (byte)(uwDataTemp >> 8);
  2438. uwDataTemp = (ushort)(decimal.Parse(textBox_FacModeSensorCal2.Text) * 10); ;//负载值0.1Nm
  2439. ConfigParam[2] = (byte)(uwDataTemp & 0xFF);
  2440. ConfigParam[3] = (byte)(uwDataTemp >> 8);
  2441. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x4104, ConfigParam);
  2442. Delay_ms(200);
  2443. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4000, null);
  2444. }
  2445. catch (Exception)
  2446. {
  2447. timer_1s.Enabled = false;
  2448. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2449. timer_1s.Enabled = true;
  2450. }
  2451. }
  2452. /// <summary>
  2453. /// 生产模式传感器标定3
  2454. /// </summary>
  2455. /// <param name="sender"></param>
  2456. /// <param name="e"></param>
  2457. private void buttonFacModeSet3_Click(object sender, EventArgs e)
  2458. {
  2459. var ConfigParam = new byte[4];
  2460. for (int i = 0; i < 4; i++)
  2461. ConfigParam[i] = 0;
  2462. ushort uwDataTemp = 0;
  2463. try
  2464. {
  2465. uwDataTemp = 3;//负载序号
  2466. ConfigParam[0] = (byte)(uwDataTemp & 0xFF);
  2467. ConfigParam[1] = (byte)(uwDataTemp >> 8);
  2468. uwDataTemp = (ushort)(decimal.Parse(textBox_FacModeSensorCal3.Text) * 10); ;//负载值0.1Nm
  2469. ConfigParam[2] = (byte)(uwDataTemp & 0xFF);
  2470. ConfigParam[3] = (byte)(uwDataTemp >> 8);
  2471. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x4104, ConfigParam);
  2472. Delay_ms(200);
  2473. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4000, null);
  2474. }
  2475. catch (Exception)
  2476. {
  2477. timer_1s.Enabled = false;
  2478. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2479. timer_1s.Enabled = true;
  2480. }
  2481. }
  2482. /// <summary>
  2483. /// 生产模式传感器标定3
  2484. /// </summary>
  2485. /// <param name="sender"></param>
  2486. /// <param name="e"></param>
  2487. private void buttonFacModeSet4_Click(object sender, EventArgs e)
  2488. {
  2489. var ConfigParam = new byte[4];
  2490. for (int i = 0; i < 4; i++)
  2491. ConfigParam[i] = 0;
  2492. ushort uwDataTemp = 0;
  2493. try
  2494. {
  2495. uwDataTemp = 4;//负载序号
  2496. ConfigParam[0] = (byte)(uwDataTemp & 0xFF);
  2497. ConfigParam[1] = (byte)(uwDataTemp >> 8);
  2498. uwDataTemp = (ushort)(decimal.Parse(textBox_FacModeSensorCal4.Text) * 10); ;//负载值0.1Nm
  2499. ConfigParam[2] = (byte)(uwDataTemp & 0xFF);
  2500. ConfigParam[3] = (byte)(uwDataTemp >> 8);
  2501. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x4104, ConfigParam);
  2502. Delay_ms(200);
  2503. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4000, null);
  2504. }
  2505. catch (Exception)
  2506. {
  2507. timer_1s.Enabled = false;
  2508. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2509. timer_1s.Enabled = true;
  2510. }
  2511. }
  2512. /// <summary>
  2513. /// 生产模式采集按钮
  2514. /// </summary>
  2515. /// <param name="sender"></param>
  2516. /// <param name="e"></param>
  2517. private void button_FacModeWorkMode_Click(object sender, EventArgs e)
  2518. {
  2519. var RunMode = new byte[1];
  2520. if (button_FacModeWorkMode.Text == "开始")
  2521. {
  2522. RunMode[0] = 0x01;
  2523. if (mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, RunMode))
  2524. {
  2525. button_FacModeWorkMode.Text = "停止";
  2526. button_FacModeWorkMode.BackColor = Color.Yellow;
  2527. }
  2528. }
  2529. else if (button_FacModeWorkMode.Text == "停止")
  2530. {
  2531. RunMode[0] = 0x00;
  2532. if (mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, RunMode))
  2533. {
  2534. button_FacModeWorkMode.Text = "开始";
  2535. button_FacModeWorkMode.BackColor = Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
  2536. }
  2537. }
  2538. }
  2539. /// <summary>
  2540. /// 生产模式开关灯
  2541. /// </summary>
  2542. /// <param name="sender"></param>
  2543. /// <param name="e"></param>
  2544. bool LightSwitchStatus = false;
  2545. private void button_FacModeLightSW_Click(object sender, EventArgs e)
  2546. {
  2547. var CtrlCode = new byte[2];
  2548. CtrlCode[0] = 0x00;
  2549. if (LightSwitchStatus)
  2550. {
  2551. CtrlCode[1] = 0xF0;
  2552. button_FacModeLightSW.BackColor = Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(128)))));
  2553. }
  2554. else
  2555. {
  2556. CtrlCode[1] = 0xF1;
  2557. button_FacModeLightSW.BackColor = Color.Yellow;
  2558. }
  2559. if(mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode))
  2560. {
  2561. LightSwitchStatus = !LightSwitchStatus;
  2562. comboBox_LightSwitch.SelectedIndex = LightSwitchStatus ? 0 : 1;
  2563. comboBox_OBC_LightSw.SelectedIndex = LightSwitchStatus ? 0 : 1;
  2564. }
  2565. }
  2566. /// <summary>
  2567. /// 生产模式电机控制
  2568. /// </summary>
  2569. /// <param name="sender"></param>
  2570. /// <param name="e"></param>
  2571. bool MotorRunStatus = false;
  2572. private void button_FacModeMotorRun_Click(object sender, EventArgs e)
  2573. {
  2574. //DebugMtbDefaultInfo = "整体运行模式=5, 位置获取模式=1, 采样模式=2, 旋转方向=0, 定位电流=1000, 拖拽电压=18, 拖拽电流=1000, 拖拽频率=20, 加速斜率=500, 减速斜率=100, 转速环控制器带宽=15, 转速环控制器m=4, 电流环控制器带宽=300, 电流环控制器m=1, 磁链观测器带宽=10, 磁链观测器m=10, 锁相环带宽=150, 锁相环m=2, 惯量=500, PWM 最大占空比=900, PWM七段式切五段式阈值=700, 功率限幅值=6500, 功率Error=500, 功率限幅Kp=20, 功率限幅Ki=3000";
  2575. //DebugCityDefaultInfo = "整体运行模式=4, 位置获取模式=1, 采样模式=2, 旋转方向=1, 定位电流=1000, 拖拽电压=18, 拖拽电流=1000, 拖拽频率=20, 加速斜率=500, 减速斜率=100, 转速环控制器带宽=15, 转速环控制器m=4, 电流环控制器带宽=300, 电流环控制器m=1, 磁链观测器带宽=10, 磁链观测器m=10, 锁相环带宽=150, 锁相环m=2, 惯量=300, PWM 最大占空比=900, PWM七段式切五段式阈值=700, 功率限幅值=6500, 功率Error=500, 功率限幅Kp=20, 功率限幅Ki=3000";
  2576. string DebugDefaultInfo = "整体运行模式=4, 位置获取模式=1, 采样模式=2, 旋转方向=1, 定位电流=1000, 拖拽电压=18, 拖拽电流=1000, 拖拽频率=20, 加速斜率=500, 减速斜率=100, 转速环控制器带宽=15, 转速环控制器m=4, 电流环控制器带宽=300, 电流环控制器m=1, 磁链观测器带宽=10, 磁链观测器m=10, 锁相环带宽=150, 锁相环m=2, 惯量=300, PWM 最大占空比=900, PWM七段式切五段式阈值=700, 功率限幅值=6500, 功率Error=500, 功率限幅Kp=20, 功率限幅Ki=3000";
  2577. string[] strDataTemp = DebugDefaultInfo.Split(new string[] { ", " }, StringSplitOptions.None);
  2578. var ConfigParam = new byte[60];
  2579. for (int i = 0; i < 60; i++)
  2580. ConfigParam[i] = 0;
  2581. ConfigParam[0] = 0x00;
  2582. ushort uwDataTemp = 0;
  2583. if (!MotorRunStatus) //停止状态
  2584. {
  2585. //设置电机调试参数
  2586. strDataTemp[0] = "整体运行模式=3";
  2587. if (radioButton_FacModeDriverCity.Checked == true)
  2588. {
  2589. strDataTemp[3] = "旋转方向=1";
  2590. strDataTemp[10] = "转速环控制器带宽=3";
  2591. strDataTemp[18] = "惯量=300";
  2592. }
  2593. else if (radioButton_FacModeDriverMtb.Checked == true)
  2594. {
  2595. strDataTemp[3] = "旋转方向=0";
  2596. strDataTemp[10] = "转速环控制器带宽=3";
  2597. strDataTemp[18] = "惯量=500";
  2598. }
  2599. else
  2600. {
  2601. timer_1s.Enabled = false;
  2602. MessageBox.Show("请选择电机类型!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2603. timer_1s.Enabled = true;
  2604. return;
  2605. }
  2606. try
  2607. {
  2608. for (int i = 0; i < strDataTemp.Length; i++)
  2609. {
  2610. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  2611. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  2612. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  2613. }
  2614. if(!mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x463C, ConfigParam))
  2615. return;
  2616. }
  2617. catch (Exception)
  2618. {
  2619. timer_1s.Enabled = false;
  2620. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2621. timer_1s.Enabled = true;
  2622. }
  2623. //设置转速百分比50%
  2624. trackBar_FacModeMotorSpeedAdj.Value = 100;
  2625. MotorRunStatus = !MotorRunStatus;
  2626. button_FacModeMotorRun.BackColor = Color.Yellow;
  2627. //电机进入配置模式
  2628. var RunMode = new byte[1];
  2629. RunMode[0] = 0x01;
  2630. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, RunMode);
  2631. button_FacModeWorkMode.Text = "停止";
  2632. button_FacModeWorkMode.BackColor = Color.Yellow;
  2633. }
  2634. else //运转状态
  2635. {
  2636. //设置电机调试参数
  2637. strDataTemp[0] = "整体运行模式=5";
  2638. if (radioButton_FacModeDriverCity.Checked == true)
  2639. {
  2640. strDataTemp[3] = "旋转方向=1";
  2641. strDataTemp[10] = "转速环控制器带宽=15";
  2642. strDataTemp[18] = "惯量=300";
  2643. }
  2644. else if (radioButton_FacModeDriverMtb.Checked == true)
  2645. {
  2646. strDataTemp[3] = "旋转方向=0";
  2647. strDataTemp[10] = "转速环控制器带宽=15";
  2648. strDataTemp[18] = "惯量=500";
  2649. }
  2650. else
  2651. {
  2652. timer_1s.Enabled = false;
  2653. MessageBox.Show("请选择电机类型!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2654. timer_1s.Enabled = true;
  2655. return;
  2656. }
  2657. try
  2658. {
  2659. for (int i = 0; i < strDataTemp.Length; i++)
  2660. {
  2661. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  2662. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  2663. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  2664. }
  2665. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x463C, ConfigParam))
  2666. return;
  2667. }
  2668. catch (Exception)
  2669. {
  2670. timer_1s.Enabled = false;
  2671. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2672. timer_1s.Enabled = true;
  2673. }
  2674. //设置转速百分比50%
  2675. trackBar_FacModeMotorSpeedAdj.Value = 5;
  2676. MotorRunStatus = !MotorRunStatus;
  2677. button_FacModeMotorRun.BackColor = Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
  2678. //电机退出配置模式
  2679. var RunMode = new byte[1];
  2680. RunMode[0] = 0x00;
  2681. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, RunMode);
  2682. button_FacModeWorkMode.Text = "开始";
  2683. button_FacModeWorkMode.BackColor = Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
  2684. }
  2685. }
  2686. /// <summary>
  2687. /// 生产模式转速调节
  2688. /// </summary>
  2689. /// <param name="sender"></param>
  2690. /// <param name="e"></param>
  2691. private void trackBar_FacModeMotorSpeedAdj_ValueChanged(object sender, EventArgs e)
  2692. {
  2693. //转速百分比更新
  2694. label_FacModeMotorSpeedSet.Text = trackBar_FacModeMotorSpeedAdj.Value.ToString() + "%";
  2695. numericUpDown_SpeedAdj.Value = trackBar_FacModeMotorSpeedAdj.Value;
  2696. //设置转速百分比
  2697. var Data = new byte[1];
  2698. Data[0] = (byte)trackBar_FacModeMotorSpeedAdj.Value;
  2699. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2C01, Data);
  2700. }
  2701. /// <summary>
  2702. /// 生产模式读取整车参数
  2703. /// </summary>
  2704. /// <param name="sender"></param>
  2705. /// <param name="e"></param>
  2706. private void button_FacModeBikeParaRead_Click(object sender, EventArgs e)
  2707. {
  2708. textBox_FacModeWheelSize.Text = "";
  2709. textBox_FacModeSpeedLimit.Text = "";
  2710. comboBox_FacModeAssist1.SelectedIndex = -1;
  2711. comboBox_FacModeAssist2.SelectedIndex = -1;
  2712. comboBox_FacModeLightVol.SelectedIndex = -1;
  2713. comboBox_FacModeStartMode.SelectedIndex = -1;
  2714. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3C00, null);
  2715. }
  2716. /// <summary>
  2717. /// 生产模式写入整车参数
  2718. /// </summary>
  2719. /// <param name="sender"></param>
  2720. /// <param name="e"></param>
  2721. private void button_FacModeBikeParaSet_Click(object sender, EventArgs e)
  2722. {
  2723. string BikeDefaultInfo = "轮胎周长=219, 电控传动比=35, 助力最大限速=25, 推行模式限速=6, 前牙盘T数=38, 后牙盘T数=11, 助力方案1=341, 助力方案2=682, 前后灯电压=6, 轮胎周长微调=0, 启动模式=2, 自动关机时间=15";
  2724. var ConfigParam = new byte[28];
  2725. for (int i = 0; i < 28; i++)
  2726. ConfigParam[i] = 0;
  2727. ConfigParam[0] = (byte)(0x01);
  2728. ushort uwDataTemp = 0;
  2729. string[] strDataTemp = BikeDefaultInfo.Split(new string[] { ", " }, StringSplitOptions.None);
  2730. try
  2731. {
  2732. for (int i = 0; i < strDataTemp.Length; i++)
  2733. {
  2734. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  2735. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  2736. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  2737. }
  2738. //周长
  2739. uwDataTemp = Convert.ToUInt16(textBox_FacModeWheelSize.Text);
  2740. ConfigParam[2] = (byte)uwDataTemp;
  2741. ConfigParam[3] = (byte)(uwDataTemp >> 8);
  2742. //限速
  2743. uwDataTemp = Convert.ToUInt16(textBox_FacModeSpeedLimit.Text);
  2744. ConfigParam[6] = (byte)uwDataTemp;
  2745. ConfigParam[7] = (byte)(uwDataTemp >> 8);
  2746. //助力方案1
  2747. if (comboBox_FacModeAssist1.SelectedIndex == 0) uwDataTemp = 0;
  2748. else if (comboBox_FacModeAssist1.SelectedIndex == 1) uwDataTemp = 341;
  2749. else if (comboBox_FacModeAssist1.SelectedIndex == 2) uwDataTemp = 682;
  2750. else
  2751. {
  2752. timer_1s.Enabled = false;
  2753. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2754. timer_1s.Enabled = true;
  2755. return;
  2756. }
  2757. ConfigParam[14] = (byte)uwDataTemp;
  2758. ConfigParam[15] = (byte)(uwDataTemp >> 8);
  2759. //助力方案2
  2760. if(comboBox_FacModeAssist2.SelectedIndex == 0) uwDataTemp = 0;
  2761. else if (comboBox_FacModeAssist2.SelectedIndex == 1) uwDataTemp = 341;
  2762. else if (comboBox_FacModeAssist2.SelectedIndex == 2) uwDataTemp = 682;
  2763. else
  2764. {
  2765. timer_1s.Enabled = false;
  2766. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2767. timer_1s.Enabled = true;
  2768. return;
  2769. }
  2770. ConfigParam[16] = (byte)uwDataTemp;
  2771. ConfigParam[17] = (byte)(uwDataTemp >> 8);
  2772. //灯压
  2773. if(comboBox_FacModeLightVol.SelectedIndex == 0) uwDataTemp = 6;
  2774. else if (comboBox_FacModeLightVol.SelectedIndex == 1) uwDataTemp = 12;
  2775. else
  2776. {
  2777. timer_1s.Enabled = false;
  2778. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2779. timer_1s.Enabled = true;
  2780. return;
  2781. }
  2782. ConfigParam[18] = (byte)uwDataTemp;
  2783. ConfigParam[19] = (byte)(uwDataTemp >> 8);
  2784. //启动模式
  2785. if (comboBox_FacModeStartMode.SelectedIndex == 0) uwDataTemp = 1;
  2786. else if (comboBox_FacModeStartMode.SelectedIndex == 2) uwDataTemp = 3;
  2787. else uwDataTemp = 2;
  2788. ConfigParam[22] = (byte)uwDataTemp;
  2789. ConfigParam[23] = (byte)(uwDataTemp >> 8);
  2790. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3D1C, ConfigParam);
  2791. }
  2792. catch (Exception)
  2793. {
  2794. timer_1s.Enabled = false;
  2795. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2796. timer_1s.Enabled = true;
  2797. }
  2798. }
  2799. /// <summary>
  2800. /// 生产模式读取助力参数
  2801. /// </summary>
  2802. /// <param name="sender"></param>
  2803. /// <param name="e"></param>
  2804. private void button_FacModeAssistParaRead_Click(object sender, EventArgs e)
  2805. {
  2806. var ConfigParam = new byte[4];
  2807. textBox_FacModeStartGain.Text = "";
  2808. textBox_FacModeCircuitK.Text = "";
  2809. textBox_FacModeFltCounter.Text = "";
  2810. textBox_FacModeSpeedLimitTh.Text ="";
  2811. textBox_FacModeSpeedLimitEnd.Text = "";
  2812. textBox_FacModeCadencePer.Text = "";
  2813. ConfigParam[0] = (byte)(1 & 0xFF);
  2814. ConfigParam[1] = (byte)(1 >> 8);
  2815. ConfigParam[2] = (byte)(1 & 0xFF);
  2816. ConfigParam[3] = (byte)(1 >> 8);
  2817. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4304, ConfigParam);
  2818. }
  2819. /// <summary>
  2820. /// 生产模式写入助力参数
  2821. /// </summary>
  2822. /// <param name="sender"></param>
  2823. /// <param name="e"></param>
  2824. private void button_FacModeAssistParaSet_Click(object sender, EventArgs e)
  2825. {
  2826. string AssistDefaultInfo = "零速启动增益=4096, 巡航启动增益=4096, 助力转矩曲线编号=1, 助力踏频曲线编号=1, 转矩曲线.a=0, 转矩曲线.b=0, 转矩曲线.c=2048, 转矩曲线.d=137, 踏频曲线.a=0, 踏频曲线.b=0, 踏频曲线.c=35000, 踏频曲线.d=0, 助力启动阈值=80, 助力停止阈值=40, 启动时电流增长阶梯=10, 启动对应踏频脉冲数=32, 转矩滤波对应踏频脉冲数=45, 待速转速=0, 待速最大电流=500, 车速限幅启动阈值=25, 车速限幅停止阈值=27, 踏频占比=1229";
  2827. var ConfigParam = new byte[80];
  2828. for (int i = 0; i < 80; i++)
  2829. ConfigParam[i] = 0;
  2830. ConfigParam[0] = (byte)(0x01);
  2831. ushort uwDataTemp = 0;
  2832. ulong ulDataTemp = 0;
  2833. int index_1 = 0, index_2 = 0;
  2834. string[] strDataTemp = AssistDefaultInfo.Split(new string[] { ", " }, StringSplitOptions.None);
  2835. try
  2836. {
  2837. //因数据类型不同,预处理
  2838. for (int i = 0; i < strDataTemp.Length; i++)
  2839. {
  2840. if (strDataTemp[i].Split('=')[0] == "转矩曲线.a")
  2841. {
  2842. index_1 = i;
  2843. break;
  2844. }
  2845. }
  2846. for (int i = 0; i < strDataTemp.Length; i++)
  2847. {
  2848. if (strDataTemp[i].Split('=')[0] == "踏频曲线.d")
  2849. {
  2850. index_2 = i;
  2851. break;
  2852. }
  2853. }
  2854. for (int i = 0; i < strDataTemp.Length; i++)
  2855. {
  2856. ulDataTemp = Convert.ToUInt32(strDataTemp[i].Split('=')[1]);
  2857. if (i < index_1)
  2858. {
  2859. ConfigParam[2 * i + 4] = (byte)ulDataTemp;
  2860. ConfigParam[2 * i + 5] = (byte)(ulDataTemp >> 8);
  2861. }
  2862. else if (i <= index_2)
  2863. {
  2864. ConfigParam[4 * i - 4] = (byte)ulDataTemp;
  2865. ConfigParam[4 * i - 3] = (byte)(ulDataTemp >> 8);
  2866. ConfigParam[4 * i - 2] = (byte)(ulDataTemp >> 16);
  2867. ConfigParam[4 * i - 1] = (byte)(ulDataTemp >> 24);
  2868. }
  2869. else
  2870. {
  2871. ConfigParam[2 * i + 20] = (byte)ulDataTemp;
  2872. ConfigParam[2 * i + 21] = (byte)(ulDataTemp >> 8);
  2873. }
  2874. }
  2875. //零速增益
  2876. uwDataTemp = Convert.ToUInt16(textBox_FacModeStartGain.Text);
  2877. ConfigParam[4] = (byte)uwDataTemp;
  2878. ConfigParam[5] = (byte)(uwDataTemp >> 8);
  2879. //电流阶梯
  2880. uwDataTemp = Convert.ToUInt16(textBox_FacModeCircuitK.Text);
  2881. ConfigParam[48] = (byte)uwDataTemp;
  2882. ConfigParam[49] = (byte)(uwDataTemp >> 8);
  2883. //滤波脉冲
  2884. uwDataTemp = Convert.ToUInt16(textBox_FacModeFltCounter.Text);
  2885. ConfigParam[52] = (byte)uwDataTemp;
  2886. ConfigParam[53] = (byte)(uwDataTemp >> 8);
  2887. //限速启动
  2888. uwDataTemp = Convert.ToUInt16(textBox_FacModeSpeedLimitTh.Text);
  2889. ConfigParam[58] = (byte)uwDataTemp;
  2890. ConfigParam[59] = (byte)(uwDataTemp >> 8);
  2891. //限速停止
  2892. uwDataTemp = Convert.ToUInt16(textBox_FacModeSpeedLimitEnd.Text);
  2893. ConfigParam[60] = (byte)uwDataTemp;
  2894. ConfigParam[61] = (byte)(uwDataTemp >> 8);
  2895. //踏频占比
  2896. uwDataTemp = Convert.ToUInt16(textBox_FacModeCadencePer.Text);
  2897. ConfigParam[62] = (byte)uwDataTemp;
  2898. ConfigParam[63] = (byte)(uwDataTemp >> 8);
  2899. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x4450, ConfigParam);
  2900. }
  2901. catch (Exception)
  2902. {
  2903. timer_1s.Enabled = false;
  2904. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2905. timer_1s.Enabled = true;
  2906. }
  2907. }
  2908. /// <summary>
  2909. /// 生产模式写入型号
  2910. /// </summary>
  2911. /// <param name="sender"></param>
  2912. /// <param name="e"></param>
  2913. private void button_FacModeSetName_Click(object sender, EventArgs e)
  2914. {
  2915. string Mode = textBox_FacModeName.Text;
  2916. var ModeArray = new byte[16];
  2917. for (ushort i = 0; i < Mode.Length; i++)
  2918. {
  2919. ModeArray[i] = (byte)Mode[i];
  2920. }
  2921. if (Mode.Length < 16)
  2922. {
  2923. ModeArray[Mode.Length] = (byte)'.';
  2924. for (ushort i = 0; i < 16 - Mode.Length - 1; i++)
  2925. {
  2926. ModeArray[Mode.Length + 1 + i] = 0x20;
  2927. }
  2928. }
  2929. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2210, ModeArray);
  2930. }
  2931. /// <summary>
  2932. /// 生产模式写入SN
  2933. /// </summary>
  2934. /// <param name="sender"></param>
  2935. /// <param name="e"></param>
  2936. private void Button_FacModeSetNum_Click(object sender, EventArgs e)
  2937. {
  2938. string SN = textBox_FacModeNum.Text;
  2939. var SNArray = new byte[16];
  2940. for (ushort i = 0; i < SN.Length; i++)
  2941. {
  2942. SNArray[i] = (byte)SN[i];
  2943. }
  2944. if (SN.Length < 16)
  2945. {
  2946. SNArray[SN.Length] = (byte)'.';
  2947. for (ushort i = 0; i < 16 - SN.Length - 1; i++)
  2948. {
  2949. SNArray[SN.Length + 1 + i] = 0x20;
  2950. }
  2951. }
  2952. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2310, SNArray);
  2953. }
  2954. /// <summary>
  2955. /// 生产模式读取版本信息
  2956. /// </summary>
  2957. /// <param name="sender"></param>
  2958. /// <param name="e"></param>
  2959. private void Button_FacModeReadVer_Click_1(object sender, EventArgs e)
  2960. {
  2961. textBox_Model.Text = "---";
  2962. textBox_SN.Text = "---";
  2963. textBox_HW.Text = "---";
  2964. textBox_FW.Text = "---";
  2965. textBox_SP.Text = "---";
  2966. textBox_OBC_ReadModel.Text = "---";
  2967. textBox_OBC_ReadSN.Text = "---";
  2968. textBox_OBC_ReadHW.Text = "---";
  2969. textBox_OBC_ReadFW.Text = "---";
  2970. textBox_OBC_ReadSP.Text = "---";
  2971. textBox_FacModeName.Text = "---";
  2972. textBox_FacModeNum.Text = "---";
  2973. textBox_FacModeHW.Text = "---";
  2974. textBox_FacModeFW.Text = "---";
  2975. textBox_FacModeSP.Text = "---";
  2976. Class_Motor_Ver.Mode = "---";
  2977. Class_Motor_Ver.SN = "---";
  2978. Class_Motor_Ver.HW = "---";
  2979. Class_Motor_Ver.FW = "---";
  2980. Class_Motor_Ver.Special = "---";
  2981. mySerialProcess.SendCmd((ushort)0x731, (byte)0x11, (ushort)0x3900, null);
  2982. }
  2983. /// <summary>
  2984. /// 生产模式写入生产信息
  2985. /// </summary>
  2986. /// <param name="sender"></param>
  2987. /// <param name="e"></param>
  2988. private void Button_FacModeSetMacDate_Click(object sender, EventArgs e)
  2989. {
  2990. var MACArray = new byte[32];
  2991. string MacDefaultInfo = "Welling";
  2992. string MacAddDefaultInfo = "Shunde";
  2993. string MacDateDefaultInfo = DateTime.Now.ToString("yyyyMMdd");
  2994. string MacPPDefaultInfo = "MM_MC1";
  2995. //填入生产商
  2996. for (ushort i = 0; i < MacDefaultInfo.Length; i++)
  2997. {
  2998. MACArray[i] = (byte)MacDefaultInfo[i];
  2999. }
  3000. if (MacDefaultInfo.Length < 8)
  3001. {
  3002. MACArray[MacDefaultInfo.Length] = (byte)'.';
  3003. for (ushort i = 0; i < 8 - MacDefaultInfo.Length - 1; i++)
  3004. {
  3005. MACArray[MacDefaultInfo.Length + 1 + i] = 0x20;
  3006. }
  3007. }
  3008. //填入生产地
  3009. for (ushort i = 0; i < MacAddDefaultInfo.Length; i++)
  3010. {
  3011. MACArray[8 + i] = (byte)MacAddDefaultInfo[i];
  3012. }
  3013. if (MacAddDefaultInfo.Length < 8)
  3014. {
  3015. MACArray[MacAddDefaultInfo.Length + 8] = (byte)'.';
  3016. for (ushort i = 0; i < 8 - MacAddDefaultInfo.Length - 1; i++)
  3017. {
  3018. MACArray[MacAddDefaultInfo.Length + 1 + i + 8] = 0x20;
  3019. }
  3020. }
  3021. //填入生产日期
  3022. for (ushort i = 0; i < MacDateDefaultInfo.Length; i++)
  3023. {
  3024. MACArray[16 + i] = (byte)MacDateDefaultInfo[i];
  3025. }
  3026. if (MacDateDefaultInfo.Length < 8)
  3027. {
  3028. MACArray[MacDateDefaultInfo.Length + 16] = (byte)'.';
  3029. for (ushort i = 0; i < 8 - MacDateDefaultInfo.Length - 1; i++)
  3030. {
  3031. MACArray[MacDateDefaultInfo.Length + 1 + i + 16] = 0x20;
  3032. }
  3033. }
  3034. //填入产品标识
  3035. for (ushort i = 0; i < MacPPDefaultInfo.Length; i++)
  3036. {
  3037. MACArray[24 + i] = (byte)MacPPDefaultInfo[i];
  3038. }
  3039. if (MacPPDefaultInfo.Length < 8)
  3040. {
  3041. MACArray[MacPPDefaultInfo.Length + 24] = (byte)'.';
  3042. for (ushort i = 0; i < 8 - MacPPDefaultInfo.Length - 1; i++)
  3043. {
  3044. MACArray[MacPPDefaultInfo.Length + 1 + i + 24] = 0x20;
  3045. }
  3046. }
  3047. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2420, MACArray);
  3048. }
  3049. /// <summary>
  3050. /// 生产模式保存测试页面
  3051. /// </summary>
  3052. /// <param name="sender"></param>
  3053. /// <param name="e"></param>
  3054. private void Button_FacModeSaveResult_Click(object sender, EventArgs e)
  3055. {
  3056. //查询传感器
  3057. richTextBox_SensorParam.Clear();
  3058. textBox_FacModeSensorADC0.Text = "";
  3059. textBox_FacModeSensorADC1.Text = "";
  3060. textBox_FacModeSensorADC2.Text = "";
  3061. textBox_FacModeSensorADC3.Text = "";
  3062. textBox_FacModeSensorADC4.Text = "";
  3063. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4000, null)) return;
  3064. Delay_ms(100);
  3065. //读取一次电机信息
  3066. var CtrlCode = new byte[2];
  3067. CtrlCode[0] = 0x00;
  3068. CtrlCode[1] = 0xF0;
  3069. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode)) return;
  3070. Delay_ms(100);
  3071. //读取一次整车参数
  3072. textBox_FacModeWheelSize.Text = "";
  3073. textBox_FacModeSpeedLimit.Text = "";
  3074. comboBox_FacModeAssist1.SelectedIndex = -1;
  3075. comboBox_FacModeAssist2.SelectedIndex = -1;
  3076. comboBox_FacModeLightVol.SelectedIndex = -1;
  3077. comboBox_FacModeStartMode.SelectedIndex = -1;
  3078. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3C00, null)) return;
  3079. Delay_ms(100);
  3080. //读取一次助力参数
  3081. var ConfigParam = new byte[4];
  3082. textBox_FacModeStartGain.Text = "";
  3083. textBox_FacModeCircuitK.Text = "";
  3084. textBox_FacModeFltCounter.Text = "";
  3085. textBox_FacModeSpeedLimitTh.Text = "";
  3086. textBox_FacModeSpeedLimitEnd.Text = "";
  3087. textBox_FacModeCadencePer.Text = "";
  3088. ConfigParam[0] = (byte)(1 & 0xFF);
  3089. ConfigParam[1] = (byte)(1 >> 8);
  3090. ConfigParam[2] = (byte)(1 & 0xFF);
  3091. ConfigParam[3] = (byte)(1 >> 8);
  3092. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4304, ConfigParam)) return;
  3093. Delay_ms(100);
  3094. //读取一次版本信息
  3095. textBox_Model.Text = "---";
  3096. textBox_SN.Text = "---";
  3097. textBox_HW.Text = "---";
  3098. textBox_FW.Text = "---";
  3099. textBox_OBC_ReadModel.Text = "---";
  3100. textBox_OBC_ReadSN.Text = "---";
  3101. textBox_OBC_ReadHW.Text = "---";
  3102. textBox_OBC_ReadFW.Text = "---";
  3103. textBox_FacModeName.Text = "---";
  3104. textBox_FacModeNum.Text = "---";
  3105. textBox_FacModeHW.Text = "---";
  3106. textBox_FacModeFW.Text = "---";
  3107. Class_Motor_Ver.Mode = "---";
  3108. Class_Motor_Ver.SN = "---";
  3109. Class_Motor_Ver.HW = "---";
  3110. Class_Motor_Ver.FW = "---";
  3111. Class_Motor_Ver.Special = "---";
  3112. if (!mySerialProcess.SendCmd((ushort)0x731, (byte)0x11, (ushort)0x3900, null)) return;
  3113. Delay_ms(100);
  3114. //提示检查
  3115. timer_1s.Enabled = false;
  3116. if (MessageBox.Show("请检查信息是否完整!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
  3117. {
  3118. MessageBox.Show("请检查参数!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  3119. timer_1s.Enabled = true;
  3120. return;
  3121. }
  3122. timer_1s.Enabled = true;
  3123. //保存页面
  3124. Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap
  3125. Graphics g = Graphics.FromImage(bit);
  3126. g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片
  3127. SaveFileDialog sf = new SaveFileDialog();
  3128. sf.Title = "页面保存";
  3129. sf.FileName = textBox_FacModeName.Text + "_" + textBox_FacModeNum.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_页面存储" + ".png";
  3130. sf.Filter = "图片|*.png";
  3131. timer_1s.Enabled = false;
  3132. if (sf.ShowDialog() == DialogResult.OK)
  3133. {
  3134. bit.Save(sf.FileName);//默认保存格式为PNG,保存成jpg格式质量不是很好
  3135. }
  3136. timer_1s.Enabled = true;
  3137. //关机保存参数
  3138. var Code = new byte[5];
  3139. Code[0] = (byte)'O';
  3140. Code[1] = (byte)'F';
  3141. Code[2] = (byte)'F';
  3142. Code[3] = (byte)'.';
  3143. Code[4] = (byte)'.';
  3144. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2505, Code);
  3145. PowerOffAckStatus = false;
  3146. Delay_ms(250);
  3147. int TryCnt = 3;
  3148. while ((TryCnt--) >= 0)
  3149. {
  3150. if (!PowerOffAckStatus)
  3151. {
  3152. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2505, Code);
  3153. PowerOffAckStatus = false;
  3154. Delay_ms(250);
  3155. }
  3156. else break;
  3157. }
  3158. if (PowerOffAckStatus)
  3159. {
  3160. timer_1s.Enabled = false;
  3161. MessageBox.Show("测试完成,已关机!", "提示", MessageBoxButtons.OK, MessageBoxIcon.None);
  3162. timer_1s.Enabled = true;
  3163. }
  3164. else
  3165. {
  3166. timer_1s.Enabled = false;
  3167. MessageBox.Show("关机失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  3168. timer_1s.Enabled = true;
  3169. }
  3170. }
  3171. /// <summary>
  3172. /// 工具箱页面保存
  3173. /// </summary>
  3174. /// <param name="sender"></param>
  3175. /// <param name="e"></param>
  3176. private void 页面保存ToolStripMenuItem_Click(object sender, EventArgs e)
  3177. {
  3178. //保存页面
  3179. Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap
  3180. Graphics g = Graphics.FromImage(bit);
  3181. g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片
  3182. SaveFileDialog sf = new SaveFileDialog();
  3183. sf.Title = "页面保存";
  3184. sf.FileName = "MotorInfo_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + ".png";
  3185. sf.Filter = "图片|*.png";
  3186. timer_1s.Enabled = false;
  3187. if (sf.ShowDialog() == DialogResult.OK)
  3188. {
  3189. bit.Save(sf.FileName);//默认保存格式为PNG,保存成jpg格式质量不是很好
  3190. }
  3191. timer_1s.Enabled = true;
  3192. }
  3193. }
  3194. }