Form1.cs 144 KB

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