Form1.cs 202 KB

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