Form1.cs 195 KB

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