Form1.cs 187 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO.Ports;
  9. using System.Threading;
  10. using System.Runtime.InteropServices;
  11. using System.Runtime.CompilerServices;
  12. using System.Collections;
  13. using System.IO;
  14. namespace MOTINOVA_MC_Test
  15. {
  16. public partial class Form1 : Form
  17. {
  18. #region------------------------本地变量-----------------------------
  19. #region 串口相关
  20. private List<byte> buffer = new List<byte>(4096);
  21. private byte[] binary_data_1 = new byte[1024];
  22. private bool Listening = false;// 侦听串口是否是接收数据标志位
  23. public static string m_strDataBits = "8";
  24. public static string m_strStopBits = "1";
  25. public static string m_strCheckBits = "None";
  26. public static bool g_blnIsOpen = false;
  27. #endregion
  28. #region FTP实例
  29. public static ftp myFtp = new ftp();
  30. public static string ServerIP = "";
  31. public static string ServerPort = "";
  32. public static string ServerUser = "";
  33. public static string ServerPasswd = "";
  34. public static string ServerPath = "";
  35. #endregion
  36. #region 离线使用标识
  37. bool Offline = true;
  38. #endregion
  39. #region 本地配置存储数据路径
  40. public static string LocalPath = "";
  41. string ConfigFileName = "";
  42. #endregion
  43. #region MC运行信息超时计数
  44. bool MC_RunInfo_Refresh = false;
  45. ushort MC_RunInfo_Refresh_Cnt = 0;
  46. #endregion
  47. #region MC故障码超时计数
  48. bool MC_ErrorCode_Refresh = false;
  49. ushort MC_ErrorCode_Refresh_Cnt = 0;
  50. #endregion
  51. #region 力矩传感器平均值采集
  52. byte[] TorqueArray = new byte[25];
  53. byte[] Sensor1Array = new byte[25];
  54. byte[] Sensor2Array = new byte[25];
  55. byte[] Sensor3Array = new byte[25];
  56. ushort TorqueArray_Index = 0;
  57. #endregion
  58. #region 自动发送助力档位计时
  59. ushort AutoSendTimeCnt;
  60. #endregion
  61. #region 骑行信息超时计数
  62. bool MC_RideInfo_Refresh = false;
  63. ushort MC_RideInfo_Refresh_Cnt = 0;
  64. #endregion
  65. #region 姿态信息超时计数
  66. bool MC_AttitudeAngle_Refresh = false;
  67. ushort MC_AttitudeAngle_Refresh_Cnt = 0;
  68. #endregion
  69. #region 自动发送查询姿态信息计时
  70. ushort AutoSendReadAttitudeAngleTimeCnt;
  71. #endregion
  72. #region 自动发送查询骑行信息计时
  73. ushort AutoSendReadRideInfoTimeCnt;
  74. #endregion
  75. #region 自动存储数据
  76. string AutoSaveFileName = "";
  77. #endregion
  78. #region 故障日志存储相关
  79. bool ErrorLogSaveFlag = false;
  80. string ErrorLogSaveFileName = "";
  81. #endregion
  82. #region CDL在线检测延时
  83. ushort CDL_OnlineCheck_Cnt = 0;
  84. bool CDL_Online_Flag = true;
  85. #endregion
  86. #region 截图悬浮窗
  87. topFormCapture myCapture;
  88. #endregion
  89. #region 解锁写保护标志
  90. bool WriteProtectUnlock = false;
  91. #endregion
  92. Form2 LoginForm = new Form2();
  93. #endregion
  94. public Form1()
  95. {
  96. InitializeComponent();
  97. //显示截图悬浮窗
  98. myCapture = new topFormCapture();
  99. myCapture.Show();
  100. //检查配置文件
  101. LocalPath = Directory.GetCurrentDirectory();
  102. if (Directory.Exists(LocalPath + "\\" + "MotorRunTest") == false)
  103. Directory.CreateDirectory(LocalPath + "\\" + "MotorRunTest");
  104. ConfigFileName = Directory.GetCurrentDirectory() + "\\" + "Config.ttcfg";
  105. if (File.Exists(ConfigFileName)) //存在配置文件,导入配置信息
  106. {
  107. //打开文件
  108. StreamReader objReader = new StreamReader(ConfigFileName);
  109. string sLine = "";
  110. ArrayList array_CfgInfo = new ArrayList();
  111. array_CfgInfo.Clear();
  112. while (sLine != null)
  113. {
  114. sLine = objReader.ReadLine();
  115. array_CfgInfo.Add(sLine);
  116. }
  117. objReader.Close();
  118. //解析配置文件
  119. try
  120. {
  121. ServerIP = array_CfgInfo[1].ToString().Split(':')[1];
  122. ServerPort = array_CfgInfo[2].ToString().Split(':')[1];
  123. ServerUser = array_CfgInfo[3].ToString().Split(':')[1];
  124. ServerPasswd = array_CfgInfo[4].ToString().Split(':')[1];
  125. ServerPath = array_CfgInfo[5].ToString().Split(':')[1];
  126. Offline = (array_CfgInfo[6].ToString().Split(':')[1] == "TRUE") ? true : false;
  127. }
  128. catch (System.Exception)
  129. {
  130. MessageBox.Show("参数格式错误,写入默认值!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  131. }
  132. }
  133. else //不存在配置文件,生成文件写入默认值
  134. {
  135. string CfgFileInfo = "";
  136. ServerIP = "10.176.42.30";
  137. ServerPort = "21";
  138. ServerUser = "factory_test";
  139. ServerPasswd = "ttium";
  140. ServerPath = "MotorRunTest";
  141. CfgFileInfo += "[ServerSet]" + "\r\n";
  142. CfgFileInfo += "IPAddress:" + ServerIP + "\r\n";
  143. CfgFileInfo += "Port:" + ServerPort + "\r\n";
  144. CfgFileInfo += "User:" + ServerUser + "\r\n";
  145. CfgFileInfo += "Password:" + ServerPasswd + "\r\n";
  146. CfgFileInfo += "PathName:" + ServerPath + "\r\n";
  147. CfgFileInfo += "Offline:" + "FALSE" + "\r\n";
  148. System.IO.File.WriteAllText(ConfigFileName, CfgFileInfo);
  149. }
  150. //配置网络FTP服务器
  151. myFtp.FtpOption(ServerIP, ServerPort, ServerUser, ServerPasswd);
  152. //创建线程,定时检测网络连接状态
  153. Thread th = new Thread(NetworkCheck);
  154. th.IsBackground = true;
  155. th.Start();
  156. }
  157. /// <summary>
  158. /// 检查网络状态线程
  159. /// </summary>
  160. private void NetworkCheck()
  161. {
  162. //初始化第一次连接FTP服务器
  163. bool Result = myFtp.CheckFtp();
  164. this.Invoke((EventHandler)(delegate
  165. {
  166. if (Result == true)//服务器连接成功
  167. {
  168. MessageBox.Show("服务器连接成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  169. myFtp.IsNetConnected = true;
  170. myCapture.label_ServerStatus.BackColor = Color.Green;
  171. }
  172. else//服务器连接失败
  173. {
  174. MessageBox.Show("服务器连接失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  175. myFtp.IsNetConnected = false;
  176. myCapture.label_ServerStatus.BackColor = Color.Red;
  177. if (Offline != true)
  178. this.Dispose();
  179. }
  180. }));
  181. //创建定时器,定时30s检查网络
  182. System.Timers.Timer timer_CheckNet = new System.Timers.Timer();
  183. timer_CheckNet.Enabled = true;
  184. timer_CheckNet.Interval = 30000;
  185. timer_CheckNet.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_CheckNet_Tick);
  186. timer_CheckNet.Start();
  187. while (true)
  188. {
  189. Thread.Sleep(1);
  190. }
  191. }
  192. /// <summary>
  193. /// 线程中定时任务,检查网络状态
  194. /// </summary>
  195. /// <param name="sender"></param>
  196. /// <param name="e"></param>
  197. private void timer_CheckNet_Tick(object sender, EventArgs e)
  198. {
  199. //连接FTP服务器
  200. bool Result = myFtp.CheckFtp();
  201. this.Invoke((EventHandler)(delegate
  202. {
  203. if (Result == true)//服务器连接成功
  204. {
  205. myFtp.IsNetConnected = true;
  206. myCapture.label_ServerStatus.BackColor = Color.Green;
  207. }
  208. else//服务器连接失败
  209. {
  210. myFtp.IsNetConnected = false;
  211. myCapture.label_ServerStatus.BackColor = Color.Red;
  212. }
  213. }));
  214. }
  215. #region 打开或关闭串口
  216. private void button_ComOpen_Click(object sender, EventArgs e)
  217. {
  218. byte[] Data = new byte[1];
  219. try
  220. {
  221. if (!serialPort1.IsOpen)
  222. {
  223. serialPort1.PortName = comboBox_ComIndex.Text;
  224. serialPort1.BaudRate = int.Parse(comboBox_Baudrate.Text);
  225. serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), m_strCheckBits);
  226. serialPort1.DataBits = Int32.Parse(m_strDataBits);
  227. serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), (m_strStopBits == "1.5") ? "3" : m_strStopBits);
  228. serialPort1.Open();
  229. comboBox_ComIndex.Enabled = false;
  230. comboBox_Baudrate.Enabled = false;
  231. button_ComOpen.Text = "断开(&C)";
  232. g_blnIsOpen = true;
  233. timer1.Enabled = true;
  234. button_RefreshPort.Enabled = false;
  235. //发送指令检测CDL是否在线
  236. SendCmd((ushort)0x7FF, (byte)0x11, (ushort)0x1100, null);
  237. CDL_OnlineCheck_Cnt = 0;
  238. CDL_Online_Flag = false;
  239. //开机
  240. Data[0] = 0xF1;
  241. SendCmd(0x7FF, 0x16, 0x2201, Data);
  242. }
  243. else
  244. {
  245. //关机
  246. Data[0] = 0xF0;
  247. SendCmd(0x7FF, 0x16, 0x2201, Data);
  248. while (Listening) Application.DoEvents();
  249. //打开时点击,则关闭串口
  250. serialPort1.Close();
  251. comboBox_ComIndex.Enabled = true;
  252. comboBox_Baudrate.Enabled = true;
  253. button_ComOpen.Text = "连接(&C)";
  254. g_blnIsOpen = false;
  255. timer1.Enabled = false;
  256. button_RefreshPort.Enabled = true;
  257. CDL_Online_Flag = true;
  258. }
  259. }
  260. catch (System.Exception ex)
  261. {
  262. MessageBox.Show(ex.Message);
  263. return;
  264. }
  265. }
  266. #endregion
  267. private void Form1_Load(object sender, EventArgs e)
  268. {
  269. //Form2 LoginForm = new Form2() ;
  270. //LoginForm.Show();
  271. string[] ports = SerialPort.GetPortNames();
  272. Array.Sort(ports);
  273. comboBox_ComIndex.Items.AddRange(ports);
  274. comboBox_ComIndex.SelectedIndex = comboBox_ComIndex.Items.Count > 0 ? 0 : -1;
  275. comboBox_Baudrate.SelectedIndex = comboBox_Baudrate.Items.IndexOf("115200");
  276. MC_Config_Clear();
  277. Motor_Config_Clear();
  278. MC_RunInfo_Clear();
  279. MC_ErrorCode_Clear();
  280. MC_Config_Ext_Clear();
  281. MC_AttitudeAngle_Clear();
  282. MC_RideInfo_Clear();
  283. MC_RunLog_Clear();
  284. label_BiuldTime.Text = System.IO.File.GetLastWriteTime(this.GetType().Assembly.Location).ToString();
  285. }
  286. #region CRC校验
  287. static UInt32[] crc32_table = new UInt32[256]
  288. {
  289. 0x00000000, 0x04C11DB7, 0x09823B6E, 0x0D4326D9, 0x130476DC, 0x17C56B6B,
  290. 0x1A864DB2, 0x1E475005, 0x2608EDB8, 0x22C9F00F, 0x2F8AD6D6, 0x2B4BCB61,
  291. 0x350C9B64, 0x31CD86D3, 0x3C8EA00A, 0x384FBDBD, 0x4C11DB70, 0x48D0C6C7,
  292. 0x4593E01E, 0x4152FDA9, 0x5F15ADAC, 0x5BD4B01B, 0x569796C2, 0x52568B75,
  293. 0x6A1936C8, 0x6ED82B7F, 0x639B0DA6, 0x675A1011, 0x791D4014, 0x7DDC5DA3,
  294. 0x709F7B7A, 0x745E66CD, 0x9823B6E0, 0x9CE2AB57, 0x91A18D8E, 0x95609039,
  295. 0x8B27C03C, 0x8FE6DD8B, 0x82A5FB52, 0x8664E6E5, 0xBE2B5B58, 0xBAEA46EF,
  296. 0xB7A96036, 0xB3687D81, 0xAD2F2D84, 0xA9EE3033, 0xA4AD16EA, 0xA06C0B5D,
  297. 0xD4326D90, 0xD0F37027, 0xDDB056FE, 0xD9714B49, 0xC7361B4C, 0xC3F706FB,
  298. 0xCEB42022, 0xCA753D95, 0xF23A8028, 0xF6FB9D9F, 0xFBB8BB46, 0xFF79A6F1,
  299. 0xE13EF6F4, 0xE5FFEB43, 0xE8BCCD9A, 0xEC7DD02D, 0x34867077, 0x30476DC0,
  300. 0x3D044B19, 0x39C556AE, 0x278206AB, 0x23431B1C, 0x2E003DC5, 0x2AC12072,
  301. 0x128E9DCF, 0x164F8078, 0x1B0CA6A1, 0x1FCDBB16, 0x018AEB13, 0x054BF6A4,
  302. 0x0808D07D, 0x0CC9CDCA, 0x7897AB07, 0x7C56B6B0, 0x71159069, 0x75D48DDE,
  303. 0x6B93DDDB, 0x6F52C06C, 0x6211E6B5, 0x66D0FB02, 0x5E9F46BF, 0x5A5E5B08,
  304. 0x571D7DD1, 0x53DC6066, 0x4D9B3063, 0x495A2DD4, 0x44190B0D, 0x40D816BA,
  305. 0xACA5C697, 0xA864DB20, 0xA527FDF9, 0xA1E6E04E, 0xBFA1B04B, 0xBB60ADFC,
  306. 0xB6238B25, 0xB2E29692, 0x8AAD2B2F, 0x8E6C3698, 0x832F1041, 0x87EE0DF6,
  307. 0x99A95DF3, 0x9D684044, 0x902B669D, 0x94EA7B2A, 0xE0B41DE7, 0xE4750050,
  308. 0xE9362689, 0xEDF73B3E, 0xF3B06B3B, 0xF771768C, 0xFA325055, 0xFEF34DE2,
  309. 0xC6BCF05F, 0xC27DEDE8, 0xCF3ECB31, 0xCBFFD686, 0xD5B88683, 0xD1799B34,
  310. 0xDC3ABDED, 0xD8FBA05A, 0x690CE0EE, 0x6DCDFD59, 0x608EDB80, 0x644FC637,
  311. 0x7A089632, 0x7EC98B85, 0x738AAD5C, 0x774BB0EB, 0x4F040D56, 0x4BC510E1,
  312. 0x46863638, 0x42472B8F, 0x5C007B8A, 0x58C1663D, 0x558240E4, 0x51435D53,
  313. 0x251D3B9E, 0x21DC2629, 0x2C9F00F0, 0x285E1D47, 0x36194D42, 0x32D850F5,
  314. 0x3F9B762C, 0x3B5A6B9B, 0x0315D626, 0x07D4CB91, 0x0A97ED48, 0x0E56F0FF,
  315. 0x1011A0FA, 0x14D0BD4D, 0x19939B94, 0x1D528623, 0xF12F560E, 0xF5EE4BB9,
  316. 0xF8AD6D60, 0xFC6C70D7, 0xE22B20D2, 0xE6EA3D65, 0xEBA91BBC, 0xEF68060B,
  317. 0xD727BBB6, 0xD3E6A601, 0xDEA580D8, 0xDA649D6F, 0xC423CD6A, 0xC0E2D0DD,
  318. 0xCDA1F604, 0xC960EBB3, 0xBD3E8D7E, 0xB9FF90C9, 0xB4BCB610, 0xB07DABA7,
  319. 0xAE3AFBA2, 0xAAFBE615, 0xA7B8C0CC, 0xA379DD7B, 0x9B3660C6, 0x9FF77D71,
  320. 0x92B45BA8, 0x9675461F, 0x8832161A, 0x8CF30BAD, 0x81B02D74, 0x857130C3,
  321. 0x5D8A9099, 0x594B8D2E, 0x5408ABF7, 0x50C9B640, 0x4E8EE645, 0x4A4FFBF2,
  322. 0x470CDD2B, 0x43CDC09C, 0x7B827D21, 0x7F436096, 0x7200464F, 0x76C15BF8,
  323. 0x68860BFD, 0x6C47164A, 0x61043093, 0x65C52D24, 0x119B4BE9, 0x155A565E,
  324. 0x18197087, 0x1CD86D30, 0x029F3D35, 0x065E2082, 0x0B1D065B, 0x0FDC1BEC,
  325. 0x3793A651, 0x3352BBE6, 0x3E119D3F, 0x3AD08088, 0x2497D08D, 0x2056CD3A,
  326. 0x2D15EBE3, 0x29D4F654, 0xC5A92679, 0xC1683BCE, 0xCC2B1D17, 0xC8EA00A0,
  327. 0xD6AD50A5, 0xD26C4D12, 0xDF2F6BCB, 0xDBEE767C, 0xE3A1CBC1, 0xE760D676,
  328. 0xEA23F0AF, 0xEEE2ED18, 0xF0A5BD1D, 0xF464A0AA, 0xF9278673, 0xFDE69BC4,
  329. 0x89B8FD09, 0x8D79E0BE, 0x803AC667, 0x84FBDBD0, 0x9ABC8BD5, 0x9E7D9662,
  330. 0x933EB0BB, 0x97FFAD0C, 0xAFB010B1, 0xAB710D06, 0xA6322BDF, 0xA2F33668,
  331. 0xBCB4666D, 0xB8757BDA, 0xB5365D03, 0xB1F740B4
  332. };
  333. static UInt32 crc32_cal(byte[] pData, UInt16 Length)
  334. {
  335. UInt32 nReg;
  336. UInt32 nTemp = 0;
  337. UInt16 i, n;
  338. nReg = 0xFFFFFFFF;
  339. for (n = 0; n < Length; n++)
  340. {
  341. nReg ^= (UInt32)pData[n];
  342. for (i = 0; i < 4; i++)
  343. {
  344. nTemp = crc32_table[((nReg >> 24) & 0xFF)];
  345. nReg <<= 8;
  346. nReg ^= nTemp;
  347. }
  348. }
  349. return nReg;
  350. }
  351. #endregion
  352. #region 平均值计算
  353. private ushort GetAvg(byte[] pData, UInt16 Length)
  354. {
  355. UInt32 Sum = 0;
  356. for(ushort i=0; i<Length; i++)
  357. {
  358. Sum += pData[i];
  359. }
  360. return (ushort)(Sum/Length);
  361. }
  362. #endregion
  363. #region 标准差计算
  364. private ushort GetStDev(byte[] pData, UInt16 Length)
  365. {
  366. double Result = 0;
  367. //计算平均值
  368. UInt32 Avg = GetAvg(pData, Length);
  369. //计算各个数值与平均值的差值平方和
  370. UInt32 Sum = 0;
  371. for (ushort i = 0; i < Length; i++)
  372. {
  373. Sum += (pData[i] - Avg) * (pData[i] - Avg);
  374. }
  375. //计算标准差
  376. Result = Math.Sqrt((double)Sum / Length);
  377. return (ushort)(Result);
  378. }
  379. #endregion
  380. #region 发送指令函数
  381. private bool SendCmd(ushort ID, byte Mode, ushort Cmd, byte[] Data)
  382. {
  383. if (!serialPort1.IsOpen)
  384. {
  385. MessageBox.Show("串口未连接");
  386. return false;
  387. }
  388. ushort DataLength = (ushort)(Cmd & 0xFF);
  389. var SendCmdTmp = new byte[DataLength + 13];
  390. //帧头
  391. SendCmdTmp[0] = 0x55;
  392. SendCmdTmp[1] = 0xAA;
  393. //ID
  394. SendCmdTmp[2] = (byte)(ID >> 8);
  395. SendCmdTmp[3] = (byte)(ID & 0xFF);
  396. //MODE
  397. SendCmdTmp[4] = Mode;
  398. //长度
  399. SendCmdTmp[5] = (byte)((Cmd & 0xFF) + 2);
  400. //命令字
  401. SendCmdTmp[6] = (byte)(Cmd >> 8);
  402. SendCmdTmp[7] = (byte)(Cmd & 0xFF);
  403. //数据段
  404. for (UInt16 i = 0; i < (Cmd & 0xFF); i++)
  405. {
  406. SendCmdTmp[8 + i] = Data[i];
  407. }
  408. //校验
  409. UInt32 CRC_Result = crc32_cal(SendCmdTmp, (ushort)(DataLength + 8));
  410. SendCmdTmp[DataLength + 8] = Convert.ToByte(CRC_Result >> 24);
  411. SendCmdTmp[DataLength + 9] = Convert.ToByte((CRC_Result >> 16) % 256);
  412. SendCmdTmp[DataLength + 10] = Convert.ToByte((CRC_Result >> 8) % 256);
  413. SendCmdTmp[DataLength + 11] = Convert.ToByte(CRC_Result % 256);
  414. //帧尾
  415. SendCmdTmp[DataLength + 12] = 0xF0;
  416. //发送数据
  417. serialPort1.Write(SendCmdTmp, 0, SendCmdTmp.Length);
  418. return true;
  419. }
  420. #endregion
  421. #region 力矩传感器查询和写入相关
  422. //发送力矩传感器校正系数
  423. private void button_WriteK_Click(object sender, EventArgs e)
  424. {
  425. if (radioButton_SingleSensor.Checked)
  426. {
  427. var K = new byte[1];
  428. ushort DataTemp;
  429. DataTemp = (ushort)(decimal.Parse(textBox_Sensor1_K.Text) * 100);
  430. K[0] = Convert.ToByte(DataTemp);
  431. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2A01, K);
  432. }
  433. else if (radioButton_ThreeSensor.Checked)
  434. {
  435. var K = new byte[2];
  436. ushort DataTemp;
  437. if ((checkBox_Sensor1.Checked == false) && (checkBox_Sensor2.Checked == false) && (checkBox_Sensor3.Checked == false))
  438. {
  439. timer1.Enabled = false;
  440. MessageBox.Show("请选择传感器", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  441. timer1.Enabled = true;
  442. return;
  443. }
  444. else
  445. {
  446. if (MessageBox.Show("确认写入", "确认写入?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  447. {
  448. if (checkBox_Sensor1.Checked == true)
  449. {
  450. if (textBox_Sensor1_K.Text != string.Empty)
  451. {
  452. K[0] = 1;
  453. DataTemp = (ushort)(decimal.Parse(textBox_Sensor1_K.Text) * 100);
  454. K[1] = Convert.ToByte(DataTemp);
  455. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3402, K);
  456. Thread.Sleep(20);
  457. }
  458. }
  459. if (checkBox_Sensor2.Checked == true)
  460. {
  461. if (textBox_Sensor2_K.Text != string.Empty)
  462. {
  463. K[0] = 2;
  464. DataTemp = (ushort)(decimal.Parse(textBox_Sensor2_K.Text) * 100);
  465. K[1] = Convert.ToByte(DataTemp);
  466. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3402, K);
  467. Thread.Sleep(20);
  468. }
  469. }
  470. if (checkBox_Sensor3.Checked == true)
  471. {
  472. if (textBox_Sensor3_K.Text != string.Empty)
  473. {
  474. K[0] = 3;
  475. DataTemp = (ushort)(decimal.Parse(textBox_Sensor3_K.Text) * 100);
  476. K[1] = Convert.ToByte(DataTemp);
  477. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3402, K);
  478. Thread.Sleep(20);
  479. }
  480. }
  481. }
  482. }
  483. }
  484. }
  485. //查询传感器零点
  486. private void button_ReadZero_Click(object sender, EventArgs e)
  487. {
  488. if (radioButton_SingleSensor.Checked)
  489. {
  490. textBox_Sensor1_Zero.Text = "---";
  491. textBox_Sensor1_K.Text = "---";
  492. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2000, null);
  493. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2900, null);
  494. }
  495. else if (radioButton_ThreeSensor.Checked)
  496. {
  497. var K = new byte[1];
  498. textBox_Sensor1_Zero.Text = "---";
  499. textBox_Sensor2_Zero.Text = "---";
  500. textBox_Sensor3_Zero.Text = "---";
  501. textBox_Sensor1_K.Text = "---";
  502. textBox_Sensor2_K.Text = "---";
  503. textBox_Sensor3_K.Text = "---";
  504. //轮询零点
  505. for (ushort i = 1; i <= 3; i++)
  506. {
  507. K[0] = (byte)i;
  508. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3201, K);
  509. }
  510. //轮询校正系数
  511. for (ushort i = 1; i <= 3; i++)
  512. {
  513. K[0] = (byte)i;
  514. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3301, K);
  515. }
  516. }
  517. }
  518. //计算校正系数
  519. private void label119_DoubleClick(object sender, EventArgs e)
  520. {
  521. float K;
  522. string DataTemp;
  523. if (radioButton_SingleSensor.Checked)
  524. {
  525. if (checkBox_Sensor1.Checked == false)
  526. {
  527. timer1.Enabled = false;
  528. MessageBox.Show("请选择传感器", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  529. timer1.Enabled = true;
  530. return;
  531. }
  532. else
  533. {
  534. if ((textBox_Sensor1_Avg.Text != "---") && (textBox_Sensor1_Avg.Text != string.Empty))
  535. {
  536. DataTemp = textBox_Sensor1_Avg.Text;
  537. DataTemp = System.Text.RegularExpressions.Regex.Replace(DataTemp, @"[^0-9]+", "");
  538. K = float.Parse(DataTemp) / 75f;
  539. textBox_Sensor1_K.Text = K.ToString("#0.00");
  540. }
  541. else
  542. {
  543. textBox_Sensor1_K.Text = "";
  544. }
  545. }
  546. }
  547. else if (radioButton_ThreeSensor.Checked)
  548. {
  549. if ((checkBox_Sensor1.Checked == false) && (checkBox_Sensor2.Checked == false) && (checkBox_Sensor3.Checked == false))
  550. {
  551. timer1.Enabled = false;
  552. MessageBox.Show("请选择传感器", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  553. timer1.Enabled = true;
  554. return;
  555. }
  556. else
  557. {
  558. if (checkBox_Sensor1.Checked == true)
  559. {
  560. if ((textBox_Sensor1_Avg.Text != "---") && (textBox_Sensor1_Avg.Text != string.Empty))
  561. {
  562. DataTemp = textBox_Sensor1_Avg.Text;
  563. DataTemp = System.Text.RegularExpressions.Regex.Replace(DataTemp, @"[^0-9]+", "");
  564. K = float.Parse(DataTemp) / 75f;
  565. textBox_Sensor1_K.Text = K.ToString("#0.00");
  566. }
  567. else
  568. {
  569. textBox_Sensor1_K.Text = "";
  570. }
  571. }
  572. if (checkBox_Sensor2.Checked == true)
  573. {
  574. if ((textBox_Sensor2_Avg.Text != "---") && (textBox_Sensor2_Avg.Text != string.Empty))
  575. {
  576. DataTemp = textBox_Sensor2_Avg.Text;
  577. DataTemp = System.Text.RegularExpressions.Regex.Replace(DataTemp, @"[^0-9]+", "");
  578. K = float.Parse(DataTemp) / 75f;
  579. textBox_Sensor2_K.Text = K.ToString("#0.00");
  580. }
  581. else
  582. {
  583. textBox_Sensor2_K.Text = "";
  584. }
  585. }
  586. if (checkBox_Sensor3.Checked == true)
  587. {
  588. if ((textBox_Sensor3_Avg.Text != "---") && (textBox_Sensor3_Avg.Text != string.Empty))
  589. {
  590. DataTemp = textBox_Sensor3_Avg.Text;
  591. DataTemp = System.Text.RegularExpressions.Regex.Replace(DataTemp, @"[^0-9]+", "");
  592. K = float.Parse(DataTemp) / 75f;
  593. textBox_Sensor3_K.Text = K.ToString("#0.00");
  594. }
  595. else
  596. {
  597. textBox_Sensor3_K.Text = "";
  598. }
  599. }
  600. }
  601. }
  602. }
  603. //发送力矩传感器启动值
  604. private void button_Write_Torque_Start_Click(object sender, EventArgs e)
  605. {
  606. ushort StarData = Convert.ToUInt16(textBox_Torque_Start.Text);
  607. var StarData_Array = new byte[2];
  608. StarData_Array[0] = (byte)(StarData & 0xFF);
  609. StarData_Array[1] = (byte)(StarData >> 8);
  610. if (MessageBox.Show("确认写入", "确认写入?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  611. {
  612. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2B02, StarData_Array);
  613. }
  614. }
  615. //查询力矩传感器启动值
  616. private void button_ReadStartData_Click(object sender, EventArgs e)
  617. {
  618. textBox_Torque_Start.Text = "---";
  619. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3500, null);
  620. }
  621. //根据采集值计算启动值
  622. private void label9_DoubleClick(object sender, EventArgs e)
  623. {
  624. ushort StartData;
  625. string DataTemp;
  626. if ((textBox_TorqueData.Text == "---") || (textBox_TorqueData.Text == string.Empty))
  627. {
  628. return;
  629. }
  630. DataTemp = textBox_TorqueData.Text;
  631. DataTemp = System.Text.RegularExpressions.Regex.Replace(DataTemp, @"[^0-9]+", "");
  632. StartData = (ushort)(Convert.ToUInt16(DataTemp) * 28);
  633. textBox_Torque_Start.Text = StartData.ToString();
  634. }
  635. #endregion
  636. #region 工作模式设置
  637. //进入运行模式
  638. private void radioButton_WorkMode_Run_Click(object sender, EventArgs e)
  639. {
  640. var RunMode = new byte[1];
  641. RunMode[0] = 0x00;
  642. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, RunMode);
  643. checkBox_ReadTorqueData.Checked = false;
  644. }
  645. //进入配置模式
  646. private void radioButton_WorkMode_Confg_Click(object sender, EventArgs e)
  647. {
  648. var RunMode = new byte[1];
  649. RunMode[0] = 0x01;
  650. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, RunMode);
  651. checkBox_AutoSend.Checked = false;
  652. }
  653. #endregion
  654. #region 助力档位设置
  655. //设置OFF档位
  656. private void radioButton_Gear_OFF_Click(object sender, EventArgs e)
  657. {
  658. var CtrlCode = new byte[2];
  659. CtrlCode[0] = 0x00;
  660. if (radioButton_Light_OFF.Checked == true)
  661. {
  662. CtrlCode[1] = 0xF0;
  663. }
  664. else if (radioButton_Light_ON.Checked == true)
  665. {
  666. CtrlCode[1] = 0xF1;
  667. }
  668. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  669. }
  670. //设置ECO档位
  671. private void radioButton_Gear_ECO_Click(object sender, EventArgs e)
  672. {
  673. var CtrlCode = new byte[2];
  674. CtrlCode[0] = 0x01;
  675. if (radioButton_Light_OFF.Checked == true)
  676. {
  677. CtrlCode[1] = 0xF0;
  678. }
  679. else if (radioButton_Light_ON.Checked == true)
  680. {
  681. CtrlCode[1] = 0xF1;
  682. }
  683. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  684. }
  685. //设置NORM档位
  686. private void radioButton_Gear_NORM_Click(object sender, EventArgs e)
  687. {
  688. var CtrlCode = new byte[2];
  689. CtrlCode[0] = 0x02;
  690. if (radioButton_Light_OFF.Checked == true)
  691. {
  692. CtrlCode[1] = 0xF0;
  693. }
  694. else if (radioButton_Light_ON.Checked == true)
  695. {
  696. CtrlCode[1] = 0xF1;
  697. }
  698. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  699. }
  700. //设置SPORT档位
  701. private void radioButton_Gear_SPORT_Click(object sender, EventArgs e)
  702. {
  703. var CtrlCode = new byte[2];
  704. CtrlCode[0] = 0x03;
  705. if (radioButton_Light_OFF.Checked == true)
  706. {
  707. CtrlCode[1] = 0xF0;
  708. }
  709. else if (radioButton_Light_ON.Checked == true)
  710. {
  711. CtrlCode[1] = 0xF1;
  712. }
  713. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  714. }
  715. //设置TURBO档位
  716. private void radioButton_Gear_TURBO_Click(object sender, EventArgs e)
  717. {
  718. var CtrlCode = new byte[2];
  719. CtrlCode[0] = 0x04;
  720. if (radioButton_Light_OFF.Checked == true)
  721. {
  722. CtrlCode[1] = 0xF0;
  723. }
  724. else if (radioButton_Light_ON.Checked == true)
  725. {
  726. CtrlCode[1] = 0xF1;
  727. }
  728. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  729. }
  730. //设置SMART档位
  731. private void radioButton_Gear_SMART_Click(object sender, EventArgs e)
  732. {
  733. var CtrlCode = new byte[2];
  734. CtrlCode[0] = 0x33;
  735. if (radioButton_Light_OFF.Checked == true)
  736. {
  737. CtrlCode[1] = 0xF0;
  738. }
  739. else if (radioButton_Light_ON.Checked == true)
  740. {
  741. CtrlCode[1] = 0xF1;
  742. }
  743. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  744. }
  745. //设置WALK档位
  746. private void radioButton_Gear_WALK_Click(object sender, EventArgs e)
  747. {
  748. var CtrlCode = new byte[2];
  749. CtrlCode[0] = 0x22;
  750. if (radioButton_Light_OFF.Checked == true)
  751. {
  752. CtrlCode[1] = 0xF0;
  753. }
  754. else if (radioButton_Light_ON.Checked == true)
  755. {
  756. CtrlCode[1] = 0xF1;
  757. }
  758. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  759. }
  760. #endregion
  761. #region 大灯开关
  762. //大灯关
  763. private void radioButton_Light_OFF_Click(object sender, EventArgs e)
  764. {
  765. var CtrlCode = new byte[2];
  766. if (radioButton_Gear_OFF.Checked == true)
  767. {
  768. CtrlCode[0] = 0x00;
  769. }
  770. else if (radioButton_Gear_ECO.Checked == true)
  771. {
  772. CtrlCode[0] = 0x01;
  773. }
  774. else if (radioButton_Gear_NORM.Checked == true)
  775. {
  776. CtrlCode[0] = 0x02;
  777. }
  778. else if (radioButton_Gear_SPORT.Checked == true)
  779. {
  780. CtrlCode[0] = 0x03;
  781. }
  782. else if (radioButton_Gear_TURBO.Checked == true)
  783. {
  784. CtrlCode[0] = 0x04;
  785. }
  786. else if (radioButton_Gear_SMART.Checked == true)
  787. {
  788. CtrlCode[0] = 0x33;
  789. }
  790. else if (radioButton_Gear_WALK.Checked == true)
  791. {
  792. CtrlCode[0] = 0x22;
  793. }
  794. CtrlCode[1] = 0xF0;
  795. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  796. }
  797. //大灯开
  798. private void radioButton_Light_ON_Click(object sender, EventArgs e)
  799. {
  800. var CtrlCode = new byte[2];
  801. if (radioButton_Gear_OFF.Checked == true)
  802. {
  803. CtrlCode[0] = 0x00;
  804. }
  805. else if (radioButton_Gear_ECO.Checked == true)
  806. {
  807. CtrlCode[0] = 0x01;
  808. }
  809. else if (radioButton_Gear_NORM.Checked == true)
  810. {
  811. CtrlCode[0] = 0x02;
  812. }
  813. else if (radioButton_Gear_SPORT.Checked == true)
  814. {
  815. CtrlCode[0] = 0x03;
  816. }
  817. else if (radioButton_Gear_TURBO.Checked == true)
  818. {
  819. CtrlCode[0] = 0x04;
  820. }
  821. else if (radioButton_Gear_SMART.Checked == true)
  822. {
  823. CtrlCode[0] = 0x33;
  824. }
  825. else if (radioButton_Gear_WALK.Checked == true)
  826. {
  827. CtrlCode[0] = 0x22;
  828. }
  829. CtrlCode[1] = 0xF1;
  830. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  831. }
  832. #endregion
  833. //数据清除指令
  834. private void button_ClearData_Click(object sender, EventArgs e)
  835. {
  836. var Code = new byte[5];
  837. Code[0] = (byte)'C';
  838. Code[1] = (byte)'L';
  839. Code[2] = (byte)'E';
  840. Code[3] = (byte)'A';
  841. Code[4] = (byte)'R';
  842. timer1.Enabled = false;
  843. if (MessageBox.Show("系统清除后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  844. {
  845. if (MessageBox.Show("请再次确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  846. {
  847. if (MessageBox.Show("请最后确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  848. {
  849. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2605, Code);
  850. }
  851. }
  852. }
  853. timer1.Enabled = true;
  854. }
  855. //查询版本信息
  856. private void button_Read_VerInfo_Click(object sender, EventArgs e)
  857. {
  858. textBox_Mode.Text = "---";
  859. textBox_SN.Text = "---";
  860. textBox_HW.Text = "---";
  861. textBox_FW.Text = "---";
  862. textBox_Firmware_Special.Text = "---";
  863. textBox_BuildTime.Text = "---";
  864. Class_Motor_Ver.Mode = "---";
  865. Class_Motor_Ver.SN = "---";
  866. Class_Motor_Ver.HW = "---";
  867. Class_Motor_Ver.FW = "---";
  868. Class_Motor_Ver.Special = "---";
  869. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
  870. textBox_TE_FW.Text = "---";
  871. SendCmd((ushort)0x651, (byte)0x11, (ushort)0x1200, null);
  872. }
  873. //串口数据接收
  874. private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  875. {
  876. try
  877. {
  878. Listening = true;
  879. int n = serialPort1.BytesToRead;
  880. byte[] buf = new byte[n];//将一次串口事件中接收到的数据暂存于buf中(注:对于超过8个字节的数据,C#的
  881. //串口接收事件将按每次处理8个字节的方式多次处理
  882. serialPort1.Read(buf, 0, n);//将接收到的数据读入buf
  883. this.Invoke((EventHandler)delegate
  884. {
  885. });
  886. buffer.AddRange(buf);//将读取的数据放入buffer中
  887. }
  888. catch (Exception ex)
  889. {
  890. MessageBox.Show(ex.Message);
  891. return;
  892. }
  893. finally
  894. {
  895. Listening = false;
  896. }
  897. }
  898. //time1定时任务
  899. private void timer1_Tick(object sender, EventArgs e)
  900. {
  901. //MC_RunInfo超时清除
  902. if (MC_RunInfo_Refresh == false)
  903. {
  904. MC_RunInfo_Refresh_Cnt++;
  905. if (MC_RunInfo_Refresh_Cnt > 50)
  906. {
  907. //运行信息清除
  908. MC_RunInfo_Clear();
  909. //传感器数值清除
  910. textBox_TorqueData.Text = "---";
  911. textBox_Torque_Stdev.Text = "---";
  912. textBox_Sensor1_Avg.Text = "---";
  913. textBox_Sensor1_Std.Text = "---";
  914. textBox_Sensor2_Avg.Text = "---";
  915. textBox_Sensor2_Std.Text = "---";
  916. textBox_Sensor3_Avg.Text = "---";
  917. textBox_Sensor3_Std.Text = "---";
  918. MC_RunInfo_Refresh_Cnt = 0;
  919. TorqueArray_Index = 0;
  920. }
  921. }
  922. else
  923. {
  924. MC_RunInfo_Refresh_Cnt = 0;
  925. }
  926. MC_RunInfo_Refresh = false;
  927. //MC故障信息超时清除
  928. if (MC_ErrorCode_Refresh == false)
  929. {
  930. MC_ErrorCode_Refresh_Cnt++;
  931. if (MC_ErrorCode_Refresh_Cnt > 50)
  932. {
  933. MC_ErrorCode_Clear();
  934. MC_ErrorCode_Refresh_Cnt = 0;
  935. }
  936. }
  937. else
  938. {
  939. MC_ErrorCode_Refresh_Cnt = 0;
  940. }
  941. MC_ErrorCode_Refresh = false;
  942. //MC骑行信息超时清除
  943. if (MC_RideInfo_Refresh == false)
  944. {
  945. MC_RideInfo_Refresh_Cnt++;
  946. if (MC_RideInfo_Refresh_Cnt > 50)
  947. {
  948. MC_RideInfo_Clear();
  949. MC_RideInfo_Refresh_Cnt = 0;
  950. }
  951. }
  952. else
  953. {
  954. MC_RideInfo_Refresh_Cnt = 0;
  955. }
  956. MC_RideInfo_Refresh = false;
  957. //MC姿态信息超时清除
  958. if (MC_AttitudeAngle_Refresh == false)
  959. {
  960. MC_AttitudeAngle_Refresh_Cnt++;
  961. if (MC_AttitudeAngle_Refresh_Cnt > 50)
  962. {
  963. MC_AttitudeAngle_Clear();
  964. MC_AttitudeAngle_Refresh_Cnt = 0;
  965. }
  966. }
  967. else
  968. {
  969. MC_AttitudeAngle_Refresh_Cnt = 0;
  970. }
  971. MC_AttitudeAngle_Refresh = false;
  972. //定时发送助力档位
  973. if (checkBox_AutoSend.Checked == true)
  974. {
  975. var CtrlCode = new byte[2];
  976. AutoSendTimeCnt++;
  977. if (AutoSendTimeCnt >= 10)
  978. {
  979. AutoSendTimeCnt = 0;
  980. //助力档位
  981. if (radioButton_Gear_OFF.Checked == true)
  982. {
  983. CtrlCode[0] = 0x00;
  984. }
  985. else if (radioButton_Gear_ECO.Checked == true)
  986. {
  987. CtrlCode[0] = 0x01;
  988. }
  989. else if (radioButton_Gear_NORM.Checked == true)
  990. {
  991. CtrlCode[0] = 0x02;
  992. }
  993. else if (radioButton_Gear_SPORT.Checked == true)
  994. {
  995. CtrlCode[0] = 0x03;
  996. }
  997. else if (radioButton_Gear_TURBO.Checked == true)
  998. {
  999. CtrlCode[0] = 0x04;
  1000. }
  1001. else if (radioButton_Gear_SMART.Checked == true)
  1002. {
  1003. CtrlCode[0] = 0x33;
  1004. }
  1005. else if (radioButton_Gear_WALK.Checked == true)
  1006. {
  1007. CtrlCode[0] = 0x22;
  1008. }
  1009. //大灯状态
  1010. if (radioButton_Light_OFF.Checked == true)
  1011. {
  1012. CtrlCode[1] = 0xF0;
  1013. }
  1014. else if (radioButton_Light_ON.Checked == true)
  1015. {
  1016. CtrlCode[1] = 0xF1;
  1017. }
  1018. if (serialPort1.IsOpen)
  1019. {
  1020. if (!SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode))
  1021. checkBox_AutoSend.Checked = false;
  1022. }
  1023. }
  1024. }
  1025. else
  1026. {
  1027. AutoSendTimeCnt = 0;
  1028. }
  1029. //定时查询姿态信息
  1030. if (checkBox_ReadAttitudeAngleAutoSend.Checked == true)
  1031. {
  1032. AutoSendReadAttitudeAngleTimeCnt++;
  1033. if (AutoSendReadAttitudeAngleTimeCnt >= 20)
  1034. {
  1035. AutoSendReadAttitudeAngleTimeCnt = 0;
  1036. if (serialPort1.IsOpen)
  1037. {
  1038. if (!SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2F00, null))
  1039. checkBox_ReadAttitudeAngleAutoSend.Checked = false;
  1040. }
  1041. }
  1042. }
  1043. else
  1044. {
  1045. AutoSendReadAttitudeAngleTimeCnt = 0;
  1046. }
  1047. //定时查询骑行信息
  1048. if ( checkBox_ReadRideInfo.Checked == true)
  1049. {
  1050. AutoSendReadRideInfoTimeCnt++;
  1051. if (AutoSendReadRideInfoTimeCnt >= 20)
  1052. {
  1053. AutoSendReadRideInfoTimeCnt = 0;
  1054. if (serialPort1.IsOpen)
  1055. {
  1056. if (!SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2E00, null))
  1057. checkBox_ReadRideInfo.Checked = false;
  1058. }
  1059. }
  1060. }
  1061. else
  1062. {
  1063. AutoSendReadRideInfoTimeCnt = 0;
  1064. }
  1065. //CDL在线检测超时判断
  1066. if (CDL_Online_Flag == false)
  1067. {
  1068. CDL_OnlineCheck_Cnt++;
  1069. if (CDL_OnlineCheck_Cnt > 10)
  1070. {
  1071. timer1.Enabled = false;
  1072. MessageBox.Show("连接失败!", "提示");
  1073. timer1.Enabled = true;
  1074. //关闭串口
  1075. serialPort1.Close();
  1076. comboBox_ComIndex.Enabled = true;
  1077. comboBox_Baudrate.Enabled = true;
  1078. button_ComOpen.Text = "连接(&C)";
  1079. g_blnIsOpen = false;
  1080. timer1.Enabled = false;
  1081. button_RefreshPort.Enabled = true;
  1082. }
  1083. }
  1084. //...
  1085. //串口解析
  1086. byte DataNum = 0; //记录每条命令数据段的长度
  1087. while (buffer.Count >= 13)//当buffer中的数据的个数大于等于4个时,即每一帧的数据长度所在位(第4位)出现时
  1088. {
  1089. if (buffer[0] == 0x55 && buffer[1] == 0xAA)//判断帧头
  1090. {
  1091. ushort PackageID;
  1092. PackageID = (ushort)(buffer[2] * 256 + buffer[3]);
  1093. if ((PackageID == 0x0710) || (PackageID == 0x0715) || (PackageID == 0x0610) || (PackageID == 0x0615) || (PackageID == 0x07FF))
  1094. {
  1095. int CmdLen = buffer[5];
  1096. if (buffer.Count < CmdLen + 11) break;//如果接收到的数据没有达到一帧数据的指定长度, 则执行下次循环
  1097. buffer.RemoveRange(0, 6);//数据达到要求长度后删去帧头,ID,模式,和命令长度
  1098. while (CmdLen > 0) //读取命令段
  1099. {
  1100. DataNum = (byte)(buffer[1]);//命令字的第二个字节的表示数据位的长度
  1101. if (DataNum <= (CmdLen - 2))
  1102. {
  1103. for (int i = 0; i < DataNum + 2; i++)
  1104. {
  1105. binary_data_1[i] = buffer[i];
  1106. }
  1107. DataCmdProcess(PackageID);//对于不同的命令段,做不同的处理
  1108. buffer.RemoveRange(0, DataNum + 2);//移除处理过的命令段与数据段
  1109. CmdLen -= (DataNum + 2);
  1110. if (CmdLen < 2)
  1111. {
  1112. buffer.RemoveRange(0, CmdLen + 2);
  1113. return;
  1114. }
  1115. }
  1116. else
  1117. {
  1118. buffer.RemoveRange(0, CmdLen + 11);
  1119. }
  1120. }
  1121. }
  1122. else
  1123. {
  1124. buffer.RemoveAt(0);//ID不对
  1125. }
  1126. }
  1127. else
  1128. {
  1129. buffer.RemoveAt(0);//帧头不对,删除帧头高字节
  1130. }
  1131. }
  1132. }
  1133. //故障转换和显示
  1134. List<string> ErrorInfo = new List<string>{ "过流保护", "低压保护", "过压保护", "堵转保护","过热保护","速度传感器故障","力矩传感器故障","霍尔故障",
  1135. "马达缺相","NTC故障","","","","MCU故障","踏频传感器故障","指拨故障","MOS短路","电压异常","电路故障","TE故障","TE电路故障","","","","","","","","","",
  1136. "",""};
  1137. private string ErrorInfoDisplay(UInt32 Code)
  1138. {
  1139. string Result = "";
  1140. if (Code == 0)
  1141. {
  1142. Result = "无故障";
  1143. }
  1144. else
  1145. {
  1146. for (int i = 0; i < 32; i++)
  1147. {
  1148. if ((Code & 0x01) == 0x01)
  1149. {
  1150. Result += ErrorInfo[i] + " ";
  1151. }
  1152. Code >>= 1;
  1153. }
  1154. }
  1155. return Result;
  1156. }
  1157. #region 命令段处理函数
  1158. private void DataCmdProcess(ushort ID)
  1159. {
  1160. ushort CmdTemp = (ushort)(binary_data_1[0] * 256 + binary_data_1[1]);
  1161. ushort DataTemp;
  1162. short DataTemp_int16;
  1163. textBox_RevCnt.Text = Convert.ToString(Convert.ToInt32(textBox_RevCnt.Text) + 1);
  1164. #region 解析发送给CDL的指令
  1165. if (ID == 0x7FF)
  1166. {
  1167. switch (CmdTemp)
  1168. {
  1169. case 0x1100://TE APP版本信息
  1170. {
  1171. unchecked
  1172. {
  1173. this.Invoke((EventHandler)(delegate
  1174. {
  1175. //CDL连接成功
  1176. CDL_Online_Flag = true;
  1177. timer1.Enabled = false;
  1178. MessageBox.Show("连接成功!", "提示");
  1179. timer1.Enabled = true;
  1180. //显示其它界面
  1181. foreach (Control c in this.Controls)
  1182. {
  1183. if (c is GroupBox)
  1184. {
  1185. if (c != groupBox1)
  1186. {
  1187. c.Visible = true;
  1188. }
  1189. }
  1190. }
  1191. button_ReadAll.Visible = true;
  1192. label1_Welcom.Visible = false;
  1193. }));
  1194. }
  1195. break;
  1196. }
  1197. default: break;
  1198. }
  1199. }
  1200. #endregion
  1201. #region 解析主控的命令
  1202. else if ((ID == 0x715) || (ID == 0x710))
  1203. {
  1204. switch (CmdTemp)
  1205. {
  1206. case 0xA318://力矩传感器零点值
  1207. {
  1208. unchecked
  1209. {
  1210. this.Invoke((EventHandler)(delegate
  1211. {
  1212. //三力矩传感器不用
  1213. }));
  1214. }
  1215. break;
  1216. }
  1217. case 0xAE1A://力矩传感器零点值,针对3力矩传感器使用
  1218. {
  1219. unchecked
  1220. {
  1221. this.Invoke((EventHandler)(delegate
  1222. {
  1223. if (binary_data_1[2] == 1)
  1224. {
  1225. DataTemp = (ushort)(binary_data_1[26] * 256 + binary_data_1[25]);
  1226. textBox_Sensor1_Zero.Text = Convert.ToString(DataTemp);
  1227. }
  1228. else if (binary_data_1[2] == 2)
  1229. {
  1230. DataTemp = (ushort)(binary_data_1[26] * 256 + binary_data_1[25]);
  1231. textBox_Sensor2_Zero.Text = Convert.ToString(DataTemp);
  1232. }
  1233. else if (binary_data_1[2] == 3)
  1234. {
  1235. DataTemp = (ushort)(binary_data_1[26] * 256 + binary_data_1[25]);
  1236. textBox_Sensor3_Zero.Text = Convert.ToString(DataTemp);
  1237. }
  1238. }));
  1239. }
  1240. break;
  1241. }
  1242. case 0xAA04://力矩传感器值校正参数
  1243. {
  1244. unchecked
  1245. {
  1246. this.Invoke((EventHandler)(delegate
  1247. {
  1248. //三力矩传感器不用
  1249. }));
  1250. }
  1251. break;
  1252. }
  1253. case 0xAF02://力矩传感器值校正参数,针对3力矩传感器使用
  1254. {
  1255. unchecked
  1256. {
  1257. this.Invoke((EventHandler)(delegate
  1258. {
  1259. if (binary_data_1[2] == 1)
  1260. {
  1261. DataTemp = (ushort)(binary_data_1[3]);
  1262. float K = (float)DataTemp / 100f;
  1263. textBox_Sensor1_K.Text = K.ToString("#0.00");
  1264. }
  1265. else if (binary_data_1[2] == 2)
  1266. {
  1267. DataTemp = (ushort)(binary_data_1[3]);
  1268. float K = (float)DataTemp / 100f;
  1269. textBox_Sensor2_K.Text = K.ToString("#0.00");
  1270. }
  1271. else if (binary_data_1[2] == 3)
  1272. {
  1273. DataTemp = (ushort)(binary_data_1[3]);
  1274. float K = (float)DataTemp / 100f;
  1275. textBox_Sensor3_K.Text = K.ToString("#0.00");
  1276. }
  1277. }));
  1278. }
  1279. break;
  1280. }
  1281. case 0xB002://力矩传感器值启动值
  1282. {
  1283. unchecked
  1284. {
  1285. this.Invoke((EventHandler)(delegate
  1286. {
  1287. DataTemp = (ushort)(binary_data_1[3] * 256 + binary_data_1[2]);
  1288. textBox_Torque_Start.Text = Convert.ToString(DataTemp);
  1289. }));
  1290. }
  1291. break;
  1292. }
  1293. case 0x1240://电机版本信息
  1294. {
  1295. unchecked
  1296. {
  1297. this.Invoke((EventHandler)(delegate
  1298. {
  1299. //电机型号
  1300. textBox_Mode.Text = "";
  1301. for (ushort i = 0; i < 16; i++)
  1302. {
  1303. if (binary_data_1[2 + i] == 0x2E)
  1304. {
  1305. break;
  1306. }
  1307. textBox_Mode.Text += ((char)binary_data_1[2 + i]).ToString();
  1308. }
  1309. Class_Motor_Ver.Mode = textBox_Mode.Text;
  1310. //电机SN
  1311. textBox_SN.Text = "";
  1312. for (ushort i = 0; i < 16; i++)
  1313. {
  1314. if (binary_data_1[18 + i] == 0x2E)
  1315. {
  1316. break;
  1317. }
  1318. textBox_SN.Text += ((char)binary_data_1[18 + i]).ToString();
  1319. }
  1320. Class_Motor_Ver.SN = textBox_SN.Text;
  1321. //电机HW
  1322. textBox_HW.Text = "";
  1323. for (ushort i = 0; i < 10; i++)//前10位是PCB版号,以'.'结束
  1324. {
  1325. if (binary_data_1[34 + i] == 0x2E)
  1326. {
  1327. break;
  1328. }
  1329. if (binary_data_1[34 + i] == 'r')
  1330. {
  1331. binary_data_1[34 + i] = (byte)'.';
  1332. }
  1333. textBox_HW.Text += ((char)binary_data_1[34 + i]).ToString();
  1334. }
  1335. switch (binary_data_1[34 + 10])//第10位代表MCU品牌
  1336. {
  1337. case (byte)'1':
  1338. textBox_HW.Text += " " + "ST" + " "; break;
  1339. case (byte)'2':
  1340. textBox_HW.Text += " " + "APM" + " "; break;
  1341. case (byte)'3':
  1342. textBox_HW.Text += " " + "HK" + " "; break;
  1343. case (byte)'4':
  1344. textBox_HW.Text += " " + "GD" + " "; break;
  1345. default:
  1346. textBox_HW.Text += " " + "NULL" + " "; break;
  1347. }
  1348. for (ushort i = 0; i < 4; i++)//后4位是MCU的ID校验码,用于识别板子的ID
  1349. {
  1350. textBox_HW.Text += Convert.ToString(binary_data_1[46 + i], 16).PadLeft(2, '0').ToUpper();//MCU ID 的CRC32值
  1351. }
  1352. Class_Motor_Ver.HW = textBox_HW.Text;
  1353. //电机FW
  1354. textBox_FW.Text = "";
  1355. for (ushort i = 0; i < 16; i++)
  1356. {
  1357. if (binary_data_1[50 + i] == 0x2E)
  1358. {
  1359. break;
  1360. }
  1361. if (binary_data_1[50 + i] == 'r')
  1362. {
  1363. binary_data_1[50 + i] = (byte)'.';
  1364. }
  1365. textBox_FW.Text += ((char)binary_data_1[50 + i]).ToString();
  1366. }
  1367. Class_Motor_Ver.FW = textBox_FW.Text;
  1368. }));
  1369. }
  1370. break;
  1371. }
  1372. case 0x1720://电机程序特性信息
  1373. {
  1374. unchecked
  1375. {
  1376. this.Invoke((EventHandler)(delegate
  1377. {
  1378. //程序特性
  1379. textBox_Firmware_Special.Text = "";
  1380. for (ushort i = 0; i < 32; i++)
  1381. {
  1382. if (binary_data_1[2 + i] == 0x2E)
  1383. {
  1384. break;
  1385. }
  1386. textBox_Firmware_Special.Text += ((char)binary_data_1[2 + i]).ToString();
  1387. }
  1388. //编译时间
  1389. textBox_BuildTime.Text = "";
  1390. do
  1391. {
  1392. ushort[] DateTime = new ushort[6];
  1393. DateTime[0] = (ushort)(binary_data_1[2 + 26] + 2000);
  1394. DateTime[1] = (ushort)(binary_data_1[2 + 27]);
  1395. DateTime[2] = (ushort)(binary_data_1[2 + 28]);
  1396. DateTime[3] = (ushort)(binary_data_1[2 + 29]);
  1397. DateTime[4] = (ushort)(binary_data_1[2 + 30]);
  1398. DateTime[5] = (ushort)(binary_data_1[2 + 31]);
  1399. //年
  1400. textBox_BuildTime.Text += Convert.ToString(DateTime[0]).PadLeft(4, '0');
  1401. //月
  1402. textBox_BuildTime.Text += Convert.ToString(DateTime[1]).PadLeft(2, '0');
  1403. //日
  1404. textBox_BuildTime.Text += Convert.ToString(DateTime[2]).PadLeft(2, '0');
  1405. textBox_BuildTime.Text += " ";
  1406. //时
  1407. textBox_BuildTime.Text += Convert.ToString(DateTime[3]).PadLeft(2, '0');
  1408. textBox_BuildTime.Text += ":";
  1409. //分
  1410. textBox_BuildTime.Text += Convert.ToString(DateTime[4]).PadLeft(2, '0');
  1411. textBox_BuildTime.Text += ":";
  1412. //秒
  1413. textBox_BuildTime.Text += Convert.ToString(DateTime[5]).PadLeft(2, '0');
  1414. } while (false);
  1415. }));
  1416. }
  1417. break;
  1418. }
  1419. case 0x1020://电机运行信息
  1420. {
  1421. unchecked
  1422. {
  1423. this.Invoke((EventHandler)(delegate
  1424. {
  1425. MC_RunInfo_Refresh = true;
  1426. //车速
  1427. DataTemp = (ushort)(binary_data_1[3] * 256 + binary_data_1[2]);
  1428. textBox_RunInfo_BikeSpeed.Text = ((float)DataTemp / 10f).ToString("0.0") + " km/h";
  1429. //输出转速
  1430. DataTemp = (ushort)(binary_data_1[5] * 256 + binary_data_1[4]);
  1431. textBox_RunInfo_OutSpeed.Text = Convert.ToString(DataTemp) + " rpm";
  1432. Class_MotorSpeed.String = textBox_RunInfo_OutSpeed.Text;
  1433. Class_MotorSpeed.Data = DataTemp;
  1434. //电功率
  1435. DataTemp = (ushort)(binary_data_1[7] * 256 + binary_data_1[6]);
  1436. textBox_RunInfo_Power.Text = Convert.ToString(DataTemp) + " W";
  1437. //母线电压
  1438. DataTemp = (ushort)(binary_data_1[9] * 256 + binary_data_1[8]);
  1439. textBox_RunInfo_Voltage.Text = Convert.ToString(DataTemp) + " mV";
  1440. //母线电流
  1441. DataTemp = (ushort)(binary_data_1[11] * 256 + binary_data_1[10]);
  1442. textBox_RunInfo_Current.Text = Convert.ToString(DataTemp) + " mA";
  1443. Class_CurrentData.String = textBox_RunInfo_Current.Text;
  1444. Class_CurrentData.Data = DataTemp;
  1445. //踏频
  1446. DataTemp = (ushort)(binary_data_1[12]);
  1447. textBox_RunInfo_Cadence.Text = Convert.ToString(DataTemp) + " rpm";
  1448. //踩踏力矩
  1449. DataTemp = (ushort)(binary_data_1[13]);
  1450. textBox_RunInfo_Torque.Text = Convert.ToString(DataTemp) + " N.m";
  1451. Class_TorqueData.String = textBox_RunInfo_Torque.Text;
  1452. Class_TorqueData.Data = DataTemp;
  1453. //踩踏方向
  1454. DataTemp = (ushort)(binary_data_1[14]);
  1455. if (DataTemp == 0)
  1456. {
  1457. textBox_RunInfo_Dir.Text = "正向";
  1458. }
  1459. else if (DataTemp == 1)
  1460. {
  1461. textBox_RunInfo_Dir.Text = "反向";
  1462. }
  1463. else if (DataTemp == 2)
  1464. {
  1465. textBox_RunInfo_Dir.Text = "停止";
  1466. }
  1467. //助力档位
  1468. DataTemp = (ushort)(binary_data_1[15]);
  1469. if (DataTemp == 0x00)
  1470. {
  1471. textBox_RunInfo_Gear.Text = "OFF";
  1472. radioButton_Gear_OFF.Checked = true;
  1473. }
  1474. else if (DataTemp == 0x01)
  1475. {
  1476. textBox_RunInfo_Gear.Text = "ECO";
  1477. radioButton_Gear_ECO.Checked = true;
  1478. }
  1479. else if (DataTemp == 0x02)
  1480. {
  1481. textBox_RunInfo_Gear.Text = "NORM";
  1482. radioButton_Gear_NORM.Checked = true;
  1483. }
  1484. else if (DataTemp == 0x03)
  1485. {
  1486. textBox_RunInfo_Gear.Text = "SPORT";
  1487. radioButton_Gear_SPORT.Checked = true;
  1488. }
  1489. else if (DataTemp == 0x04)
  1490. {
  1491. textBox_RunInfo_Gear.Text = "TURBO";
  1492. radioButton_Gear_TURBO.Checked = true;
  1493. }
  1494. else if (DataTemp == 0x33)
  1495. {
  1496. textBox_RunInfo_Gear.Text = "SMART";
  1497. radioButton_Gear_SMART.Checked = true;
  1498. }
  1499. else if (DataTemp == 0x22)
  1500. {
  1501. textBox_RunInfo_Gear.Text = "WALK";
  1502. radioButton_Gear_WALK.Checked = true;
  1503. }
  1504. //大灯状态
  1505. DataTemp = (ushort)(binary_data_1[16]);
  1506. if (DataTemp == 0xF0)
  1507. {
  1508. textBox_RunInfo_Light.Text = "OFF";
  1509. radioButton_Light_OFF.Checked = true;
  1510. }
  1511. else if (DataTemp == 0xF1)
  1512. {
  1513. textBox_RunInfo_Light.Text = "ON";
  1514. radioButton_Light_ON.Checked = true;
  1515. }
  1516. //剩余电量
  1517. DataTemp = (ushort)(binary_data_1[17]);
  1518. if (DataTemp == 0xFF)
  1519. {
  1520. textBox_RunInfo_SOC.Text = "---";
  1521. }
  1522. else
  1523. {
  1524. textBox_RunInfo_SOC.Text = Convert.ToString(DataTemp) + " %";
  1525. }
  1526. //剩余续航里程
  1527. DataTemp = (ushort)(binary_data_1[19] * 256 + binary_data_1[18]);
  1528. if ((DataTemp == 0xEEEE) || (DataTemp == 0xFFFF))
  1529. {
  1530. textBox_RunInfo_Range.Text = "---";
  1531. }
  1532. else
  1533. {
  1534. textBox_RunInfo_Range.Text = Convert.ToString(DataTemp) + " km";
  1535. }
  1536. //ODO里程
  1537. DataTemp = (ushort)(binary_data_1[21] * 256 + binary_data_1[20]);
  1538. textBox_RunInfo_ODO_Km.Text = Convert.ToString(DataTemp) + " km";
  1539. //平均功耗
  1540. DataTemp = (ushort)(binary_data_1[22]);
  1541. textBox_RunInfo_PowerAvg.Text = Convert.ToString(DataTemp * 10) + " mAh/km";
  1542. //PCB温度
  1543. DataTemp = (ushort)(binary_data_1[23]);
  1544. textBox_RunInfo_T_PCB.Text = Convert.ToString((int)(DataTemp - 40));
  1545. //绕组温度
  1546. DataTemp = (ushort)(binary_data_1[24]);
  1547. textBox_RunInfo_T_Roil.Text = Convert.ToString((int)(DataTemp - 40));
  1548. //MCU温度
  1549. DataTemp = (ushort)(binary_data_1[25]);
  1550. textBox_RunInfo_T_MCU.Text = Convert.ToString((int)(DataTemp - 40));
  1551. //单次里程
  1552. DataTemp = (ushort)(binary_data_1[27] * 256 + binary_data_1[26]);
  1553. textBox_RunInfo_Distance.Text = ((float)DataTemp / 10f).ToString("0.0") + " km";
  1554. //单次时间
  1555. DataTemp = (ushort)(binary_data_1[29] * 256 + binary_data_1[28]);
  1556. textBox_RunInfo_Time.Text = Convert.ToString(DataTemp) + " s";
  1557. //传感器1
  1558. DataTemp = (ushort)(binary_data_1[30]);
  1559. textBox_Sensor1.Text = Convert.ToString(DataTemp) + " N.m";
  1560. Class_Sensor1Data.String = textBox_Sensor1.Text;
  1561. Class_Sensor1Data.Data = DataTemp;
  1562. //传感器2
  1563. DataTemp = (ushort)(binary_data_1[31]);
  1564. textBox_Sensor2.Text = Convert.ToString(DataTemp) + " N.m";
  1565. Class_Sensor2Data.String = textBox_Sensor2.Text;
  1566. Class_Sensor2Data.Data = DataTemp;
  1567. //传感器3
  1568. DataTemp = (ushort)(binary_data_1[32]);
  1569. textBox_Sensor3.Text = Convert.ToString(DataTemp) + " N.m";
  1570. Class_Sensor3Data.String = textBox_Sensor3.Text;
  1571. Class_Sensor3Data.Data = DataTemp;
  1572. //计算传感器均值和标准差
  1573. TorqueArray[TorqueArray_Index] = binary_data_1[13];
  1574. Sensor1Array[TorqueArray_Index] = binary_data_1[30];
  1575. Sensor2Array[TorqueArray_Index] = binary_data_1[31];
  1576. Sensor3Array[TorqueArray_Index] = binary_data_1[32];
  1577. TorqueArray_Index++;
  1578. if (TorqueArray_Index >= 25)
  1579. {
  1580. textBox_TorqueData.Text = Convert.ToString(GetAvg(TorqueArray, 25)) + " N.m";
  1581. textBox_Torque_Stdev.Text = Convert.ToString(GetStDev(TorqueArray, 25));
  1582. textBox_Sensor1_Avg.Text = Convert.ToString(GetAvg(Sensor1Array, 25)) + " N.m";
  1583. textBox_Sensor1_Std.Text = Convert.ToString(GetStDev(Sensor1Array, 25));
  1584. textBox_Sensor2_Avg.Text = Convert.ToString(GetAvg(Sensor2Array, 25)) + " N.m";
  1585. textBox_Sensor2_Std.Text = Convert.ToString(GetStDev(Sensor2Array, 25));
  1586. textBox_Sensor3_Avg.Text = Convert.ToString(GetAvg(Sensor3Array, 25)) + " N.m";
  1587. textBox_Sensor3_Std.Text = Convert.ToString(GetStDev(Sensor3Array, 25));
  1588. TorqueArray_Index = 0;
  1589. }
  1590. //数据自动保存
  1591. DateAutoSave();
  1592. }));
  1593. }
  1594. break;
  1595. }
  1596. case 0xA020://用户参数
  1597. {
  1598. unchecked
  1599. {
  1600. this.Invoke((EventHandler)(delegate
  1601. {
  1602. //指拨模式
  1603. DataTemp = (ushort)(binary_data_1[2]);
  1604. textBox_Config_GasMode.Text = Convert.ToString(DataTemp, 16).PadLeft(2, '0').ToUpper();
  1605. //启动模式
  1606. DataTemp = (ushort)(binary_data_1[3]);
  1607. if (DataTemp == 0x01)
  1608. comboBox_StartMode.Text = "柔和";
  1609. else if (DataTemp == 0x02)
  1610. comboBox_StartMode.Text = "正常";
  1611. else if (DataTemp == 0x03)
  1612. comboBox_StartMode.Text = "强劲";
  1613. //停机时间
  1614. DataTemp = (ushort)(binary_data_1[5] * 256 + binary_data_1[4]);
  1615. textBox_Config_StopTime.Text = Convert.ToString(DataTemp);
  1616. //限速值
  1617. DataTemp = (ushort)(binary_data_1[6]);
  1618. textBox_Config_MaxSpeed.Text = Convert.ToString(DataTemp);
  1619. //下降速度
  1620. DataTemp = (ushort)(binary_data_1[7]);
  1621. if (DataTemp == 0x01)
  1622. comboBox_DownDec.Text = "超慢";
  1623. else if (DataTemp == 0x02)
  1624. comboBox_DownDec.Text = "慢";
  1625. else if (DataTemp == 0x03)
  1626. comboBox_DownDec.Text = "正常";
  1627. else if (DataTemp == 0x02)
  1628. comboBox_DownDec.Text = "快";
  1629. else if (DataTemp == 0x03)
  1630. comboBox_DownDec.Text = "超快";
  1631. //前飞
  1632. DataTemp = (ushort)(binary_data_1[8]);
  1633. textBox_Config_Teeth_F.Text = Convert.ToString(DataTemp);
  1634. //后飞
  1635. DataTemp = (ushort)(binary_data_1[9]);
  1636. textBox_Config_Teeth_B.Text = Convert.ToString(DataTemp);
  1637. //限流
  1638. DataTemp = (ushort)(binary_data_1[10]);
  1639. textBox_Config_MaxCurrent.Text = Convert.ToString(DataTemp);
  1640. //温度预警
  1641. DataTemp = (ushort)(binary_data_1[11]);
  1642. textBox_Config_T_Alarm.Text = Convert.ToString((Int16)(DataTemp - 40));
  1643. //温度保护
  1644. DataTemp = (ushort)(binary_data_1[12]);
  1645. textBox_Config_T_Protect.Text = Convert.ToString((Int16)(DataTemp - 40));
  1646. //无PBU标志
  1647. DataTemp = (ushort)(binary_data_1[13]);
  1648. if (DataTemp == 0x55)
  1649. {
  1650. comboBox_NoWatch.Text = "有";
  1651. }
  1652. else if (DataTemp == 0xAA)
  1653. {
  1654. comboBox_NoWatch.Text = "无";
  1655. }
  1656. //轮胎周长
  1657. DataTemp = (ushort)(binary_data_1[14]);
  1658. textBox_Config_WheelSize.Text = Convert.ToString(DataTemp);
  1659. //电机系列号
  1660. DataTemp = (ushort)(binary_data_1[15]);
  1661. comboBox_SerNum.Text = Convert.ToString(DataTemp);
  1662. //ECO助力增益
  1663. DataTemp = (ushort)(binary_data_1[16]);
  1664. textBox_Config_ECO_K.Text = Convert.ToString(DataTemp);
  1665. //ECO加速
  1666. DataTemp = (ushort)(binary_data_1[17]);
  1667. textBox_Config_ECO_Acc.Text = Convert.ToString(DataTemp);
  1668. //NORM助力增益
  1669. DataTemp = (ushort)(binary_data_1[18]);
  1670. textBox_Config_NORM_K.Text = Convert.ToString(DataTemp);
  1671. //NORM加速
  1672. DataTemp = (ushort)(binary_data_1[19]);
  1673. textBox_Config_NORM_Acc.Text = Convert.ToString(DataTemp);
  1674. //SPORT助力增益
  1675. DataTemp = (ushort)(binary_data_1[20]);
  1676. textBox_Config_SPORT_K.Text = Convert.ToString(DataTemp);
  1677. //SPORT加速
  1678. DataTemp = (ushort)(binary_data_1[21]);
  1679. textBox_Config_SPORT_Acc.Text = Convert.ToString(DataTemp);
  1680. //TURBO助力增益
  1681. DataTemp = (ushort)(binary_data_1[22]);
  1682. textBox_Config_TURBO_K.Text = Convert.ToString(DataTemp);
  1683. //TURBO加速
  1684. DataTemp = (ushort)(binary_data_1[23]);
  1685. textBox_Config_TURBO_Acc.Text = Convert.ToString(DataTemp);
  1686. //SMART助力增益
  1687. DataTemp = (ushort)(binary_data_1[24]);
  1688. textBox_Config_SMART_K.Text = Convert.ToString(DataTemp);
  1689. //SMART加速
  1690. DataTemp = (ushort)(binary_data_1[25]);
  1691. textBox_Config_SMART_Acc.Text = Convert.ToString(DataTemp);
  1692. //速度传感器信号个数
  1693. DataTemp = (ushort)(binary_data_1[26]);
  1694. textBox_Config_SpeedNum.Text = Convert.ToString(DataTemp);
  1695. //踏频启动信号个数
  1696. DataTemp = (ushort)(binary_data_1[27]);
  1697. textBox_Config_CadenceNum.Text = Convert.ToString(DataTemp);
  1698. //无速度传感器
  1699. DataTemp = (ushort)(binary_data_1[28]);
  1700. if (DataTemp == 0x55)
  1701. {
  1702. comboBox_SpeedSing.Text = "传感器";
  1703. }
  1704. else if (DataTemp == 0xAA)
  1705. {
  1706. comboBox_SpeedSing.Text = "踏频";
  1707. }
  1708. else if (DataTemp == 0xEE)
  1709. {
  1710. comboBox_SpeedSing.Text = "通信";
  1711. }
  1712. //周长微调
  1713. DataTemp_int16 = (short)binary_data_1[29];
  1714. textBox_Config_WheelAdj.Text = Convert.ToString((DataTemp_int16 >= 128) ? (DataTemp_int16 - 256) : DataTemp_int16);
  1715. //低压保护阈值
  1716. DataTemp = (ushort)(binary_data_1[31] * 256 + binary_data_1[30]);
  1717. textBox_Config_UV_Protect.Text = Convert.ToString(DataTemp);
  1718. //推行限速值
  1719. DataTemp = (ushort)(binary_data_1[32]);
  1720. textBox_Config_WalkSpeedLimit.Text = ((float)DataTemp / 10f).ToString("#0.0");
  1721. //推行马达转速
  1722. DataTemp = (ushort)(binary_data_1[33]);
  1723. textBox_Config_WalkMotorSpeed.Text = Convert.ToString(DataTemp);
  1724. }));
  1725. }
  1726. break;
  1727. }
  1728. case 0xA110://马达参数
  1729. {
  1730. unchecked
  1731. {
  1732. this.Invoke((EventHandler)(delegate
  1733. {
  1734. //额定功率
  1735. DataTemp = (ushort)(binary_data_1[3] * 256 + binary_data_1[2]);
  1736. textBox_Motor_Power.Text = Convert.ToString(DataTemp);
  1737. //额定转速
  1738. DataTemp = (ushort)(binary_data_1[5] * 256 + binary_data_1[4]);
  1739. textBox_Motor_Speed.Text = Convert.ToString(DataTemp);
  1740. //定子电阻
  1741. DataTemp = (ushort)(binary_data_1[7] * 256 + binary_data_1[6]);
  1742. textBox_Motor_R.Text = Convert.ToString(DataTemp);
  1743. //Lq
  1744. DataTemp = (ushort)(binary_data_1[9] * 256 + binary_data_1[8]);
  1745. textBox_Motor_Lq.Text = Convert.ToString(DataTemp);
  1746. //Ld
  1747. DataTemp = (ushort)(binary_data_1[11] * 256 + binary_data_1[10]);
  1748. textBox_Motor_Ld.Text = Convert.ToString(DataTemp);
  1749. //反电动势
  1750. DataTemp = (ushort)(binary_data_1[13] * 256 + binary_data_1[12]);
  1751. textBox_Motor_E.Text = Convert.ToString(DataTemp);
  1752. //额定电压
  1753. DataTemp = (ushort)(binary_data_1[14]);
  1754. textBox_Motor_Voltage.Text = Convert.ToString(DataTemp);
  1755. }));
  1756. }
  1757. break;
  1758. }
  1759. case 0xA230://历史信息1
  1760. {
  1761. unchecked
  1762. {
  1763. this.Invoke((EventHandler)(delegate
  1764. {
  1765. UInt32 DataTemp_Uint32;
  1766. ushort DataTemp_Uint16;
  1767. short DataTemp_Int16;
  1768. //开机次数
  1769. DataTemp_Uint32 = (UInt32)((binary_data_1[5] << 24) + (binary_data_1[4] << 16) + (binary_data_1[3] << 8) + binary_data_1[2]);
  1770. textBox_Runlog1_PowerCount.Text = Convert.ToString(DataTemp_Uint32);
  1771. //使用时间
  1772. DataTemp_Uint32 = (UInt32)((binary_data_1[9] << 24) + (binary_data_1[8] << 16) + (binary_data_1[7] << 8) + binary_data_1[6]);
  1773. textBox_Runlog1_RunTime.Text = Convert.ToString(DataTemp_Uint32);
  1774. //过流保护
  1775. DataTemp_Uint16 = (ushort)(binary_data_1[11] * 256 + binary_data_1[10]);
  1776. textBox_Runlog1_OC.Text = Convert.ToString(DataTemp_Uint16);
  1777. //低压保护
  1778. DataTemp_Uint16 = (ushort)(binary_data_1[13] * 256 + binary_data_1[12]);
  1779. textBox_Runlog1_UV.Text = Convert.ToString(DataTemp_Uint16);
  1780. //过压保护
  1781. DataTemp_Uint16 = (ushort)(binary_data_1[15] * 256 + binary_data_1[14]);
  1782. textBox_Runlog1_OV.Text = Convert.ToString(DataTemp_Uint16);
  1783. //堵转保护
  1784. DataTemp_Uint16 = (ushort)(binary_data_1[17] * 256 + binary_data_1[16]);
  1785. textBox_Runlog1_Lock.Text = Convert.ToString(DataTemp_Uint16);
  1786. //过热保护
  1787. DataTemp_Uint16 = (ushort)(binary_data_1[19] * 256 + binary_data_1[18]);
  1788. textBox_Runlog1_OT.Text = Convert.ToString(DataTemp_Uint16);
  1789. //速度传感器故障
  1790. DataTemp_Uint16 = (ushort)(binary_data_1[21] * 256 + binary_data_1[20]);
  1791. textBox_Runlog1_SPSFault.Text = Convert.ToString(DataTemp_Uint16);
  1792. //力矩传感器
  1793. DataTemp_Uint16 = (ushort)(binary_data_1[23] * 256 + binary_data_1[22]);
  1794. textBox_Runlog1_TQSFault.Text = Convert.ToString(DataTemp_Uint16);
  1795. //霍尔故障
  1796. DataTemp_Uint16 = (ushort)(binary_data_1[25] * 256 + binary_data_1[24]);
  1797. textBox_Runlog1_HallFault.Text = Convert.ToString(DataTemp_Uint16);
  1798. //马达缺相
  1799. DataTemp_Uint16 = (ushort)(binary_data_1[27] * 256 + binary_data_1[26]);
  1800. textBox_Runlog1_Phase.Text = Convert.ToString(DataTemp_Uint16);
  1801. //NTC故障
  1802. DataTemp_Uint16 = (ushort)(binary_data_1[29] * 256 + binary_data_1[28]);
  1803. textBox_Runlog1_NTCFault.Text = Convert.ToString(DataTemp_Uint16);
  1804. //BMS校验失败
  1805. DataTemp_Uint16 = (ushort)(binary_data_1[31] * 256 + binary_data_1[30]);
  1806. textBox_Runlog1_BMSFault.Text = Convert.ToString(DataTemp_Uint16);
  1807. //HMI校验失败
  1808. DataTemp_Uint16 = (ushort)(binary_data_1[33] * 256 + binary_data_1[32]);
  1809. textBox_Runlog1_HMIFault.Text = Convert.ToString(DataTemp_Uint16);
  1810. //PBU校验失败
  1811. DataTemp_Uint16 = (ushort)(binary_data_1[35] * 256 + binary_data_1[34]);
  1812. textBox_Runlog1_PBUFault.Text = Convert.ToString(DataTemp_Uint16);
  1813. //PCB最高温
  1814. DataTemp_Int16 = (short)(ushort)binary_data_1[36];
  1815. textBox_Runlog1_PCB_MAX.Text = Convert.ToString(DataTemp_Int16 - 40);
  1816. //PCB最低温
  1817. DataTemp_Int16 = (short)(ushort)binary_data_1[37];
  1818. textBox_Runlog1_PCB_MIN.Text = Convert.ToString(DataTemp_Int16 - 40);
  1819. //ODO里程
  1820. DataTemp_Uint32 = (UInt32)((binary_data_1[41] << 24) + (binary_data_1[40] << 16) + (binary_data_1[39] << 8) + binary_data_1[38]);
  1821. textBox_ODO_Km.Text = ((float)DataTemp_Uint32 / 10f).ToString("0.0");
  1822. //绕组最高温
  1823. DataTemp_Int16 = (short)(ushort)binary_data_1[42];
  1824. textBox_Runlog1_Coil_MAX.Text = Convert.ToString(DataTemp_Int16 - 40);
  1825. //绕组最低温
  1826. DataTemp_Int16 = (short)(ushort)binary_data_1[43];
  1827. textBox_Runlog1_Coil_MIN.Text = Convert.ToString(DataTemp_Int16 - 40);
  1828. //MCU最高温
  1829. DataTemp_Int16 = (short)(ushort)binary_data_1[44];
  1830. textBox_Runlog1_MCU_MAX.Text = Convert.ToString(DataTemp_Int16 - 40);
  1831. //MCU最低温
  1832. DataTemp_Int16 = (short)(ushort)binary_data_1[45];
  1833. textBox_Runlog1_MCU_MIN.Text = Convert.ToString(DataTemp_Int16 - 40);
  1834. //ODO时间
  1835. DataTemp_Uint32 = (UInt32)((binary_data_1[49] << 24) + (binary_data_1[48] << 16) + (binary_data_1[47] << 8) + binary_data_1[46]);
  1836. textBox_ODO_Time.Text = Convert.ToString(DataTemp_Uint32);
  1837. }));
  1838. }
  1839. break;
  1840. }
  1841. case 0xAB09://存储器数据
  1842. case 0xAB0A:
  1843. case 0xAB0B:
  1844. case 0xAB0C:
  1845. {
  1846. unchecked
  1847. {
  1848. this.Invoke((EventHandler)(delegate
  1849. {
  1850. long ByteNum = 0;
  1851. ByteNum = ((binary_data_1[6] << 24) + (binary_data_1[7] << 16) + (binary_data_1[8] << 8) + binary_data_1[9]) - ((binary_data_1[2] << 24) + (binary_data_1[3] << 16) + (binary_data_1[4] << 8) + binary_data_1[5]) + 1;
  1852. if (ByteNum > 4) //数据长度超过4Bytes,按照16进制显示字节流
  1853. {
  1854. for (int i = 0; i < ByteNum; i++)
  1855. {
  1856. richTextBox_DebugInfo.AppendText(Convert.ToString(binary_data_1[10 + i], 16).PadLeft(2, '0').ToUpper() + " ");
  1857. }
  1858. }
  1859. else //根据设置自动转换10进制显示
  1860. {
  1861. if (radioButton_DebugDec.Checked == true) //10进制显示
  1862. {
  1863. switch (ByteNum)
  1864. {
  1865. case 1:
  1866. {
  1867. richTextBox_DebugInfo.AppendText(Convert.ToString(binary_data_1[10]) + ", ");
  1868. break;
  1869. }
  1870. case 2:
  1871. {
  1872. richTextBox_DebugInfo.AppendText(Convert.ToString(binary_data_1[10] + (binary_data_1[11] << 8)) + ", ");
  1873. break;
  1874. }
  1875. case 3:
  1876. {
  1877. richTextBox_DebugInfo.AppendText(Convert.ToString(binary_data_1[10] + (binary_data_1[11] << 8) + (binary_data_1[12] << 16)) + ", ");
  1878. break;
  1879. }
  1880. case 4:
  1881. {
  1882. richTextBox_DebugInfo.AppendText(Convert.ToString(binary_data_1[10] + (binary_data_1[11] << 8) + (binary_data_1[12] << 16) + (binary_data_1[13] << 24)) + ", ");
  1883. break;
  1884. }
  1885. default:
  1886. {
  1887. richTextBox_DebugInfo.AppendText("无效数据" + ", ");
  1888. break;
  1889. }
  1890. }
  1891. }
  1892. else if (radioButton_DebugHex.Checked == true)//16进制显示
  1893. {
  1894. switch (ByteNum)
  1895. {
  1896. case 1:
  1897. {
  1898. richTextBox_DebugInfo.AppendText("0x" + Convert.ToString(binary_data_1[10], 16).PadLeft(2, '0').ToUpper() + ", ");
  1899. break;
  1900. }
  1901. case 2:
  1902. {
  1903. richTextBox_DebugInfo.AppendText("0x" + Convert.ToString(binary_data_1[10] + (binary_data_1[11] << 8), 16).PadLeft(4, '0').ToUpper() + ", ");
  1904. break;
  1905. }
  1906. case 3:
  1907. {
  1908. richTextBox_DebugInfo.AppendText("0x" + Convert.ToString(binary_data_1[10] + (binary_data_1[11] << 8) + (binary_data_1[12] << 16), 16).PadLeft(6, '0').ToUpper() + ", ");
  1909. break;
  1910. }
  1911. case 4:
  1912. {
  1913. richTextBox_DebugInfo.AppendText("0x" + Convert.ToString(binary_data_1[10] + (binary_data_1[11] << 8) + (binary_data_1[12] << 16) + (binary_data_1[13] << 24), 16).PadLeft(8, '0').ToUpper() + ", ");
  1914. break;
  1915. }
  1916. default:
  1917. {
  1918. richTextBox_DebugInfo.AppendText("无效数据" + ", ");
  1919. break;
  1920. }
  1921. }
  1922. }
  1923. }
  1924. }));
  1925. }
  1926. break;
  1927. }
  1928. case 0xAB88://故障日志
  1929. {
  1930. unchecked
  1931. {
  1932. this.Invoke((EventHandler)(delegate
  1933. {
  1934. //电机存储的故障日志,一包128字节包含2条故障日志
  1935. var ErrorLogSaveInfo1 = new MC_ErrorLogSaveInfo_Struct_t();
  1936. var ErrorLogSaveInfo2 = new MC_ErrorLogSaveInfo_Struct_t();
  1937. ErrorLogSaveInfo1 = MyConverter.ByteToStructure<MC_ErrorLogSaveInfo_Struct_t>(binary_data_1, 10);
  1938. ErrorLogSaveInfo2 = MyConverter.ByteToStructure<MC_ErrorLogSaveInfo_Struct_t>(binary_data_1, 74);
  1939. //数据存储
  1940. ErrorLogDateSave(ErrorLogSaveInfo1, ErrorLogSaveInfo2);
  1941. }));
  1942. }
  1943. break;
  1944. }
  1945. case 0xAC10://运行历史2
  1946. {
  1947. unchecked
  1948. {
  1949. this.Invoke((EventHandler)(delegate
  1950. {
  1951. ushort DataTemp_Uint16;
  1952. //MCU故障
  1953. DataTemp_Uint16 = (ushort)(binary_data_1[3] * 256 + binary_data_1[2]);
  1954. textBox_Runlog2_MCUFault.Text = Convert.ToString(DataTemp_Uint16);
  1955. //踏频传感器故障
  1956. DataTemp_Uint16 = (ushort)(binary_data_1[5] * 256 + binary_data_1[4]);
  1957. textBox_Runlog2_CDSFault.Text = Convert.ToString(DataTemp_Uint16);
  1958. //指拨故障
  1959. DataTemp_Uint16 = (ushort)(binary_data_1[7] * 256 + binary_data_1[6]);
  1960. textBox_Runlog2_GasFault.Text = Convert.ToString(DataTemp_Uint16);
  1961. //MOS短路
  1962. DataTemp_Uint16 = (ushort)(binary_data_1[9] * 256 + binary_data_1[8]);
  1963. textBox_Runlog2_MOS_SC.Text = Convert.ToString(DataTemp_Uint16);
  1964. //电压异常
  1965. DataTemp_Uint16 = (ushort)(binary_data_1[11] * 256 + binary_data_1[10]);
  1966. textBox_Runlog2_VolFault.Text = Convert.ToString(DataTemp_Uint16);
  1967. //电路异常
  1968. DataTemp_Uint16 = (ushort)(binary_data_1[13] * 256 + binary_data_1[12]);
  1969. textBox_Runlog2_CircuitFault.Text = Convert.ToString(DataTemp_Uint16);
  1970. //TE MCU异常
  1971. DataTemp_Uint16 = (ushort)(binary_data_1[15] * 256 + binary_data_1[14]);
  1972. textBox_Runlog2_TE_MCUFault.Text = Convert.ToString(DataTemp_Uint16);
  1973. //TE电路故障
  1974. DataTemp_Uint16 = (ushort)(binary_data_1[17] * 256 + binary_data_1[16]);
  1975. textBox_Runlog2_TE_CircuitFault.Text = Convert.ToString(DataTemp_Uint16);
  1976. }));
  1977. }
  1978. break;
  1979. }
  1980. case 0x1104://故障码
  1981. {
  1982. unchecked
  1983. {
  1984. this.Invoke((EventHandler)(delegate
  1985. {
  1986. MC_ErrorCode_Refresh = true;
  1987. textBox_RunInfo_ErrorCode.Text =
  1988. Convert.ToString(binary_data_1[5], 16).PadLeft(2, '0').ToUpper() +
  1989. Convert.ToString(binary_data_1[4], 16).PadLeft(2, '0').ToUpper() +
  1990. Convert.ToString(binary_data_1[3], 16).PadLeft(2, '0').ToUpper() +
  1991. Convert.ToString(binary_data_1[2], 16).PadLeft(2, '0').ToUpper() +
  1992. "H";
  1993. }));
  1994. }
  1995. break;
  1996. }
  1997. case 0xA903://反馈指令
  1998. {
  1999. unchecked
  2000. {
  2001. this.Invoke((EventHandler)(delegate
  2002. {
  2003. timer1.Enabled = false;
  2004. MessageBox.Show("OK", "反馈指令", MessageBoxButtons.OK, MessageBoxIcon.Information);
  2005. timer1.Enabled = true;
  2006. }));
  2007. }
  2008. break;
  2009. }
  2010. case 0xAD20://用户参数2
  2011. {
  2012. unchecked
  2013. {
  2014. this.Invoke((EventHandler)(delegate
  2015. {
  2016. short DataTemp_Int16;
  2017. sbyte DataTemp_byte;
  2018. //俯仰角零偏
  2019. DataTemp_Int16 = (short)(binary_data_1[3] * 256 + binary_data_1[2]);
  2020. textBox_Config_Angle_Pitch.Text = ((float)DataTemp_Int16 / 10f).ToString("0.0");
  2021. //横滚角零偏
  2022. DataTemp_Int16 = (short)(binary_data_1[5] * 256 + binary_data_1[4]);
  2023. textBox_Config_Angle_Roll.Text = ((float)DataTemp_Int16 / 10f).ToString("0.0");
  2024. //支持姿态传感器
  2025. DataTemp = (ushort)(binary_data_1[6]);
  2026. if (DataTemp == 0x55)
  2027. {
  2028. comboBox_AttiSensor.Text = "不支持";
  2029. }
  2030. else if (DataTemp == 0xAA)
  2031. {
  2032. comboBox_AttiSensor.Text = "支持";
  2033. }
  2034. //前灯模式
  2035. DataTemp = (ushort)(binary_data_1[7] & 0xF0);
  2036. if (DataTemp == 0x10)
  2037. {
  2038. comboBox_FrontLightMode.Text = "模式1";
  2039. }
  2040. else if (DataTemp == 0x20)
  2041. {
  2042. comboBox_FrontLightMode.Text = "模式2";
  2043. }
  2044. else
  2045. {
  2046. comboBox_FrontLightMode.Text = "0";
  2047. }
  2048. //尾灯模式
  2049. DataTemp = (ushort)(binary_data_1[7] & 0x0F);
  2050. if (DataTemp == 0x01)
  2051. {
  2052. comboBox_TailLightMode.Text = "模式1";
  2053. }
  2054. else if (DataTemp == 0x02)
  2055. {
  2056. comboBox_TailLightMode.Text = "模式2";
  2057. }
  2058. else if (DataTemp == 0x03)
  2059. {
  2060. comboBox_TailLightMode.Text = "模式3";
  2061. }
  2062. else if (DataTemp == 0x04)
  2063. {
  2064. comboBox_TailLightMode.Text = "模式4";
  2065. }
  2066. else if (DataTemp == 0x05)
  2067. {
  2068. comboBox_TailLightMode.Text = "模式5";
  2069. }
  2070. else if (DataTemp == 0x06)
  2071. {
  2072. comboBox_TailLightMode.Text = "模式6";
  2073. }
  2074. else
  2075. {
  2076. comboBox_TailLightMode.Text = "0";
  2077. }
  2078. //前灯电压
  2079. DataTemp = (ushort)(binary_data_1[8]);
  2080. if (DataTemp == 0x06)
  2081. {
  2082. comboBox_FrontLigthVol.Text = "6V";
  2083. }
  2084. else if (DataTemp == 0x0C)
  2085. {
  2086. comboBox_FrontLigthVol.Text = "12V";
  2087. }
  2088. else if (DataTemp == 0xFF)
  2089. {
  2090. comboBox_FrontLigthVol.Text = "随电池";
  2091. }
  2092. else if (DataTemp == 0x00)
  2093. {
  2094. comboBox_FrontLigthVol.Text = "硬件";
  2095. }
  2096. else
  2097. {
  2098. comboBox_FrontLigthVol.Text = "无效";
  2099. }
  2100. //尾灯电压
  2101. DataTemp = (ushort)(binary_data_1[9]);
  2102. if (DataTemp == 0x06)
  2103. {
  2104. comboBox_TailLightVol.Text = "6V";
  2105. }
  2106. else if (DataTemp == 0x0C)
  2107. {
  2108. comboBox_TailLightVol.Text = "12V";
  2109. }
  2110. else if (DataTemp == 0xFF)
  2111. {
  2112. comboBox_TailLightVol.Text = "随电池";
  2113. }
  2114. else if (DataTemp == 0x00)
  2115. {
  2116. comboBox_TailLightVol.Text = "硬件";
  2117. }
  2118. else
  2119. {
  2120. comboBox_TailLightVol.Text = "无效";
  2121. }
  2122. //超速报警阈值
  2123. DataTemp = (ushort)(binary_data_1[11] * 256 + binary_data_1[10]);
  2124. textBox_Config_SpeedAlarm.Text = ((float)DataTemp / 10f).ToString("#0.0");
  2125. //尾灯脉宽
  2126. DataTemp = (ushort)(binary_data_1[13] * 256 + binary_data_1[12]);
  2127. textBox_Config_Pulse.Text = Convert.ToString(DataTemp);
  2128. //尾灯周期
  2129. DataTemp = (ushort)(binary_data_1[15] * 256 + binary_data_1[14]);
  2130. textBox_Config_Period.Text = Convert.ToString(DataTemp);
  2131. //起始限速
  2132. DataTemp_byte = (sbyte)(binary_data_1[16]);
  2133. textBox_Config_SpeedlimitStart.Text = ((float)(DataTemp_byte) / 10f).ToString("#0.0");
  2134. //结束限速
  2135. DataTemp_byte = (sbyte)(binary_data_1[17]);
  2136. textBox_Config_SpeedlimitStop.Text = ((float)DataTemp_byte / 10f).ToString("#0.0");
  2137. //限速微调
  2138. DataTemp_int16 = (short)binary_data_1[18];
  2139. textBox_Config_SpeedlimitAdj.Text = Convert.ToString((DataTemp_int16 >= 128) ? (DataTemp_int16 - 256) : DataTemp_int16);
  2140. }));
  2141. }
  2142. break;
  2143. }
  2144. case 0x1510:
  2145. {
  2146. unchecked
  2147. {
  2148. this.Invoke((EventHandler)(delegate
  2149. {
  2150. long DataTemp32;
  2151. MC_RideInfo_Refresh = true;
  2152. //ODO里程
  2153. DataTemp32 = (long)((binary_data_1[5] << 24) + (binary_data_1[4] << 16) + (binary_data_1[3] << 8) + binary_data_1[2]);
  2154. textBox_RideInfo_ODO_Km.Text = ((float)DataTemp32 / 10f).ToString("0.0") + " km";
  2155. //ODO时间
  2156. DataTemp32 = (long)((binary_data_1[9] << 24) + (binary_data_1[8] << 16) + (binary_data_1[7] << 8) + binary_data_1[6]);
  2157. if (DataTemp32 >= 60 * 1000)
  2158. textBox_RideInfo_ODO_Time.Text = Convert.ToString(DataTemp32 / 60) + " h";
  2159. else
  2160. textBox_RideInfo_ODO_Time.Text = Convert.ToString(DataTemp32 / 60) + " h " + Convert.ToString(DataTemp32 % 60) + " m";
  2161. //TRIP里程
  2162. DataTemp32 = (long)((binary_data_1[13] << 24) + (binary_data_1[12] << 16) + (binary_data_1[11] << 8) + binary_data_1[10]);
  2163. textBox_RideInfo_Trip_Km.Text = ((float)DataTemp32 / 10f).ToString("0.0") + " km";
  2164. //TRIP时间
  2165. DataTemp32 = (long)((binary_data_1[17] << 24) + (binary_data_1[16] << 16) + (binary_data_1[15] << 8) + binary_data_1[14]);
  2166. if (DataTemp32 >= 60 * 1000)
  2167. textBox_RideInfo_Trip_Time.Text = Convert.ToString(DataTemp32 / 60) + " h";
  2168. else
  2169. textBox_RideInfo_Trip_Time.Text = Convert.ToString(DataTemp32 / 60) + " h " + Convert.ToString(DataTemp32 % 60) + " m";
  2170. }));
  2171. }
  2172. break;
  2173. }
  2174. case 0x1608://姿态信息
  2175. {
  2176. unchecked
  2177. {
  2178. this.Invoke((EventHandler)(delegate
  2179. {
  2180. short DataTemp_Int16;
  2181. MC_AttitudeAngle_Refresh = true;
  2182. //俯仰角度绝对
  2183. DataTemp_Int16 = (short)(binary_data_1[3] * 256 + binary_data_1[2]);
  2184. textBox_Attitude_PitchAngle_Abs.Text = ((float)DataTemp_Int16 / 10f).ToString("0.0");
  2185. //横滚角度绝对
  2186. DataTemp_Int16 = (short)(binary_data_1[5] * 256 + binary_data_1[4]);
  2187. textBox_Attitude_RollAngle_Abs.Text = ((float)DataTemp_Int16 / 10f).ToString("0.0");
  2188. //俯仰角度相对
  2189. DataTemp_Int16 = (short)(binary_data_1[7] * 256 + binary_data_1[6]);
  2190. textBox_Attitude_PitchAngle_Rel.Text = ((float)DataTemp_Int16 / 10f).ToString("0.0");
  2191. //横滚角度相对
  2192. DataTemp_Int16 = (short)(binary_data_1[9] * 256 + binary_data_1[8]);
  2193. textBox_Attitude_RollAngle_Rel.Text = ((float)DataTemp_Int16 / 10f).ToString("0.0");
  2194. }));
  2195. }
  2196. break;
  2197. }
  2198. default: break;
  2199. }
  2200. //存储器导出数据显示
  2201. if ((CmdTemp & 0xFF00) == 0xAB00)
  2202. {
  2203. unchecked
  2204. {
  2205. this.Invoke((EventHandler)(delegate
  2206. {
  2207. long ByteNum = 0;
  2208. ByteNum = ((binary_data_1[6] << 24) + (binary_data_1[7] << 16) + (binary_data_1[8] << 8) + binary_data_1[9]) - ((binary_data_1[2] << 24) + (binary_data_1[3] << 16) + (binary_data_1[4] << 8) + binary_data_1[5]) + 1;
  2209. if (ByteNum > 4) //数据长度超过4Bytes,按照16进制显示字节流
  2210. {
  2211. for (int i = 0; i < ByteNum; i++)
  2212. {
  2213. richTextBox_DebugInfo.AppendText(Convert.ToString(binary_data_1[10 + i], 16).PadLeft(2, '0').ToUpper() + " ");
  2214. }
  2215. }
  2216. }));
  2217. }
  2218. }
  2219. }
  2220. #endregion
  2221. #region 解析MC TE的命令
  2222. else if ((ID == 0x615) || (ID == 0x610))
  2223. {
  2224. switch (CmdTemp)
  2225. {
  2226. case 0x1240://TE APP版本信息
  2227. {
  2228. unchecked
  2229. {
  2230. this.Invoke((EventHandler)(delegate
  2231. {
  2232. //电机FW
  2233. textBox_TE_FW.Text = "";
  2234. for (ushort i = 0; i < 16; i++)
  2235. {
  2236. if (binary_data_1[50 + i] == 0x2E)
  2237. {
  2238. break;
  2239. }
  2240. if (binary_data_1[50 + i] == 'r')
  2241. {
  2242. binary_data_1[50 + i] = (byte)'.';
  2243. }
  2244. textBox_TE_FW.Text += ((char)binary_data_1[50 + i]).ToString();
  2245. }
  2246. }));
  2247. }
  2248. break;
  2249. }
  2250. default: break;
  2251. }
  2252. #endregion
  2253. }
  2254. }
  2255. #endregion
  2256. //用户参数显示清除
  2257. private void MC_Config_Clear()
  2258. {
  2259. foreach (Control c in groupBox10.Controls)
  2260. {
  2261. if ((c is TextBox) || (c is ComboBox))
  2262. {
  2263. c.Text = "---";
  2264. }
  2265. }
  2266. }
  2267. //用户参数2清除
  2268. private void MC_Config_Ext_Clear()
  2269. {
  2270. foreach (Control c in groupBox4.Controls)
  2271. {
  2272. if ((c is TextBox) || (c is ComboBox))
  2273. {
  2274. c.Text = "---";
  2275. }
  2276. }
  2277. }
  2278. //马达参数显示清除
  2279. private void Motor_Config_Clear()
  2280. {
  2281. foreach (Control c in groupBox11.Controls)
  2282. {
  2283. if (c is TextBox)
  2284. {
  2285. c.Text = "---";
  2286. }
  2287. }
  2288. }
  2289. //电机运行信息清除
  2290. private void MC_RunInfo_Clear()
  2291. {
  2292. foreach (Control c in groupBox3.Controls)
  2293. {
  2294. if (c is TextBox)
  2295. {
  2296. c.Text = "---";
  2297. }
  2298. }
  2299. }
  2300. //姿态传感器信息清除
  2301. private void MC_AttitudeAngle_Clear()
  2302. {
  2303. foreach (Control c in groupBox13.Controls)
  2304. {
  2305. if (c is TextBox)
  2306. {
  2307. c.Text = "---";
  2308. }
  2309. }
  2310. }
  2311. //骑行信息清除
  2312. private void MC_RideInfo_Clear()
  2313. {
  2314. foreach (Control c in groupBox12.Controls)
  2315. {
  2316. if (c is TextBox)
  2317. {
  2318. c.Text = "---";
  2319. }
  2320. }
  2321. }
  2322. //历史信息清除
  2323. private void MC_RunLog_Clear()
  2324. {
  2325. foreach (Control c in groupBox14.Controls)
  2326. {
  2327. if (c is TextBox)
  2328. {
  2329. c.Text = "---";
  2330. }
  2331. }
  2332. }
  2333. //电机故障码显示清除
  2334. private void MC_ErrorCode_Clear()
  2335. {
  2336. //故障码
  2337. textBox_RunInfo_ErrorCode.Text = "---";
  2338. }
  2339. //接收计数清零
  2340. private void label58_Click(object sender, EventArgs e)
  2341. {
  2342. textBox_RevCnt.Text = "0";
  2343. }
  2344. //读取用户参数
  2345. private void button_Read_Config_Click(object sender, EventArgs e)
  2346. {
  2347. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1A00, null);
  2348. MC_Config_Clear();
  2349. }
  2350. //写入用户参数
  2351. private void button_Write_Config_Click(object sender, EventArgs e)
  2352. {
  2353. var ConfigParam = new byte[32];
  2354. for (int i = 0; i < 32; i++)
  2355. ConfigParam[i] = 0;
  2356. ushort DataTemp;
  2357. //指拨参数
  2358. ConfigParam[0] = byte.Parse(textBox_Config_GasMode.Text, System.Globalization.NumberStyles.HexNumber);
  2359. //启动模式
  2360. if (comboBox_StartMode.Text == "柔和")
  2361. ConfigParam[1] = 0x01;
  2362. else if (comboBox_StartMode.Text == "强劲")
  2363. ConfigParam[1] = 0x03;
  2364. else
  2365. ConfigParam[1] = 0x02;
  2366. //停机时间
  2367. DataTemp = Convert.ToUInt16(textBox_Config_StopTime.Text);
  2368. ConfigParam[2] = (byte)(DataTemp & 0xFF);
  2369. ConfigParam[3] = (byte)(DataTemp >> 8);
  2370. //限速
  2371. ConfigParam[4] = Convert.ToByte(textBox_Config_MaxSpeed.Text);
  2372. //下降速度
  2373. if (comboBox_DownDec.Text == "超慢")
  2374. ConfigParam[5] = 0x01;
  2375. else if (comboBox_DownDec.Text == "慢")
  2376. ConfigParam[5] = 0x02;
  2377. else if (comboBox_DownDec.Text == "快")
  2378. ConfigParam[5] = 0x04;
  2379. else if (comboBox_DownDec.Text == "超快")
  2380. ConfigParam[5] = 0x05;
  2381. else
  2382. ConfigParam[5] = 0x03;
  2383. //前飞
  2384. ConfigParam[6] = Convert.ToByte(textBox_Config_Teeth_F.Text);
  2385. //后飞
  2386. ConfigParam[7] = Convert.ToByte(textBox_Config_Teeth_B.Text);
  2387. //限流
  2388. ConfigParam[8] = Convert.ToByte(textBox_Config_MaxCurrent.Text);
  2389. //温度预警
  2390. ConfigParam[9] = (byte)(Convert.ToByte(textBox_Config_T_Alarm.Text) + 40);
  2391. //温度保护
  2392. ConfigParam[10] = (byte)(Convert.ToByte(textBox_Config_T_Protect.Text) + 40);
  2393. //无PBU
  2394. if (comboBox_NoWatch.Text == "无")
  2395. ConfigParam[11] = 0xAA;
  2396. else
  2397. ConfigParam[11] = 0x55;
  2398. //轮胎周长
  2399. ConfigParam[12] = Convert.ToByte(textBox_Config_WheelSize.Text);
  2400. //电机系列号
  2401. ConfigParam[13] = Convert.ToByte(comboBox_SerNum.Text);
  2402. //ECO助力增益
  2403. ConfigParam[14] = Convert.ToByte(textBox_Config_ECO_K.Text);
  2404. //ECO加速
  2405. ConfigParam[15] = Convert.ToByte(textBox_Config_ECO_Acc.Text);
  2406. //NORM助力增益
  2407. ConfigParam[16] = Convert.ToByte(textBox_Config_NORM_K.Text);
  2408. //NORM加速
  2409. ConfigParam[17] = Convert.ToByte(textBox_Config_NORM_Acc.Text);
  2410. //SPORT助力增益
  2411. ConfigParam[18] = Convert.ToByte(textBox_Config_SPORT_K.Text);
  2412. //SPORT加速
  2413. ConfigParam[19] = Convert.ToByte(textBox_Config_SPORT_Acc.Text);
  2414. //TURBO助力增益
  2415. ConfigParam[20] = Convert.ToByte(textBox_Config_TURBO_K.Text);
  2416. //TURBO加速
  2417. ConfigParam[21] = Convert.ToByte(textBox_Config_TURBO_Acc.Text);
  2418. //SMART助力增益
  2419. ConfigParam[22] = Convert.ToByte(textBox_Config_SMART_K.Text);
  2420. //SMART加速
  2421. ConfigParam[23] = Convert.ToByte(textBox_Config_SMART_Acc.Text);
  2422. //速度传感器信号个数
  2423. ConfigParam[24] = Convert.ToByte(textBox_Config_SpeedNum.Text);
  2424. //踏频启动信号个数
  2425. ConfigParam[25] = Convert.ToByte(textBox_Config_CadenceNum.Text);
  2426. //速度传感器
  2427. if (comboBox_SpeedSing.Text == "踏频")
  2428. ConfigParam[26] = 0xAA;
  2429. else if (comboBox_SpeedSing.Text == "通信")
  2430. ConfigParam[26] = 0xEE;
  2431. else
  2432. ConfigParam[26] = 0x55;
  2433. //周长微调
  2434. ConfigParam[27] = (byte)Convert.ToInt16(textBox_Config_WheelAdj.Text);
  2435. //低压保护阈值
  2436. DataTemp = Convert.ToUInt16(textBox_Config_UV_Protect.Text);
  2437. ConfigParam[28] = (byte)(DataTemp & 0xFF);
  2438. ConfigParam[29] = (byte)(DataTemp >> 8);
  2439. //推行限速
  2440. DataTemp = (ushort)(decimal.Parse(textBox_Config_WalkSpeedLimit.Text) * 10);
  2441. ConfigParam[30] = (byte)Convert.ToByte(DataTemp);
  2442. //推行马达转速
  2443. ConfigParam[31] = (byte)Convert.ToByte(textBox_Config_WalkMotorSpeed.Text);
  2444. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1B20, ConfigParam);
  2445. }
  2446. //读取马达参数
  2447. private void button_Read_MortoConfig_Click(object sender, EventArgs e)
  2448. {
  2449. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1C00, null);
  2450. Motor_Config_Clear();
  2451. }
  2452. //写入马达参数
  2453. private void button_Write_MotorConfig_Click(object sender, EventArgs e)
  2454. {
  2455. var ConfigParam = new byte[16];
  2456. for (int i = 0; i < 16; i++)
  2457. ConfigParam[i] = 0;
  2458. ushort DataTemp;
  2459. //额定功率
  2460. DataTemp = Convert.ToUInt16(textBox_Motor_Power.Text);
  2461. ConfigParam[0] = (byte)(DataTemp & 0xFF);
  2462. ConfigParam[1] = (byte)(DataTemp >> 8);
  2463. //额定转速
  2464. DataTemp = Convert.ToUInt16(textBox_Motor_Speed.Text);
  2465. ConfigParam[2] = (byte)(DataTemp & 0xFF);
  2466. ConfigParam[3] = (byte)(DataTemp >> 8);
  2467. //定子电阻
  2468. DataTemp = Convert.ToUInt16(textBox_Motor_R.Text);
  2469. ConfigParam[4] = (byte)(DataTemp & 0xFF);
  2470. ConfigParam[5] = (byte)(DataTemp >> 8);
  2471. //Lq
  2472. DataTemp = Convert.ToUInt16(textBox_Motor_Lq.Text);
  2473. ConfigParam[6] = (byte)(DataTemp & 0xFF);
  2474. ConfigParam[7] = (byte)(DataTemp >> 8);
  2475. //Ld
  2476. DataTemp = Convert.ToUInt16(textBox_Motor_Ld.Text);
  2477. ConfigParam[8] = (byte)(DataTemp & 0xFF);
  2478. ConfigParam[9] = (byte)(DataTemp >> 8);
  2479. //反电动势
  2480. DataTemp = Convert.ToUInt16(textBox_Motor_E.Text);
  2481. ConfigParam[10] = (byte)(DataTemp & 0xFF);
  2482. ConfigParam[11] = (byte)(DataTemp >> 8);
  2483. //额定电压
  2484. ConfigParam[12] = Convert.ToByte(textBox_Motor_Voltage.Text);
  2485. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1D10, ConfigParam);
  2486. }
  2487. //写入电机型号
  2488. private void button_Write_Mode_Click(object sender, EventArgs e)
  2489. {
  2490. string Mode = textBox_Mode.Text;
  2491. var ModeArray = new byte[16];
  2492. for (ushort i = 0; i < Mode.Length; i++)
  2493. {
  2494. ModeArray[i] = (byte)Mode[i];
  2495. }
  2496. if (Mode.Length < 16)
  2497. {
  2498. ModeArray[Mode.Length] = (byte)'.';
  2499. for (ushort i = 0; i < 16 - Mode.Length - 1; i++)
  2500. {
  2501. ModeArray[Mode.Length + 1 + i] = 0x20;
  2502. }
  2503. }
  2504. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2210, ModeArray);
  2505. }
  2506. //写入电机SN
  2507. private void button_Write_SN_Click(object sender, EventArgs e)
  2508. {
  2509. string SN = textBox_SN.Text;
  2510. var SNArray = new byte[16];
  2511. for (ushort i = 0; i < SN.Length; i++)
  2512. {
  2513. SNArray[i] = (byte)SN[i];
  2514. }
  2515. if (SN.Length < 16)
  2516. {
  2517. SNArray[SN.Length] = (byte)'.';
  2518. for (ushort i = 0; i < 16 - SN.Length - 1; i++)
  2519. {
  2520. SNArray[SN.Length + 1 + i] = 0x20;
  2521. }
  2522. }
  2523. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2310, SNArray);
  2524. }
  2525. //系统复位
  2526. private void button_Reset_Click(object sender, EventArgs e)
  2527. {
  2528. var Code = new byte[5];
  2529. Code[0] = (byte)'R';
  2530. Code[1] = (byte)'E';
  2531. Code[2] = (byte)'S';
  2532. Code[3] = (byte)'E';
  2533. Code[4] = (byte)'T';
  2534. timer1.Enabled = false;
  2535. if (MessageBox.Show("系统将重启", "确认重启?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2536. {
  2537. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2505, Code);
  2538. }
  2539. timer1.Enabled = true;
  2540. }
  2541. private void button_RefreshPort_Click(object sender, EventArgs e)
  2542. {
  2543. string[] ports = SerialPort.GetPortNames();
  2544. Array.Sort(ports);
  2545. comboBox_ComIndex.Items.Clear();
  2546. comboBox_ComIndex.Items.AddRange(ports);
  2547. comboBox_ComIndex.SelectedIndex = comboBox_ComIndex.Items.Count > 0 ? 0 : -1;
  2548. }
  2549. private void checkBox_AutoSend_CheckedChanged(object sender, EventArgs e)
  2550. {
  2551. if (checkBox_AutoSend.Checked == false)
  2552. {
  2553. radioButton_Gear_ECO.Enabled = true;
  2554. radioButton_Gear_NORM.Enabled = true;
  2555. radioButton_Gear_SPORT.Enabled = true;
  2556. radioButton_Gear_TURBO.Enabled = true;
  2557. radioButton_Gear_WALK.Enabled = true;
  2558. radioButton_Gear_SMART.Enabled = true;
  2559. radioButton_Gear_OFF.Enabled = true;
  2560. }
  2561. else
  2562. {
  2563. radioButton_Gear_ECO.Enabled = false;
  2564. radioButton_Gear_NORM.Enabled = false;
  2565. radioButton_Gear_SPORT.Enabled = false;
  2566. radioButton_Gear_TURBO.Enabled = false;
  2567. radioButton_Gear_WALK.Enabled = false;
  2568. radioButton_Gear_SMART.Enabled = false;
  2569. radioButton_Gear_OFF.Enabled = false;
  2570. }
  2571. }
  2572. private void button_Read_Config_Ext_Click(object sender, EventArgs e)
  2573. {
  2574. MC_Config_Ext_Clear();
  2575. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3000, null);
  2576. }
  2577. private void button_Write_Config_Ext_Click(object sender, EventArgs e)
  2578. {
  2579. var ConfigParam = new byte[32];
  2580. for (int i = 0; i < 32; i++)
  2581. ConfigParam[i] = 0;
  2582. short DataTemp_Int16;
  2583. ushort DataTemp;
  2584. //俯仰角零偏
  2585. DataTemp_Int16 = (short)(decimal.Parse(textBox_Config_Angle_Pitch.Text) * 10);
  2586. ConfigParam[0] = (byte)(DataTemp_Int16 & 0xFF);
  2587. ConfigParam[1] = (byte)(DataTemp_Int16 >> 8);
  2588. //横滚角零偏
  2589. DataTemp_Int16 = (short)(decimal.Parse(textBox_Config_Angle_Roll.Text) * 10);
  2590. ConfigParam[2] = (byte)(DataTemp_Int16 & 0xFF);
  2591. ConfigParam[3] = (byte)(DataTemp_Int16 >> 8);
  2592. //支持姿态传感器
  2593. if (comboBox_AttiSensor.Text == "支持")
  2594. ConfigParam[4] = 0xAA;
  2595. else
  2596. ConfigParam[4] = 0x55;
  2597. //前灯模式
  2598. if (comboBox_FrontLightMode.Text == "模式1")
  2599. {
  2600. ConfigParam[5] = 0x10;
  2601. }
  2602. else if (comboBox_FrontLightMode.Text == "模式2")
  2603. {
  2604. ConfigParam[5] = 0x20;
  2605. }
  2606. else
  2607. {
  2608. ConfigParam[5] = 0x20;
  2609. }
  2610. //尾灯模式
  2611. if (comboBox_TailLightMode.Text == "模式1")
  2612. {
  2613. ConfigParam[5] |= 0x01;
  2614. }
  2615. else if (comboBox_TailLightMode.Text == "模式2")
  2616. {
  2617. ConfigParam[5] |= 0x02;
  2618. }
  2619. else if (comboBox_TailLightMode.Text == "模式3")
  2620. {
  2621. ConfigParam[5] |= 0x03;
  2622. }
  2623. else if (comboBox_TailLightMode.Text == "模式4")
  2624. {
  2625. ConfigParam[5] |= 0x04;
  2626. }
  2627. else if (comboBox_TailLightMode.Text == "模式5")
  2628. {
  2629. ConfigParam[5] |= 0x05;
  2630. }
  2631. else if (comboBox_TailLightMode.Text == "模式6")
  2632. {
  2633. ConfigParam[5] |= 0x06;
  2634. }
  2635. else
  2636. ConfigParam[5] |= 0x02;
  2637. //前灯电压
  2638. if (comboBox_FrontLigthVol.Text == "6V")
  2639. ConfigParam[6] = 0x06;
  2640. else if (comboBox_FrontLigthVol.Text == "12V")
  2641. ConfigParam[6] = 0x0C;
  2642. else if (comboBox_FrontLigthVol.Text == "随电池")
  2643. ConfigParam[6] = 0xFF;
  2644. else
  2645. ConfigParam[6] = 0x00;
  2646. //尾灯电压
  2647. if (comboBox_TailLightVol.Text == "6V")
  2648. ConfigParam[7] = 0x06;
  2649. else if (comboBox_TailLightVol.Text == "12V")
  2650. ConfigParam[7] = 0x0C;
  2651. else if (comboBox_TailLightVol.Text == "随电池")
  2652. ConfigParam[7] = 0xFF;
  2653. else
  2654. ConfigParam[7] = 0x00;
  2655. //超速报警阈值
  2656. DataTemp = (ushort)(decimal.Parse(textBox_Config_SpeedAlarm.Text) * 10);
  2657. ConfigParam[8] = (byte)(DataTemp & 0xFF);
  2658. ConfigParam[9] = (byte)(DataTemp >> 8);
  2659. //尾灯脉宽
  2660. DataTemp = Convert.ToUInt16(textBox_Config_Pulse.Text);
  2661. ConfigParam[10] = (byte)(DataTemp & 0xFF);
  2662. ConfigParam[11] = (byte)(DataTemp >> 8);
  2663. //尾灯周期
  2664. DataTemp = Convert.ToUInt16(textBox_Config_Period.Text);
  2665. ConfigParam[12] = (byte)(DataTemp & 0xFF);
  2666. ConfigParam[13] = (byte)(DataTemp >> 8);
  2667. //起始限速
  2668. DataTemp_Int16 = (short)(decimal.Parse(textBox_Config_SpeedlimitStart.Text) * 10);
  2669. ConfigParam[14] = (byte)(DataTemp_Int16 & 0xFF);
  2670. //结束限速
  2671. DataTemp_Int16 = (short)(decimal.Parse(textBox_Config_SpeedlimitStop.Text) * 10);
  2672. ConfigParam[15] = (byte)(DataTemp_Int16 & 0xFF);
  2673. //限速微调
  2674. ConfigParam[16] = (byte)Convert.ToInt16(textBox_Config_SpeedlimitAdj.Text);
  2675. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3120, ConfigParam);
  2676. }
  2677. private void button_Read_AttitudeAngle_Click(object sender, EventArgs e)
  2678. {
  2679. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2F00, null);
  2680. MC_AttitudeAngle_Clear();
  2681. }
  2682. private void button_Read_RidedInfo_Click(object sender, EventArgs e)
  2683. {
  2684. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2E00, null);
  2685. MC_RideInfo_Clear();
  2686. }
  2687. private void button_ReadRunLog_Click(object sender, EventArgs e)
  2688. {
  2689. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1E00, null);
  2690. MC_RunLog_Clear();
  2691. }
  2692. private void timer2_Tick(object sender, EventArgs e)
  2693. {
  2694. //定时显示系统时钟
  2695. label_DateTime.Text = DateTime.Now.ToString();
  2696. //定时读取存储器
  2697. if (checkBox_DebugAuto.Checked == true)
  2698. {
  2699. var Address = new byte[4];
  2700. UInt32 Address_Begin;
  2701. UInt32 Addres_End;
  2702. if (!serialPort1.IsOpen)
  2703. {
  2704. checkBox_DebugAuto.Checked = false;
  2705. return;
  2706. }
  2707. if (checkBox_DebugReset.Checked == true)
  2708. richTextBox_DebugInfo.Clear();
  2709. do
  2710. {
  2711. if ((textBox_DebugAddrBegin.Text == string.Empty) || (textBox_DebugAddrEnd.Text == string.Empty))
  2712. {
  2713. checkBox_DebugAuto.Checked = false;
  2714. MessageBox.Show("请输入起始和结束地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2715. continue;
  2716. }
  2717. else if ((textBox_DebugAddrBegin.Text.Length != 8) || (textBox_DebugAddrEnd.Text.Length != 8))
  2718. {
  2719. checkBox_DebugAuto.Checked = false;
  2720. MessageBox.Show("请检查地址长度!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2721. continue;
  2722. }
  2723. else
  2724. {
  2725. Address = HexStringToBytes(textBox_DebugAddrBegin.Text, false);
  2726. Address_Begin = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
  2727. Address = HexStringToBytes(textBox_DebugAddrEnd.Text, false);
  2728. Addres_End = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
  2729. if (Addres_End < Address_Begin)
  2730. {
  2731. checkBox_DebugAuto.Checked = false;
  2732. MessageBox.Show("结束地址必须大于或等于起始地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2733. continue;
  2734. }
  2735. else
  2736. {
  2737. var SendByte = new byte[8];
  2738. SendByte[0] = (byte)(Address_Begin >> 24);
  2739. SendByte[1] = (byte)(Address_Begin >> 16);
  2740. SendByte[2] = (byte)(Address_Begin >> 8);
  2741. SendByte[3] = (byte)(Address_Begin);
  2742. SendByte[4] = (byte)(Addres_End >> 24);
  2743. SendByte[5] = (byte)(Addres_End >> 16);
  2744. SendByte[6] = (byte)(Addres_End >> 8);
  2745. SendByte[7] = (byte)(Addres_End);
  2746. if (!SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2D08, SendByte))
  2747. checkBox_DebugAuto.Checked = false;
  2748. }
  2749. }
  2750. } while (false);
  2751. }
  2752. }
  2753. private void checkBox_ReadTorqueData_CheckedChanged(object sender, EventArgs e)
  2754. {
  2755. if (checkBox_ReadTorqueData.Checked == true)
  2756. {
  2757. #if true
  2758. foreach (Control c in groupBox3.Controls)
  2759. {
  2760. if (c is TextBox)
  2761. {
  2762. if ((c != textBox_RunInfo_Range) && (c != textBox_RunInfo_ErrorCode))
  2763. {
  2764. if ((c.Text == string.Empty) || (c.Text == "---"))
  2765. {
  2766. timer1.Enabled = false;
  2767. MessageBox.Show("请确认进入配置模式!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2768. timer1.Enabled = true;
  2769. checkBox_Savedata.Checked = false;
  2770. checkBox_ReadTorqueData.Checked = false;
  2771. return;
  2772. }
  2773. else
  2774. {
  2775. LoginForm.Show();
  2776. }
  2777. }
  2778. }
  2779. }
  2780. #else
  2781. if (radioButton_WorkMode_Confg.Checked == false)
  2782. {
  2783. timer1.Enabled = false;
  2784. MessageBox.Show("请确认进入配置模式", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2785. timer1.Enabled = true;
  2786. checkBox_ReadTorqueData.Checked = false;
  2787. }
  2788. else
  2789. {
  2790. LoginForm.Show();
  2791. }
  2792. #endif
  2793. }
  2794. else
  2795. {
  2796. LoginForm.Hide();
  2797. }
  2798. }
  2799. private void checkBox_Savedata_CheckedChanged(object sender, EventArgs e)
  2800. {
  2801. if (checkBox_Savedata.Checked == true)
  2802. {
  2803. //判断是否显示运行信息
  2804. foreach (Control c in groupBox3.Controls)
  2805. {
  2806. if (c is TextBox)
  2807. {
  2808. if ((c != textBox_RunInfo_Range) && (c != textBox_RunInfo_ErrorCode))
  2809. {
  2810. if ((c.Text == string.Empty) || (c.Text == "---"))
  2811. {
  2812. timer1.Enabled = false;
  2813. MessageBox.Show("请设置电机进入配置模式!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2814. timer1.Enabled = true;
  2815. checkBox_Savedata.Checked = false;
  2816. return;
  2817. }
  2818. }
  2819. }
  2820. }
  2821. //判断是否查询版本信息
  2822. foreach (Control c in groupBox9.Controls)
  2823. {
  2824. if (c is TextBox)
  2825. {
  2826. if ((c.Text == string.Empty) || (c.Text == "---"))
  2827. {
  2828. if ((c == textBox_Mode) || (c == textBox_SN))
  2829. {
  2830. timer1.Enabled = false;
  2831. MessageBox.Show("请查询版本信息!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2832. timer1.Enabled = true;
  2833. checkBox_Savedata.Checked = false;
  2834. return;
  2835. }
  2836. }
  2837. }
  2838. }
  2839. AutoSaveFileName = textBox_Mode.Text + "_" + textBox_SN.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_实时数据" + ".txt"; ;
  2840. timer1.Enabled = false;
  2841. MessageBox.Show("开始记录数据!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2842. timer1.Enabled = true;
  2843. }
  2844. else
  2845. {
  2846. AutoSaveFileName = "";
  2847. timer1.Enabled = false;
  2848. MessageBox.Show("停止记录数据!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2849. timer1.Enabled = true;
  2850. }
  2851. }
  2852. private void DateAutoSave()
  2853. {
  2854. if (checkBox_Savedata.Checked == true)
  2855. {
  2856. if (AutoSaveFileName == string.Empty)
  2857. {
  2858. return;
  2859. }
  2860. string SaveData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace(" ", "_")+" ";
  2861. foreach (Control c in groupBox3.Controls)
  2862. {
  2863. if (c is TextBox)
  2864. {
  2865. SaveData += c.Text + " , ";
  2866. }
  2867. }
  2868. SaveData += "\r\n";
  2869. System.IO.File.AppendAllText(AutoSaveFileName, SaveData);//sb.ToString());
  2870. }
  2871. }
  2872. private void button_DataExport_Click(object sender, EventArgs e)
  2873. {
  2874. //增加控件查空处理,存在没有数据时提示
  2875. //判断是否查询力矩传感器
  2876. foreach (Control c in groupBox2.Controls)
  2877. {
  2878. if (c is TextBox)
  2879. {
  2880. if ((c != textBox_TorqueData) && (c != textBox_Torque_Stdev))
  2881. {
  2882. if ((c.Text == string.Empty) || (c.Text == "---"))
  2883. {
  2884. timer1.Enabled = false;
  2885. MessageBox.Show("请查询力矩传感器!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2886. timer1.Enabled = true;
  2887. return;
  2888. }
  2889. }
  2890. }
  2891. }
  2892. //判断是否显示运行信息
  2893. foreach (Control c in groupBox3.Controls)
  2894. {
  2895. if (c is TextBox)
  2896. {
  2897. if ((c != textBox_RunInfo_Range) && (c != textBox_RunInfo_ErrorCode))
  2898. {
  2899. if ((c.Text == string.Empty) || (c.Text == "---"))
  2900. {
  2901. timer1.Enabled = false;
  2902. MessageBox.Show("请设置电机进入配置模式!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2903. timer1.Enabled = true;
  2904. return;
  2905. }
  2906. }
  2907. }
  2908. }
  2909. //判断是否查询用户参数1
  2910. foreach (Control c in groupBox10.Controls)
  2911. {
  2912. if (c is TextBox)
  2913. {
  2914. if (c.Text == string.Empty)
  2915. {
  2916. timer1.Enabled = false;
  2917. MessageBox.Show("请查询用户参数1!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2918. timer1.Enabled = true;
  2919. return;
  2920. }
  2921. }
  2922. }
  2923. //判断是否查询用户参数2
  2924. foreach (Control c in groupBox4.Controls)
  2925. {
  2926. if (c is TextBox)
  2927. {
  2928. if (c.Text == string.Empty)
  2929. {
  2930. timer1.Enabled = false;
  2931. MessageBox.Show("请查询用户参数2!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2932. timer1.Enabled = true;
  2933. return;
  2934. }
  2935. }
  2936. }
  2937. #if false
  2938. //判断是否显示骑行信息
  2939. foreach (Control c in groupBox12.Controls)
  2940. {
  2941. if (c is TextBox)
  2942. {
  2943. if ((c.Text == string.Empty) || (c.Text == "---"))
  2944. {
  2945. timer1.Enabled = false;
  2946. MessageBox.Show("请自动查询骑行信息!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2947. timer1.Enabled = true;
  2948. return;
  2949. }
  2950. }
  2951. }
  2952. #endif
  2953. #if false
  2954. //判断是否查询姿态信息
  2955. foreach (Control c in groupBox13.Controls)
  2956. {
  2957. if (c is TextBox)
  2958. {
  2959. if ((c.Text == string.Empty) || (c.Text == "---"))
  2960. {
  2961. timer1.Enabled = false;
  2962. MessageBox.Show("请自动查询姿态信息!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2963. timer1.Enabled = true;
  2964. return;
  2965. }
  2966. }
  2967. }
  2968. #endif
  2969. //判断是否查询马达参数信息
  2970. foreach (Control c in groupBox11.Controls)
  2971. {
  2972. if (c is TextBox)
  2973. {
  2974. if ((c.Text == string.Empty) || (c.Text == "---"))
  2975. {
  2976. timer1.Enabled = false;
  2977. MessageBox.Show("请查询马达参数!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2978. timer1.Enabled = true ;
  2979. return;
  2980. }
  2981. }
  2982. }
  2983. //判断是否查询版本信息
  2984. foreach (Control c in groupBox9.Controls)
  2985. {
  2986. if (c is TextBox)
  2987. {
  2988. if ((c.Text == string.Empty) || (c.Text == "---"))
  2989. {
  2990. if (c == textBox_TE_FW)//忽略TE版本和软件标识
  2991. {
  2992. continue;
  2993. }
  2994. else
  2995. {
  2996. timer1.Enabled = false;
  2997. MessageBox.Show("请查询版本信息!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2998. timer1.Enabled = true;
  2999. return;
  3000. }
  3001. }
  3002. }
  3003. }
  3004. //判断是否查询历史信息
  3005. foreach (Control c in groupBox14.Controls)
  3006. {
  3007. if (c is TextBox)
  3008. {
  3009. if ((c.Text == string.Empty) || (c.Text == "---"))
  3010. {
  3011. timer1.Enabled = false;
  3012. MessageBox.Show("请查询历史信息!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  3013. timer1.Enabled = true;
  3014. return;
  3015. }
  3016. }
  3017. }
  3018. //...
  3019. Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap
  3020. Graphics g = Graphics.FromImage(bit);
  3021. g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片
  3022. SaveFileDialog sf = new SaveFileDialog();
  3023. sf.Title = "数据保存";
  3024. sf.FileName = textBox_Mode.Text + "_" + textBox_SN.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_页面存储" + ".png";
  3025. sf.Filter = "图片|*.png";
  3026. if (sf.ShowDialog() == DialogResult.OK)
  3027. {
  3028. bit.Save(sf.FileName);//默认保存格式为PNG,保存成jpg格式质量不是很好
  3029. }
  3030. }
  3031. private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  3032. {
  3033. }
  3034. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  3035. {
  3036. timer1.Enabled = false;
  3037. DialogResult result = MessageBox.Show("确定关闭吗!", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
  3038. if (result == DialogResult.OK)
  3039. {
  3040. if (serialPort1.IsOpen == true)
  3041. {
  3042. //发送指令退出配置模式
  3043. var RunMode = new byte[1];
  3044. RunMode[0] = 0x00;
  3045. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, RunMode);
  3046. serialPort1.Close();
  3047. myCapture.Dispose();
  3048. }
  3049. e.Cancel = false; //点击OK
  3050. }
  3051. else
  3052. {
  3053. e.Cancel = true;
  3054. timer1.Enabled = true;
  3055. }
  3056. }
  3057. private void trackBar_MotorSpeedSet_Scroll(object sender, EventArgs e)
  3058. {
  3059. var Data = new byte[1];
  3060. label_MotorSpeedSet.Text = Convert.ToString(trackBar_MotorSpeedSet.Value) + "%";
  3061. Data[0] = (byte)trackBar_MotorSpeedSet.Value;
  3062. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2C01, Data);
  3063. }
  3064. private void button_Recovery_Click(object sender, EventArgs e)
  3065. {
  3066. var Code = new byte[8];
  3067. Code[0] = (byte)'R';
  3068. Code[1] = (byte)'E';
  3069. Code[2] = (byte)'C';
  3070. Code[3] = (byte)'O';
  3071. Code[4] = (byte)'V';
  3072. Code[5] = (byte)'E';
  3073. Code[6] = (byte)'R';
  3074. Code[7] = (byte)'Y';
  3075. timer1.Enabled = false;
  3076. if (MessageBox.Show("参数还原后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  3077. {
  3078. if (MessageBox.Show("请再次确认", "确认还原?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  3079. {
  3080. if (MessageBox.Show("请最后确认", "确认还原?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  3081. {
  3082. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2708, Code);
  3083. }
  3084. }
  3085. }
  3086. timer1.Enabled = true;
  3087. }
  3088. #region 字符串转换为字节流
  3089. public static byte[] HexStringToBytes(string hexStr, bool Flag_Space)
  3090. {
  3091. if (string.IsNullOrEmpty(hexStr))
  3092. {
  3093. return new byte[0];
  3094. }
  3095. if (hexStr.StartsWith("0x"))
  3096. {
  3097. hexStr = hexStr.Remove(0, 2);
  3098. }
  3099. var count = hexStr.Length;
  3100. var byteCount = 0;
  3101. if (Flag_Space == true)//相邻字节含空格
  3102. {
  3103. if ((count % 3) == 0)//最后字节含空格
  3104. {
  3105. byteCount = count / 3;
  3106. }
  3107. else if ((count % 3) == 2)//最后字节不含空格
  3108. {
  3109. byteCount = (count + 1) / 3;
  3110. }
  3111. else//数据无效
  3112. {
  3113. MessageBox.Show("相邻字节请插入空格!", "提示");
  3114. }
  3115. var result = new byte[byteCount];
  3116. for (int ii = 0; ii < byteCount; ++ii)
  3117. {
  3118. var tempBytes = Byte.Parse(hexStr.Substring(3 * ii, 2), System.Globalization.NumberStyles.HexNumber);
  3119. result[ii] = tempBytes;
  3120. }
  3121. return result;
  3122. }
  3123. else if (Flag_Space == false)//相邻字节不含空格
  3124. {
  3125. if (count % 2 == 1)
  3126. {
  3127. MessageBox.Show("请删除相邻字节之间空格!", "提示");
  3128. }
  3129. byteCount = count / 2;
  3130. var result = new byte[byteCount];
  3131. for (int ii = 0; ii < byteCount; ++ii)
  3132. {
  3133. var tempBytes = Byte.Parse(hexStr.Substring(2 * ii, 2), System.Globalization.NumberStyles.HexNumber);
  3134. result[ii] = tempBytes;
  3135. }
  3136. return result;
  3137. }
  3138. else
  3139. {
  3140. return new byte[0];
  3141. }
  3142. }
  3143. #endregion
  3144. #region 故障日志结构体定义
  3145. //故障日志记录信息 60 bytes,1K空间可以保存最近16条故障记录
  3146. [StructLayout(LayoutKind.Sequential)]
  3147. public struct MC_ErrorLogSaveInfo_Struct_t
  3148. {
  3149. public UInt16 Error_Index; //故障列表索引 2 bytes, 地址偏移 0
  3150. public UInt16 NotesInfo1; //备注信息1,地址偏移 2
  3151. public UInt16 NotesInfo2; //备注信息2,地址偏移 4
  3152. public UInt16 NotesInfo3; //备注信息3,地址偏移 6
  3153. public UInt32 ErrorCode; //故障码 4 bytes, 地址偏移 8
  3154. public UInt32 RunTime; //运行时间 4bytes, 地址偏移 12
  3155. //电机运行信息,占用空间32bytes
  3156. [StructLayout(LayoutKind.Sequential)]
  3157. public struct MC_RunInfo_Struct_t
  3158. {
  3159. public ushort BikeSpeed; //车速 0.1km/h,地址偏移0
  3160. public ushort MotorSpeed; //输出转速 1rpm,地址偏移2
  3161. public ushort Power; //电功率 1W,地址偏移4
  3162. public ushort BusVoltage; //母线电压 1mV,地址偏移6
  3163. public ushort BusCurrent; //母线电流 1mA,地址偏移8
  3164. public byte Cadence; //踏频 1rpm,地址偏移10
  3165. public byte Torque; //踩踏力矩 1Nm,地址偏移11
  3166. public byte CadenceDir; //踩踏方向 0-正,1-反,2-停止,地址偏移12
  3167. public byte GearSt; //助力档位,地址偏移13
  3168. public byte LightSwitch; //灯开关 0xF0-关,0xF1-开,地址偏移14
  3169. public byte SOC; //剩余电量 1%,地址偏移15
  3170. public ushort RemainDistance; //续航里程 1km,地址偏移16
  3171. public ushort ODO_Km; //总里程 1km,地址偏移18
  3172. public byte PowerPerKm; //平均功耗 0.01Ah/km ,地址偏移20
  3173. public byte T_PCB; //PCB温度 +40℃,地址偏移21
  3174. public byte T_Coil; //绕组温度 +40℃,地址偏移22
  3175. public byte T_MCU; //MCU温度 +40℃,地址偏移23
  3176. public ushort Ride_Km; //开机后骑行里程 0.1km, 地址偏移24
  3177. public ushort Ride_Time; //开机后骑行时间 1s,地址偏移26
  3178. public UInt32 RS; //预留 4bytes
  3179. };
  3180. [MarshalAs(UnmanagedType.Struct)]
  3181. public MC_RunInfo_Struct_t RunInfo; //运行信息 32bytes, 地址偏移 16
  3182. //姿态传感器数值
  3183. [StructLayout(LayoutKind.Sequential)]
  3184. public struct MC_AttitudeAngle_Struct_t
  3185. {
  3186. public Int16 Angle_Pitch_Absolute; //俯仰角绝对角度
  3187. public Int16 Angle_Roll_Absolute; //横滚角绝对角度
  3188. public Int16 Angle_Pitch_Relative; //俯仰角相对角度
  3189. public Int16 Angle_Roll_Relative; //横滚角相对角度
  3190. };
  3191. [MarshalAs(UnmanagedType.Struct)]
  3192. public MC_AttitudeAngle_Struct_t AttitudeAngle; //车辆姿态角度 8bytes, 地址偏移 48
  3193. //电机控制参数
  3194. [StructLayout(LayoutKind.Sequential)]
  3195. public struct MC_CalParam_Struct_t
  3196. {
  3197. public byte AssistRunMode; //助力模式
  3198. public Int16 Ref_Torque; //力矩模式输入FOC值
  3199. public Int16 Ref_Speed; //速度模式输入FOC值
  3200. public byte Foc_Flag; //FCO运算标志
  3201. };
  3202. [MarshalAs(UnmanagedType.Struct)]
  3203. public MC_CalParam_Struct_t MC_CalParam; //马达控制参数 8bytes , 地址偏移 56
  3204. };
  3205. #endregion
  3206. public static class MyConverter
  3207. {
  3208. /// <summary>
  3209. /// 由结构体转换为byte数组
  3210. /// </summary>
  3211. public static byte[] StructureToByte<T>(T structure)
  3212. {
  3213. int size = Marshal.SizeOf(typeof(T));
  3214. byte[] buffer = new byte[size];
  3215. IntPtr bufferIntPtr = Marshal.AllocHGlobal(size);
  3216. try
  3217. {
  3218. Marshal.StructureToPtr(structure, bufferIntPtr, true);
  3219. Marshal.Copy(bufferIntPtr, buffer, 0, size);
  3220. }
  3221. finally
  3222. {
  3223. Marshal.FreeHGlobal(bufferIntPtr);
  3224. }
  3225. return buffer;
  3226. }
  3227. /// <summary>
  3228. /// 由byte数组转换为结构体
  3229. /// </summary>
  3230. public static T ByteToStructure<T>(byte[] dataBuffer, ushort StarIndex)
  3231. {
  3232. object structure = null;
  3233. int size = Marshal.SizeOf(typeof(T));
  3234. IntPtr allocIntPtr = Marshal.AllocHGlobal(size);
  3235. try
  3236. {
  3237. Marshal.Copy(dataBuffer, StarIndex, allocIntPtr, size);
  3238. structure = Marshal.PtrToStructure(allocIntPtr, typeof(T));
  3239. }
  3240. finally
  3241. {
  3242. Marshal.FreeHGlobal(allocIntPtr);
  3243. }
  3244. return (T)structure;
  3245. }
  3246. }
  3247. private void button_ReadErrorLog_Click(object sender, EventArgs e)
  3248. {
  3249. var Address = new byte[4];
  3250. UInt32 Address_Begin;
  3251. UInt32 Addres_End;
  3252. string textBox_Address_Begin = "0801F800";
  3253. string textBox_Address_End = "0801FBFF";
  3254. if (!serialPort1.IsOpen)
  3255. {
  3256. timer1.Enabled = false;
  3257. MessageBox.Show("串口未连接");
  3258. timer1.Enabled = true;
  3259. return;
  3260. }
  3261. if ((textBox_Address_Begin == string.Empty) || (textBox_Address_End == string.Empty))
  3262. {
  3263. timer1.Enabled = false;
  3264. MessageBox.Show("请输入地址!", "提示");
  3265. timer1.Enabled = true;
  3266. return;
  3267. }
  3268. else
  3269. {
  3270. Address = HexStringToBytes(textBox_Address_Begin, false);
  3271. Address_Begin = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
  3272. Address = HexStringToBytes(textBox_Address_End, false);
  3273. Addres_End = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
  3274. if (Addres_End <= Address_Begin)
  3275. {
  3276. timer1.Enabled = false;
  3277. MessageBox.Show("结束地址应大于起始地址!", "提示");
  3278. timer1.Enabled = true;
  3279. return;
  3280. }
  3281. else if (((Addres_End - Address_Begin + 1) % 128) != 0) //
  3282. {
  3283. timer1.Enabled = false;
  3284. MessageBox.Show("(结束地址 - 起始地址 + 1)应为128整数倍!", "提示");
  3285. timer1.Enabled = true;
  3286. return;
  3287. }
  3288. else
  3289. {
  3290. button_ReadErrorLog.Text = "读取中";
  3291. button_ReadErrorLog.Enabled = false;
  3292. //设置保存路经
  3293. SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  3294. saveFileDialog1.Title = "设置保存路经";
  3295. saveFileDialog1.RestoreDirectory = true;
  3296. saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
  3297. if (((textBox_Mode.Text == string.Empty) || (textBox_Mode.Text == "---")) && ((textBox_SN.Text == string.Empty) || (textBox_SN.Text == "---")))
  3298. {
  3299. timer1.Enabled = false;
  3300. MessageBox.Show("未查询电机版本信息!", "提示");
  3301. timer1.Enabled = true;
  3302. saveFileDialog1.FileName = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_故障日志"; ;
  3303. }
  3304. else
  3305. {
  3306. saveFileDialog1.FileName = textBox_Mode.Text + "_" + textBox_SN.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_故障日志";
  3307. }
  3308. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  3309. {
  3310. ErrorLogSaveFileName = saveFileDialog1.FileName.ToString();
  3311. }
  3312. else
  3313. {
  3314. button_ReadErrorLog.Enabled = true;
  3315. button_ReadErrorLog.Text = "读取";
  3316. return;
  3317. }
  3318. string SaveInfo = "读取时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n\r\n";
  3319. SaveInfo += "电机型号:" + textBox_Mode.Text + "\r\n";
  3320. SaveInfo += "序列号:" + textBox_SN.Text + "\r\n";
  3321. SaveInfo += "硬件版本:" + textBox_HW.Text + "\r\n";
  3322. SaveInfo += "软件版本:" + textBox_FW.Text + "\r\n";
  3323. SaveInfo += "TE软件版本:" + textBox_TE_FW.Text + "\r\n\r\n";
  3324. System.IO.File.WriteAllText(ErrorLogSaveFileName, SaveInfo + "无日志记录");
  3325. ErrorLogSaveFlag = false;
  3326. UInt32 ReadCount = 0;
  3327. ReadCount = (Addres_End - Address_Begin + 1) / 128;
  3328. for (ushort i = 0; i < ReadCount; i++)
  3329. {
  3330. var SendByte = new byte[8];
  3331. UInt32 Address_Temp_Begin;
  3332. UInt32 Address_Temp_End;
  3333. Address_Temp_Begin = Address_Begin + 128 * (UInt32)i;
  3334. SendByte[0] = (byte)(Address_Temp_Begin >> 24);
  3335. SendByte[1] = (byte)(Address_Temp_Begin >> 16);
  3336. SendByte[2] = (byte)(Address_Temp_Begin >> 8);
  3337. SendByte[3] = (byte)(Address_Temp_Begin);
  3338. Address_Temp_End = Address_Temp_Begin + 127;
  3339. SendByte[4] = (byte)(Address_Temp_End >> 24);
  3340. SendByte[5] = (byte)(Address_Temp_End >> 16);
  3341. SendByte[6] = (byte)(Address_Temp_End >> 8);
  3342. SendByte[7] = (byte)(Address_Temp_End);
  3343. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2D08, SendByte);
  3344. Thread.Sleep(200);
  3345. }
  3346. timer1.Enabled = false;
  3347. MessageBox.Show("读取完成,请查看日志!", "提示");
  3348. timer1.Enabled = true;
  3349. button_ReadErrorLog.Enabled = true;
  3350. button_ReadErrorLog.Text = "读取";
  3351. }
  3352. }
  3353. }
  3354. private void ErrorLogDateSave(MC_ErrorLogSaveInfo_Struct_t Log1, MC_ErrorLogSaveInfo_Struct_t Log2)
  3355. {
  3356. if (ErrorLogSaveFileName.Substring(ErrorLogSaveFileName.Length - 4, 4) != ".txt")
  3357. {
  3358. timer1.Enabled = false;
  3359. MessageBox.Show("日志存储失败!", "提示");
  3360. timer1.Enabled = true;
  3361. return;
  3362. }
  3363. string SaveData = "";
  3364. //Log1
  3365. if (Log1.Error_Index != 0xFFFF)
  3366. {
  3367. if (ErrorLogSaveFlag == false)
  3368. {
  3369. ErrorLogSaveFlag = true;
  3370. string SaveInfo = "读取时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n\r\n";
  3371. SaveInfo += "电机型号:" + textBox_Mode.Text + "\r\n";
  3372. SaveInfo += "序列号:" + textBox_SN.Text + "\r\n";
  3373. SaveInfo += "硬件版本:" + textBox_HW.Text + "\r\n";
  3374. SaveInfo += "软件版本:" + textBox_FW.Text + "\r\n";
  3375. SaveInfo += "TE软件版本:" + textBox_TE_FW.Text + "\r\n\r\n";
  3376. System.IO.File.WriteAllText(ErrorLogSaveFileName, SaveInfo);
  3377. }
  3378. SaveData += "故障序号:" + Convert.ToString(Log1.Error_Index) + ", ";
  3379. SaveData += "备注1:" + Convert.ToString(Log1.NotesInfo1) + ", ";
  3380. SaveData += "备注2:" + Convert.ToString(Log1.NotesInfo2) + ", ";
  3381. SaveData += "备注3:" + Convert.ToString(Log1.NotesInfo3) + ", ";
  3382. SaveData += "故障码:" + "0x" + Convert.ToString(Log1.ErrorCode, 16).PadLeft(8, '0').ToUpper() + ", ";
  3383. SaveData += "运行时间:" + Convert.ToString(Log1.RunTime) + "min" + ", ";
  3384. SaveData += "车速:" + ((float)Log1.RunInfo.BikeSpeed / 10f).ToString("0.0") + "km/h" + ", ";
  3385. SaveData += "电机转速:" + Convert.ToString(Log1.RunInfo.MotorSpeed) + "rpm" + ", ";
  3386. SaveData += "功率:" + Convert.ToString(Log1.RunInfo.Power * 2) + "w" + ", ";
  3387. SaveData += "电压:" + Convert.ToString(Log1.RunInfo.BusVoltage) + "mV" + ", ";
  3388. SaveData += "电流:" + Convert.ToString(Log1.RunInfo.BusCurrent) + "mA" + ", ";
  3389. SaveData += "踏频:" + Convert.ToString(Log1.RunInfo.Cadence) + "rpm" + ", ";
  3390. SaveData += "力矩:" + Convert.ToString(Log1.RunInfo.Torque) + "N.m" + ", ";
  3391. SaveData += "方向:" + ((Log1.RunInfo.CadenceDir == 0) ? "正" : ((Log1.RunInfo.CadenceDir == 1) ? "反" : "停止")) + ", ";
  3392. SaveData += "档位:" + "0x" + Convert.ToString(Log1.RunInfo.GearSt, 16).PadLeft(2, '0').ToUpper() + ", ";
  3393. SaveData += "灯开关:" + ((Log1.RunInfo.LightSwitch == 0xF1) ? "开" : "关") + ", ";
  3394. SaveData += "电量:" + Convert.ToString(Log1.RunInfo.SOC) + "%" + ", ";
  3395. if ((Log1.RunInfo.RemainDistance == 0xEEEE) || (Log1.RunInfo.RemainDistance == 0xFFFF))
  3396. {
  3397. SaveData += "续航:" + "---km" + ", ";
  3398. }
  3399. else
  3400. {
  3401. SaveData += "续航:" + Convert.ToString(Log1.RunInfo.RemainDistance) + "km" + ", ";
  3402. }
  3403. SaveData += "总里程:" + Convert.ToString(Log1.RunInfo.ODO_Km) + "km" + ", ";
  3404. SaveData += "功耗:" + Convert.ToString(Log1.RunInfo.PowerPerKm * 10) + "mAh/km" + ", ";
  3405. SaveData += "PCB温度:" + Convert.ToString((int)(Log1.RunInfo.T_PCB - 40)) + "DegreeC" + ", ";
  3406. SaveData += "绕组温度:" + Convert.ToString((int)(Log1.RunInfo.T_Coil - 40)) + "DegreeC" + ", ";
  3407. SaveData += "MCU温度:" + Convert.ToString((int)(Log1.RunInfo.T_MCU - 40)) + "DegreeC" + ", ";
  3408. SaveData += "单次里程:" + Convert.ToString((float)Log1.RunInfo.Ride_Km / 10f) + "km" + ", ";
  3409. SaveData += "单次时间:" + Convert.ToString(Log1.RunInfo.Ride_Time) + "s" + ", ";
  3410. SaveData += "俯仰绝对角度:" + ((float)Log1.AttitudeAngle.Angle_Pitch_Absolute / 10f).ToString("0.0") + "Degree" + ", ";
  3411. SaveData += "横滚绝对角度:" + ((float)Log1.AttitudeAngle.Angle_Roll_Absolute / 10f).ToString("0.0") + "Degree" + ", ";
  3412. SaveData += "俯仰相对角度:" + ((float)Log1.AttitudeAngle.Angle_Pitch_Relative / 10f).ToString("0.0") + "Degree" + ", ";
  3413. SaveData += "横滚相对角度:" + ((float)Log1.AttitudeAngle.Angle_Roll_Relative / 10f).ToString("0.0") + "Degree" + ", ";
  3414. SaveData += "运行模式:" + Convert.ToString(Log1.MC_CalParam.AssistRunMode) + ", ";
  3415. SaveData += "力矩环给定:" + Convert.ToString(Log1.MC_CalParam.Ref_Torque) + ", ";
  3416. SaveData += "速度环给定:" + Convert.ToString(Log1.MC_CalParam.Ref_Speed) + ", ";
  3417. SaveData += "FOC_Flag:" + Convert.ToString(Log1.MC_CalParam.Foc_Flag) + ", ";
  3418. SaveData += "\r\n\r\n";
  3419. System.IO.File.AppendAllText(ErrorLogSaveFileName, SaveData);
  3420. SaveData = "";
  3421. }
  3422. //Log2
  3423. if (Log2.Error_Index != 0xFFFF)
  3424. {
  3425. if (ErrorLogSaveFlag == false)
  3426. {
  3427. ErrorLogSaveFlag = true;
  3428. string SaveInfo = "读取时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n\r\n";
  3429. SaveInfo += "电机型号:" + textBox_Mode.Text + "\r\n";
  3430. SaveInfo += "序列号:" + textBox_SN.Text + "\r\n";
  3431. SaveInfo += "硬件版本:" + textBox_HW.Text + "\r\n";
  3432. SaveInfo += "软件版本:" + textBox_FW.Text + "\r\n";
  3433. SaveInfo += "TE软件版本:" + textBox_TE_FW.Text + "\r\n\r\n";
  3434. System.IO.File.WriteAllText(ErrorLogSaveFileName, SaveInfo);
  3435. }
  3436. SaveData += "故障序号:" + Convert.ToString(Log2.Error_Index) + ", ";
  3437. SaveData += "备注1:" + Convert.ToString(Log2.NotesInfo1) + ", ";
  3438. SaveData += "备注2:" + Convert.ToString(Log2.NotesInfo2) + ", ";
  3439. SaveData += "备注3:" + Convert.ToString(Log2.NotesInfo3) + ", ";
  3440. SaveData += "故障码:" + "0x" + Convert.ToString(Log2.ErrorCode, 16).PadLeft(8, '0').ToUpper() + ", ";
  3441. SaveData += "运行时间:" + Convert.ToString(Log2.RunTime) + "min" + ", ";
  3442. SaveData += "车速:" + ((float)Log2.RunInfo.BikeSpeed / 10f).ToString("0.0") + "km/h" + ", ";
  3443. SaveData += "电机转速:" + Convert.ToString(Log2.RunInfo.MotorSpeed) + "rpm" + ", ";
  3444. SaveData += "功率:" + Convert.ToString(Log2.RunInfo.Power * 2) + "w" + ", ";
  3445. SaveData += "电压:" + Convert.ToString(Log2.RunInfo.BusVoltage) + "mV" + ", ";
  3446. SaveData += "电流:" + Convert.ToString(Log2.RunInfo.BusCurrent) + "mA" + ", ";
  3447. SaveData += "踏频:" + Convert.ToString(Log2.RunInfo.Cadence) + "rpm" + ", ";
  3448. SaveData += "力矩:" + Convert.ToString(Log2.RunInfo.Torque) + "N.m" + ", ";
  3449. SaveData += "方向:" + ((Log2.RunInfo.CadenceDir == 0) ? "正" : ((Log2.RunInfo.CadenceDir == 1) ? "反" : "停止")) + ", ";
  3450. SaveData += "档位:" + "0x" + Convert.ToString(Log2.RunInfo.GearSt, 16).PadLeft(2, '0').ToUpper() + ", ";
  3451. SaveData += "灯开关:" + ((Log2.RunInfo.LightSwitch == 0xF1) ? "开" : "关") + ", ";
  3452. SaveData += "电量:" + Convert.ToString(Log2.RunInfo.SOC) + "%" + ", ";
  3453. if ((Log1.RunInfo.RemainDistance == 0xEEEE) || (Log1.RunInfo.RemainDistance == 0xFFFF))
  3454. {
  3455. SaveData += "续航:" + "---km" + ", ";
  3456. }
  3457. else
  3458. {
  3459. SaveData += "续航:" + Convert.ToString(Log1.RunInfo.RemainDistance) + "km" + ", ";
  3460. }
  3461. SaveData += "总里程:" + Convert.ToString(Log2.RunInfo.ODO_Km) + "km" + ", ";
  3462. SaveData += "功耗:" + Convert.ToString(Log2.RunInfo.PowerPerKm * 10) + "mAh/km" + ", ";
  3463. SaveData += "PCB温度:" + Convert.ToString((int)(Log2.RunInfo.T_PCB - 40)) + "DegreeC" + ", ";
  3464. SaveData += "绕组温度:" + Convert.ToString((int)(Log2.RunInfo.T_Coil - 40)) + "DegreeC" + ", ";
  3465. SaveData += "MCU温度:" + Convert.ToString((int)(Log2.RunInfo.T_MCU - 40)) + "DegreeC" + ", ";
  3466. SaveData += "单次里程:" + Convert.ToString((float)Log2.RunInfo.Ride_Km / 10f) + "km" + ", ";
  3467. SaveData += "单次时间:" + Convert.ToString(Log2.RunInfo.Ride_Time) + "s" + ", ";
  3468. SaveData += "俯仰绝对角度:" + ((float)Log2.AttitudeAngle.Angle_Pitch_Absolute / 10f).ToString("0.0") + "Degree" + ", ";
  3469. SaveData += "横滚绝对角度:" + ((float)Log2.AttitudeAngle.Angle_Roll_Absolute / 10f).ToString("0.0") + "Degree" + ", ";
  3470. SaveData += "俯仰相对角度:" + ((float)Log2.AttitudeAngle.Angle_Pitch_Relative / 10f).ToString("0.0") + "Degree" + ", ";
  3471. SaveData += "横滚相对角度:" + ((float)Log2.AttitudeAngle.Angle_Roll_Relative / 10f).ToString("0.0") + "Degree" + ", ";
  3472. SaveData += "运行模式:" + Convert.ToString(Log2.MC_CalParam.AssistRunMode) + ", ";
  3473. SaveData += "力矩环给定:" + Convert.ToString(Log2.MC_CalParam.Ref_Torque) + ", ";
  3474. SaveData += "速度环给定:" + Convert.ToString(Log2.MC_CalParam.Ref_Speed) + ", ";
  3475. SaveData += "FOC_Flag:" + Convert.ToString(Log2.MC_CalParam.Foc_Flag) + ", ";
  3476. SaveData += "\r\n\r\n";
  3477. System.IO.File.AppendAllText(ErrorLogSaveFileName, SaveData);
  3478. SaveData = "";
  3479. }
  3480. }
  3481. private void button_ClearData_MouseHover(object sender, EventArgs e)
  3482. {
  3483. // 创建the ToolTip
  3484. ToolTip toolTip1 = new ToolTip();
  3485. // 设置显示样式
  3486. toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
  3487. toolTip1.InitialDelay = 50;//事件触发多久后出现提示
  3488. toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  3489. toolTip1.ShowAlways = true;//是否显示提示框
  3490. // 设置伴随的对象.
  3491. toolTip1.SetToolTip(this.button_ClearData, "清除电机内部所有数据,包括配置参数、历史记录等。");//设置提示按钮和提示内容
  3492. }
  3493. private void button_Recovery_MouseHover(object sender, EventArgs e)
  3494. {
  3495. // 创建the ToolTip
  3496. ToolTip toolTip1 = new ToolTip();
  3497. // 设置显示样式
  3498. toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
  3499. toolTip1.InitialDelay = 50;//事件触发多久后出现提示
  3500. toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  3501. toolTip1.ShowAlways = true;//是否显示提示框
  3502. // 设置伴随的对象.
  3503. toolTip1.SetToolTip(this.button_Recovery, "还原配置参数为初始值。");//设置提示按钮和提示内容
  3504. }
  3505. private void button_DataExport_MouseHover(object sender, EventArgs e)
  3506. {
  3507. // 创建the ToolTip
  3508. ToolTip toolTip1 = new ToolTip();
  3509. // 设置显示样式
  3510. toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
  3511. toolTip1.InitialDelay = 50;//事件触发多久后出现提示
  3512. toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  3513. toolTip1.ShowAlways = true;//是否显示提示框
  3514. // 设置伴随的对象.
  3515. toolTip1.SetToolTip(this.button_DataExport, "存储窗体页面,请先查询相关信息后操作。");//设置提示按钮和提示内容
  3516. }
  3517. private void label9_MouseHover(object sender, EventArgs e)
  3518. {
  3519. // 创建the ToolTip
  3520. ToolTip toolTip1 = new ToolTip();
  3521. // 设置显示样式
  3522. toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
  3523. toolTip1.InitialDelay = 50;//事件触发多久后出现提示
  3524. toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  3525. toolTip1.ShowAlways = true;//是否显示提示框
  3526. // 设置伴随的对象.
  3527. toolTip1.SetToolTip(this.label9, "双击自动计算启动值。");//设置提示按钮和提示内容
  3528. }
  3529. private void button_ReadErrorLog_MouseHover(object sender, EventArgs e)
  3530. {
  3531. // 创建the ToolTip
  3532. ToolTip toolTip1 = new ToolTip();
  3533. // 设置显示样式
  3534. toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
  3535. toolTip1.InitialDelay = 50;//事件触发多久后出现提示
  3536. toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  3537. toolTip1.ShowAlways = true;//是否显示提示框
  3538. // 设置伴随的对象.
  3539. toolTip1.SetToolTip(this.button_ReadErrorLog, "导出电机内部存储的故障日志,读取完后自动保存。");//设置提示按钮和提示内容
  3540. }
  3541. private void label119_MouseHover(object sender, EventArgs e)
  3542. {
  3543. // 创建the ToolTip
  3544. ToolTip toolTip1 = new ToolTip();
  3545. // 设置显示样式
  3546. toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
  3547. toolTip1.InitialDelay = 50;//事件触发多久后出现提示
  3548. toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  3549. toolTip1.ShowAlways = true;//是否显示提示框
  3550. // 设置伴随的对象.
  3551. toolTip1.SetToolTip(this.label119, "双击自动计算校正系数。");//设置提示按钮和提示内容
  3552. }
  3553. private void button_LogClear_Click(object sender, EventArgs e)
  3554. {
  3555. var Code = new byte[9];
  3556. Code[0] = (byte)'L';
  3557. Code[1] = (byte)'O';
  3558. Code[2] = (byte)'G';
  3559. Code[3] = (byte)' ';
  3560. Code[4] = (byte)'C';
  3561. Code[5] = (byte)'L';
  3562. Code[6] = (byte)'E';
  3563. Code[7] = (byte)'A';
  3564. Code[8] = (byte)'R';
  3565. timer1.Enabled = false;
  3566. if (MessageBox.Show("历史记录信息清除后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  3567. {
  3568. if (MessageBox.Show("请再次确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  3569. {
  3570. if (MessageBox.Show("请最后确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  3571. {
  3572. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3909, Code);
  3573. }
  3574. }
  3575. }
  3576. timer1.Enabled = true;
  3577. }
  3578. private void textBox_RunInfo_ErrorCode_MouseHover(object sender, EventArgs e)
  3579. {
  3580. // 创建the ToolTip
  3581. ToolTip toolTip1 = new ToolTip();
  3582. // 设置显示样式
  3583. toolTip1.AutoPopDelay = 30000;//提示信息的可见时间
  3584. toolTip1.InitialDelay = 50;//事件触发多久后出现提示
  3585. toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  3586. toolTip1.ShowAlways = true;//是否显示提示框
  3587. // 设置伴随的对象.
  3588. string ErrorCode = this.textBox_RunInfo_ErrorCode.Text;
  3589. UInt32 Code = ErrorCode.Contains("H") ? Convert.ToUInt32(ErrorCode.Split('H')[0], 16) : 0;
  3590. toolTip1.SetToolTip(this.textBox_RunInfo_ErrorCode, ErrorInfoDisplay(Code));//设置故障内容显示
  3591. }
  3592. private void button_LogClear_MouseHover(object sender, EventArgs e)
  3593. {
  3594. // 创建the ToolTip
  3595. ToolTip toolTip1 = new ToolTip();
  3596. // 设置显示样式
  3597. toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
  3598. toolTip1.InitialDelay = 50;//事件触发多久后出现提示
  3599. toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  3600. toolTip1.ShowAlways = true;//是否显示提示框
  3601. // 设置伴随的对象.
  3602. toolTip1.SetToolTip(this.button_LogClear, "清除电机历史记录信息");//设置提示按钮和提示内容
  3603. }
  3604. private void button_ReadAll_Click(object sender, EventArgs e)
  3605. {
  3606. if (!serialPort1.IsOpen)
  3607. {
  3608. MessageBox.Show("串口未连接");
  3609. return;
  3610. }
  3611. //查询力矩传感器
  3612. var K = new byte[1];
  3613. textBox_Sensor1_Zero.Text = "---";
  3614. textBox_Sensor2_Zero.Text = "---";
  3615. textBox_Sensor3_Zero.Text = "---";
  3616. textBox_Sensor1_K.Text = "---";
  3617. textBox_Sensor2_K.Text = "---";
  3618. textBox_Sensor3_K.Text = "---";
  3619. //轮询零点
  3620. for (ushort i = 1; i <= 3; i++)
  3621. {
  3622. K[0] = (byte)i;
  3623. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3201, K);
  3624. }
  3625. //轮询校正系数
  3626. for (ushort i = 1; i <= 3; i++)
  3627. {
  3628. K[0] = (byte)i;
  3629. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3301, K);
  3630. }
  3631. textBox_Torque_Start.Text = "---";
  3632. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3500, null);
  3633. //查询用户参数1
  3634. MC_Config_Clear();
  3635. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1A00, null);
  3636. //查询用户参数2
  3637. MC_Config_Ext_Clear();
  3638. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3000, null);
  3639. //查询姿态传感器参数
  3640. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2F00, null);
  3641. MC_AttitudeAngle_Clear();
  3642. //查询马达参数
  3643. Motor_Config_Clear();
  3644. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1C00, null);
  3645. //查询版本信息
  3646. textBox_Mode.Text = "---";
  3647. textBox_SN.Text = "---";
  3648. textBox_HW.Text = "---";
  3649. textBox_FW.Text = "---";
  3650. textBox_Firmware_Special.Text = "---";
  3651. Class_Motor_Ver.Mode = "---";
  3652. Class_Motor_Ver.SN = "---";
  3653. Class_Motor_Ver.HW = "---";
  3654. Class_Motor_Ver.FW = "---";
  3655. Class_Motor_Ver.Special = "---";
  3656. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
  3657. textBox_TE_FW.Text = "---";
  3658. SendCmd((ushort)0x651, (byte)0x11, (ushort)0x1200, null);
  3659. //查询历史信息
  3660. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1E00, null);
  3661. MC_RunLog_Clear();
  3662. //查询骑行历史
  3663. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2E00, null);
  3664. MC_RideInfo_Clear();
  3665. }
  3666. //双击时允许解锁写保护
  3667. private void label127_DoubleClick(object sender, EventArgs e)
  3668. {
  3669. WriteProtectUnlock = true;
  3670. }
  3671. //双击解锁写保护
  3672. private void label1_Welcom_DoubleClick(object sender, EventArgs e)
  3673. {
  3674. if (WriteProtectUnlock == false)
  3675. return;
  3676. button_ClearData.Enabled = true;
  3677. button_Recovery.Enabled = true;
  3678. button_Write_Config.Enabled = true;
  3679. button_Write_Config_Ext.Enabled = true;
  3680. button_Write_MotorConfig.Enabled = true;
  3681. button_Write_Mode.Enabled = true;
  3682. button_Write_SN.Enabled = true;
  3683. }
  3684. //读取指定地址的数据
  3685. private void button_DebugRead_Click(object sender, EventArgs e)
  3686. {
  3687. var Address = new byte[4];
  3688. UInt32 Address_Begin;
  3689. UInt32 Addres_End;
  3690. if (checkBox_DebugReset.Checked == true)
  3691. richTextBox_DebugInfo.Clear();
  3692. if ((textBox_DebugAddrBegin.Text == string.Empty) || (textBox_DebugAddrEnd.Text == string.Empty))
  3693. {
  3694. MessageBox.Show("请输入起始和结束地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  3695. return;
  3696. }
  3697. else if ((textBox_DebugAddrBegin.Text.Length != 8) || (textBox_DebugAddrEnd.Text.Length != 8))
  3698. {
  3699. MessageBox.Show("请检查地址长度!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  3700. return;
  3701. }
  3702. else
  3703. {
  3704. Address = HexStringToBytes(textBox_DebugAddrBegin.Text, false);
  3705. Address_Begin = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
  3706. Address = HexStringToBytes(textBox_DebugAddrEnd.Text, false);
  3707. Addres_End = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
  3708. if (Addres_End < Address_Begin)
  3709. {
  3710. MessageBox.Show("结束地址必须大于或等于起始地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  3711. return;
  3712. }
  3713. else
  3714. {
  3715. var SendByte = new byte[8];
  3716. SendByte[0] = (byte)(Address_Begin >> 24);
  3717. SendByte[1] = (byte)(Address_Begin >> 16);
  3718. SendByte[2] = (byte)(Address_Begin >> 8);
  3719. SendByte[3] = (byte)(Address_Begin);
  3720. SendByte[4] = (byte)(Addres_End >> 24);
  3721. SendByte[5] = (byte)(Addres_End >> 16);
  3722. SendByte[6] = (byte)(Addres_End >> 8);
  3723. SendByte[7] = (byte)(Addres_End);
  3724. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2D08, SendByte);
  3725. }
  3726. }
  3727. }
  3728. //选择单力矩
  3729. private void radioButton_SingleSensor_CheckedChanged(object sender, EventArgs e)
  3730. {
  3731. checkBox_Sensor2.Checked = true;
  3732. label5.Visible = false;
  3733. textBox_Sensor2_Avg.Visible = false;
  3734. textBox_Sensor2_Std.Visible = false;
  3735. textBox_Sensor2_Zero.Visible = false;
  3736. textBox_Sensor2_K.Visible = false;
  3737. checkBox_Sensor2.Visible = false;
  3738. checkBox_Sensor2.Checked = false;
  3739. label115.Visible = false;
  3740. textBox_Sensor3_Avg.Visible = false;
  3741. textBox_Sensor3_Std.Visible = false;
  3742. textBox_Sensor3_Zero.Visible = false;
  3743. textBox_Sensor3_K.Visible = false;
  3744. checkBox_Sensor3.Visible = false;
  3745. checkBox_Sensor3.Checked = false;
  3746. //this.BackColor = Color.White;
  3747. }
  3748. //选择三力矩
  3749. private void radioButton_ThreeSensor_CheckedChanged(object sender, EventArgs e)
  3750. {
  3751. checkBox_Sensor1.Checked = true;
  3752. label5.Visible = true;
  3753. textBox_Sensor2_Avg.Visible = true;
  3754. textBox_Sensor2_Std.Visible = true;
  3755. textBox_Sensor2_Zero.Visible = true;
  3756. textBox_Sensor2_K.Visible = true;
  3757. checkBox_Sensor2.Visible = true;
  3758. checkBox_Sensor2.Checked = true;
  3759. label115.Visible = true;
  3760. textBox_Sensor3_Avg.Visible = true;
  3761. textBox_Sensor3_Std.Visible = true;
  3762. textBox_Sensor3_Zero.Visible = true;
  3763. textBox_Sensor3_K.Visible = true;
  3764. checkBox_Sensor3.Visible = true;
  3765. checkBox_Sensor3.Checked = true;
  3766. }
  3767. }
  3768. }