Form1.cs 173 KB

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