Form1.cs 211 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Runtime.InteropServices;
  9. using System.Text.RegularExpressions;
  10. using System.Threading;
  11. using System.Windows.Forms;
  12. namespace Welling_Motor_Debug_Tool
  13. {
  14. public partial class Form1 : Form
  15. {
  16. #region 变量定义
  17. //串口实例
  18. Serial_Process mySerialProcess = new Serial_Process();
  19. string PortNumSave = "";
  20. string PortBaudrateSave = "";
  21. //FTP实例
  22. ftp myFtp = new ftp();
  23. //本地存储数据路径
  24. string LocalPath = "";
  25. string ConfigFileName = "C:\\Temp\\Config.ttcfg";
  26. //MC运行信息超时计数
  27. bool MC_RunInfo_Refresh = false;
  28. ushort MC_RunInfo_Refresh_Cnt = 0;
  29. //MC故障码超时计数
  30. bool MC_ErrorCode_Refresh = false;
  31. ushort MC_ErrorCode_Refresh_Cnt = 0;
  32. //配置参数实例
  33. Params myParams = new Params();
  34. //记录文件名称
  35. string RunInfoSaveFilename = "";
  36. //BMS运行信息超时计数
  37. bool BMS_RunInfo_Refresh = false;
  38. ushort BMS_RunInfo_Refresh_Cnt = 0;
  39. //生产模式关机指令反馈装
  40. bool PowerOffAckStatus = false;
  41. #endregion
  42. #region 故障日志结构体定义
  43. //故障日志记录信息 60 bytes,1K空间可以保存最近16条故障记录
  44. [StructLayout(LayoutKind.Sequential)]
  45. public struct MC_ErrorLogSaveInfo_Struct_t
  46. {
  47. public UInt16 Error_Index; //故障列表索引 2 bytes, 地址偏移 0
  48. public UInt16 NotesInfo1; //备注信息1,地址偏移 2
  49. public UInt16 NotesInfo2; //备注信息2,地址偏移 4
  50. public UInt16 NotesInfo3; //备注信息3,地址偏移 6
  51. public UInt32 ErrorCode; //故障码 4 bytes, 地址偏移 8
  52. public UInt32 RunTime; //运行时间 4bytes, 地址偏移 12
  53. //电机运行信息,占用空间32bytes
  54. [StructLayout(LayoutKind.Sequential)]
  55. public struct MC_RunInfo_Struct_t
  56. {
  57. public ushort BikeSpeed; //车速 0.1km/h,地址偏移0
  58. public ushort MotorSpeed; //输出转速 1rpm,地址偏移2
  59. public ushort Power; //电功率 1W,地址偏移4
  60. public ushort BusVoltage; //母线电压 1mV,地址偏移6
  61. public ushort BusCurrent; //母线电流 1mA,地址偏移8
  62. public byte Cadence; //踏频 1rpm,地址偏移10
  63. public byte Torque; //踩踏力矩 1Nm,地址偏移11
  64. public byte CadenceDir; //踩踏方向 0-正,1-反,2-停止,地址偏移12
  65. public byte GearSt; //助力档位,地址偏移13
  66. public byte LightSwitch; //灯开关 0xF0-关,0xF1-开,地址偏移14
  67. public byte SOC; //剩余电量 1%,地址偏移15
  68. public ushort RemainDistance; //续航里程 1km,地址偏移16
  69. public ushort Torque_AD; //力矩寄存器,地址偏移18
  70. public byte PowerPerKm; //平均功耗 0.01Ah/km ,地址偏移20
  71. public byte T_PCB; //PCB温度 +40℃,地址偏移21
  72. public byte T_Coil; //绕组温度 +40℃,地址偏移22
  73. public byte T_MCU; //MCU温度 +40℃,地址偏移23
  74. public ushort Ride_Km; //开机后骑行里程 0.1km, 地址偏移24
  75. public ushort Ride_Time; //开机后骑行时间 1s,地址偏移26
  76. public UInt32 RS; //预留 4bytes
  77. };
  78. [MarshalAs(UnmanagedType.Struct)]
  79. public MC_RunInfo_Struct_t RunInfo; //运行信息 32bytes, 地址偏移 16
  80. //控制变量
  81. public Int16 IqCurrent; //Pu, 地址偏移 48
  82. public Int16 IqVoltage; //Pu, 地址偏移 50
  83. public Int16 IdCurrent; //Pu, 地址偏移 52
  84. public Int16 IdVoltage; //Pu, 地址偏移 54
  85. //状态机
  86. public UInt16 FSM; //高到低依次表示系统状态机、二层状态机、电机状态机、助力状态机, 地址偏移 56
  87. //预留
  88. public UInt16 RS1;
  89. public UInt16 RS2;
  90. public UInt16 RS3;
  91. };
  92. #endregion
  93. public Form1()
  94. {
  95. InitializeComponent();
  96. //检查配置文件
  97. if (File.Exists(ConfigFileName)) //存在配置文件,导入配置信息
  98. {
  99. //打开文件
  100. StreamReader objReader = new StreamReader(ConfigFileName);
  101. string sLine = "";
  102. ArrayList array_CfgInfo = new ArrayList();
  103. array_CfgInfo.Clear();
  104. while (sLine != null)
  105. {
  106. sLine = objReader.ReadLine();
  107. array_CfgInfo.Add(sLine);
  108. }
  109. objReader.Close();
  110. //解析配置文件
  111. try
  112. {
  113. PortNumSave = array_CfgInfo[1].ToString().Split(':')[1];
  114. PortBaudrateSave = array_CfgInfo[2].ToString().Split(':')[1];
  115. 连接开机ToolStripMenuItem.Checked = (array_CfgInfo[3].ToString().Split(':')[1] == "True");
  116. 断开关机ToolStripMenuItem.Checked = (array_CfgInfo[4].ToString().Split(':')[1] == "True");
  117. 识别通讯盒ToolStripMenuItem.Checked = (array_CfgInfo[5].ToString().Split(':')[1] == "True");
  118. 写入存储ToolStripMenuItem.Checked = (array_CfgInfo[8].ToString().Split(':')[1] == "True");
  119. 允许ToolStripMenuItem.Checked = (array_CfgInfo[9].ToString().Split(':')[1] == "True");
  120. 不允许ToolStripMenuItem.Checked = (array_CfgInfo[9].ToString().Split(':')[1] == "False");
  121. toolStripTextBox_ServerIP.Text = array_CfgInfo[12].ToString().Split(':')[1];
  122. toolStripTextBox_ServerPort.Text = array_CfgInfo[13].ToString().Split(':')[1];
  123. toolStripTextBox_ServerUser.Text = array_CfgInfo[14].ToString().Split(':')[1];
  124. toolStripTextBox_ServerPasswd.Text = array_CfgInfo[15].ToString().Split(':')[1];
  125. toolStripTextBox_ServerPath.Text = array_CfgInfo[16].ToString().Split(':')[1];
  126. toolStripTextBox_MAC.Text= array_CfgInfo[19].ToString().Split(':')[1];
  127. toolStripTextBoxMACAddr.Text = array_CfgInfo[20].ToString().Split(':')[1];
  128. toolStripTextBox_MACDate.Text = DateTime.Now.ToString("yyyyMMdd");
  129. toolStripTextBox_MACPD.Text = array_CfgInfo[22].ToString().Split(':')[1];
  130. }
  131. catch (System.Exception)
  132. {
  133. timer_1s.Enabled = false;
  134. MessageBox.Show("参数格式错误,写入默认值!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  135. timer_1s.Enabled = true;
  136. }
  137. }
  138. else //不存在配置文件,生成文件写入默认值
  139. {
  140. ConfigFileSave(false, ConfigFileName);
  141. }
  142. //配置网络FTP服务器
  143. myFtp.FtpOption(toolStripTextBox_ServerIP.Text, toolStripTextBox_ServerPort.Text, toolStripTextBox_ServerUser.Text, toolStripTextBox_ServerPasswd.Text);
  144. //配置本地数据存储路径
  145. LocalPath = Directory.GetCurrentDirectory() + "\\" + toolStripTextBox_ServerPath.Text;
  146. //检查本地文件夹
  147. if (!Directory.Exists(LocalPath))
  148. Directory.CreateDirectory(LocalPath);
  149. //创建线程,定时检测网络连接状态
  150. Thread th = new Thread(NetworkCheck);
  151. th.IsBackground = true;
  152. th.Start();
  153. }
  154. /// <summary>
  155. /// 检查网络状态线程
  156. /// </summary>
  157. private void NetworkCheck()
  158. {
  159. //初始化第一次连接FTP服务器
  160. bool Result = myFtp.CheckFtp();
  161. this.Invoke((EventHandler)(delegate
  162. {
  163. if (Result == true)//服务器连接成功
  164. {
  165. label_Server_ComStatus.Text = "网络已连接";
  166. label_ServerStatus.BackColor = Color.Green;
  167. myFtp.IsNetConnected = true;
  168. }
  169. else//服务器连接失败
  170. {
  171. label_Server_ComStatus.Text = "网络断开";
  172. label_ServerStatus.BackColor = Color.Red;
  173. myFtp.IsNetConnected = false;
  174. if (允许ToolStripMenuItem.Checked == false) ///不允许离线使用,自动关闭
  175. {
  176. timer_1s.Enabled = false;
  177. MessageBox.Show("网络断开,将自动关闭!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  178. timer_1s.Enabled = true;
  179. this.Close();
  180. }
  181. else //离线使用
  182. {
  183. timer_1s.Enabled = false;
  184. MessageBox.Show("网络断开,离线使用!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  185. timer_1s.Enabled = true;
  186. }
  187. }
  188. }));
  189. //创建定时器,定时30s检查网络
  190. System.Timers.Timer timer_CheckNet = new System.Timers.Timer();
  191. timer_CheckNet.Enabled = true;
  192. timer_CheckNet.Interval = 30000;
  193. timer_CheckNet.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_CheckNet_Tick);
  194. timer_CheckNet.Start();
  195. while (true)
  196. {
  197. Thread.Sleep(1);
  198. }
  199. }
  200. /// <summary>
  201. /// 线程中定时任务,检查网络状态
  202. /// </summary>
  203. /// <param name="sender"></param>
  204. /// <param name="e"></param>
  205. private void timer_CheckNet_Tick(object sender, EventArgs e)
  206. {
  207. //连接FTP服务器
  208. bool Result = myFtp.CheckFtp();
  209. this.Invoke((EventHandler)(delegate
  210. {
  211. if (Result == true)//服务器连接成功
  212. {
  213. label_Server_ComStatus.Text = "网络已连接";
  214. label_ServerStatus.BackColor = Color.Green;
  215. myFtp.IsNetConnected = true;
  216. }
  217. else//服务器连接失败
  218. {
  219. label_Server_ComStatus.Text = "网络断开";
  220. label_ServerStatus.BackColor = Color.Red;
  221. myFtp.IsNetConnected = false;
  222. }
  223. }));
  224. }
  225. #region 非独占性延时函数
  226. public static void Delay_ms(int milliSecond)
  227. {
  228. int start = Environment.TickCount;
  229. while (Math.Abs(Environment.TickCount - start) < milliSecond)//毫秒
  230. {
  231. Application.DoEvents();
  232. }
  233. }
  234. #endregion
  235. private void Form1_Load(object sender, System.EventArgs e)
  236. {
  237. //页面初始化
  238. label_BuildTime.Text= "编译时间:"+ System.IO.File.GetLastWriteTime(this.GetType().Assembly.Location).ToString();
  239. //下拉控件初始值设定
  240. comboBox_WorkMode.SelectedIndex = 0;
  241. comboBox_GearSt.SelectedIndex = 0;
  242. comboBox_LightSwitch.SelectedIndex = 1;
  243. comboBox_OBC_SetGearST.SelectedIndex = 0;
  244. comboBox_OBC_LightSw.SelectedIndex = 1;
  245. comboBox_AssistTorque.SelectedIndex = 0;
  246. comboBox_AssistCadence.SelectedIndex = 0;
  247. //combobox事件定义
  248. this.comboBox_WorkMode.SelectedIndexChanged += new System.EventHandler(this.comboBox_WorkMode_SelectedIndexChanged);
  249. this.comboBox_GearSt.SelectedIndexChanged += new System.EventHandler(this.comboBox_GearSt_SelectedIndexChanged);
  250. this.comboBox_LightSwitch.SelectedIndexChanged += new System.EventHandler(this.comboBox_GearSt_SelectedIndexChanged);
  251. this.comboBox_OBC_SetGearST.SelectedIndexChanged += new System.EventHandler(this.comboBox_OBC_SetGearST_SelectedIndexChanged);
  252. this.comboBox_OBC_LightSw.SelectedIndexChanged += new System.EventHandler(this.comboBox_OBC_SetGearST_SelectedIndexChanged);
  253. //端口设置
  254. mySerialProcess.Init();
  255. toolStripComboBox_ComNum.Items.AddRange(mySerialProcess.refreshPort());
  256. if ((PortNumSave != string.Empty) && (PortBaudrateSave != string.Empty)) //有数值时采用上次使用的值
  257. {
  258. try
  259. {
  260. toolStripComboBox_ComNum.SelectedIndex = toolStripComboBox_ComNum.Items.IndexOf(PortNumSave);
  261. toolStripComboBox_Baudrate.SelectedIndex = toolStripComboBox_Baudrate.Items.IndexOf(PortBaudrateSave);
  262. PortConnect();
  263. }
  264. catch (System.Exception)
  265. {
  266. toolStripComboBox_ComNum.SelectedIndex = toolStripComboBox_ComNum.Items.Count > 0 ? 0 : -1;
  267. toolStripComboBox_Baudrate.SelectedIndex = toolStripComboBox_Baudrate.Items.IndexOf("115200");
  268. timer_1s.Enabled = false;
  269. MessageBox.Show("参数格式错误,写入默认值!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  270. timer_1s.Enabled = true;
  271. }
  272. }
  273. else //否则采用默认值
  274. {
  275. toolStripComboBox_ComNum.SelectedIndex = toolStripComboBox_ComNum.Items.Count > 0 ? 0 : -1;
  276. toolStripComboBox_Baudrate.SelectedIndex = toolStripComboBox_Baudrate.Items.IndexOf("115200");
  277. }
  278. //创建线程,定时解析数据
  279. Thread th = new Thread(Task_SerialProcess);
  280. th.IsBackground = true;
  281. th.Start();
  282. }
  283. #region 结构体与字节流相互转换
  284. public static class MyConverter
  285. {
  286. /// <summary>
  287. /// 由结构体转换为byte数组
  288. /// </summary>
  289. public static byte[] StructureToByte<T>(T structure)
  290. {
  291. int size = Marshal.SizeOf(typeof(T));
  292. byte[] buffer = new byte[size];
  293. IntPtr bufferIntPtr = Marshal.AllocHGlobal(size);
  294. try
  295. {
  296. Marshal.StructureToPtr(structure, bufferIntPtr, true);
  297. Marshal.Copy(bufferIntPtr, buffer, 0, size);
  298. }
  299. finally
  300. {
  301. Marshal.FreeHGlobal(bufferIntPtr);
  302. }
  303. return buffer;
  304. }
  305. /// <summary>
  306. /// 由byte数组转换为结构体
  307. /// </summary>
  308. public static T ByteToStructure<T>(byte[] dataBuffer, ushort StarIndex)
  309. {
  310. object structure = null;
  311. int size = Marshal.SizeOf(typeof(T));
  312. IntPtr allocIntPtr = Marshal.AllocHGlobal(size);
  313. try
  314. {
  315. Marshal.Copy(dataBuffer, StarIndex, allocIntPtr, size);
  316. structure = Marshal.PtrToStructure(allocIntPtr, typeof(T));
  317. }
  318. finally
  319. {
  320. Marshal.FreeHGlobal(allocIntPtr);
  321. }
  322. return (T)structure;
  323. }
  324. }
  325. #endregion
  326. #region 故障日志解析显示
  327. private void ErrorLogDisplay(MC_ErrorLogSaveInfo_Struct_t Log1, MC_ErrorLogSaveInfo_Struct_t Log2)
  328. {
  329. string SaveData = "";
  330. //Log1
  331. if (Log1.Error_Index != 0xFFFF)
  332. {
  333. SaveData += "故障序号:" + Convert.ToString(Log1.Error_Index) + ", ";
  334. SaveData += "备注1:" + Convert.ToString(Log1.NotesInfo1) + ", ";
  335. SaveData += "备注2:" + Convert.ToString(Log1.NotesInfo2) + ", ";
  336. SaveData += "备注3:" + Convert.ToString(Log1.NotesInfo3) + ", ";
  337. SaveData += "故障码:" + "0x" + Convert.ToString(Log1.ErrorCode, 16).PadLeft(8, '0').ToUpper() + ", ";
  338. SaveData += "运行时间:" + Convert.ToString(Log1.RunTime) + "min" + ", ";
  339. SaveData += "车速:" + ((float)Log1.RunInfo.BikeSpeed / 10f).ToString("0.0") + "km/h" + ", ";
  340. SaveData += "电机转速:" + Convert.ToString(Log1.RunInfo.MotorSpeed) + "rpm" + ", ";
  341. SaveData += "功率:" + Convert.ToString(Log1.RunInfo.Power * 2) + "w" + ", ";
  342. SaveData += "电压:" + Convert.ToString(Log1.RunInfo.BusVoltage) + "mV" + ", ";
  343. SaveData += "电流:" + Convert.ToString(Log1.RunInfo.BusCurrent) + "mA" + ", ";
  344. SaveData += "踏频:" + Convert.ToString(Log1.RunInfo.Cadence) + "rpm" + ", ";
  345. SaveData += "力矩:" + Convert.ToString(Log1.RunInfo.Torque) + "N.m" + ", ";
  346. SaveData += "方向:" + ((Log1.RunInfo.CadenceDir == 0) ? "正" : ((Log1.RunInfo.CadenceDir == 1) ? "反" : "停止")) + ", ";
  347. SaveData += "档位:" + "0x" + Convert.ToString(Log1.RunInfo.GearSt, 16).PadLeft(2, '0').ToUpper() + ", ";
  348. SaveData += "灯开关:" + ((Log1.RunInfo.LightSwitch == 0xF1) ? "开" : "关") + ", ";
  349. SaveData += "电量:" + Convert.ToString(Log1.RunInfo.SOC) + "%" + ", ";
  350. if ((Log1.RunInfo.RemainDistance == 0xEEEE) || (Log1.RunInfo.RemainDistance == 0xFFFF))
  351. {
  352. SaveData += "续航:" + "---km" + ", ";
  353. }
  354. else
  355. {
  356. SaveData += "续航:" + Convert.ToString(Log1.RunInfo.RemainDistance) + "km" + ", ";
  357. }
  358. SaveData += "力矩AD:" + Convert.ToString(Log1.RunInfo.Torque_AD) + ", ";
  359. SaveData += "功耗:" + Convert.ToString(Log1.RunInfo.PowerPerKm * 10) + "mAh/km" + ", ";
  360. SaveData += "PCB温度:" + Convert.ToString((int)(Log1.RunInfo.T_PCB - 40)) + "℃" + ", ";
  361. SaveData += "绕组温度:" + Convert.ToString((int)(Log1.RunInfo.T_Coil - 40)) + "℃" + ", ";
  362. SaveData += "MCU温度:" + Convert.ToString((int)(Log1.RunInfo.T_MCU - 40)) + "℃" + ", ";
  363. SaveData += "单次里程:" + Convert.ToString((float)Log1.RunInfo.Ride_Km / 10f) + "km" + ", ";
  364. SaveData += "单次时间:" + Convert.ToString(Log1.RunInfo.Ride_Time) + "s" + ", ";
  365. SaveData += "q轴电流:" + Convert.ToString(Log1.IqCurrent) + ", ";
  366. SaveData += "q轴电压:" + Convert.ToString(Log1.IqVoltage) + ", ";
  367. SaveData += "d轴电流:" + Convert.ToString(Log1.IdCurrent) + ", ";
  368. SaveData += "d轴电压:" + Convert.ToString(Log1.IdVoltage) + ", ";
  369. SaveData += "系统状态机:" + Convert.ToString((Log1.FSM & 0xF000) >> 12) + ", ";
  370. SaveData += "二层状态机:" + Convert.ToString((Log1.FSM & 0x0F00) >> 8) + ", ";
  371. SaveData += "电机状态机:" + Convert.ToString((Log1.FSM & 0x00F0) >> 4) + ", ";
  372. SaveData += "助力状态机:" + Convert.ToString((Log1.FSM & 0x000F)) + ", ";
  373. SaveData += "\r\n\r\n";
  374. richTextBox_Record.AppendText(SaveData);
  375. SaveData = "";
  376. }
  377. //Log2
  378. if (Log2.Error_Index != 0xFFFF)
  379. {
  380. SaveData += "故障序号:" + Convert.ToString(Log2.Error_Index) + ", ";
  381. SaveData += "备注1:" + Convert.ToString(Log2.NotesInfo1) + ", ";
  382. SaveData += "备注2:" + Convert.ToString(Log2.NotesInfo2) + ", ";
  383. SaveData += "备注3:" + Convert.ToString(Log2.NotesInfo3) + ", ";
  384. SaveData += "故障码:" + "0x" + Convert.ToString(Log2.ErrorCode, 16).PadLeft(8, '0').ToUpper() + ", ";
  385. SaveData += "运行时间:" + Convert.ToString(Log2.RunTime) + "min" + ", ";
  386. SaveData += "车速:" + ((float)Log2.RunInfo.BikeSpeed / 10f).ToString("0.0") + "km/h" + ", ";
  387. SaveData += "电机转速:" + Convert.ToString(Log2.RunInfo.MotorSpeed) + "rpm" + ", ";
  388. SaveData += "功率:" + Convert.ToString(Log2.RunInfo.Power * 2) + "w" + ", ";
  389. SaveData += "电压:" + Convert.ToString(Log2.RunInfo.BusVoltage) + "mV" + ", ";
  390. SaveData += "电流:" + Convert.ToString(Log2.RunInfo.BusCurrent) + "mA" + ", ";
  391. SaveData += "踏频:" + Convert.ToString(Log2.RunInfo.Cadence) + "rpm" + ", ";
  392. SaveData += "力矩:" + Convert.ToString(Log2.RunInfo.Torque) + "N.m" + ", ";
  393. SaveData += "方向:" + ((Log2.RunInfo.CadenceDir == 0) ? "正" : ((Log2.RunInfo.CadenceDir == 1) ? "反" : "停止")) + ", ";
  394. SaveData += "档位:" + "0x" + Convert.ToString(Log2.RunInfo.GearSt, 16).PadLeft(2, '0').ToUpper() + ", ";
  395. SaveData += "灯开关:" + ((Log2.RunInfo.LightSwitch == 0xF1) ? "开" : "关") + ", ";
  396. SaveData += "电量:" + Convert.ToString(Log2.RunInfo.SOC) + "%" + ", ";
  397. if ((Log2.RunInfo.RemainDistance == 0xEEEE) || (Log2.RunInfo.RemainDistance == 0xFFFF))
  398. {
  399. SaveData += "续航:" + "---km" + ", ";
  400. }
  401. else
  402. {
  403. SaveData += "续航:" + Convert.ToString(Log2.RunInfo.RemainDistance) + "km" + ", ";
  404. }
  405. SaveData += "力矩AD:" + Convert.ToString(Log2.RunInfo.Torque_AD) + ", ";
  406. SaveData += "功耗:" + Convert.ToString(Log2.RunInfo.PowerPerKm * 10) + "mAh/km" + ", ";
  407. SaveData += "PCB温度:" + Convert.ToString((int)(Log2.RunInfo.T_PCB - 40)) + "DegreeC" + ", ";
  408. SaveData += "绕组温度:" + Convert.ToString((int)(Log2.RunInfo.T_Coil - 40)) + "DegreeC" + ", ";
  409. SaveData += "MCU温度:" + Convert.ToString((int)(Log2.RunInfo.T_MCU - 40)) + "DegreeC" + ", ";
  410. SaveData += "单次里程:" + Convert.ToString((float)Log2.RunInfo.Ride_Km / 10f) + "km" + ", ";
  411. SaveData += "单次时间:" + Convert.ToString(Log2.RunInfo.Ride_Time) + "s" + ", ";
  412. SaveData += "q轴电流:" + Convert.ToString(Log2.IqCurrent) + ", ";
  413. SaveData += "q轴电压:" + Convert.ToString(Log2.IqVoltage) + ", ";
  414. SaveData += "d轴电流:" + Convert.ToString(Log2.IdCurrent) + ", ";
  415. SaveData += "d轴电压:" + Convert.ToString(Log2.IdVoltage) + ", ";
  416. SaveData += "系统状态机:" + Convert.ToString((Log1.FSM & 0xF000) >> 12) + ", ";
  417. SaveData += "二层状态机:" + Convert.ToString((Log1.FSM & 0x0F00) >> 8) + ", ";
  418. SaveData += "电机状态机:" + Convert.ToString((Log1.FSM & 0x00F0) >> 4) + ", ";
  419. SaveData += "助力状态机:" + Convert.ToString((Log1.FSM & 0x000F)) + ", ";
  420. SaveData += "\r\n\r\n";
  421. richTextBox_Record.AppendText(SaveData);
  422. SaveData = "";
  423. }
  424. }
  425. #endregion
  426. #region 串口数据解析线程
  427. /// <summary>
  428. /// 串口数据解析线程
  429. /// </summary>
  430. private void Task_SerialProcess()
  431. {
  432. //创建定时器,定时读取串口数据
  433. System.Timers.Timer timer_ReadSerial = new System.Timers.Timer();
  434. timer_ReadSerial.Enabled = true;
  435. timer_ReadSerial.Interval = 20;
  436. timer_ReadSerial.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_ReadSerial_Tick);
  437. timer_ReadSerial.Start();
  438. while (true)
  439. {
  440. Thread.Sleep(1);
  441. };
  442. }
  443. /// <summary>
  444. /// 串口解析定时器
  445. /// </summary>
  446. /// <param name="sender"></param>
  447. /// <param name="e"></param>
  448. private void timer_ReadSerial_Tick(object sender, EventArgs e)
  449. {
  450. //串口解析
  451. Serial_process(mySerialProcess.buffer);
  452. }
  453. #endregion
  454. #region 数据解析
  455. public void Serial_process(List<byte> buffer)
  456. {
  457. byte DataNum = 0; //记录每条命令数据段的长度
  458. ushort PackageID, Cmd;
  459. byte[] Data = new byte[1024];
  460. while (buffer.Count >= 13)//当buffer中的数据的个数大于等于4个时,即每一帧的数据长度所在位(第4位)出现时
  461. {
  462. if (buffer[0] == 0x55 && buffer[1] == 0xAA)//判断帧头
  463. {
  464. PackageID = (ushort)(buffer[2] * 256 + buffer[3]);
  465. if ((PackageID == 0x0710) || (PackageID == 0x0715) || (PackageID == 0x07FF) || (PackageID == 0x0720) || (PackageID == 0x0713))
  466. {
  467. int CmdLen = buffer[5];
  468. if (buffer.Count < CmdLen + 11) break;//如果接收到的数据没有达到一帧数据的指定长度, 则执行下次循环
  469. buffer.RemoveRange(0, 6);//数据达到要求长度后删去帧头,ID,模式,和命令长度
  470. while (CmdLen > 0) //读取命令段
  471. {
  472. Cmd = (ushort)(buffer[0] * 256 + buffer[1]);
  473. DataNum = (byte)(buffer[1]);//命令字的第二个字节的表示数据位的长度
  474. if (DataNum <= (CmdLen - 2))
  475. {
  476. for (int i = 0; i < DataNum; i++)
  477. {
  478. Data[i] = buffer[i + 2];
  479. }
  480. DataCmdProcess(PackageID, Cmd, Data);//对于不同的命令段,做不同的处理
  481. buffer.RemoveRange(0, DataNum);//移除处理过的数据段
  482. CmdLen -= (DataNum + 2);
  483. if (CmdLen < 2)
  484. {
  485. buffer.RemoveRange(0, CmdLen + 2);
  486. return;
  487. }
  488. }
  489. else
  490. {
  491. buffer.RemoveRange(0, CmdLen + 11);
  492. }
  493. }
  494. }
  495. else
  496. {
  497. buffer.RemoveAt(0);//ID不对
  498. }
  499. }
  500. else
  501. {
  502. buffer.RemoveAt(0);//帧头不对,删除帧头高字节
  503. }
  504. }
  505. }
  506. private void DataCmdProcess(ushort ID, ushort CmdTemp, byte[] Data)
  507. {
  508. int DataTemp;
  509. ushort uData16;
  510. unchecked
  511. {
  512. this.Invoke((EventHandler)(delegate { label_RecCount.Text = Convert.ToString(Convert.ToInt32(label_RecCount.Text) + 1); }));
  513. }
  514. #region 解析发送给CDL的指令
  515. if (ID == 0x7FF)
  516. {
  517. switch (CmdTemp)
  518. {
  519. case 0x1100://
  520. {
  521. unchecked
  522. {
  523. this.Invoke((EventHandler)(delegate
  524. {
  525. //CDL连接成功
  526. mySerialProcess.CDL_Online_Flag = true;
  527. MessageBox.Show("连接成功!", "提示");
  528. }));
  529. }
  530. break;
  531. }
  532. default: break;
  533. }
  534. }
  535. #endregion
  536. #region 解析电机的命令
  537. else if ((ID == 0x715) || (ID == 0x710))
  538. {
  539. switch (CmdTemp)
  540. {
  541. case 0x1020://电机运行信息
  542. {
  543. unchecked
  544. {
  545. this.Invoke((EventHandler)(delegate
  546. {
  547. MC_RunInfo_Refresh = true;
  548. //车速
  549. DataTemp = (int)(Data[1] * 256 + Data[0]);
  550. textBox_RunInfo_BikeSpeed.Text = ((float)DataTemp / 10f).ToString("0.0") + " km/h";
  551. //输出转速
  552. DataTemp = (ushort)(Data[3] * 256 + Data[2]);
  553. textBox_RunInfo_MotorSpeed.Text = Convert.ToString(DataTemp) + " rpm";
  554. Class_MotorSpeed.String = textBox_RunInfo_MotorSpeed.Text;
  555. Class_MotorSpeed.Data = DataTemp;
  556. //电功率
  557. DataTemp = (ushort)(Data[5] * 256 + Data[4]);
  558. textBox_RunInfo_Power.Text = Convert.ToString(DataTemp) + " W";
  559. //母线电压
  560. DataTemp = (ushort)(Data[7] * 256 + Data[6]);
  561. textBox_RunInfo_Vol.Text = Convert.ToString(DataTemp) + " mV";
  562. //textBox_RunInfo_Vol.Text = (DataTemp / 1000f).ToString("0.0") + " V";
  563. //母线电流
  564. DataTemp = (short)(Data[9] * 256 + Data[8]);
  565. textBox_RunInfo_Current.Text = Convert.ToString(DataTemp) + " mA";
  566. //textBox_RunInfo_Current.Text = (DataTemp / 1000f).ToString("0.0") + " A";
  567. Class_CurrentData.String = textBox_RunInfo_Current.Text;
  568. Class_CurrentData.Data = DataTemp;
  569. //踏频
  570. DataTemp = (ushort)(Data[10]);
  571. textBox_RunInfo_Cadence.Text = Convert.ToString(DataTemp) + " rpm";
  572. Class_CadenceData.String = textBox_RunInfo_Cadence.Text;
  573. Class_CadenceData.Data = DataTemp;
  574. //踩踏力矩
  575. DataTemp = (ushort)(Data[11]);
  576. textBox_RunInfo_Torque.Text = Convert.ToString(DataTemp) + " N.m";
  577. Class_TorqueData.String = textBox_RunInfo_Torque.Text;
  578. Class_TorqueData.Data = DataTemp;
  579. //踩踏方向
  580. DataTemp = (ushort)(Data[12]);
  581. if (DataTemp == 0)
  582. {
  583. textBox_RunInfo_Dir.Text = "正向";
  584. }
  585. else if (DataTemp == 1)
  586. {
  587. textBox_RunInfo_Dir.Text = "反向";
  588. }
  589. else if (DataTemp == 2)
  590. {
  591. textBox_RunInfo_Dir.Text = "停止";
  592. }
  593. //助力档位
  594. DataTemp = (ushort)(Data[13]);
  595. if (DataTemp == 0x33)
  596. {
  597. textBox_RunInfo_GearSt.Text = "SMART";
  598. //comboBox_GearSt.SelectedIndex = 5;
  599. //comboBox_OBC_SetGearST.SelectedIndex = 5;
  600. }
  601. else if (DataTemp == 0x22)
  602. {
  603. textBox_RunInfo_GearSt.Text = "WALK";
  604. //comboBox_GearSt.SelectedIndex = 6;
  605. //comboBox_OBC_SetGearST.SelectedIndex = 6;
  606. }
  607. else
  608. {
  609. string[] Gears = new string[5] { "OFF", "ECO", "NORM", "SPORT", "TURBO" };
  610. textBox_RunInfo_GearSt.Text = Gears[DataTemp];
  611. //comboBox_GearSt.SelectedIndex = DataTemp;
  612. //comboBox_OBC_SetGearST.SelectedIndex = DataTemp;
  613. }
  614. //大灯状态
  615. DataTemp = (ushort)(Data[14]);
  616. if (DataTemp == 0xF0)
  617. {
  618. textBox_RunInfo_LightSwitch.Text = "OFF";
  619. //comboBox_LightSwitch.SelectedIndex = 1;
  620. //comboBox_OBC_LightSw.SelectedIndex = 1;
  621. }
  622. else if (DataTemp == 0xF1)
  623. {
  624. textBox_RunInfo_LightSwitch.Text = "ON";
  625. //comboBox_LightSwitch.SelectedIndex = 0;
  626. //comboBox_OBC_LightSw.SelectedIndex = 0;
  627. }
  628. //剩余电量
  629. DataTemp = (ushort)(Data[15]);
  630. textBox_RunInfo_SOC.Text = Convert.ToString(DataTemp) + " %";
  631. //剩余续航里程
  632. DataTemp = (ushort)(Data[17] * 256 + Data[16]);
  633. if ((DataTemp == 0xEEEE) || (DataTemp == 0xFFFF))
  634. {
  635. textBox_RunInfo_Range.Text = "---";
  636. }
  637. else
  638. {
  639. textBox_RunInfo_Range.Text = Convert.ToString(DataTemp) + " km";
  640. }
  641. //预留(显示力矩Reg),空2bytes
  642. DataTemp = (ushort)(Data[19] * 256 + Data[18]);
  643. textBox_TorqueReg.Text = Convert.ToString(DataTemp);
  644. //平均功耗
  645. DataTemp = (ushort)(Data[20]);
  646. textBox_RunInfo_AvgPower.Text = Convert.ToString(DataTemp * 10) + " mAh/km";
  647. //PCB温度
  648. DataTemp = (ushort)(Data[21]);
  649. textBox_RunInfo_T_PCB.Text = Convert.ToString((int)(DataTemp - 40))+" ℃";
  650. Class_MotorTemp.String = textBox_RunInfo_T_PCB.Text;
  651. Class_MotorTemp.Data = DataTemp - 40;
  652. //绕组温度
  653. DataTemp = (ushort)(Data[22]);
  654. textBox_RunInfo_T_Coil.Text = Convert.ToString((int)(DataTemp - 40)) + " ℃";
  655. //MCU温度
  656. DataTemp = (ushort)(Data[23]);
  657. textBox_RunInfo_T_MCU.Text = Convert.ToString((int)(DataTemp - 40)) + " ℃";
  658. //单次里程
  659. DataTemp = (ushort)(Data[25] * 256 + Data[24]);
  660. textBox_RunInfo_Trip.Text = ((float)DataTemp / 10f).ToString("0.0") + " km";
  661. //单次时间
  662. DataTemp = (ushort)(Data[27] * 256 + Data[26]);
  663. textBox_RunInfo_Trip_Time.Text = Convert.ToString(DataTemp) + " s";
  664. //数据保存
  665. RunInfoAutoSave();
  666. }));
  667. }
  668. break;
  669. }
  670. case 0x1104://故障码
  671. {
  672. unchecked
  673. {
  674. this.Invoke((EventHandler)(delegate
  675. {
  676. MC_ErrorCode_Refresh = true;
  677. textBox_ErrorCode.Text =
  678. Convert.ToString(Data[3], 16).PadLeft(2, '0').ToUpper() +
  679. Convert.ToString(Data[2], 16).PadLeft(2, '0').ToUpper() +
  680. Convert.ToString(Data[1], 16).PadLeft(2, '0').ToUpper() +
  681. Convert.ToString(Data[0], 16).PadLeft(2, '0').ToUpper() +
  682. "H";
  683. }));
  684. }
  685. break;
  686. }
  687. case 0xA903://反馈指令
  688. {
  689. unchecked
  690. {
  691. this.Invoke((EventHandler)(delegate
  692. {
  693. timer_1s.Enabled = false;
  694. MessageBox.Show("OK", "反馈指令", MessageBoxButtons.OK, MessageBoxIcon.Information);
  695. timer_1s.Enabled = true;
  696. //关机反馈
  697. PowerOffAckStatus = true;
  698. }));
  699. }
  700. break;
  701. }
  702. case 0xB226://马达参数
  703. {
  704. unchecked
  705. {
  706. this.Invoke((EventHandler)(delegate
  707. {
  708. richTextBox_MotorParam.Clear();
  709. for (int i = 0; i < myParams.MotorParma.Count; i++)
  710. {
  711. richTextBox_MotorParam.AppendText(myParams.MotorParma[i] + "=" + Convert.ToString((short)(Data[2 * i + 1] * 256 + Data[2 * i])) + ", ");
  712. }
  713. richTextBox_MotorParam.Text = richTextBox_MotorParam.Text.Substring(0, richTextBox_MotorParam.Text.Length - 2);
  714. }));
  715. }
  716. break;
  717. }
  718. case 0xB31A://整车信息
  719. {
  720. unchecked
  721. {
  722. this.Invoke((EventHandler)(delegate
  723. {
  724. richTextBox_BikeParam.Clear();
  725. for (int i = 0; i < myParams.BikeParma.Count; i++)
  726. {
  727. richTextBox_BikeParam.AppendText(myParams.BikeParma[i] + "=" + Convert.ToString((short)(Data[2 * i + 1] * 256 + Data[2 * i])) + ", ");
  728. if (i == 8) //更新前后灯参数
  729. {
  730. comboBox_TailLightMode.SelectedIndex = ((Data[2 * i + 1] >> 4) & 0x0F) - 1;
  731. comboBox_TailLightVol.SelectedIndex = ((Data[2 * i + 1] & 0x0F) == 6) ? 0 : 1;
  732. comboBox_FrontLightVol.SelectedIndex = ((Data[2 * i] & 0x0F) == 6) ? 0 : 1;
  733. }
  734. else if (i == 11) //更新开关机参数
  735. {
  736. textBox_PowerOnDelay.Text = Convert.ToString((Data[2 * i + 1] >> 4) & 0x0F);
  737. textBox_PowerOffDelay.Text = Convert.ToString(Data[2 * i + 1] & 0x0F);
  738. textBox_AutoPowerOff.Text = Convert.ToString(Data[2 * i]);
  739. }
  740. }
  741. richTextBox_BikeParam.Text = richTextBox_BikeParam.Text.Substring(0, richTextBox_BikeParam.Text.Length - 2);
  742. //更新生产模式中整车参数
  743. textBox_FacModeWheelSize.Text = (Data[1] * 256 + Data[0]).ToString();//周长
  744. textBox_FacModeSpeedLimit.Text= (Data[5] * 256 + Data[4]).ToString();//限速
  745. DataTemp = (int)(Data[13] * 256 + Data[12]);//助力方案1
  746. if (DataTemp == 0) comboBox_FacModeAssist1.SelectedIndex = 0;
  747. else if (DataTemp == 341) comboBox_FacModeAssist1.SelectedIndex = 1;
  748. else if (DataTemp == 682) comboBox_FacModeAssist1.SelectedIndex = 2;
  749. else comboBox_FacModeAssist1.SelectedIndex = 3;
  750. DataTemp = (int)(Data[15] * 256 + Data[14]);//助力方案2
  751. if (DataTemp == 0) comboBox_FacModeAssist2.SelectedIndex = 0;
  752. else if (DataTemp == 341) comboBox_FacModeAssist2.SelectedIndex = 1;
  753. else if (DataTemp == 682) comboBox_FacModeAssist2.SelectedIndex = 2;
  754. else comboBox_FacModeAssist2.SelectedIndex = 3;
  755. DataTemp = (int)(Data[17] * 256 + Data[16]);//灯压
  756. if (DataTemp == 6) comboBox_FacModeLightVol.SelectedIndex = 0;
  757. else if (DataTemp == 12) comboBox_FacModeLightVol.SelectedIndex = 1;
  758. else comboBox_FacModeLightVol.SelectedIndex = 2;
  759. DataTemp = (int)(Data[21] * 256 + Data[20]);//启动模式
  760. if (DataTemp == 1) comboBox_FacModeStartMode.SelectedIndex = 0;
  761. else if (DataTemp == 3) comboBox_FacModeStartMode.SelectedIndex = 2;
  762. else comboBox_FacModeStartMode.SelectedIndex = 1;
  763. }));
  764. }
  765. break;
  766. }
  767. case 0xB420://控制器参数
  768. {
  769. unchecked
  770. {
  771. this.Invoke((EventHandler)(delegate
  772. {
  773. richTextBox_ControlParam.Clear();
  774. for (int i = 0; i < myParams.ControlParma.Count; i++)
  775. {
  776. richTextBox_ControlParam.AppendText(myParams.ControlParma[i] + "=" + Convert.ToString((ushort)(Data[2 * i + 1] * 256 + Data[2 * i])) + ", ");
  777. }
  778. richTextBox_ControlParam.Text = richTextBox_ControlParam.Text.Substring(0, richTextBox_ControlParam.Text.Length - 2);
  779. }));
  780. }
  781. break;
  782. }
  783. case 0xB528://传感器参数
  784. {
  785. unchecked
  786. {
  787. this.Invoke((EventHandler)(delegate
  788. {
  789. richTextBox_SensorParam.Clear();
  790. for (int i = 0; i < myParams.SensorParam.Count; i++)
  791. {
  792. richTextBox_SensorParam.AppendText(myParams.SensorParam[i] + "=" + Convert.ToString((ushort)(Data[2 * i + 1] * 256 + Data[2 * i])) + ", ");
  793. }
  794. richTextBox_SensorParam.Text = richTextBox_SensorParam.Text.Substring(0, richTextBox_SensorParam.Text.Length - 2);
  795. //更新生产模式页面传感器零点和标定值
  796. textBox_FacModeSensorADC0.Text = (Data[1] * 256 + Data[0]).ToString();
  797. textBox_FacModeSensorADC1.Text = (Data[15] * 256 + Data[14]).ToString();
  798. textBox_FacModeSensorADC2.Text = (Data[19] * 256 + Data[18]).ToString();
  799. textBox_FacModeSensorADC3.Text = (Data[23] * 256 + Data[22]).ToString();
  800. textBox_FacModeSensorADC4.Text = (Data[27] * 256 + Data[26]).ToString();
  801. }));
  802. }
  803. break;
  804. }
  805. case 0xB64C://助力参数
  806. {
  807. unchecked
  808. {
  809. this.Invoke((EventHandler)(delegate
  810. {
  811. richTextBox_AssistParam.Clear();
  812. int index_1 = myParams.AssistParam.IndexOf("转矩曲线.a");
  813. int index_2 = myParams.AssistParam.IndexOf("踏频曲线.d");
  814. for (int i = 0; i < myParams.AssistParam.Count; i++)
  815. {
  816. if (i < index_1) //数据占用2bytes
  817. {
  818. richTextBox_AssistParam.AppendText(myParams.AssistParam[i] + "=" + Convert.ToString((ushort)(Data[2 * i + 1] * 256 + Data[2 * i])) + ", ");
  819. }
  820. else if (i <= index_2) //数据占用4bytes
  821. {
  822. richTextBox_AssistParam.AppendText(myParams.AssistParam[i] + "=" + Convert.ToString((long)((Data[4 * i - 5] << 24) + (Data[4 * i - 6] << 16) + (Data[4 * i - 7] << 8) + Data[4 * i - 8])) + ", ");
  823. }
  824. else //数据占用2bytes
  825. {
  826. richTextBox_AssistParam.AppendText(myParams.AssistParam[i] + "=" + Convert.ToString((ushort)(Data[2 * i + 17] * 256 + Data[2 * i + 16])) + ", ");
  827. }
  828. }
  829. richTextBox_AssistParam.Text = richTextBox_AssistParam.Text.Substring(0, richTextBox_AssistParam.Text.Length - 2);
  830. //更新生产模式中助力参数
  831. textBox_FacModeStartGain.Text = (Data[1] * 256 + Data[0]).ToString();//零速增益
  832. textBox_FacModeCircuitK.Text = (Data[45] * 256 + Data[44]).ToString();//电流阶梯
  833. textBox_FacModeFltCounter.Text = (Data[49] * 256 + Data[48]).ToString();//滤波脉冲
  834. textBox_FacModeSpeedLimitTh.Text = (Data[55] * 256 + Data[54]).ToString();//限速启动
  835. textBox_FacModeSpeedLimitEnd.Text = (Data[57] * 256 + Data[56]).ToString();//限速停止
  836. textBox_FacModeCadencePer.Text = (Data[59] * 256 + Data[58]).ToString();//踏频占比
  837. }));
  838. }
  839. break;
  840. }
  841. case 0xB74C://历史记录
  842. {
  843. unchecked
  844. {
  845. this.Invoke((EventHandler)(delegate
  846. {
  847. richTextBox_Record.Clear();
  848. for (int i = 0; i < myParams.Record.Count; i++)
  849. {
  850. richTextBox_Record.AppendText(myParams.Record[i] + "=" + Convert.ToString(Data[2 * i + 1] * 256 + Data[2 * i]) + ", ");
  851. }
  852. richTextBox_Record.Text = richTextBox_Record.Text.Substring(0, richTextBox_Record.Text.Length - 2);
  853. }));
  854. }
  855. break;
  856. }
  857. case 0xB83A://调试参数
  858. {
  859. unchecked
  860. {
  861. this.Invoke((EventHandler)(delegate
  862. {
  863. richTextBox_DebugParam.Clear();
  864. for (int i = 0; i < myParams.DebugParam.Count; i++)
  865. {
  866. richTextBox_DebugParam.AppendText(myParams.DebugParam[i] + "=" + Convert.ToString((short)(Data[2 * i + 1] * 256 + Data[2 * i])) + ", ");
  867. }
  868. richTextBox_DebugParam.Text = richTextBox_DebugParam.Text.Substring(0, richTextBox_DebugParam.Text.Length - 2);
  869. }));
  870. }
  871. break;
  872. }
  873. case 0xB904://存储标志
  874. {
  875. unchecked
  876. {
  877. this.Invoke((EventHandler)(delegate
  878. {
  879. ushort Flag = 0;
  880. Flag = (ushort)(Data[1] * 256 + Data[0]);//EEPROM存储标志
  881. if (Flag == 0)
  882. {
  883. radioButton_EE_SaveNo.Checked = true;
  884. radioButton_EE_SaveYes.Checked = false;
  885. }
  886. else
  887. {
  888. radioButton_EE_SaveNo.Checked = false;
  889. radioButton_EE_SaveYes.Checked = true;
  890. }
  891. Flag = (ushort)(Data[3] * 256 + Data[2]);//SIP偏移校准存储标志
  892. if (Flag == 0)
  893. {
  894. radioButton_SIP_SaveNo.Checked = true;
  895. radioButton_SIP_SaveYes.Checked = false;
  896. }
  897. else
  898. {
  899. radioButton_SIP_SaveNo.Checked = false;
  900. radioButton_SIP_SaveYes.Checked = true;
  901. }
  902. }));
  903. }
  904. break;
  905. }
  906. case 0x1240://电机版本信息
  907. {
  908. unchecked
  909. {
  910. this.Invoke((EventHandler)(delegate
  911. {
  912. //电机型号
  913. textBox_Model.Text = "";
  914. textBox_OBC_ReadModel.Text = "";
  915. textBox_FacModeName.Text = "";
  916. for (ushort i = 0; i < 16; i++)
  917. {
  918. if (Data[i] == 0x2E)
  919. {
  920. break;
  921. }
  922. textBox_Model.Text += ((char)Data[i]).ToString();
  923. textBox_OBC_ReadModel.Text += ((char)Data[i]).ToString();
  924. textBox_FacModeName.Text += ((char)Data[i]).ToString();
  925. }
  926. Class_Motor_Ver.Mode = textBox_Model.Text;
  927. //电机SN
  928. textBox_SN.Text = "";
  929. textBox_OBC_ReadSN.Text = "";
  930. textBox_FacModeNum.Text = "";
  931. for (ushort i = 0; i < 16; i++)
  932. {
  933. if (Data[16 + i] == 0x2E)
  934. {
  935. break;
  936. }
  937. textBox_SN.Text += ((char)Data[16 + i]).ToString();
  938. textBox_OBC_ReadSN.Text += ((char)Data[16 + i]).ToString();
  939. textBox_FacModeNum.Text += ((char)Data[16 + i]).ToString();
  940. }
  941. Class_Motor_Ver.SN = textBox_SN.Text;
  942. //电机HW
  943. textBox_HW.Text = "";
  944. textBox_OBC_ReadHW.Text = "";
  945. textBox_FacModeHW.Text = "";
  946. for (ushort i = 0; i < 16; i++)
  947. {
  948. if (Data[32 + i] == 0x2E)
  949. {
  950. break;
  951. }
  952. textBox_HW.Text += ((char)Data[32 + i]).ToString();
  953. textBox_OBC_ReadHW.Text += ((char)Data[32 + i]).ToString();
  954. textBox_FacModeHW.Text += ((char)Data[32 + i]).ToString();
  955. }
  956. Class_Motor_Ver.HW = textBox_HW.Text;
  957. //电机FW
  958. textBox_FW.Text = "";
  959. textBox_OBC_ReadFW.Text = "";
  960. textBox_FacModeFW.Text = "";
  961. for (ushort i = 0; i < 16; i++)
  962. {
  963. if (Data[48 + i] == 0x2E)
  964. {
  965. break;
  966. }
  967. if (Data[48 + i] == 'r')
  968. {
  969. Data[48 + i] = (byte)'.';
  970. }
  971. textBox_FW.Text += ((char)Data[48 + i]).ToString();
  972. textBox_OBC_ReadFW.Text += ((char)Data[48 + i]).ToString();
  973. textBox_FacModeFW.Text += ((char)Data[48 + i]).ToString();
  974. }
  975. Class_Motor_Ver.FW = textBox_FW.Text;
  976. }));
  977. }
  978. break;
  979. }
  980. case 0xA610://自定义1
  981. {
  982. unchecked
  983. {
  984. this.Invoke((EventHandler)(delegate
  985. {
  986. textBox_User1.Text = "";
  987. for (ushort i = 0; i < 16; i++)
  988. {
  989. if (Data[i] == 0x2E)
  990. {
  991. break;
  992. }
  993. textBox_User1.Text += ((char)Data[i]).ToString();
  994. }
  995. }));
  996. }
  997. break;
  998. }
  999. case 0xA710://自定义2
  1000. {
  1001. unchecked
  1002. {
  1003. this.Invoke((EventHandler)(delegate
  1004. {
  1005. textBox_User2.Text = "";
  1006. for (ushort i = 0; i < 16; i++)
  1007. {
  1008. if (Data[i] == 0x2E)
  1009. {
  1010. break;
  1011. }
  1012. textBox_User2.Text += ((char)Data[i]).ToString();
  1013. }
  1014. }));
  1015. }
  1016. break;
  1017. }
  1018. case 0xA810://自定义1
  1019. {
  1020. unchecked
  1021. {
  1022. this.Invoke((EventHandler)(delegate
  1023. {
  1024. textBox_User3.Text = "";
  1025. for (ushort i = 0; i < 16; i++)
  1026. {
  1027. if (Data[i] == 0x2E)
  1028. {
  1029. break;
  1030. }
  1031. textBox_User3.Text += ((char)Data[i]).ToString();
  1032. }
  1033. }));
  1034. }
  1035. break;
  1036. }
  1037. case 0xA520://生产信息
  1038. {
  1039. unchecked
  1040. {
  1041. this.Invoke((EventHandler)(delegate
  1042. {
  1043. //生产商
  1044. textBox_MAC.Text = "";
  1045. for (ushort i = 0; i < 8; i++)
  1046. {
  1047. if (Data[i] == 0x2E)
  1048. {
  1049. break;
  1050. }
  1051. textBox_MAC.Text += ((char)Data[i]).ToString();
  1052. }
  1053. //生产地
  1054. textBox_MACAdd.Text = "";
  1055. for (ushort i = 0; i < 8; i++)
  1056. {
  1057. if (Data[8 + i] == 0x2E)
  1058. {
  1059. break;
  1060. }
  1061. textBox_MACAdd.Text += ((char)Data[8 + i]).ToString();
  1062. }
  1063. //生产日期
  1064. textBox_MACDate.Text = "";
  1065. for (ushort i = 0; i < 8; i++)
  1066. {
  1067. if (Data[16 + i] == 0x2E)
  1068. {
  1069. break;
  1070. }
  1071. textBox_MACDate.Text += ((char)Data[16 + i]).ToString();
  1072. }
  1073. //品牌信息
  1074. textBox_PP.Text = "";
  1075. for (ushort i = 0; i < 8; i++)
  1076. {
  1077. if (Data[24 + i] == 0x2E)
  1078. {
  1079. break;
  1080. }
  1081. textBox_PP.Text += ((char)Data[24 + i]).ToString();
  1082. }
  1083. }));
  1084. }
  1085. break;
  1086. }
  1087. case 0xA408://密钥
  1088. {
  1089. unchecked
  1090. {
  1091. this.Invoke((EventHandler)(delegate
  1092. {
  1093. textBox_Key.Text = "";
  1094. for (ushort i = 0; i < 8; i++)
  1095. {
  1096. textBox_Key.Text += ((char)Data[i]).ToString();
  1097. }
  1098. }));
  1099. }
  1100. break;
  1101. }
  1102. case 0xAB09://存储器数据
  1103. case 0xAB0A:
  1104. case 0xAB0B:
  1105. case 0xAB0C:
  1106. {
  1107. unchecked
  1108. {
  1109. this.Invoke((EventHandler)(delegate
  1110. {
  1111. long ByteNum = 0;
  1112. ByteNum = ((Data[4] << 24) + (Data[5] << 16) + (Data[6] << 8) + Data[7]) - ((Data[0] << 24) + (Data[1] << 16) + (Data[2] << 8) + Data[3]) + 1;
  1113. if (ByteNum > 4) //数据长度超过4Bytes,按照16进制显示字节流
  1114. {
  1115. for (int i = 0; i < ByteNum; i++)
  1116. {
  1117. richTextBox_RamFlasgData.AppendText(Convert.ToString(Data[8 + i], 16).PadLeft(2, '0').ToUpper() + " ");
  1118. }
  1119. }
  1120. else //根据设置自动转换10进制显示
  1121. {
  1122. if (checkBox_ReadRanFlash_ChangeFormat.Checked == true) //10进制显示
  1123. {
  1124. switch (ByteNum)
  1125. {
  1126. case 1:
  1127. {
  1128. richTextBox_RamFlasgData.AppendText(Convert.ToString(Data[8]) + ", ");
  1129. break;
  1130. }
  1131. case 2:
  1132. {
  1133. richTextBox_RamFlasgData.AppendText(Convert.ToString(Data[8] + (Data[9] << 8)) + ", ");
  1134. break;
  1135. }
  1136. case 3:
  1137. {
  1138. richTextBox_RamFlasgData.AppendText(Convert.ToString(Data[8] + (Data[9] << 8) + (Data[10] << 16)) + ", ");
  1139. break;
  1140. }
  1141. case 4:
  1142. {
  1143. richTextBox_RamFlasgData.AppendText(Convert.ToString(Data[8] + (Data[9] << 8) + (Data[10] << 16) + (Data[11] << 24)) + ", ");
  1144. break;
  1145. }
  1146. default:
  1147. {
  1148. richTextBox_RamFlasgData.AppendText("无效数据" + ", ");
  1149. break;
  1150. }
  1151. }
  1152. }
  1153. else //16进制显示
  1154. {
  1155. switch (ByteNum)
  1156. {
  1157. case 1:
  1158. {
  1159. richTextBox_RamFlasgData.AppendText("0x" + Convert.ToString(Data[8], 16).PadLeft(2, '0').ToUpper() + ", ");
  1160. break;
  1161. }
  1162. case 2:
  1163. {
  1164. richTextBox_RamFlasgData.AppendText("0x" + Convert.ToString(Data[8] + (Data[9] << 8), 16).PadLeft(4, '0').ToUpper() + ", ");
  1165. break;
  1166. }
  1167. case 3:
  1168. {
  1169. richTextBox_RamFlasgData.AppendText("0x" + Convert.ToString(Data[8] + (Data[9] << 8) + (Data[10] << 16), 16).PadLeft(6, '0').ToUpper() + ", ");
  1170. break;
  1171. }
  1172. case 4:
  1173. {
  1174. richTextBox_RamFlasgData.AppendText("0x" + Convert.ToString(Data[8] + (Data[9] << 8) + (Data[10] << 16) + (Data[11] << 24), 16).PadLeft(8, '0').ToUpper() + ", ");
  1175. break;
  1176. }
  1177. default:
  1178. {
  1179. richTextBox_RamFlasgData.AppendText("无效数据" + ", ");
  1180. break;
  1181. }
  1182. }
  1183. }
  1184. }
  1185. }));
  1186. }
  1187. break;
  1188. }
  1189. case 0xAB88://故障日志
  1190. {
  1191. unchecked
  1192. {
  1193. this.Invoke((EventHandler)(delegate
  1194. {
  1195. //电机存储的故障日志,一包128字节包含2条故障日志
  1196. var ErrorLogSaveInfo1 = new MC_ErrorLogSaveInfo_Struct_t();
  1197. var ErrorLogSaveInfo2 = new MC_ErrorLogSaveInfo_Struct_t();
  1198. ErrorLogSaveInfo1 = MyConverter.ByteToStructure<MC_ErrorLogSaveInfo_Struct_t>(Data, 8);
  1199. ErrorLogSaveInfo2 = MyConverter.ByteToStructure<MC_ErrorLogSaveInfo_Struct_t>(Data, 72);
  1200. //数据显示
  1201. ErrorLogDisplay(ErrorLogSaveInfo1, ErrorLogSaveInfo2);
  1202. }));
  1203. }
  1204. break;
  1205. }
  1206. case 0x1401://在线检测结果
  1207. {
  1208. unchecked
  1209. {
  1210. this.Invoke((EventHandler)(delegate
  1211. {
  1212. textBox_Online.Text = Convert.ToString(Data[3], 16).PadLeft(2, '0').ToUpper() + "H";
  1213. }));
  1214. }
  1215. break;
  1216. }
  1217. case 0x1510://骑行历史信息
  1218. {
  1219. unchecked
  1220. {
  1221. this.Invoke((EventHandler)(delegate
  1222. {
  1223. long DataTemp32;
  1224. //ODO里程
  1225. DataTemp32 = (long)((Data[3] << 24) + (Data[2] << 16) + (Data[1] << 8) + Data[0]);
  1226. textBox_OBC_ODO_KM.Text = ((float)DataTemp32 / 10f).ToString("0.0") + " km";
  1227. //ODO时间
  1228. DataTemp32 = (long)((Data[7] << 24) + (Data[6] << 16) + (Data[5] << 8) + Data[4]);
  1229. if (DataTemp32 >= 60 * 100000)
  1230. textBox_OBC_ODO_TIME.Text = Convert.ToString(DataTemp32 / 60) + " h";
  1231. else
  1232. textBox_OBC_ODO_TIME.Text = Convert.ToString(DataTemp32 / 60) + " h " + Convert.ToString(DataTemp32 % 60) + " m";
  1233. //TRIP里程
  1234. DataTemp32 = (long)((Data[11] << 24) + (Data[10] << 16) + (Data[9] << 8) + Data[8]);
  1235. textBox_OBC_TRIP_KM.Text = ((float)DataTemp32 / 10f).ToString("0.0") + " km";
  1236. //TRIP时间
  1237. DataTemp32 = (long)((Data[15] << 24) + (Data[14] << 16) + (Data[13] << 8) + Data[12]);
  1238. if (DataTemp32 >= 60 * 100000)
  1239. textBox_OBC_TRIP_TIME.Text = Convert.ToString(DataTemp32 / 60) + " h";
  1240. else
  1241. textBox_OBC_TRIP_TIME.Text = Convert.ToString(DataTemp32 / 60) + " h " + Convert.ToString(DataTemp32 % 60) + " m";
  1242. }));
  1243. }
  1244. break;
  1245. }
  1246. case 0x1720://客户信息
  1247. {
  1248. unchecked
  1249. {
  1250. this.Invoke((EventHandler)(delegate
  1251. {
  1252. //程序特性
  1253. textBox_SP.Text = "";
  1254. textBox_OBC_ReadSP.Text = "";
  1255. textBox_FacModeSP.Text = "";
  1256. for (ushort i = 0; i < 32; i++)
  1257. {
  1258. if (Data[i] == 0x2E)
  1259. {
  1260. break;
  1261. }
  1262. if (Data[i] == 'r')
  1263. {
  1264. Data[i] = (byte)'.';
  1265. }
  1266. textBox_SP.Text += ((char)Data[i]).ToString();
  1267. textBox_OBC_ReadSP.Text += ((char)Data[i]).ToString();
  1268. textBox_FacModeSP.Text += ((char)Data[i]).ToString(); ;
  1269. }
  1270. //Git提交版本信息
  1271. textBox_FacModeGit.Text = "";
  1272. for (ushort i = 0; i < 9; i++)
  1273. {
  1274. textBox_FacModeGit.Text += ((char)Data[23 + i]).ToString();
  1275. }
  1276. Class_Motor_Ver.Special = textBox_SP.Text;
  1277. }));
  1278. }
  1279. break;
  1280. }
  1281. default:
  1282. {
  1283. break;
  1284. }
  1285. }
  1286. }
  1287. #endregion
  1288. #region 解析BMS广播的命令
  1289. else if (ID == 0x720)
  1290. {
  1291. switch (CmdTemp)
  1292. {
  1293. case 0x1010://BMS运行信息
  1294. {
  1295. unchecked
  1296. {
  1297. this.Invoke((EventHandler)(delegate
  1298. {
  1299. BMS_RunInfo_Refresh = true;
  1300. richTextBox_OBC_BMS_RunInfo.Clear();
  1301. //电压
  1302. uData16 = (ushort)(Data[1] * 256 + Data[0]);
  1303. richTextBox_OBC_BMS_RunInfo.AppendText("电压:" + Convert.ToString(uData16) + " mV" + "\r\n");
  1304. //电流
  1305. uData16 = (ushort)(Data[3] * 256 + Data[2]);
  1306. richTextBox_OBC_BMS_RunInfo.AppendText("电流:" + Convert.ToString(uData16) + " mA" + "\r\n");
  1307. //剩余容量
  1308. uData16 = (ushort)(Data[5] * 256 + Data[4]);
  1309. richTextBox_OBC_BMS_RunInfo.AppendText("剩余容量:" + Convert.ToString(uData16) + " mAh" + "\r\n");
  1310. //满充容量
  1311. uData16 = (ushort)(Data[7] * 256 + Data[6]);
  1312. richTextBox_OBC_BMS_RunInfo.AppendText("满充容量:" + Convert.ToString(uData16) + " mV" + "\r\n");
  1313. //电芯温度
  1314. richTextBox_OBC_BMS_RunInfo.AppendText("电芯温度:" + Convert.ToString((int)(Data[8] - 40) + " ℃" + "\r\n"));
  1315. //剩余电量
  1316. richTextBox_OBC_BMS_RunInfo.AppendText("剩余电量:" + Convert.ToString(Data[9]) + " %" + "\r\n");
  1317. //电池状态
  1318. richTextBox_OBC_BMS_RunInfo.AppendText("电池状态:" + Convert.ToString(Data[10], 16).PadLeft(2, '0').ToUpper() + " H" + "\r\n");
  1319. //SOH
  1320. richTextBox_OBC_BMS_RunInfo.AppendText("电池寿命:" + Convert.ToString(Data[11]) + " %");
  1321. //循环次数
  1322. richTextBox_OBC_BMS_RunInfo.AppendText("循环次数:" + Convert.ToString((ushort)(Data[13] * 256 + Data[12])) + " 次");
  1323. }));
  1324. }
  1325. break;
  1326. }
  1327. default: break;
  1328. }
  1329. }
  1330. #endregion
  1331. #region 解析电机发送OBC的命令
  1332. else if (ID == 0x713)
  1333. {
  1334. switch (CmdTemp)
  1335. {
  1336. case 0x5408://电机用户参数
  1337. {
  1338. unchecked
  1339. {
  1340. this.Invoke((EventHandler)(delegate
  1341. {
  1342. richTextBox_OBC_ReadUserInfo.Clear();
  1343. //默认周长
  1344. richTextBox_OBC_ReadUserInfo.AppendText("默认周长:" + Convert.ToString((int)Data[0]) + " cm" + "\r\n");
  1345. //启动模式
  1346. richTextBox_OBC_ReadUserInfo.AppendText("启动模式:" + ((Data[1] == 0x01) ? "柔和" : (Data[1] == 0x02) ? "正常" : (Data[1] == 0x03) ? "强劲" : "无效") + "\r\n");
  1347. comboBox_OBC_StartMode.SelectedIndex = Data[1] - 1;
  1348. //限速
  1349. richTextBox_OBC_ReadUserInfo.AppendText("限速:" + Convert.ToString(Data[2]) + " km/h" + "\r\n");
  1350. //周长微调
  1351. numericUpDown_OBC_WheelAdj.Value = (int)(Data[3] > 128 ? (Data[3] - 256) : Data[3]);
  1352. richTextBox_OBC_ReadUserInfo.AppendText("周长微调:" + Convert.ToString(numericUpDown_OBC_WheelAdj.Value) + " cm" + "\r\n");
  1353. //助力方案
  1354. richTextBox_OBC_ReadUserInfo.AppendText("助力方案:" + Convert.ToString(Data[4]) + "\r\n");
  1355. comboBox_OBC_AssistFunc.SelectedIndex = Data[4] - 1;
  1356. //关机时间
  1357. richTextBox_OBC_ReadUserInfo.AppendText("关机时间:" + Convert.ToString(Data[5]) + " min" + "\r\n");
  1358. numericUpDown_OBC_OffTime.Value = (int)Data[5];
  1359. }));
  1360. }
  1361. break;
  1362. }
  1363. case 0x5303://电机应答反馈
  1364. {
  1365. unchecked
  1366. {
  1367. this.Invoke((EventHandler)(delegate
  1368. {
  1369. timer_1s.Enabled = false;
  1370. MessageBox.Show("OK", "反馈指令", MessageBoxButtons.OK, MessageBoxIcon.Information);
  1371. timer_1s.Enabled = true;
  1372. }));
  1373. }
  1374. break;
  1375. }
  1376. default: break;
  1377. }
  1378. }
  1379. #endregion
  1380. }
  1381. #endregion
  1382. /// <summary>
  1383. /// 端口连接或断开
  1384. /// </summary>
  1385. private void PortConnect()
  1386. {
  1387. bool result = false;
  1388. if (连接ToolStripMenuItem.Text == "连接")
  1389. {
  1390. result = mySerialProcess.SerialOpen(toolStripComboBox_ComNum.Text, toolStripComboBox_Baudrate.Text, 连接开机ToolStripMenuItem.Checked, 识别通讯盒ToolStripMenuItem.Checked);
  1391. if (result)
  1392. {
  1393. toolStripComboBox_ComNum.Enabled = false;
  1394. toolStripComboBox_Baudrate.Enabled = false;
  1395. 刷新ToolStripMenuItem.Enabled = false;
  1396. 连接ToolStripMenuItem.Text = "断开";
  1397. 连接ToolStripMenuItem.Checked = true;
  1398. label_COM_Sta.Text = toolStripComboBox_ComNum.Text + "已连接," + toolStripComboBox_Baudrate.Text;
  1399. label_PortStatus.BackColor = Color.Green;
  1400. }
  1401. }
  1402. else if (连接ToolStripMenuItem.Text == "断开")
  1403. {
  1404. checkBox_OBC_StartSetGearSt.Checked = false;
  1405. checkBox_OBC_StartReadBMS.Checked = false;
  1406. checkBox_ReadRanFlash_AutoSW.Checked = false;
  1407. mySerialProcess.SerialClose(断开关机ToolStripMenuItem.Checked);
  1408. toolStripComboBox_ComNum.Enabled = true;
  1409. toolStripComboBox_Baudrate.Enabled = true;
  1410. 刷新ToolStripMenuItem.Enabled = true;
  1411. 连接ToolStripMenuItem.Text = "连接";
  1412. 连接ToolStripMenuItem.Checked = false;
  1413. label_COM_Sta.Text = "连接状态:未连接";
  1414. label_PortStatus.BackColor = Color.Red;
  1415. }
  1416. }
  1417. /// <summary>
  1418. /// 端口连接事件
  1419. /// </summary>
  1420. /// <param name="sender"></param>
  1421. /// <param name="e"></param>
  1422. private void 连接ToolStripMenuItem_Click(object sender, System.EventArgs e)
  1423. {
  1424. PortConnect();
  1425. }
  1426. /// <summary>
  1427. /// 端口号刷新事件
  1428. /// </summary>
  1429. /// <param name="sender"></param>
  1430. /// <param name="e"></param>
  1431. private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
  1432. {
  1433. toolStripComboBox_ComNum.Items.Clear();
  1434. toolStripComboBox_ComNum.Items.AddRange(mySerialProcess.refreshPort());
  1435. toolStripComboBox_ComNum.SelectedIndex = toolStripComboBox_ComNum.Items.Count > 0 ? 0 : -1;
  1436. }
  1437. /// <summary>
  1438. /// 转速调节事件
  1439. /// </summary>
  1440. /// <param name="sender"></param>
  1441. /// <param name="e"></param>
  1442. private void numericUpDown_SpeedAdj_ValueChanged(object sender, EventArgs e)
  1443. {
  1444. //生产模式转速设置值更新
  1445. trackBar_FacModeMotorSpeedAdj.Value = (int)numericUpDown_SpeedAdj.Value;
  1446. label_FacModeMotorSpeedSet.Text = trackBar_FacModeMotorSpeedAdj.Value.ToString() + "%";
  1447. //设置转速百分比
  1448. var Data = new byte[1];
  1449. Data[0] = (byte)numericUpDown_SpeedAdj.Value;
  1450. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2C01, Data);
  1451. }
  1452. /// <summary>
  1453. /// 存储标志修改事件
  1454. /// </summary>
  1455. /// <param name="sender"></param>
  1456. /// <param name="e"></param>
  1457. private void 写入存储ToolStripMenuItem_Click(object sender, EventArgs e)
  1458. {
  1459. 写入存储ToolStripMenuItem.Checked = !写入存储ToolStripMenuItem.Checked;
  1460. ConfigFileSave(true, ConfigFileName);
  1461. }
  1462. /// <summary>
  1463. /// 1s定时器
  1464. /// </summary>
  1465. /// <param name="sender"></param>
  1466. /// <param name="e"></param>
  1467. private void timer_1s_Tick(object sender, EventArgs e)
  1468. {
  1469. //时间更新
  1470. label_SystemTime.Text = "系统时间:" + DateTime.Now.ToString();
  1471. //MC_Runinfo超时清除
  1472. if (MC_RunInfo_Refresh == false)
  1473. {
  1474. MC_RunInfo_Refresh_Cnt++;
  1475. if (MC_RunInfo_Refresh_Cnt > 10)//5s
  1476. {
  1477. MC_RunInfo_Clear();
  1478. MC_RunInfo_Refresh_Cnt = 0;
  1479. }
  1480. }
  1481. else
  1482. {
  1483. MC_RunInfo_Refresh_Cnt = 0;
  1484. }
  1485. MC_RunInfo_Refresh = false;
  1486. //BMS_RunInfo超时清除
  1487. if (BMS_RunInfo_Refresh == false)
  1488. {
  1489. BMS_RunInfo_Refresh_Cnt++;
  1490. if (BMS_RunInfo_Refresh_Cnt > 10)//5s
  1491. {
  1492. richTextBox_OBC_BMS_RunInfo.Clear();
  1493. BMS_RunInfo_Refresh_Cnt = 0;
  1494. }
  1495. }
  1496. else
  1497. {
  1498. BMS_RunInfo_Refresh_Cnt = 0;
  1499. }
  1500. BMS_RunInfo_Refresh = false;
  1501. //故障码超时清除
  1502. if (MC_ErrorCode_Refresh == false)
  1503. {
  1504. MC_ErrorCode_Refresh_Cnt++;
  1505. if (MC_ErrorCode_Refresh_Cnt > 10)//5s
  1506. {
  1507. textBox_ErrorCode.Text = "---";
  1508. MC_ErrorCode_Refresh_Cnt = 0;
  1509. }
  1510. }
  1511. else
  1512. {
  1513. MC_ErrorCode_Refresh_Cnt = 0;
  1514. }
  1515. MC_ErrorCode_Refresh = false;
  1516. //CDL连接超时
  1517. if (识别通讯盒ToolStripMenuItem.Checked)
  1518. {
  1519. if (mySerialProcess.CDL_Online_Flag == false)
  1520. {
  1521. mySerialProcess.CDL_OnlineCheck_Cnt++;
  1522. if (mySerialProcess.CDL_OnlineCheck_Cnt > 1)//1s
  1523. {
  1524. timer_1s.Enabled = false;
  1525. MessageBox.Show("连接失败!", "提示");
  1526. timer_1s.Enabled = true;
  1527. //关闭串口
  1528. mySerialProcess.SerialClose(断开关机ToolStripMenuItem.Checked);
  1529. toolStripComboBox_ComNum.Enabled = true;
  1530. toolStripComboBox_Baudrate.Enabled = true;
  1531. 刷新ToolStripMenuItem.Enabled = true;
  1532. 连接ToolStripMenuItem.Text = "连接";
  1533. 连接ToolStripMenuItem.Checked = false;
  1534. label_COM_Sta.Text = "连接状态:未连接";
  1535. label_PortStatus.BackColor = Color.Red;
  1536. }
  1537. }
  1538. }
  1539. //OBC定时发送设置档位
  1540. if (checkBox_OBC_StartSetGearSt.Checked == true)
  1541. {
  1542. var CtrlCode = new byte[2];
  1543. if (comboBox_OBC_SetGearST.SelectedIndex == 5)//档位
  1544. CtrlCode[0] = 0x33;
  1545. else if (comboBox_OBC_SetGearST.SelectedIndex == 6)//档位
  1546. CtrlCode[0] = 0x22;
  1547. else
  1548. CtrlCode[0] = (byte)comboBox_OBC_SetGearST.SelectedIndex;
  1549. if (comboBox_OBC_LightSw.SelectedIndex == 0)//车灯
  1550. CtrlCode[1] = 0xF1;
  1551. else
  1552. CtrlCode[1] = 0xF0;
  1553. if (!mySerialProcess.SendCmdWithoutNotice((ushort)0x731, (byte)0x0C, (ushort)0x3002, CtrlCode))
  1554. {
  1555. checkBox_OBC_StartSetGearSt.Checked = false;
  1556. }
  1557. }
  1558. //OBC定时发送查询电池
  1559. if (checkBox_OBC_StartReadBMS.Checked == true)
  1560. {
  1561. if (!mySerialProcess.SendCmd((ushort)0x732, (byte)0x11, (ushort)0x5000, null))
  1562. {
  1563. checkBox_OBC_StartReadBMS.Checked = false;
  1564. }
  1565. }
  1566. //定时读取存储器
  1567. if (checkBox_ReadRanFlash_AutoSW.Checked == true)
  1568. {
  1569. var Address = new byte[4];
  1570. UInt32 Address_Begin;
  1571. UInt32 Addres_End;
  1572. if (checkBox_ReadRanFlash_AutoClear.Checked == true)
  1573. richTextBox_RamFlasgData.Clear();
  1574. do
  1575. {
  1576. if ((textBox_Address_Begin.Text == string.Empty) || (textBox_Address_End.Text == string.Empty))
  1577. {
  1578. checkBox_ReadRanFlash_AutoSW.Checked = false;
  1579. MessageBox.Show("请输入起始和结束地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  1580. continue;
  1581. }
  1582. else if ((textBox_Address_Begin.Text.Length != 8) || (textBox_Address_End.Text.Length != 8))
  1583. {
  1584. checkBox_ReadRanFlash_AutoSW.Checked = false;
  1585. MessageBox.Show("请检查地址长度!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  1586. continue;
  1587. }
  1588. else
  1589. {
  1590. Address = mySerialProcess.HexStringToBytes(textBox_Address_Begin.Text, false);
  1591. Address_Begin = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
  1592. Address = mySerialProcess.HexStringToBytes(textBox_Address_End.Text, false);
  1593. Addres_End = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
  1594. if (Addres_End < Address_Begin)
  1595. {
  1596. checkBox_ReadRanFlash_AutoSW.Checked = false;
  1597. MessageBox.Show("结束地址必须大于或等于起始地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  1598. continue;
  1599. }
  1600. else
  1601. {
  1602. var SendByte = new byte[8];
  1603. SendByte[0] = (byte)(Address_Begin >> 24);
  1604. SendByte[1] = (byte)(Address_Begin >> 16);
  1605. SendByte[2] = (byte)(Address_Begin >> 8);
  1606. SendByte[3] = (byte)(Address_Begin);
  1607. SendByte[4] = (byte)(Addres_End >> 24);
  1608. SendByte[5] = (byte)(Addres_End >> 16);
  1609. SendByte[6] = (byte)(Addres_End >> 8);
  1610. SendByte[7] = (byte)(Addres_End);
  1611. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2D08, SendByte))
  1612. checkBox_ReadRanFlash_AutoSW.Checked = false;
  1613. }
  1614. }
  1615. } while (false);
  1616. }
  1617. }
  1618. /// <summary>
  1619. /// 字符串转成字节流
  1620. /// </summary>
  1621. /// <param name="hexStr"></param>
  1622. /// <param name="Flag_Space"></param>
  1623. /// <returns></returns>
  1624. public static byte[] HexStringToBytes(string hexStr, bool Flag_Space)
  1625. {
  1626. if (string.IsNullOrEmpty(hexStr))
  1627. {
  1628. return new byte[0];
  1629. }
  1630. if (hexStr.StartsWith("0x"))
  1631. {
  1632. hexStr = hexStr.Remove(0, 2);
  1633. }
  1634. var count = hexStr.Length;
  1635. var byteCount = 0;
  1636. if (Flag_Space == true)//相邻字节含空格
  1637. {
  1638. if ((count % 3) == 0)//最后字节含空格
  1639. {
  1640. byteCount = count / 3;
  1641. }
  1642. else if ((count % 3) == 2)//最后字节不含空格
  1643. {
  1644. byteCount = (count + 1) / 3;
  1645. }
  1646. else//数据无效
  1647. {
  1648. MessageBox.Show("相邻字节请插入空格!", "提示");
  1649. }
  1650. var result = new byte[byteCount];
  1651. for (int ii = 0; ii < byteCount; ++ii)
  1652. {
  1653. var tempBytes = Byte.Parse(hexStr.Substring(3 * ii, 2), System.Globalization.NumberStyles.HexNumber);
  1654. result[ii] = tempBytes;
  1655. }
  1656. return result;
  1657. }
  1658. else if (Flag_Space == false)//相邻字节不含空格
  1659. {
  1660. if (count % 2 == 1)
  1661. {
  1662. MessageBox.Show("请删除相邻字节之间空格!", "提示");
  1663. }
  1664. byteCount = count / 2;
  1665. var result = new byte[byteCount];
  1666. for (int ii = 0; ii < byteCount; ++ii)
  1667. {
  1668. var tempBytes = Byte.Parse(hexStr.Substring(2 * ii, 2), System.Globalization.NumberStyles.HexNumber);
  1669. result[ii] = tempBytes;
  1670. }
  1671. return result;
  1672. }
  1673. else
  1674. {
  1675. return new byte[0];
  1676. }
  1677. }
  1678. /// <summary>
  1679. /// 运行信息清除
  1680. /// </summary>
  1681. private void MC_RunInfo_Clear()
  1682. {
  1683. foreach (Control c in groupBox2.Controls)
  1684. {
  1685. if ((c is TextBox))
  1686. {
  1687. if(c.Name != textBox_ErrorCode.ToString())
  1688. c.Text = "---";
  1689. }
  1690. }
  1691. }
  1692. /// <summary>
  1693. /// 读取马达参数
  1694. /// </summary>
  1695. /// <param name="sender"></param>
  1696. /// <param name="e"></param>
  1697. private void button_Read_MotorParam_Click(object sender, EventArgs e)
  1698. {
  1699. richTextBox_MotorParam.Clear();
  1700. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3A00, null);
  1701. }
  1702. /// <summary>
  1703. /// 写入马达参数
  1704. /// </summary>
  1705. /// <param name="sender"></param>
  1706. /// <param name="e"></param>
  1707. private void button_Write_Click(object sender, EventArgs e)
  1708. {
  1709. var ConfigParam = new byte[40];
  1710. for (int i = 0; i < 40; i++)
  1711. ConfigParam[i] = 0;
  1712. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  1713. short wDataTemp = 0;
  1714. string[] strDataTemp = richTextBox_MotorParam.Text.Split(new string[] { ", "},StringSplitOptions.None);
  1715. try
  1716. {
  1717. for (int i = 0; i < strDataTemp.Length; i++)
  1718. {
  1719. wDataTemp = Convert.ToInt16(strDataTemp[i].Split('=')[1]);
  1720. ConfigParam[2 * i + 2] = (byte)wDataTemp;
  1721. ConfigParam[2 * i + 3] = (byte)(wDataTemp >> 8);
  1722. }
  1723. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3B28, ConfigParam);
  1724. }
  1725. catch (Exception)
  1726. {
  1727. timer_1s.Enabled = false;
  1728. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1729. timer_1s.Enabled = true;
  1730. }
  1731. }
  1732. /// <summary>
  1733. /// 读取整车信息
  1734. /// </summary>
  1735. /// <param name="sender"></param>
  1736. /// <param name="e"></param>
  1737. private void button_ReadBikeParam_Click(object sender, EventArgs e)
  1738. {
  1739. richTextBox_BikeParam.Clear();
  1740. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3C00, null);
  1741. }
  1742. /// <summary>
  1743. /// 写入整车信息
  1744. /// </summary>
  1745. /// <param name="sender"></param>
  1746. /// <param name="e"></param>
  1747. private void button_WriteBikeParam_Click(object sender, EventArgs e)
  1748. {
  1749. var ConfigParam = new byte[28];
  1750. for (int i = 0; i < 28; i++)
  1751. ConfigParam[i] = 0;
  1752. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  1753. string[] strDataTemp = richTextBox_BikeParam.Text.Split(new string[] { ", " }, StringSplitOptions.None);
  1754. try
  1755. {
  1756. for (int i = 0; i < strDataTemp.Length; i++)
  1757. {
  1758. short wDataTemp = 0;
  1759. //前后灯参数修改
  1760. if (i == 8)
  1761. {
  1762. wDataTemp |= (short)((comboBox_TailLightMode.SelectedIndex + 1) << 12); //Bit15~Bit12:尾灯模式
  1763. wDataTemp |= (short)((comboBox_TailLightVol.SelectedIndex == 0 ? 6 : 12) << 8); //Bit11~Bit8:尾灯电压
  1764. wDataTemp |= (short)(comboBox_FrontLightVol.SelectedIndex == 0 ? 6 : 12); //Bit7~Bit0:前灯电压
  1765. }
  1766. //开关机参数
  1767. else if (i == 11)
  1768. {
  1769. wDataTemp |= (short)(Convert.ToInt16(textBox_PowerOnDelay.Text) << 12);
  1770. wDataTemp |= (short)(Convert.ToInt16(textBox_PowerOffDelay.Text) << 8);
  1771. wDataTemp |= (short)(Convert.ToInt16(textBox_AutoPowerOff.Text));
  1772. }
  1773. else
  1774. {
  1775. wDataTemp = Convert.ToInt16(strDataTemp[i].Split('=')[1]);
  1776. }
  1777. ConfigParam[2 * i + 2] = (byte)wDataTemp;
  1778. ConfigParam[2 * i + 3] = (byte)(wDataTemp >> 8);
  1779. }
  1780. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3D1C, ConfigParam);
  1781. }
  1782. catch (Exception)
  1783. {
  1784. timer_1s.Enabled = false;
  1785. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1786. timer_1s.Enabled = true;
  1787. }
  1788. }
  1789. /// <summary>
  1790. /// 读取控制器参数
  1791. /// </summary>
  1792. /// <param name="sender"></param>
  1793. /// <param name="e"></param>
  1794. private void button_ReadControlParam_Click(object sender, EventArgs e)
  1795. {
  1796. richTextBox_ControlParam.Clear();
  1797. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3E00, null);
  1798. }
  1799. /// <summary>
  1800. /// 写入控制器参数
  1801. /// </summary>
  1802. /// <param name="sender"></param>
  1803. /// <param name="e"></param>
  1804. private void button_WriteControlParam_Click(object sender, EventArgs e)
  1805. {
  1806. var ConfigParam = new byte[34];
  1807. for (int i = 0; i < 34; i++)
  1808. ConfigParam[i] = 0;
  1809. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  1810. ushort uwDataTemp = 0;
  1811. string[] strDataTemp = richTextBox_ControlParam.Text.Split(new string[] { ", " }, StringSplitOptions.None);
  1812. try
  1813. {
  1814. for (int i = 0; i < strDataTemp.Length; i++)
  1815. {
  1816. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  1817. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  1818. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  1819. }
  1820. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3F22, ConfigParam);
  1821. }
  1822. catch (Exception)
  1823. {
  1824. timer_1s.Enabled = false;
  1825. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1826. timer_1s.Enabled = true;
  1827. }
  1828. }
  1829. /// <summary>
  1830. /// 读取助力参数
  1831. /// </summary>
  1832. /// <param name="sender"></param>
  1833. /// <param name="e"></param>
  1834. private void button_ReadAssistParam_Click(object sender, EventArgs e)
  1835. {
  1836. var ConfigParam = new byte[4];
  1837. int AssistNum = 0;
  1838. try
  1839. {
  1840. AssistNum = Convert.ToInt32(comboBox_AssistTorque.Text);
  1841. ConfigParam[0] = (byte)(AssistNum & 0xFF);
  1842. ConfigParam[1] = (byte)(AssistNum >> 8);
  1843. AssistNum = Convert.ToInt32(comboBox_AssistCadence.Text);
  1844. ConfigParam[2] = (byte)(AssistNum & 0xFF);
  1845. ConfigParam[3] = (byte)(AssistNum >> 8);
  1846. richTextBox_AssistParam.Clear();
  1847. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4304, ConfigParam);
  1848. }
  1849. catch (Exception)
  1850. {
  1851. timer_1s.Enabled = false;
  1852. MessageBox.Show("请选择助力参数编号!","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
  1853. timer_1s.Enabled = true;
  1854. }
  1855. }
  1856. /// <summary>
  1857. /// 写入助力参数
  1858. /// </summary>
  1859. /// <param name="sender"></param>
  1860. /// <param name="e"></param>
  1861. private void button_WriteAssistParam_Click(object sender, EventArgs e)
  1862. {
  1863. var ConfigParam = new byte[80];
  1864. for (int i = 0; i < 80; i++)
  1865. ConfigParam[i] = 0;
  1866. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  1867. long lDataTemp = 0;
  1868. string[] strDataTemp = richTextBox_AssistParam.Text.Split(new string[] { ", " }, StringSplitOptions.None);
  1869. int index_1 = 0, index_2 = 0;
  1870. try
  1871. {
  1872. for (int i = 0; i < strDataTemp.Length; i++)
  1873. {
  1874. if (strDataTemp[i].Split('=')[0] == "转矩曲线.a")
  1875. {
  1876. index_1 = i;
  1877. break;
  1878. }
  1879. }
  1880. for (int i = 0; i < strDataTemp.Length; i++)
  1881. {
  1882. if (strDataTemp[i].Split('=')[0] == "踏频曲线.d")
  1883. {
  1884. index_2 = i;
  1885. break;
  1886. }
  1887. }
  1888. for (int i = 0; i < strDataTemp.Length; i++)
  1889. {
  1890. lDataTemp = Convert.ToInt32(strDataTemp[i].Split('=')[1]);
  1891. if (i < index_1)
  1892. {
  1893. ConfigParam[2 * i + 4] = (byte)lDataTemp;
  1894. ConfigParam[2 * i + 5] = (byte)(lDataTemp >> 8);
  1895. }
  1896. else if (i <= index_2)
  1897. {
  1898. ConfigParam[4 * i - 4] = (byte)lDataTemp;
  1899. ConfigParam[4 * i - 3] = (byte)(lDataTemp >> 8);
  1900. ConfigParam[4 * i - 2] = (byte)(lDataTemp >> 16);
  1901. ConfigParam[4 * i - 1] = (byte)(lDataTemp >> 24);
  1902. }
  1903. else
  1904. {
  1905. ConfigParam[2 * i + 20] = (byte)lDataTemp;
  1906. ConfigParam[2 * i + 21] = (byte)(lDataTemp >> 8);
  1907. }
  1908. }
  1909. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x4450, ConfigParam);
  1910. }
  1911. catch (Exception)
  1912. {
  1913. timer_1s.Enabled = false;
  1914. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1915. timer_1s.Enabled = true;
  1916. }
  1917. }
  1918. /// <summary>
  1919. /// 读取调试参数
  1920. /// </summary>
  1921. /// <param name="sender"></param>
  1922. /// <param name="e"></param>
  1923. private void button_ReadDebugParam_Click(object sender, EventArgs e)
  1924. {
  1925. richTextBox_DebugParam.Clear();
  1926. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4500, null);
  1927. }
  1928. /// <summary>
  1929. /// 写入调试参数
  1930. /// </summary>
  1931. /// <param name="sender"></param>
  1932. /// <param name="e"></param>
  1933. private void button_WriteDebugParam_Click(object sender, EventArgs e)
  1934. {
  1935. var ConfigParam = new byte[60];
  1936. for (int i = 0; i < 60; i++)
  1937. ConfigParam[i] = 0;
  1938. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  1939. ushort uwDataTemp = 0;
  1940. string[] strDataTemp = richTextBox_DebugParam.Text.Split(new string[] { ", " }, StringSplitOptions.None);
  1941. try
  1942. {
  1943. for (int i = 0; i < strDataTemp.Length; i++)
  1944. {
  1945. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  1946. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  1947. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  1948. }
  1949. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x463C, ConfigParam);
  1950. }
  1951. catch (Exception)
  1952. {
  1953. timer_1s.Enabled = false;
  1954. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1955. timer_1s.Enabled = true;
  1956. }
  1957. }
  1958. /// <summary>
  1959. /// 读取历史
  1960. /// </summary>
  1961. /// <param name="sender"></param>
  1962. /// <param name="e"></param>
  1963. private void button_ReadRecord_Click(object sender, EventArgs e)
  1964. {
  1965. richTextBox_Record.Clear();
  1966. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1E00, null);
  1967. }
  1968. /// <summary>
  1969. /// 读取传感器参数
  1970. /// </summary>
  1971. /// <param name="sender"></param>
  1972. /// <param name="e"></param>
  1973. private void button_ReadSensorParam_Click(object sender, EventArgs e)
  1974. {
  1975. richTextBox_SensorParam.Clear();
  1976. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4000, null);
  1977. }
  1978. /// <summary>
  1979. /// 写入力矩传感器标定值
  1980. /// </summary>
  1981. /// <param name="sender"></param>
  1982. /// <param name="e"></param>
  1983. private void button_Write_Cal_Click(object sender, EventArgs e)
  1984. {
  1985. var ConfigParam = new byte[4];
  1986. for (int i = 0; i < 4; i++)
  1987. ConfigParam[i] = 0;
  1988. ushort uwDataTemp = 0;
  1989. try
  1990. {
  1991. uwDataTemp = Convert.ToUInt16(comboBox_TorqueSet.Text);//负载序号
  1992. ConfigParam[0] = (byte)(uwDataTemp & 0xFF);
  1993. ConfigParam[1] = (byte)(uwDataTemp >> 8);
  1994. uwDataTemp = (ushort)(decimal.Parse(textBox_Load.Text) * 10); ;//负载值0.1Nm
  1995. ConfigParam[2] = (byte)(uwDataTemp & 0xFF);
  1996. ConfigParam[3] = (byte)(uwDataTemp >> 8);
  1997. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x4104, ConfigParam);
  1998. }
  1999. catch (Exception)
  2000. {
  2001. timer_1s.Enabled = false;
  2002. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2003. timer_1s.Enabled = true;
  2004. }
  2005. }
  2006. /// <summary>
  2007. /// 写入传感器参数
  2008. /// </summary>
  2009. /// <param name="sender"></param>
  2010. /// <param name="e"></param>
  2011. private void button_WriteSensorParam_Click(object sender, EventArgs e)
  2012. {
  2013. var ConfigParam = new byte[14];
  2014. for (int i = 0; i < 14; i++)
  2015. ConfigParam[i] = 0;
  2016. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  2017. ushort uwDataTemp = 0;
  2018. //力矩传感器参数只写入前面14项与力矩传感器相关的参数
  2019. string[] strDataTemp = richTextBox_SensorParam.Text.Split(new string[] { ", " }, StringSplitOptions.None);
  2020. int index = 0;
  2021. try
  2022. {
  2023. for (int i = 0; i < strDataTemp.Length; i++)
  2024. {
  2025. if (strDataTemp[i].Split('=')[0] == "踏频传感器一圈脉冲数")
  2026. {
  2027. index = i;
  2028. break;
  2029. }
  2030. }
  2031. for (int i = 0; i < (strDataTemp.Length - index); i++)
  2032. {
  2033. uwDataTemp = Convert.ToUInt16(strDataTemp[i + index].Split('=')[1]);
  2034. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  2035. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  2036. }
  2037. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x420E, ConfigParam);
  2038. }
  2039. catch (Exception)
  2040. {
  2041. timer_1s.Enabled = false;
  2042. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2043. timer_1s.Enabled = true;
  2044. }
  2045. }
  2046. /// <summary>
  2047. /// 写入型号
  2048. /// </summary>
  2049. /// <param name="sender"></param>
  2050. /// <param name="e"></param>
  2051. private void button_WriteModel_Click(object sender, EventArgs e)
  2052. {
  2053. string Mode = textBox_Model.Text;
  2054. var ModeArray = new byte[16];
  2055. for (ushort i = 0; i < Mode.Length; i++)
  2056. {
  2057. ModeArray[i] = (byte)Mode[i];
  2058. }
  2059. if (Mode.Length < 16)
  2060. {
  2061. ModeArray[Mode.Length] = (byte)'.';
  2062. for (ushort i = 0; i < 16 - Mode.Length - 1; i++)
  2063. {
  2064. ModeArray[Mode.Length + 1 + i] = 0x20;
  2065. }
  2066. }
  2067. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2210, ModeArray);
  2068. }
  2069. /// <summary>
  2070. /// 写入SN
  2071. /// </summary>
  2072. /// <param name="sender"></param>
  2073. /// <param name="e"></param>
  2074. private void button_WriteSN_Click(object sender, EventArgs e)
  2075. {
  2076. string SN = textBox_SN.Text;
  2077. var SNArray = new byte[16];
  2078. for (ushort i = 0; i < SN.Length; i++)
  2079. {
  2080. SNArray[i] = (byte)SN[i];
  2081. }
  2082. if (SN.Length < 16)
  2083. {
  2084. SNArray[SN.Length] = (byte)'.';
  2085. for (ushort i = 0; i < 16 - SN.Length - 1; i++)
  2086. {
  2087. SNArray[SN.Length + 1 + i] = 0x20;
  2088. }
  2089. }
  2090. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2310, SNArray);
  2091. }
  2092. /// <summary>
  2093. /// 查询版本信息
  2094. /// </summary>
  2095. /// <param name="sender"></param>
  2096. /// <param name="e"></param>
  2097. private void button_ReadVersion_Click(object sender, EventArgs e)
  2098. {
  2099. textBox_Model.Text = "---";
  2100. textBox_SN.Text = "---";
  2101. textBox_HW.Text = "---";
  2102. textBox_FW.Text = "---";
  2103. textBox_SP.Text = "---";
  2104. textBox_OBC_ReadModel.Text = "---";
  2105. textBox_OBC_ReadSN.Text = "---";
  2106. textBox_OBC_ReadHW.Text = "---";
  2107. textBox_OBC_ReadFW.Text = "---";
  2108. textBox_OBC_ReadSP.Text = "---";
  2109. textBox_FacModeName.Text = "---";
  2110. textBox_FacModeNum.Text = "---";
  2111. textBox_FacModeHW.Text = "---";
  2112. textBox_FacModeFW.Text = "---";
  2113. textBox_FacModeSP.Text = "---";
  2114. Class_Motor_Ver.Mode = "---";
  2115. Class_Motor_Ver.SN = "---";
  2116. Class_Motor_Ver.HW = "---";
  2117. Class_Motor_Ver.FW = "---";
  2118. Class_Motor_Ver.Special = "---";
  2119. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
  2120. }
  2121. /// <summary>
  2122. /// 查询自定义1
  2123. /// </summary>
  2124. /// <param name="sender"></param>
  2125. /// <param name="e"></param>
  2126. private void button_ReadUser1_Click(object sender, EventArgs e)
  2127. {
  2128. textBox_User1.Text = "---";
  2129. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1300, null);
  2130. }
  2131. /// <summary>
  2132. /// 查询自定义2
  2133. /// </summary>
  2134. /// <param name="sender"></param>
  2135. /// <param name="e"></param>
  2136. private void button_ReadUser2_Click(object sender, EventArgs e)
  2137. {
  2138. textBox_User2.Text = "---";
  2139. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1500, null);
  2140. }
  2141. /// <summary>
  2142. /// 查询自定义3
  2143. /// </summary>
  2144. /// <param name="sender"></param>
  2145. /// <param name="e"></param>
  2146. private void button_ReadUser3_Click(object sender, EventArgs e)
  2147. {
  2148. textBox_User3.Text = "---";
  2149. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1700, null);
  2150. }
  2151. /// <summary>
  2152. /// 写入自定义字符串1
  2153. /// </summary>
  2154. /// <param name="sender"></param>
  2155. /// <param name="e"></param>
  2156. private void button_WriteUser1_Click(object sender, EventArgs e)
  2157. {
  2158. string User = textBox_User1.Text;
  2159. var UserArray = new byte[16];
  2160. for (ushort i = 0; i < User.Length; i++)
  2161. {
  2162. UserArray[i] = (byte)User[i];
  2163. }
  2164. if (User.Length < 16)
  2165. {
  2166. UserArray[User.Length] = (byte)'.';
  2167. for (ushort i = 0; i < 16 - User.Length - 1; i++)
  2168. {
  2169. UserArray[User.Length + 1 + i] = 0x20;
  2170. }
  2171. }
  2172. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1410, UserArray);
  2173. }
  2174. /// <summary>
  2175. /// 写入自定义字符串2
  2176. /// </summary>
  2177. /// <param name="sender"></param>
  2178. /// <param name="e"></param>
  2179. private void button_WriteUser2_Click(object sender, EventArgs e)
  2180. {
  2181. string User = textBox_User2.Text;
  2182. var UserArray = new byte[16];
  2183. for (ushort i = 0; i < User.Length; i++)
  2184. {
  2185. UserArray[i] = (byte)User[i];
  2186. }
  2187. if (User.Length < 16)
  2188. {
  2189. UserArray[User.Length] = (byte)'.';
  2190. for (ushort i = 0; i < 16 - User.Length - 1; i++)
  2191. {
  2192. UserArray[User.Length + 1 + i] = 0x20;
  2193. }
  2194. }
  2195. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1610, UserArray);
  2196. }
  2197. /// <summary>
  2198. /// 写入自定义字符串3
  2199. /// </summary>
  2200. /// <param name="sender"></param>
  2201. /// <param name="e"></param>
  2202. private void button_WriteUser3_Click(object sender, EventArgs e)
  2203. {
  2204. string User = textBox_User3.Text;
  2205. var UserArray = new byte[16];
  2206. for (ushort i = 0; i < User.Length; i++)
  2207. {
  2208. UserArray[i] = (byte)User[i];
  2209. }
  2210. if (User.Length < 16)
  2211. {
  2212. UserArray[User.Length] = (byte)'.';
  2213. for (ushort i = 0; i < 16 - User.Length - 1; i++)
  2214. {
  2215. UserArray[User.Length + 1 + i] = 0x20;
  2216. }
  2217. }
  2218. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1810, UserArray);
  2219. }
  2220. /// <summary>
  2221. /// 查询生产信息
  2222. /// </summary>
  2223. /// <param name="sender"></param>
  2224. /// <param name="e"></param>
  2225. private void button_ReadMAC_Click(object sender, EventArgs e)
  2226. {
  2227. textBox_MAC.Text = "---";
  2228. textBox_MACAdd.Text = "---";
  2229. textBox_MACDate.Text = "---";
  2230. textBox_PP.Text = "---";
  2231. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1F00, null);
  2232. }
  2233. /// <summary>
  2234. /// 写入生产信息
  2235. /// </summary>
  2236. /// <param name="sender"></param>
  2237. /// <param name="e"></param>
  2238. private void button_WriteMac_Click(object sender, EventArgs e)
  2239. {
  2240. var MACArray = new byte[32];
  2241. //填入生产商
  2242. for (ushort i = 0; i < textBox_MAC.Text.Length; i++)
  2243. {
  2244. MACArray[i] = (byte)textBox_MAC.Text[i];
  2245. }
  2246. if (textBox_MAC.Text.Length < 8)
  2247. {
  2248. MACArray[textBox_MAC.Text.Length] = (byte)'.';
  2249. for (ushort i = 0; i < 8 - textBox_MAC.Text.Length - 1; i++)
  2250. {
  2251. MACArray[textBox_MAC.Text.Length + 1 + i] = 0x20;
  2252. }
  2253. }
  2254. //填入生产地
  2255. for (ushort i = 0; i < textBox_MACAdd.Text.Length; i++)
  2256. {
  2257. MACArray[8 + i] = (byte)textBox_MACAdd.Text[i];
  2258. }
  2259. if (textBox_MACAdd.Text.Length < 8)
  2260. {
  2261. MACArray[textBox_MACAdd.Text.Length + 8] = (byte)'.';
  2262. for (ushort i = 0; i < 8 - textBox_MACAdd.Text.Length - 1; i++)
  2263. {
  2264. MACArray[textBox_MACAdd.Text.Length + 1 + i + 8] = 0x20;
  2265. }
  2266. }
  2267. //填入生产日期
  2268. for (ushort i = 0; i < textBox_MACDate.Text.Length; i++)
  2269. {
  2270. MACArray[16 + i] = (byte)textBox_MACDate.Text[i];
  2271. }
  2272. if (textBox_MACDate.Text.Length < 8)
  2273. {
  2274. MACArray[textBox_MACDate.Text.Length + 16] = (byte)'.';
  2275. for (ushort i = 0; i < 8 - textBox_MACDate.Text.Length - 1; i++)
  2276. {
  2277. MACArray[textBox_MACDate.Text.Length + 1 + i + 16] = 0x20;
  2278. }
  2279. }
  2280. //填入品牌
  2281. for (ushort i = 0; i < textBox_PP.Text.Length; i++)
  2282. {
  2283. MACArray[24 + i] = (byte)textBox_PP.Text[i];
  2284. }
  2285. if (textBox_PP.Text.Length < 8)
  2286. {
  2287. MACArray[textBox_PP.Text.Length + 24] = (byte)'.';
  2288. for (ushort i = 0; i < 8 - textBox_PP.Text.Length - 1; i++)
  2289. {
  2290. MACArray[textBox_PP.Text.Length + 1 + i + 24] = 0x20;
  2291. }
  2292. }
  2293. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2420, MACArray);
  2294. }
  2295. /// <summary>
  2296. /// 查询密钥
  2297. /// </summary>
  2298. /// <param name="sender"></param>
  2299. /// <param name="e"></param>
  2300. private void button_ReadKey_Click(object sender, EventArgs e)
  2301. {
  2302. textBox_Key.Text = "---";
  2303. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1000, null);
  2304. }
  2305. /// <summary>
  2306. /// 写入校验密钥
  2307. /// </summary>
  2308. /// <param name="sender"></param>
  2309. /// <param name="e"></param>
  2310. private void button_WriteKey_Click(object sender, EventArgs e)
  2311. {
  2312. string Key = textBox_Key.Text;
  2313. var KeyArray = new byte[8];
  2314. if (Key.Length != 8)
  2315. {
  2316. timer_1s.Enabled = false;
  2317. MessageBox.Show("请检查字符长度是否为8", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2318. timer_1s.Enabled = true;
  2319. return;
  2320. }
  2321. for (ushort i = 0; i < Key.Length; i++)
  2322. {
  2323. KeyArray[i] = (byte)Key[i];
  2324. }
  2325. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1108, KeyArray);
  2326. }
  2327. /// <summary>
  2328. /// 查询在线检测结果
  2329. /// </summary>
  2330. /// <param name="sender"></param>
  2331. /// <param name="e"></param>
  2332. private void button_ReadOnLine_Click(object sender, EventArgs e)
  2333. {
  2334. textBox_Online.Text = "---";
  2335. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2100, null);
  2336. }
  2337. /// <summary>
  2338. /// 系统清除
  2339. /// </summary>
  2340. /// <param name="sender"></param>
  2341. /// <param name="e"></param>
  2342. private void button_SystemClear_Click(object sender, EventArgs e)
  2343. {
  2344. var Code = new byte[5];
  2345. Code[0] = (byte)'C';
  2346. Code[1] = (byte)'L';
  2347. Code[2] = (byte)'E';
  2348. Code[3] = (byte)'A';
  2349. Code[4] = (byte)'R';
  2350. timer_1s.Enabled = false;
  2351. if (MessageBox.Show("系统清除后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2352. {
  2353. if (MessageBox.Show("请再次确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2354. {
  2355. if (MessageBox.Show("请最后确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2356. {
  2357. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2605, Code);
  2358. MessageBox.Show("系统已关机,请重启!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  2359. }
  2360. }
  2361. }
  2362. timer_1s.Enabled = true;
  2363. }
  2364. /// <summary>
  2365. /// 参数还原
  2366. /// </summary>
  2367. /// <param name="sender"></param>
  2368. /// <param name="e"></param>
  2369. private void button_Recovery_Click(object sender, EventArgs e)
  2370. {
  2371. var Code = new byte[8];
  2372. Code[0] = (byte)'R';
  2373. Code[1] = (byte)'E';
  2374. Code[2] = (byte)'C';
  2375. Code[3] = (byte)'O';
  2376. Code[4] = (byte)'V';
  2377. Code[5] = (byte)'E';
  2378. Code[6] = (byte)'R';
  2379. Code[7] = (byte)'Y';
  2380. timer_1s.Enabled = false;
  2381. if (MessageBox.Show("参数还原后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2382. {
  2383. if (MessageBox.Show("请再次确认", "确认还原?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2384. {
  2385. if (MessageBox.Show("请最后确认", "确认还原?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2386. {
  2387. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2708, Code);
  2388. MessageBox.Show("系统已关机,请重启!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  2389. }
  2390. }
  2391. }
  2392. timer_1s.Enabled = true;
  2393. }
  2394. /// <summary>
  2395. /// 系统复位
  2396. /// </summary>
  2397. /// <param name="sender"></param>
  2398. /// <param name="e"></param>
  2399. private void button_Reset_Click(object sender, EventArgs e)
  2400. {
  2401. var Code_Off = new byte[5];
  2402. var Code_Reset = new byte[5];
  2403. Code_Off[0] = (byte)'O';
  2404. Code_Off[1] = (byte)'F';
  2405. Code_Off[2] = (byte)'F';
  2406. Code_Off[3] = (byte)'.';
  2407. Code_Off[4] = (byte)'.';
  2408. Code_Reset[0] = (byte)'R';
  2409. Code_Reset[1] = (byte)'E';
  2410. Code_Reset[2] = (byte)'S';
  2411. Code_Reset[3] = (byte)'E';
  2412. Code_Reset[4] = (byte)'T';
  2413. timer_1s.Enabled = false;
  2414. if (MessageBox.Show("系统将关机", "确认关机?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2415. {
  2416. timer_1s.Enabled = true;
  2417. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2505, Code_Off);
  2418. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2505, Code_Reset);
  2419. PowerOffAckStatus = false;
  2420. Delay_ms(250);
  2421. int TryCnt = 3;
  2422. while ((TryCnt--) >= 0)
  2423. {
  2424. if (!PowerOffAckStatus)
  2425. {
  2426. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2505, Code_Off);
  2427. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2505, Code_Reset);
  2428. PowerOffAckStatus = false;
  2429. Delay_ms(250);
  2430. }
  2431. else break;
  2432. }
  2433. if (PowerOffAckStatus)
  2434. {
  2435. timer_1s.Enabled = false;
  2436. MessageBox.Show("已关机!", "提示", MessageBoxButtons.OK, MessageBoxIcon.None);
  2437. timer_1s.Enabled = true;
  2438. }
  2439. else
  2440. {
  2441. timer_1s.Enabled = false;
  2442. MessageBox.Show("关机失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2443. timer_1s.Enabled = true;
  2444. }
  2445. }
  2446. timer_1s.Enabled = true;
  2447. }
  2448. /// <summary>
  2449. /// 记录清除
  2450. /// </summary>
  2451. /// <param name="sender"></param>
  2452. /// <param name="e"></param>
  2453. private void button_ClearLog_Click(object sender, EventArgs e)
  2454. {
  2455. var Code = new byte[9];
  2456. Code[0] = (byte)'L';
  2457. Code[1] = (byte)'O';
  2458. Code[2] = (byte)'G';
  2459. Code[3] = (byte)' ';
  2460. Code[4] = (byte)'C';
  2461. Code[5] = (byte)'L';
  2462. Code[6] = (byte)'E';
  2463. Code[7] = (byte)'A';
  2464. Code[8] = (byte)'R';
  2465. timer_1s.Enabled = false;
  2466. if (MessageBox.Show("历史记录信息清除后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2467. {
  2468. if (MessageBox.Show("请再次确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2469. {
  2470. if (MessageBox.Show("请最后确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2471. {
  2472. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3909, Code);
  2473. MessageBox.Show("系统已关机,请重启!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  2474. }
  2475. }
  2476. }
  2477. timer_1s.Enabled = true;
  2478. }
  2479. /// <summary>
  2480. /// 打开系统计算器
  2481. /// </summary>
  2482. /// <param name="sender"></param>
  2483. /// <param name="e"></param>
  2484. private void 计算器ToolStripMenuItem_Click(object sender, EventArgs e)
  2485. {
  2486. System.Diagnostics.Process.Start("calc.exe");
  2487. }
  2488. /// <summary>
  2489. ///故障转换和显示
  2490. /// </summary>
  2491. List<string> ErrorInfo = new List<string>{ "过流保护", "低压保护", "过压保护", "堵转保护","过热保护","速度传感器故障","力矩传感器故障","霍尔故障",
  2492. "马达缺相","NTC故障","BMS校验失败","","OBC校验失败","MCU故障","踏频传感器故障","指拨故障","MOS短路","电压异常","","","","","","","","","","","","",
  2493. "",""};
  2494. private string ErrorInfoDisplay(UInt32 Code)
  2495. {
  2496. string Result = "";
  2497. if (Code == 0)
  2498. {
  2499. Result = "无故障";
  2500. }
  2501. else
  2502. {
  2503. for (int i = 0; i < 32; i++)
  2504. {
  2505. if ((Code & 0x01) == 0x01)
  2506. {
  2507. Result += ErrorInfo[i] + " ";
  2508. }
  2509. Code >>= 1;
  2510. }
  2511. }
  2512. return Result;
  2513. }
  2514. /// <summary>
  2515. /// 鼠标滑过故障码显示具体内容
  2516. /// </summary>
  2517. /// <param name="sender"></param>
  2518. /// <param name="e"></param>
  2519. private void textBox_ErrorCode_MouseHover(object sender, EventArgs e)
  2520. {
  2521. // 创建the ToolTip
  2522. ToolTip toolTip1 = new ToolTip();
  2523. // 设置显示样式
  2524. toolTip1.AutoPopDelay = 30000;//提示信息的可见时间
  2525. toolTip1.InitialDelay = 50;//事件触发多久后出现提示
  2526. toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  2527. toolTip1.ShowAlways = true;//是否显示提示框
  2528. // 设置伴随的对象.
  2529. string ErrorCode = this.textBox_ErrorCode.Text;
  2530. UInt32 Code = ErrorCode.Contains("H") ? Convert.ToUInt32(ErrorCode.Split('H')[0], 16) : 0;
  2531. toolTip1.SetToolTip(this.textBox_ErrorCode, ErrorInfoDisplay(Code));//设置故障内容显示
  2532. }
  2533. /// <summary>
  2534. /// 数据记录
  2535. /// </summary>
  2536. private void RunInfoAutoSave()
  2537. {
  2538. if (运行信息记录ToolStripMenuItem.Checked == true)
  2539. {
  2540. if (RunInfoSaveFilename == string.Empty)
  2541. {
  2542. return;
  2543. }
  2544. string SaveData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff").Replace(" ", "_") + " --> ";
  2545. foreach (Control c in groupBox2.Controls)
  2546. {
  2547. if (c is TextBox)
  2548. {
  2549. SaveData += c.Text + " , ";
  2550. }
  2551. }
  2552. SaveData += "\r\n";
  2553. System.IO.File.AppendAllText(RunInfoSaveFilename, SaveData);//sb.ToString());
  2554. }
  2555. }
  2556. /// <summary>
  2557. /// 打开或关闭数据记录
  2558. /// </summary>
  2559. /// <param name="sender"></param>
  2560. /// <param name="e"></param>
  2561. private void 记录数据ToolStripMenuItem_Click(object sender, EventArgs e)
  2562. {
  2563. if (运行信息记录ToolStripMenuItem.Checked == false)
  2564. {
  2565. if (mySerialProcess.mySerial.IsOpen == true)
  2566. {
  2567. RunInfoSaveFilename = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_运行信息" + ".txt";
  2568. System.IO.File.AppendAllText(RunInfoSaveFilename, "时间 | 车速 | 转速 | 踏频 | 力矩Reg | 力矩 | 踩踏方向 | 电压 | 电流 | 功率 | 电量 " +
  2569. "| PCB温度 | 绕组温度 | MOS温度 | 挡位 | 灯开关 | 续航 | 平均功耗 | 单次里程 | 单次时间 | 故障码" + "\r\n");
  2570. timer_1s.Enabled = false;
  2571. MessageBox.Show("开始记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  2572. timer_1s.Enabled = true;
  2573. 运行信息记录ToolStripMenuItem.Checked = true;
  2574. }
  2575. else
  2576. {
  2577. timer_1s.Enabled = false;
  2578. MessageBox.Show("请连接电机", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  2579. timer_1s.Enabled = true;
  2580. }
  2581. }
  2582. else
  2583. {
  2584. 运行信息记录ToolStripMenuItem.Checked = false;
  2585. timer_1s.Enabled = false;
  2586. MessageBox.Show("停止记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  2587. timer_1s.Enabled = true;
  2588. }
  2589. }
  2590. /// <summary>
  2591. /// 仪表启动控制档位开关事件
  2592. /// </summary>
  2593. /// <param name="sender"></param>
  2594. /// <param name="e"></param>
  2595. private void checkBox_OBC_StartSetGearSt_CheckedChanged(object sender, EventArgs e)
  2596. {
  2597. if (checkBox_OBC_StartSetGearSt.Checked == true)
  2598. {
  2599. //检查串口是否打开
  2600. if (!mySerialProcess.mySerial.IsOpen)
  2601. {
  2602. timer_1s.Enabled = false;
  2603. MessageBox.Show("串口未连接!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2604. timer_1s.Enabled = true;
  2605. checkBox_OBC_StartSetGearSt.Checked = false;
  2606. return;
  2607. }
  2608. //检查是否选择档位和打开车灯
  2609. if ((comboBox_OBC_SetGearST.SelectedIndex == -1) || (comboBox_OBC_LightSw.SelectedIndex == -1))
  2610. {
  2611. timer_1s.Enabled = false;
  2612. MessageBox.Show("请检查档位设置和大灯开关!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2613. timer_1s.Enabled = true;
  2614. checkBox_OBC_StartSetGearSt.Checked = false;
  2615. return;
  2616. }
  2617. }
  2618. }
  2619. /// <summary>
  2620. /// OBC查询电机用户参数
  2621. /// </summary>
  2622. /// <param name="sender"></param>
  2623. /// <param name="e"></param>
  2624. private void button_OBC_ReadUserInfo_Click(object sender, EventArgs e)
  2625. {
  2626. richTextBox_OBC_ReadUserInfo.Clear();
  2627. mySerialProcess.SendCmd((ushort)0x731, (byte)0x11, (ushort)0x3300, null);
  2628. }
  2629. /// <summary>
  2630. /// OBC设置电机用户参数
  2631. /// </summary>
  2632. /// <param name="sender"></param>
  2633. /// <param name="e"></param>
  2634. private void button_OBC_WriteUserInfo_Click(object sender, EventArgs e)
  2635. {
  2636. var UserInfoCode = new byte[8];
  2637. for (int i = 0; i < UserInfoCode.Length; i++)
  2638. UserInfoCode[i] = 0;
  2639. if (comboBox_OBC_StartMode.SelectedIndex == -1)//检查数据有效性
  2640. {
  2641. timer_1s.Enabled = false;
  2642. MessageBox.Show("请选择启动模式!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2643. timer_1s.Enabled = true;
  2644. return;
  2645. }
  2646. if (comboBox_OBC_AssistFunc.SelectedIndex == -1)//检查数据有效性
  2647. {
  2648. timer_1s.Enabled = false;
  2649. MessageBox.Show("请选择助力方案!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2650. timer_1s.Enabled = true;
  2651. return;
  2652. }
  2653. UserInfoCode[0] = (byte)(int)numericUpDown_OBC_WheelAdj.Value;
  2654. UserInfoCode[1] = (byte)(comboBox_OBC_StartMode.SelectedIndex + 1);
  2655. UserInfoCode[2] = (byte)(comboBox_OBC_AssistFunc.SelectedIndex + 1);
  2656. UserInfoCode[3] = (byte)(uint)numericUpDown_OBC_OffTime.Value;
  2657. mySerialProcess.SendCmd((ushort)0x731, (byte)0x16, (ushort)0x3408, UserInfoCode);
  2658. }
  2659. /// <summary>
  2660. /// OBC查询电机版本信息
  2661. /// </summary>
  2662. /// <param name="sender"></param>
  2663. /// <param name="e"></param>
  2664. private void button_OBC_ReadVerInfo_Click(object sender, EventArgs e)
  2665. {
  2666. textBox_Model.Text = "---";
  2667. textBox_SN.Text = "---";
  2668. textBox_HW.Text = "---";
  2669. textBox_FW.Text = "---";
  2670. textBox_SP.Text = "---";
  2671. textBox_OBC_ReadModel.Text = "---";
  2672. textBox_OBC_ReadSN.Text = "---";
  2673. textBox_OBC_ReadHW.Text = "---";
  2674. textBox_OBC_ReadFW.Text = "---";
  2675. textBox_OBC_ReadSP.Text = "---";
  2676. textBox_FacModeName.Text = "---";
  2677. textBox_FacModeNum.Text = "---";
  2678. textBox_FacModeHW.Text = "---";
  2679. textBox_FacModeFW.Text = "---";
  2680. textBox_FacModeSP.Text = "---";
  2681. Class_Motor_Ver.Mode = "---";
  2682. Class_Motor_Ver.SN = "---";
  2683. Class_Motor_Ver.HW = "---";
  2684. Class_Motor_Ver.FW = "---";
  2685. Class_Motor_Ver.Special = "---";
  2686. mySerialProcess.SendCmd((ushort)0x731, (byte)0x11, (ushort)0x3900, null);
  2687. }
  2688. /// <summary>
  2689. /// OBC读取骑行历史
  2690. /// </summary>
  2691. /// <param name="sender"></param>
  2692. /// <param name="e"></param>
  2693. private void button_OBC_ReadRideInfo_Click(object sender, EventArgs e)
  2694. {
  2695. textBox_OBC_TRIP_KM.Text = "";
  2696. textBox_OBC_TRIP_TIME.Text = "";
  2697. textBox_OBC_ODO_KM.Text = "";
  2698. textBox_OBC_ODO_TIME.Text = "";
  2699. mySerialProcess.SendCmd((ushort)0x731, (byte)0x11, (ushort)0x3500, null);
  2700. }
  2701. /// <summary>
  2702. /// OBC清除TRIP里程
  2703. /// </summary>
  2704. /// <param name="sender"></param>
  2705. /// <param name="e"></param>
  2706. private void button_OBC_ClearTrip_Click(object sender, EventArgs e)
  2707. {
  2708. var Code = new byte[5];
  2709. Code[0] = (byte)'C';
  2710. Code[1] = (byte)'L';
  2711. Code[2] = (byte)'E';
  2712. Code[3] = (byte)'A';
  2713. Code[4] = (byte)'R';
  2714. timer_1s.Enabled = false;
  2715. if (MessageBox.Show("TRIP清除后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2716. mySerialProcess.SendCmd((ushort)0x731, (byte)0x16, (ushort)0x3605, Code);
  2717. timer_1s.Enabled = true;
  2718. }
  2719. private void checkBox_OBC_StartReadBMS_CheckedChanged(object sender, EventArgs e)
  2720. {
  2721. if (checkBox_OBC_StartReadBMS.Checked == true)
  2722. {
  2723. //检查串口是否打开
  2724. if (!mySerialProcess.mySerial.IsOpen)
  2725. {
  2726. timer_1s.Enabled = false;
  2727. MessageBox.Show("串口未连接!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2728. timer_1s.Enabled = true;
  2729. checkBox_OBC_StartReadBMS.Checked = false;
  2730. return;
  2731. }
  2732. }
  2733. }
  2734. /// <summary>
  2735. /// 写入力矩传感器参数
  2736. /// </summary>
  2737. /// <param name="sender"></param>
  2738. /// <param name="e"></param>
  2739. private void button_WriteTqSensorParam_Click(object sender, EventArgs e)
  2740. {
  2741. var ConfigParam = new byte[46];
  2742. for (int i = 0; i < 14; i++)
  2743. ConfigParam[i] = 0;
  2744. ConfigParam[0] = (byte)((写入存储ToolStripMenuItem.Checked) ? 0x01 : 0x00);
  2745. ushort uwDataTemp = 0;
  2746. //力矩传感器参数只写入前面14项与力矩传感器相关的参数
  2747. string[] strDataTemp = richTextBox_SensorParam.Text.Split(new string[] { ", " }, StringSplitOptions.None);
  2748. int index = 0;
  2749. try
  2750. {
  2751. for (int i = 0; i < strDataTemp.Length; i++)
  2752. {
  2753. if (strDataTemp[i].Split('=')[0] == "第四阶段转矩点采集值")
  2754. {
  2755. index = i;
  2756. break;
  2757. }
  2758. }
  2759. for (int i = 0; i < (index + 1); i++)
  2760. {
  2761. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  2762. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  2763. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  2764. }
  2765. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x472E, ConfigParam);
  2766. }
  2767. catch (Exception)
  2768. {
  2769. timer_1s.Enabled = false;
  2770. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2771. timer_1s.Enabled = true;
  2772. }
  2773. }
  2774. /// <summary>
  2775. /// 查询存储标志
  2776. /// </summary>
  2777. /// <param name="sender"></param>
  2778. /// <param name="e"></param>
  2779. private void button_ReadSaveFlag_Click(object sender, EventArgs e)
  2780. {
  2781. radioButton_EE_SaveYes.Checked = false;
  2782. radioButton_EE_SaveNo.Checked = false;
  2783. radioButton_SIP_SaveYes.Checked = false;
  2784. radioButton_SIP_SaveNo.Checked = false;
  2785. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4800, null);
  2786. }
  2787. /// <summary>
  2788. /// 写入存储标志
  2789. /// </summary>
  2790. /// <param name="sender"></param>
  2791. /// <param name="e"></param>
  2792. private void button_WriteSaveFlag_Click(object sender, EventArgs e)
  2793. {
  2794. var SaveFlag = new byte[6];
  2795. for (int i = 0; i < 6; i++)
  2796. SaveFlag[i] = 0;
  2797. if ((radioButton_EE_SaveYes.Checked == false) && (radioButton_EE_SaveNo.Checked == false))
  2798. {
  2799. timer_1s.Enabled = false;
  2800. MessageBox.Show("请选择EEPROM存储标志", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2801. timer_1s.Enabled = true;
  2802. return;
  2803. }
  2804. if ((radioButton_SIP_SaveYes.Checked == false) && (radioButton_SIP_SaveNo.Checked == false))
  2805. {
  2806. timer_1s.Enabled = false;
  2807. MessageBox.Show("请选择SIP校验存储标志", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2808. timer_1s.Enabled = true;
  2809. return;
  2810. }
  2811. SaveFlag[0] = (byte)((checkBox_SaveYes.Checked) ? 0x01 : 0x00);
  2812. SaveFlag[2] = (byte)(radioButton_EE_SaveYes.Checked ? 0x01 : 0x00);
  2813. SaveFlag[3] = 0;
  2814. SaveFlag[4] = (byte)(radioButton_SIP_SaveYes.Checked ? 0x01 : 0x00);
  2815. SaveFlag[5] = 0;
  2816. timer_1s.Enabled = false;
  2817. if (MessageBox.Show("确认修改存储标志?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2818. {
  2819. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x4906, SaveFlag);
  2820. }
  2821. timer_1s.Enabled = true;
  2822. }
  2823. /// <summary>
  2824. /// 读取指定地址数据
  2825. /// </summary>
  2826. /// <param name="sender"></param>
  2827. /// <param name="e"></param>
  2828. private void button_ReadRamFlash_Click(object sender, EventArgs e)
  2829. {
  2830. var Address = new byte[4];
  2831. UInt32 Address_Begin;
  2832. UInt32 Addres_End;
  2833. if(checkBox_ReadRanFlash_AutoClear.Checked == true)
  2834. richTextBox_RamFlasgData.Clear();
  2835. if ((textBox_Address_Begin.Text == string.Empty) || (textBox_Address_End.Text == string.Empty))
  2836. {
  2837. timer_1s.Enabled = false;
  2838. MessageBox.Show("请输入起始和结束地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2839. timer_1s.Enabled = true;
  2840. return;
  2841. }
  2842. else if ((textBox_Address_Begin.Text.Length != 8) || (textBox_Address_End.Text.Length != 8))
  2843. {
  2844. timer_1s.Enabled = false;
  2845. MessageBox.Show("请检查地址长度!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2846. timer_1s.Enabled = true;
  2847. return;
  2848. }
  2849. else
  2850. {
  2851. Address = mySerialProcess.HexStringToBytes(textBox_Address_Begin.Text, false);
  2852. Address_Begin = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
  2853. Address = mySerialProcess.HexStringToBytes(textBox_Address_End.Text, false);
  2854. Addres_End = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
  2855. if (Addres_End < Address_Begin)
  2856. {
  2857. timer_1s.Enabled = false;
  2858. MessageBox.Show("结束地址必须大于或等于起始地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2859. timer_1s.Enabled = true;
  2860. return;
  2861. }
  2862. else
  2863. {
  2864. var SendByte = new byte[8];
  2865. SendByte[0] = (byte)(Address_Begin >> 24);
  2866. SendByte[1] = (byte)(Address_Begin >> 16);
  2867. SendByte[2] = (byte)(Address_Begin >> 8);
  2868. SendByte[3] = (byte)(Address_Begin);
  2869. SendByte[4] = (byte)(Addres_End >> 24);
  2870. SendByte[5] = (byte)(Addres_End >> 16);
  2871. SendByte[6] = (byte)(Addres_End >> 8);
  2872. SendByte[7] = (byte)(Addres_End);
  2873. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2D08, SendByte);
  2874. }
  2875. }
  2876. }
  2877. /// <summary>
  2878. /// 工作模式选择
  2879. /// </summary>
  2880. /// <param name="sender"></param>
  2881. /// <param name="e"></param>
  2882. private void comboBox_WorkMode_SelectedIndexChanged(object sender, EventArgs e)
  2883. {
  2884. var RunMode = new byte[1];
  2885. if (comboBox_WorkMode.Text == "配置模式")
  2886. RunMode[0] = 0x01;
  2887. else
  2888. RunMode[0] = 0x00;
  2889. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, RunMode);
  2890. }
  2891. /// <summary>
  2892. /// 助力档位选择
  2893. /// </summary>
  2894. /// <param name="sender"></param>
  2895. /// <param name="e"></param>
  2896. private void comboBox_GearSt_SelectedIndexChanged(object sender, EventArgs e)
  2897. {
  2898. var CtrlCode = new byte[2];
  2899. if (comboBox_GearSt.SelectedIndex == 5)//档位
  2900. CtrlCode[0] = 0x33;
  2901. else if (comboBox_GearSt.SelectedIndex == 6)//档位
  2902. CtrlCode[0] = 0x22;
  2903. else
  2904. CtrlCode[0] = (byte)comboBox_GearSt.SelectedIndex;
  2905. if (comboBox_LightSwitch.SelectedIndex == 0)//车灯
  2906. CtrlCode[1] = 0xF1;
  2907. else
  2908. CtrlCode[1] = 0xF0;
  2909. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  2910. comboBox_OBC_SetGearST.SelectedIndex = comboBox_GearSt.SelectedIndex;
  2911. comboBox_OBC_LightSw.SelectedIndex = comboBox_LightSwitch.SelectedIndex;
  2912. }
  2913. /// <summary>
  2914. /// 仪表档位选择
  2915. /// </summary>
  2916. /// <param name="sender"></param>
  2917. /// <param name="e"></param>
  2918. private void comboBox_OBC_SetGearST_SelectedIndexChanged(object sender, EventArgs e)
  2919. {
  2920. comboBox_GearSt.SelectedIndex = comboBox_OBC_SetGearST.SelectedIndex;
  2921. comboBox_LightSwitch.SelectedIndex = comboBox_OBC_LightSw.SelectedIndex;
  2922. }
  2923. /// <summary>
  2924. /// 生产模式初始化
  2925. /// </summary>
  2926. /// <param name="sender"></param>
  2927. /// <param name="e"></param>
  2928. private void button_FacModeInit_Click(object sender, EventArgs e)
  2929. {
  2930. var Code = new byte[5];
  2931. Code[0] = (byte)'C';
  2932. Code[1] = (byte)'L';
  2933. Code[2] = (byte)'E';
  2934. Code[3] = (byte)'A';
  2935. Code[4] = (byte)'R';
  2936. timer_1s.Enabled = false;
  2937. if (MessageBox.Show("电机即将初始化!", "确认?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2938. {
  2939. if (MessageBox.Show("请再次确认", "确认?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2940. {
  2941. timer_1s.Enabled = true;
  2942. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2605, Code)) return;
  2943. PowerOffAckStatus = false;
  2944. Delay_ms(250);
  2945. int TryCnt = 3;
  2946. while ((TryCnt--) >= 0)
  2947. {
  2948. if (!PowerOffAckStatus)
  2949. {
  2950. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2605, Code)) return;
  2951. PowerOffAckStatus = false;
  2952. Delay_ms(250);
  2953. }
  2954. else break;
  2955. }
  2956. if (PowerOffAckStatus)
  2957. {
  2958. timer_1s.Enabled = false;
  2959. MessageBox.Show("系统已关机,请保持无负载重新开机!", "提示", MessageBoxButtons.OK, MessageBoxIcon.None);
  2960. timer_1s.Enabled = true;
  2961. }
  2962. else
  2963. {
  2964. timer_1s.Enabled = false;
  2965. MessageBox.Show("初始化失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  2966. timer_1s.Enabled = true;
  2967. }
  2968. timer_1s.Enabled = true;
  2969. }
  2970. }
  2971. timer_1s.Enabled = true;
  2972. //清除历史测试记录
  2973. textBox_FacModeSensorADC0.Text = "";
  2974. textBox_FacModeSensorADC1.Text = "";
  2975. textBox_FacModeSensorADC2.Text = "";
  2976. textBox_FacModeSensorADC3.Text = "";
  2977. textBox_FacModeSensorADC4.Text = "";
  2978. checkBox_FacModeCheckBikeSpeed.Checked = false;
  2979. checkBox_FacModeCheckCadence.Checked = false;
  2980. checkBox_FacModeCheckNTC.Checked = false;
  2981. checkBox_FacModeCheckVol.Checked = false;
  2982. LightSwitchStatus = false;
  2983. MotorRunStatus = false;
  2984. trackBar_FacModeMotorSpeedAdj.Value = 5;
  2985. textBox_FacModeWheelSize.Text = "";
  2986. textBox_FacModeSpeedLimit.Text = "";
  2987. comboBox_FacModeAssist1.SelectedIndex = -1;
  2988. comboBox_FacModeAssist2.SelectedIndex = -1;
  2989. comboBox_FacModeLightVol.SelectedIndex = -1;
  2990. comboBox_FacModeStartMode.SelectedIndex = -1;
  2991. textBox_FacModeStartGain.Text = "";
  2992. textBox_FacModeCircuitK.Text = "";
  2993. textBox_FacModeFltCounter.Text = "";
  2994. textBox_FacModeSpeedLimitTh.Text = "";
  2995. textBox_FacModeSpeedLimitEnd.Text = "";
  2996. textBox_FacModeCadencePer.Text = "";
  2997. textBox_FacModeName.Text = "";
  2998. textBox_FacModeNum.Text = "";
  2999. textBox_FacModeHW.Text = "";
  3000. textBox_FacModeFW.Text = "";
  3001. }
  3002. /// <summary>
  3003. /// 生产模式读取传感器参数
  3004. /// </summary>
  3005. /// <param name="sender"></param>
  3006. /// <param name="e"></param>
  3007. private void button_FacModeSensorRead_Click(object sender, EventArgs e)
  3008. {
  3009. richTextBox_SensorParam.Clear();
  3010. textBox_FacModeSensorADC0.Text = "";
  3011. textBox_FacModeSensorADC1.Text = "";
  3012. textBox_FacModeSensorADC2.Text = "";
  3013. textBox_FacModeSensorADC3.Text = "";
  3014. textBox_FacModeSensorADC4.Text = "";
  3015. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4000, null);
  3016. }
  3017. /// <summary>
  3018. /// 生产模式传感器标定1
  3019. /// </summary>
  3020. /// <param name="sender"></param>
  3021. /// <param name="e"></param>
  3022. private void buttonFacModeSet1_Click(object sender, EventArgs e)
  3023. {
  3024. var ConfigParam = new byte[4];
  3025. for (int i = 0; i < 4; i++)
  3026. ConfigParam[i] = 0;
  3027. ushort uwDataTemp = 0;
  3028. try
  3029. {
  3030. uwDataTemp = 1;//负载序号
  3031. ConfigParam[0] = (byte)(uwDataTemp & 0xFF);
  3032. ConfigParam[1] = (byte)(uwDataTemp >> 8);
  3033. uwDataTemp = (ushort)(decimal.Parse(textBox_FacModeSensorCal1.Text) * 10); ;//负载值0.1Nm
  3034. ConfigParam[2] = (byte)(uwDataTemp & 0xFF);
  3035. ConfigParam[3] = (byte)(uwDataTemp >> 8);
  3036. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x4104, ConfigParam);
  3037. Delay_ms(200);
  3038. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4000, null);
  3039. }
  3040. catch (Exception)
  3041. {
  3042. timer_1s.Enabled = false;
  3043. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  3044. timer_1s.Enabled = true;
  3045. }
  3046. }
  3047. /// <summary>
  3048. /// 生产模式传感器标定2
  3049. /// </summary>
  3050. /// <param name="sender"></param>
  3051. /// <param name="e"></param>
  3052. private void buttonFacModeSet2_Click(object sender, EventArgs e)
  3053. {
  3054. var ConfigParam = new byte[4];
  3055. for (int i = 0; i < 4; i++)
  3056. ConfigParam[i] = 0;
  3057. ushort uwDataTemp = 0;
  3058. try
  3059. {
  3060. uwDataTemp = 2;//负载序号
  3061. ConfigParam[0] = (byte)(uwDataTemp & 0xFF);
  3062. ConfigParam[1] = (byte)(uwDataTemp >> 8);
  3063. uwDataTemp = (ushort)(decimal.Parse(textBox_FacModeSensorCal2.Text) * 10); ;//负载值0.1Nm
  3064. ConfigParam[2] = (byte)(uwDataTemp & 0xFF);
  3065. ConfigParam[3] = (byte)(uwDataTemp >> 8);
  3066. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x4104, ConfigParam);
  3067. Delay_ms(200);
  3068. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4000, null);
  3069. }
  3070. catch (Exception)
  3071. {
  3072. timer_1s.Enabled = false;
  3073. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  3074. timer_1s.Enabled = true;
  3075. }
  3076. }
  3077. /// <summary>
  3078. /// 生产模式传感器标定3
  3079. /// </summary>
  3080. /// <param name="sender"></param>
  3081. /// <param name="e"></param>
  3082. private void buttonFacModeSet3_Click(object sender, EventArgs e)
  3083. {
  3084. var ConfigParam = new byte[4];
  3085. for (int i = 0; i < 4; i++)
  3086. ConfigParam[i] = 0;
  3087. ushort uwDataTemp = 0;
  3088. try
  3089. {
  3090. uwDataTemp = 3;//负载序号
  3091. ConfigParam[0] = (byte)(uwDataTemp & 0xFF);
  3092. ConfigParam[1] = (byte)(uwDataTemp >> 8);
  3093. uwDataTemp = (ushort)(decimal.Parse(textBox_FacModeSensorCal3.Text) * 10); ;//负载值0.1Nm
  3094. ConfigParam[2] = (byte)(uwDataTemp & 0xFF);
  3095. ConfigParam[3] = (byte)(uwDataTemp >> 8);
  3096. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x4104, ConfigParam);
  3097. Delay_ms(200);
  3098. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4000, null);
  3099. }
  3100. catch (Exception)
  3101. {
  3102. timer_1s.Enabled = false;
  3103. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  3104. timer_1s.Enabled = true;
  3105. }
  3106. }
  3107. /// <summary>
  3108. /// 生产模式传感器标定3
  3109. /// </summary>
  3110. /// <param name="sender"></param>
  3111. /// <param name="e"></param>
  3112. private void buttonFacModeSet4_Click(object sender, EventArgs e)
  3113. {
  3114. var ConfigParam = new byte[4];
  3115. for (int i = 0; i < 4; i++)
  3116. ConfigParam[i] = 0;
  3117. ushort uwDataTemp = 0;
  3118. try
  3119. {
  3120. uwDataTemp = 4;//负载序号
  3121. ConfigParam[0] = (byte)(uwDataTemp & 0xFF);
  3122. ConfigParam[1] = (byte)(uwDataTemp >> 8);
  3123. uwDataTemp = (ushort)(decimal.Parse(textBox_FacModeSensorCal4.Text) * 10); ;//负载值0.1Nm
  3124. ConfigParam[2] = (byte)(uwDataTemp & 0xFF);
  3125. ConfigParam[3] = (byte)(uwDataTemp >> 8);
  3126. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x4104, ConfigParam);
  3127. Delay_ms(200);
  3128. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4000, null);
  3129. }
  3130. catch (Exception)
  3131. {
  3132. timer_1s.Enabled = false;
  3133. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  3134. timer_1s.Enabled = true;
  3135. }
  3136. }
  3137. /// <summary>
  3138. /// 生产模式采集按钮
  3139. /// </summary>
  3140. /// <param name="sender"></param>
  3141. /// <param name="e"></param>
  3142. private void button_FacModeWorkMode_Click(object sender, EventArgs e)
  3143. {
  3144. var RunMode = new byte[1];
  3145. if (button_FacModeWorkMode.Text == "开始")
  3146. {
  3147. RunMode[0] = 0x01;
  3148. if (mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, RunMode))
  3149. {
  3150. button_FacModeWorkMode.Text = "停止";
  3151. button_FacModeWorkMode.BackColor = Color.Yellow;
  3152. }
  3153. }
  3154. else if (button_FacModeWorkMode.Text == "停止")
  3155. {
  3156. RunMode[0] = 0x00;
  3157. if (mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, RunMode))
  3158. {
  3159. button_FacModeWorkMode.Text = "开始";
  3160. button_FacModeWorkMode.BackColor = Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
  3161. }
  3162. }
  3163. }
  3164. /// <summary>
  3165. /// 生产模式开关灯
  3166. /// </summary>
  3167. /// <param name="sender"></param>
  3168. /// <param name="e"></param>
  3169. bool LightSwitchStatus = false;
  3170. private void button_FacModeLightSW_Click(object sender, EventArgs e)
  3171. {
  3172. var CtrlCode = new byte[2];
  3173. CtrlCode[0] = 0x00;
  3174. if (LightSwitchStatus)
  3175. {
  3176. CtrlCode[1] = 0xF0;
  3177. button_FacModeLightSW.BackColor = Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(128)))));
  3178. }
  3179. else
  3180. {
  3181. CtrlCode[1] = 0xF1;
  3182. button_FacModeLightSW.BackColor = Color.Yellow;
  3183. }
  3184. if(mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode))
  3185. {
  3186. LightSwitchStatus = !LightSwitchStatus;
  3187. comboBox_LightSwitch.SelectedIndex = LightSwitchStatus ? 0 : 1;
  3188. comboBox_OBC_LightSw.SelectedIndex = LightSwitchStatus ? 0 : 1;
  3189. }
  3190. }
  3191. /// <summary>
  3192. /// 生产模式电机控制
  3193. /// </summary>
  3194. /// <param name="sender"></param>
  3195. /// <param name="e"></param>
  3196. bool MotorRunStatus = false;
  3197. private void button_FacModeMotorRun_Click(object sender, EventArgs e)
  3198. {
  3199. string DebugDefaultInfo = "整体运行模式=4, 位置获取模式=1, 采样模式=2, 旋转方向=1, 定位电流=1000, 拖拽电压=18, 拖拽电流=1000, 拖拽频率=20, 加速斜率=500, 减速斜率=100, 转速环控制器带宽=15, 转速环控制器m=4, 电流环控制器带宽=300, 电流环控制器m=1, 磁链观测器带宽=10, 磁链观测器m=10, 锁相环带宽=150, 锁相环m=2, 惯量=300, PWM 最大占空比=900, PWM七段式切五段式阈值=700, 功率限幅值=6500, 功率Error=500, 功率限幅Kp=20, 功率限幅Ki=3000";
  3200. string[] strDataTemp = DebugDefaultInfo.Split(new string[] { ", " }, StringSplitOptions.None);
  3201. var ConfigParam = new byte[60];
  3202. for (int i = 0; i < 60; i++)
  3203. ConfigParam[i] = 0;
  3204. ConfigParam[0] = 0x00;
  3205. ushort uwDataTemp = 0;
  3206. if (!MotorRunStatus) //停止状态
  3207. {
  3208. //设置电机系列
  3209. if (radioButton_FacModeDriverCity.Checked == true)
  3210. {
  3211. strDataTemp[0] = "整体运行模式=3";
  3212. strDataTemp[3] = "旋转方向=1";
  3213. strDataTemp[18] = "惯量=300";
  3214. }
  3215. else if (radioButton_FacModeDriverMtb.Checked == true)
  3216. {
  3217. strDataTemp[0] = "整体运行模式=3";
  3218. strDataTemp[3] = "旋转方向=0";
  3219. strDataTemp[18] = "惯量=500";
  3220. }
  3221. else
  3222. {
  3223. timer_1s.Enabled = false;
  3224. MessageBox.Show("请选择电机类型!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  3225. timer_1s.Enabled = true;
  3226. return;
  3227. }
  3228. //设置带载类型
  3229. if (label_Load.Text == "空载")
  3230. {
  3231. strDataTemp[10] = "转速环控制器带宽=3";
  3232. }
  3233. else if (label_Load.Text == "带载")
  3234. {
  3235. strDataTemp[10] = "转速环控制器带宽=10";
  3236. }
  3237. else
  3238. {
  3239. timer_1s.Enabled = false;
  3240. MessageBox.Show("请选择带载类型!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  3241. timer_1s.Enabled = true;
  3242. return;
  3243. }
  3244. //选择位置模式
  3245. if (label_MotorSensor.Text == "有感")
  3246. {
  3247. strDataTemp[1] = "位置获取模式=1";
  3248. }
  3249. else if (label_MotorSensor.Text == "无感")
  3250. {
  3251. strDataTemp[1] = "位置获取模式=0";
  3252. }
  3253. else
  3254. {
  3255. timer_1s.Enabled = false;
  3256. MessageBox.Show("请选择位置模式!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  3257. timer_1s.Enabled = true;
  3258. return;
  3259. }
  3260. //发送数据
  3261. try
  3262. {
  3263. for (int i = 0; i < strDataTemp.Length; i++)
  3264. {
  3265. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  3266. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  3267. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  3268. }
  3269. if(!mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x463C, ConfigParam))
  3270. return;
  3271. }
  3272. catch (Exception)
  3273. {
  3274. timer_1s.Enabled = false;
  3275. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  3276. timer_1s.Enabled = true;
  3277. }
  3278. //设置转速百分比100%
  3279. trackBar_FacModeMotorSpeedAdj.Value = 100;
  3280. MotorRunStatus = !MotorRunStatus;
  3281. button_FacModeMotorRun.BackColor = Color.Yellow;
  3282. //电机进入配置模式
  3283. var RunMode = new byte[1];
  3284. RunMode[0] = 0x01;
  3285. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, RunMode);
  3286. button_FacModeWorkMode.Text = "停止";
  3287. button_FacModeWorkMode.BackColor = Color.Yellow;
  3288. }
  3289. else //运转状态
  3290. {
  3291. //设置电机调试参数
  3292. if (radioButton_FacModeDriverCity.Checked == true)
  3293. {
  3294. strDataTemp[0] = "整体运行模式=4";
  3295. strDataTemp[1] = "位置获取模式=1";
  3296. strDataTemp[3] = "旋转方向=1";
  3297. strDataTemp[10] = "转速环控制器带宽=15";
  3298. strDataTemp[18] = "惯量=300";
  3299. }
  3300. else if (radioButton_FacModeDriverMtb.Checked == true)
  3301. {
  3302. strDataTemp[0] = "整体运行模式=5";
  3303. strDataTemp[1] = "位置获取模式=1";
  3304. strDataTemp[3] = "旋转方向=0";
  3305. strDataTemp[10] = "转速环控制器带宽=15";
  3306. strDataTemp[18] = "惯量=500";
  3307. }
  3308. else
  3309. {
  3310. timer_1s.Enabled = false;
  3311. MessageBox.Show("请选择电机类型!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  3312. timer_1s.Enabled = true;
  3313. return;
  3314. }
  3315. try
  3316. {
  3317. for (int i = 0; i < strDataTemp.Length; i++)
  3318. {
  3319. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  3320. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  3321. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  3322. }
  3323. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x463C, ConfigParam))
  3324. return;
  3325. }
  3326. catch (Exception)
  3327. {
  3328. timer_1s.Enabled = false;
  3329. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  3330. timer_1s.Enabled = true;
  3331. }
  3332. //设置转速百分比2%
  3333. trackBar_FacModeMotorSpeedAdj.Value = 2;
  3334. MotorRunStatus = !MotorRunStatus;
  3335. button_FacModeMotorRun.BackColor = Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
  3336. //电机退出配置模式
  3337. var RunMode = new byte[1];
  3338. RunMode[0] = 0x00;
  3339. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, RunMode);
  3340. button_FacModeWorkMode.Text = "开始";
  3341. button_FacModeWorkMode.BackColor = Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
  3342. }
  3343. }
  3344. /// <summary>
  3345. /// 生产模式转速调节
  3346. /// </summary>
  3347. /// <param name="sender"></param>
  3348. /// <param name="e"></param>
  3349. private void trackBar_FacModeMotorSpeedAdj_ValueChanged(object sender, EventArgs e)
  3350. {
  3351. //转速百分比更新
  3352. label_FacModeMotorSpeedSet.Text = trackBar_FacModeMotorSpeedAdj.Value.ToString() + "%";
  3353. numericUpDown_SpeedAdj.Value = trackBar_FacModeMotorSpeedAdj.Value;
  3354. }
  3355. /// <summary>
  3356. /// 生产模式读取整车参数
  3357. /// </summary>
  3358. /// <param name="sender"></param>
  3359. /// <param name="e"></param>
  3360. private void button_FacModeBikeParaRead_Click(object sender, EventArgs e)
  3361. {
  3362. textBox_FacModeWheelSize.Text = "";
  3363. textBox_FacModeSpeedLimit.Text = "";
  3364. comboBox_FacModeAssist1.SelectedIndex = -1;
  3365. comboBox_FacModeAssist2.SelectedIndex = -1;
  3366. comboBox_FacModeLightVol.SelectedIndex = -1;
  3367. comboBox_FacModeStartMode.SelectedIndex = -1;
  3368. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3C00, null);
  3369. }
  3370. /// <summary>
  3371. /// 生产模式写入整车参数
  3372. /// </summary>
  3373. /// <param name="sender"></param>
  3374. /// <param name="e"></param>
  3375. private void button_FacModeBikeParaSet_Click(object sender, EventArgs e)
  3376. {
  3377. string BikeDefaultInfo = "轮胎周长=219, 电控传动比=35, 助力最大限速=25, 推行模式限速=6, 前牙盘T数=38, 后牙盘T数=11, 助力方案1=341, 助力方案2=682, 前后灯电压=6, 轮胎周长微调=0, 启动模式=2, 自动关机时间=15";
  3378. var ConfigParam = new byte[28];
  3379. for (int i = 0; i < 28; i++)
  3380. ConfigParam[i] = 0;
  3381. ConfigParam[0] = (byte)(0x01);
  3382. ushort uwDataTemp = 0;
  3383. string[] strDataTemp = BikeDefaultInfo.Split(new string[] { ", " }, StringSplitOptions.None);
  3384. try
  3385. {
  3386. for (int i = 0; i < strDataTemp.Length; i++)
  3387. {
  3388. uwDataTemp = Convert.ToUInt16(strDataTemp[i].Split('=')[1]);
  3389. ConfigParam[2 * i + 2] = (byte)uwDataTemp;
  3390. ConfigParam[2 * i + 3] = (byte)(uwDataTemp >> 8);
  3391. }
  3392. //周长
  3393. uwDataTemp = Convert.ToUInt16(textBox_FacModeWheelSize.Text);
  3394. ConfigParam[2] = (byte)uwDataTemp;
  3395. ConfigParam[3] = (byte)(uwDataTemp >> 8);
  3396. //限速
  3397. uwDataTemp = Convert.ToUInt16(textBox_FacModeSpeedLimit.Text);
  3398. ConfigParam[6] = (byte)uwDataTemp;
  3399. ConfigParam[7] = (byte)(uwDataTemp >> 8);
  3400. //助力方案1
  3401. if (comboBox_FacModeAssist1.SelectedIndex == 0) uwDataTemp = 0;
  3402. else if (comboBox_FacModeAssist1.SelectedIndex == 1) uwDataTemp = 341;
  3403. else if (comboBox_FacModeAssist1.SelectedIndex == 2) uwDataTemp = 682;
  3404. else
  3405. {
  3406. timer_1s.Enabled = false;
  3407. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  3408. timer_1s.Enabled = true;
  3409. return;
  3410. }
  3411. ConfigParam[14] = (byte)uwDataTemp;
  3412. ConfigParam[15] = (byte)(uwDataTemp >> 8);
  3413. //助力方案2
  3414. if(comboBox_FacModeAssist2.SelectedIndex == 0) uwDataTemp = 0;
  3415. else if (comboBox_FacModeAssist2.SelectedIndex == 1) uwDataTemp = 341;
  3416. else if (comboBox_FacModeAssist2.SelectedIndex == 2) uwDataTemp = 682;
  3417. else
  3418. {
  3419. timer_1s.Enabled = false;
  3420. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  3421. timer_1s.Enabled = true;
  3422. return;
  3423. }
  3424. ConfigParam[16] = (byte)uwDataTemp;
  3425. ConfigParam[17] = (byte)(uwDataTemp >> 8);
  3426. //灯压
  3427. if(comboBox_FacModeLightVol.SelectedIndex == 0) uwDataTemp = 6;
  3428. else if (comboBox_FacModeLightVol.SelectedIndex == 1) uwDataTemp = 12;
  3429. else
  3430. {
  3431. timer_1s.Enabled = false;
  3432. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  3433. timer_1s.Enabled = true;
  3434. return;
  3435. }
  3436. ConfigParam[18] = (byte)uwDataTemp;
  3437. ConfigParam[19] = (byte)(uwDataTemp >> 8);
  3438. //启动模式
  3439. if (comboBox_FacModeStartMode.SelectedIndex == 0) uwDataTemp = 1;
  3440. else if (comboBox_FacModeStartMode.SelectedIndex == 2) uwDataTemp = 3;
  3441. else uwDataTemp = 2;
  3442. ConfigParam[22] = (byte)uwDataTemp;
  3443. ConfigParam[23] = (byte)(uwDataTemp >> 8);
  3444. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3D1C, ConfigParam);
  3445. }
  3446. catch (Exception)
  3447. {
  3448. timer_1s.Enabled = false;
  3449. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  3450. timer_1s.Enabled = true;
  3451. }
  3452. }
  3453. /// <summary>
  3454. /// 生产模式读取助力参数
  3455. /// </summary>
  3456. /// <param name="sender"></param>
  3457. /// <param name="e"></param>
  3458. private void button_FacModeAssistParaRead_Click(object sender, EventArgs e)
  3459. {
  3460. var ConfigParam = new byte[4];
  3461. textBox_FacModeStartGain.Text = "";
  3462. textBox_FacModeCircuitK.Text = "";
  3463. textBox_FacModeFltCounter.Text = "";
  3464. textBox_FacModeSpeedLimitTh.Text ="";
  3465. textBox_FacModeSpeedLimitEnd.Text = "";
  3466. textBox_FacModeCadencePer.Text = "";
  3467. ConfigParam[0] = (byte)(1 & 0xFF);
  3468. ConfigParam[1] = (byte)(1 >> 8);
  3469. ConfigParam[2] = (byte)(1 & 0xFF);
  3470. ConfigParam[3] = (byte)(1 >> 8);
  3471. mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4304, ConfigParam);
  3472. }
  3473. /// <summary>
  3474. /// 生产模式写入助力参数
  3475. /// </summary>
  3476. /// <param name="sender"></param>
  3477. /// <param name="e"></param>
  3478. private void button_FacModeAssistParaSet_Click(object sender, EventArgs e)
  3479. {
  3480. string AssistDefaultInfo = "零速启动增益=4096, 巡航启动增益=4096, 助力转矩曲线编号=1, 助力踏频曲线编号=1, 转矩曲线.a=0, 转矩曲线.b=0, 转矩曲线.c=2048, 转矩曲线.d=137, 踏频曲线.a=0, 踏频曲线.b=0, 踏频曲线.c=35000, 踏频曲线.d=0, 助力启动阈值=80, 助力停止阈值=40, 启动时电流增长阶梯=10, 启动对应踏频脉冲数=32, 转矩滤波对应踏频脉冲数=45, 待速转速=0, 待速最大电流=500, 车速限幅启动阈值=25, 车速限幅停止阈值=27, 踏频占比=1229";
  3481. var ConfigParam = new byte[80];
  3482. for (int i = 0; i < 80; i++)
  3483. ConfigParam[i] = 0;
  3484. ConfigParam[0] = (byte)(0x01);
  3485. ushort uwDataTemp = 0;
  3486. ulong ulDataTemp = 0;
  3487. int index_1 = 0, index_2 = 0;
  3488. string[] strDataTemp = AssistDefaultInfo.Split(new string[] { ", " }, StringSplitOptions.None);
  3489. try
  3490. {
  3491. //因数据类型不同,预处理
  3492. for (int i = 0; i < strDataTemp.Length; i++)
  3493. {
  3494. if (strDataTemp[i].Split('=')[0] == "转矩曲线.a")
  3495. {
  3496. index_1 = i;
  3497. break;
  3498. }
  3499. }
  3500. for (int i = 0; i < strDataTemp.Length; i++)
  3501. {
  3502. if (strDataTemp[i].Split('=')[0] == "踏频曲线.d")
  3503. {
  3504. index_2 = i;
  3505. break;
  3506. }
  3507. }
  3508. for (int i = 0; i < strDataTemp.Length; i++)
  3509. {
  3510. ulDataTemp = Convert.ToUInt32(strDataTemp[i].Split('=')[1]);
  3511. if (i < index_1)
  3512. {
  3513. ConfigParam[2 * i + 4] = (byte)ulDataTemp;
  3514. ConfigParam[2 * i + 5] = (byte)(ulDataTemp >> 8);
  3515. }
  3516. else if (i <= index_2)
  3517. {
  3518. ConfigParam[4 * i - 4] = (byte)ulDataTemp;
  3519. ConfigParam[4 * i - 3] = (byte)(ulDataTemp >> 8);
  3520. ConfigParam[4 * i - 2] = (byte)(ulDataTemp >> 16);
  3521. ConfigParam[4 * i - 1] = (byte)(ulDataTemp >> 24);
  3522. }
  3523. else
  3524. {
  3525. ConfigParam[2 * i + 20] = (byte)ulDataTemp;
  3526. ConfigParam[2 * i + 21] = (byte)(ulDataTemp >> 8);
  3527. }
  3528. }
  3529. //零速增益
  3530. uwDataTemp = Convert.ToUInt16(textBox_FacModeStartGain.Text);
  3531. ConfigParam[4] = (byte)uwDataTemp;
  3532. ConfigParam[5] = (byte)(uwDataTemp >> 8);
  3533. //电流阶梯
  3534. uwDataTemp = Convert.ToUInt16(textBox_FacModeCircuitK.Text);
  3535. ConfigParam[48] = (byte)uwDataTemp;
  3536. ConfigParam[49] = (byte)(uwDataTemp >> 8);
  3537. //滤波脉冲
  3538. uwDataTemp = Convert.ToUInt16(textBox_FacModeFltCounter.Text);
  3539. ConfigParam[52] = (byte)uwDataTemp;
  3540. ConfigParam[53] = (byte)(uwDataTemp >> 8);
  3541. //限速启动
  3542. uwDataTemp = Convert.ToUInt16(textBox_FacModeSpeedLimitTh.Text);
  3543. ConfigParam[58] = (byte)uwDataTemp;
  3544. ConfigParam[59] = (byte)(uwDataTemp >> 8);
  3545. //限速停止
  3546. uwDataTemp = Convert.ToUInt16(textBox_FacModeSpeedLimitEnd.Text);
  3547. ConfigParam[60] = (byte)uwDataTemp;
  3548. ConfigParam[61] = (byte)(uwDataTemp >> 8);
  3549. //踏频占比
  3550. uwDataTemp = Convert.ToUInt16(textBox_FacModeCadencePer.Text);
  3551. ConfigParam[62] = (byte)uwDataTemp;
  3552. ConfigParam[63] = (byte)(uwDataTemp >> 8);
  3553. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x4450, ConfigParam);
  3554. }
  3555. catch (Exception)
  3556. {
  3557. timer_1s.Enabled = false;
  3558. MessageBox.Show("数据格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  3559. timer_1s.Enabled = true;
  3560. }
  3561. }
  3562. /// <summary>
  3563. /// 生产模式写入型号
  3564. /// </summary>
  3565. /// <param name="sender"></param>
  3566. /// <param name="e"></param>
  3567. private void button_FacModeSetName_Click(object sender, EventArgs e)
  3568. {
  3569. string Mode = textBox_FacModeName.Text;
  3570. var ModeArray = new byte[16];
  3571. for (ushort i = 0; i < Mode.Length; i++)
  3572. {
  3573. ModeArray[i] = (byte)Mode[i];
  3574. }
  3575. if (Mode.Length < 16)
  3576. {
  3577. ModeArray[Mode.Length] = (byte)'.';
  3578. for (ushort i = 0; i < 16 - Mode.Length - 1; i++)
  3579. {
  3580. ModeArray[Mode.Length + 1 + i] = 0x20;
  3581. }
  3582. }
  3583. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2210, ModeArray);
  3584. }
  3585. /// <summary>
  3586. /// 生产模式写入SN
  3587. /// </summary>
  3588. /// <param name="sender"></param>
  3589. /// <param name="e"></param>
  3590. private void Button_FacModeSetNum_Click(object sender, EventArgs e)
  3591. {
  3592. string SN = textBox_FacModeNum.Text;
  3593. var SNArray = new byte[16];
  3594. for (ushort i = 0; i < SN.Length; i++)
  3595. {
  3596. SNArray[i] = (byte)SN[i];
  3597. }
  3598. if (SN.Length < 16)
  3599. {
  3600. SNArray[SN.Length] = (byte)'.';
  3601. for (ushort i = 0; i < 16 - SN.Length - 1; i++)
  3602. {
  3603. SNArray[SN.Length + 1 + i] = 0x20;
  3604. }
  3605. }
  3606. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2310, SNArray);
  3607. }
  3608. /// <summary>
  3609. /// 生产模式读取版本信息
  3610. /// </summary>
  3611. /// <param name="sender"></param>
  3612. /// <param name="e"></param>
  3613. private void Button_FacModeReadVer_Click_1(object sender, EventArgs e)
  3614. {
  3615. textBox_Model.Text = "---";
  3616. textBox_SN.Text = "---";
  3617. textBox_HW.Text = "---";
  3618. textBox_FW.Text = "---";
  3619. textBox_SP.Text = "---";
  3620. textBox_OBC_ReadModel.Text = "---";
  3621. textBox_OBC_ReadSN.Text = "---";
  3622. textBox_OBC_ReadHW.Text = "---";
  3623. textBox_OBC_ReadFW.Text = "---";
  3624. textBox_OBC_ReadSP.Text = "---";
  3625. textBox_FacModeName.Text = "---";
  3626. textBox_FacModeNum.Text = "---";
  3627. textBox_FacModeHW.Text = "---";
  3628. textBox_FacModeFW.Text = "---";
  3629. textBox_FacModeSP.Text = "---";
  3630. textBox_FacModeGit.Text = "---";
  3631. Class_Motor_Ver.Mode = "---";
  3632. Class_Motor_Ver.SN = "---";
  3633. Class_Motor_Ver.HW = "---";
  3634. Class_Motor_Ver.FW = "---";
  3635. Class_Motor_Ver.Special = "---";
  3636. mySerialProcess.SendCmd((ushort)0x731, (byte)0x11, (ushort)0x3900, null);
  3637. }
  3638. /// <summary>
  3639. /// 生产模式写入生产信息
  3640. /// </summary>
  3641. /// <param name="sender"></param>
  3642. /// <param name="e"></param>
  3643. private void Button_FacModeSetMacDate_Click(object sender, EventArgs e)
  3644. {
  3645. var MACArray = new byte[32];
  3646. string MacDefaultInfo = "";
  3647. string MacAddDefaultInfo = "";
  3648. string MacDateDefaultInfo = DateTime.Now.ToString("yyyyMMdd");
  3649. string MacPPDefaultInfo = "";
  3650. do
  3651. {
  3652. //填入生产商
  3653. if (toolStripTextBox_MAC.Text == string.Empty)
  3654. {
  3655. break;
  3656. }
  3657. MacDefaultInfo = toolStripTextBox_MAC.Text;
  3658. for (ushort i = 0; i < MacDefaultInfo.Length; i++)
  3659. {
  3660. MACArray[i] = (byte)MacDefaultInfo[i];
  3661. }
  3662. if (MacDefaultInfo.Length < 8)
  3663. {
  3664. MACArray[MacDefaultInfo.Length] = (byte)'.';
  3665. for (ushort i = 0; i < 8 - MacDefaultInfo.Length - 1; i++)
  3666. {
  3667. MACArray[MacDefaultInfo.Length + 1 + i] = 0x20;
  3668. }
  3669. }
  3670. //填入生产地
  3671. if (toolStripTextBoxMACAddr.Text == string.Empty)
  3672. {
  3673. break;
  3674. }
  3675. MacAddDefaultInfo = toolStripTextBoxMACAddr.Text;
  3676. for (ushort i = 0; i < MacAddDefaultInfo.Length; i++)
  3677. {
  3678. MACArray[8 + i] = (byte)MacAddDefaultInfo[i];
  3679. }
  3680. if (MacAddDefaultInfo.Length < 8)
  3681. {
  3682. MACArray[MacAddDefaultInfo.Length + 8] = (byte)'.';
  3683. for (ushort i = 0; i < 8 - MacAddDefaultInfo.Length - 1; i++)
  3684. {
  3685. MACArray[MacAddDefaultInfo.Length + 1 + i + 8] = 0x20;
  3686. }
  3687. }
  3688. //填入生产日期
  3689. if (toolStripTextBox_MACDate.Text == string.Empty)
  3690. {
  3691. break;
  3692. }
  3693. MacDateDefaultInfo = toolStripTextBox_MACDate.Text;
  3694. for (ushort i = 0; i < MacDateDefaultInfo.Length; i++)
  3695. {
  3696. MACArray[16 + i] = (byte)MacDateDefaultInfo[i];
  3697. }
  3698. if (MacDateDefaultInfo.Length < 8)
  3699. {
  3700. MACArray[MacDateDefaultInfo.Length + 16] = (byte)'.';
  3701. for (ushort i = 0; i < 8 - MacDateDefaultInfo.Length - 1; i++)
  3702. {
  3703. MACArray[MacDateDefaultInfo.Length + 1 + i + 16] = 0x20;
  3704. }
  3705. }
  3706. //填入产品标识
  3707. if (toolStripTextBox_MACPD.Text == string.Empty)
  3708. {
  3709. break;
  3710. }
  3711. MacPPDefaultInfo = toolStripTextBox_MACPD.Text;
  3712. for (ushort i = 0; i < MacPPDefaultInfo.Length; i++)
  3713. {
  3714. MACArray[24 + i] = (byte)MacPPDefaultInfo[i];
  3715. }
  3716. if (MacPPDefaultInfo.Length < 8)
  3717. {
  3718. MACArray[MacPPDefaultInfo.Length + 24] = (byte)'.';
  3719. for (ushort i = 0; i < 8 - MacPPDefaultInfo.Length - 1; i++)
  3720. {
  3721. MACArray[MacPPDefaultInfo.Length + 1 + i + 24] = 0x20;
  3722. }
  3723. }
  3724. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2420, MACArray);
  3725. return;
  3726. } while (false);
  3727. timer_1s.Enabled = false;
  3728. MessageBox.Show("生产信息配置不完整!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  3729. timer_1s.Enabled = true;
  3730. }
  3731. /// <summary>
  3732. /// 生产模式保存测试页面
  3733. /// </summary>
  3734. /// <param name="sender"></param>
  3735. /// <param name="e"></param>
  3736. private void Button_FacModeSaveResult_Click(object sender, EventArgs e)
  3737. {
  3738. Button_FacModeSaveResult.Text = "存储中…";
  3739. do
  3740. {
  3741. //读取一次马达参数
  3742. richTextBox_MotorParam.Clear();
  3743. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3A00, null)) break; ;
  3744. Delay_ms(100);
  3745. //读取一次整车参数
  3746. richTextBox_BikeParam.Clear();
  3747. textBox_FacModeWheelSize.Text = "";
  3748. textBox_FacModeSpeedLimit.Text = "";
  3749. comboBox_FacModeAssist1.SelectedIndex = -1;
  3750. comboBox_FacModeAssist2.SelectedIndex = -1;
  3751. comboBox_FacModeLightVol.SelectedIndex = -1;
  3752. comboBox_FacModeStartMode.SelectedIndex = -1;
  3753. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3C00, null)) break;
  3754. Delay_ms(100);
  3755. //读取一次控制参数
  3756. richTextBox_ControlParam.Clear();
  3757. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3E00, null)) break;
  3758. Delay_ms(100);
  3759. //查询传感器
  3760. richTextBox_SensorParam.Clear();
  3761. textBox_FacModeSensorADC0.Text = "";
  3762. textBox_FacModeSensorADC1.Text = "";
  3763. textBox_FacModeSensorADC2.Text = "";
  3764. textBox_FacModeSensorADC3.Text = "";
  3765. textBox_FacModeSensorADC4.Text = "";
  3766. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4000, null)) break;
  3767. Delay_ms(100);
  3768. //读取一次助力参数
  3769. var ConfigParam = new byte[4];
  3770. textBox_FacModeStartGain.Text = "";
  3771. textBox_FacModeCircuitK.Text = "";
  3772. textBox_FacModeFltCounter.Text = "";
  3773. textBox_FacModeSpeedLimitTh.Text = "";
  3774. textBox_FacModeSpeedLimitEnd.Text = "";
  3775. textBox_FacModeCadencePer.Text = "";
  3776. richTextBox_AssistParam.Clear();
  3777. ConfigParam[0] = (byte)(1 & 0xFF);
  3778. ConfigParam[1] = (byte)(1 >> 8);
  3779. ConfigParam[2] = (byte)(1 & 0xFF);
  3780. ConfigParam[3] = (byte)(1 >> 8);
  3781. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4304, ConfigParam)) break;
  3782. Delay_ms(100);
  3783. //读取一次调试信息
  3784. richTextBox_DebugParam.Clear();
  3785. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4500, null)) break;
  3786. Delay_ms(100);
  3787. //读取一次历史信息
  3788. richTextBox_Record.Clear();
  3789. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1E00, null)) break;
  3790. Delay_ms(100);
  3791. //读取一次电机运行信息
  3792. var CtrlCode = new byte[2];
  3793. CtrlCode[0] = 0x00;
  3794. CtrlCode[1] = 0xF0;
  3795. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode)) break;
  3796. Delay_ms(100);
  3797. //读取一次版本信息
  3798. textBox_Model.Text = "---";
  3799. textBox_SN.Text = "---";
  3800. textBox_HW.Text = "---";
  3801. textBox_FW.Text = "---";
  3802. textBox_OBC_ReadModel.Text = "---";
  3803. textBox_OBC_ReadSN.Text = "---";
  3804. textBox_OBC_ReadHW.Text = "---";
  3805. textBox_OBC_ReadFW.Text = "---";
  3806. textBox_FacModeName.Text = "---";
  3807. textBox_FacModeNum.Text = "---";
  3808. textBox_FacModeHW.Text = "---";
  3809. textBox_FacModeFW.Text = "---";
  3810. Class_Motor_Ver.Mode = "---";
  3811. Class_Motor_Ver.SN = "---";
  3812. Class_Motor_Ver.HW = "---";
  3813. Class_Motor_Ver.FW = "---";
  3814. Class_Motor_Ver.Special = "---";
  3815. if (!mySerialProcess.SendCmd((ushort)0x731, (byte)0x11, (ushort)0x3900, null)) break;
  3816. Delay_ms(100);
  3817. //读取一次生产信息
  3818. textBox_MAC.Text = "";
  3819. textBox_MACAdd.Text = "";
  3820. textBox_MACDate.Text = "";
  3821. textBox_PP.Text = "";
  3822. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1F00, null)) break;
  3823. Delay_ms(100);
  3824. //读取一次自定义信息
  3825. textBox_User1.Text = "---";
  3826. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1300, null)) break;
  3827. Delay_ms(100);
  3828. textBox_User2.Text = "---";
  3829. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1500, null)) break;
  3830. Delay_ms(100);
  3831. textBox_User3.Text = "---";
  3832. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1700, null)) break;
  3833. Delay_ms(100);
  3834. //读取一次密钥
  3835. textBox_Key.Text = "---";
  3836. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1000, null)) break;
  3837. Delay_ms(100);
  3838. //读取一次存储标志
  3839. radioButton_EE_SaveYes.Checked = false;
  3840. radioButton_EE_SaveNo.Checked = false;
  3841. radioButton_SIP_SaveYes.Checked = false;
  3842. radioButton_SIP_SaveNo.Checked = false;
  3843. if (!mySerialProcess.SendCmd((ushort)0x751, (byte)0x11, (ushort)0x4800, null)) break;
  3844. Delay_ms(100);
  3845. //提示检查
  3846. #if false
  3847. timer_1s.Enabled = false;
  3848. if (MessageBox.Show("请检查信息是否完整!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
  3849. {
  3850. MessageBox.Show("请检查参数!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  3851. timer_1s.Enabled = true;
  3852. return;
  3853. }
  3854. timer_1s.Enabled = true;
  3855. Delay_ms(200);
  3856. #endif
  3857. //判断数据是否有效
  3858. if ((textBox_FacModeName.Text == "---") || (textBox_FacModeNum.Text == "--"))
  3859. {
  3860. MessageBox.Show("SN无效,上传失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  3861. break;
  3862. }
  3863. if ((textBox_FacModeName.Text == string.Empty) || (textBox_FacModeNum.Text == string.Empty))
  3864. {
  3865. MessageBox.Show("SN无效,上传失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  3866. break;
  3867. }
  3868. if (!textBox_FacModeName.Text.Contains("MIGIC"))
  3869. {
  3870. MessageBox.Show("型号名无效,上传失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  3871. break;
  3872. }
  3873. //设置文件存储路径
  3874. string Save_Path = LocalPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\"; ;
  3875. string PD_PathName = textBox_FacModeName.Text + "_" + textBox_FacModeNum.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_"); ;
  3876. Save_Path += PD_PathName;
  3877. if (!Directory.Exists(Save_Path))
  3878. Directory.CreateDirectory(Save_Path);
  3879. //保存页面
  3880. string PIC_SaveFileName = "";
  3881. Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap
  3882. Graphics g = Graphics.FromImage(bit);
  3883. g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片
  3884. PIC_SaveFileName = Save_Path + "\\" + textBox_FacModeName.Text + "_" + textBox_FacModeNum.Text + "_测试页面" + ".png";
  3885. bit.Save(PIC_SaveFileName);
  3886. //保存数据
  3887. string DataFileName = "";
  3888. string FileInfo = "";
  3889. DataFileName = Save_Path + "\\" + textBox_FacModeName.Text + "_" + textBox_FacModeNum.Text + "_记录数据" + ".txt";
  3890. FileInfo += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n";
  3891. FileInfo += "\r\n";
  3892. FileInfo += "[电机信息]\r\n";
  3893. FileInfo += "电机型号: " + textBox_FacModeName.Text + "\r\n";
  3894. FileInfo += "电机编号: " + textBox_FacModeNum.Text + "\r\n";
  3895. FileInfo += "硬件版本: " + textBox_HW.Text + "\r\n";
  3896. FileInfo += "软件版本: " + textBox_FW.Text + "\r\n";
  3897. FileInfo += "软件标识: " + textBox_SP.Text + "\r\n";
  3898. FileInfo += "\r\n";
  3899. FileInfo += "[马达参数]\r\n";
  3900. FileInfo += richTextBox_MotorParam.Text + "\r\n";
  3901. FileInfo += "\r\n";
  3902. FileInfo += "[整车参数]\r\n";
  3903. FileInfo += richTextBox_BikeParam.Text + "\r\n";
  3904. FileInfo += "\r\n";
  3905. FileInfo += "[控制参数]\r\n";
  3906. FileInfo += richTextBox_ControlParam.Text + "\r\n";
  3907. FileInfo += "\r\n";
  3908. FileInfo += "[传感器参数]\r\n";
  3909. FileInfo += richTextBox_SensorParam.Text + "\r\n";
  3910. FileInfo += "\r\n";
  3911. FileInfo += "[助力参数]\r\n";
  3912. FileInfo += richTextBox_AssistParam.Text + "\r\n";
  3913. FileInfo += "\r\n";
  3914. FileInfo += "[调试参数]\r\n";
  3915. FileInfo += richTextBox_DebugParam.Text + "\r\n";
  3916. FileInfo += "\r\n";
  3917. FileInfo += "[历史信息]\r\n";
  3918. FileInfo += richTextBox_Record.Text + "\r\n";
  3919. FileInfo += "\r\n";
  3920. FileInfo += "[生产信息]\r\n";
  3921. FileInfo += "生产商: " + textBox_MAC.Text + "\r\n";
  3922. FileInfo += "生产地: " + textBox_MACAdd.Text + "\r\n";
  3923. FileInfo += "生产日期: " + textBox_MACDate.Text + "\r\n";
  3924. FileInfo += "产品标识: " + textBox_PP.Text + "\r\n";
  3925. FileInfo += "\r\n";
  3926. FileInfo += "[自定义信息]\r\n";
  3927. FileInfo += "自定义1: " + textBox_User1.Text + "\r\n";
  3928. FileInfo += "自定义2: " + textBox_User2.Text + "\r\n";
  3929. FileInfo += "自定义3: " + textBox_User3.Text + "\r\n";
  3930. FileInfo += "\r\n";
  3931. FileInfo += "[校验密钥]\r\n";
  3932. FileInfo += textBox_Key.Text + "\r\n";
  3933. FileInfo += "\r\n";
  3934. FileInfo += "[存储标志]\r\n";
  3935. FileInfo += "EEPROM: " + ((radioButton_EE_SaveYes.Checked == true) ? "已存储" : "未存储") + "\r\n";
  3936. FileInfo += "SIP: " + ((radioButton_SIP_SaveYes.Checked == true) ? "已存储" : "未存储") + "\r\n";
  3937. FileInfo += "\r\n";
  3938. System.IO.File.WriteAllText(DataFileName, FileInfo);
  3939. timer_1s.Enabled = false;
  3940. MessageBox.Show("存储完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  3941. timer_1s.Enabled = true;
  3942. //数据上传
  3943. if (myFtp.IsNetConnected == true)
  3944. {
  3945. string DataNow= DateTime.Now.ToString("yyyy-MM-dd");
  3946. //检查路径
  3947. if (myFtp.DirectoryExist("\\", toolStripTextBox_ServerPath.Text) == false)
  3948. {
  3949. myFtp.MakeDir(toolStripTextBox_ServerPath.Text);
  3950. }
  3951. if (myFtp.DirectoryExist(toolStripTextBox_ServerPath.Text, DataNow) == false)
  3952. {
  3953. myFtp.MakeDir(toolStripTextBox_ServerPath.Text + "\\" + DataNow);
  3954. }
  3955. if (myFtp.DirectoryExist(toolStripTextBox_ServerPath.Text + "\\" + DataNow, PD_PathName) == false)
  3956. {
  3957. myFtp.MakeDir(toolStripTextBox_ServerPath.Text + "\\" + DataNow + "\\" + PD_PathName);
  3958. }
  3959. //上传文件
  3960. bool result1 = myFtp.UploadFile(PIC_SaveFileName, toolStripTextBox_ServerPath.Text + "\\" + DataNow + "\\" + PD_PathName);
  3961. bool result2 = myFtp.UploadFile(DataFileName, toolStripTextBox_ServerPath.Text + "\\" + DataNow + "\\" + PD_PathName);
  3962. if ((result1 & result2) == true)
  3963. {
  3964. timer_1s.Enabled = false;
  3965. MessageBox.Show("数据已上传!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  3966. timer_1s.Enabled = true;
  3967. }
  3968. else
  3969. {
  3970. timer_1s.Enabled = false;
  3971. MessageBox.Show("数据上传失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  3972. timer_1s.Enabled = true;
  3973. }
  3974. }
  3975. else
  3976. {
  3977. timer_1s.Enabled = false;
  3978. MessageBox.Show("服务器断开!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  3979. timer_1s.Enabled = true;
  3980. }
  3981. Button_FacModeSaveResult.Text = "保存数据";
  3982. return;
  3983. } while (false);
  3984. timer_1s.Enabled = false;
  3985. MessageBox.Show("保存失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  3986. timer_1s.Enabled = true;
  3987. Button_FacModeSaveResult.Text = "保存数据";
  3988. }
  3989. /// <summary>
  3990. /// 工具箱页面保存
  3991. /// </summary>
  3992. /// <param name="sender"></param>
  3993. /// <param name="e"></param>
  3994. private void 页面保存ToolStripMenuItem_Click(object sender, EventArgs e)
  3995. {
  3996. //保存页面
  3997. Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap
  3998. Graphics g = Graphics.FromImage(bit);
  3999. g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片
  4000. SaveFileDialog sf = new SaveFileDialog();
  4001. sf.Title = "页面保存";
  4002. sf.FileName = "MotorInfo_" + textBox_FacModeName.Text + "_" + textBox_FacModeNum.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + ".png";
  4003. sf.Filter = "图片|*.png";
  4004. timer_1s.Enabled = false;
  4005. if (sf.ShowDialog() == DialogResult.OK)
  4006. {
  4007. bit.Save(sf.FileName);//默认保存格式为PNG,保存成jpg格式质量不是很好
  4008. }
  4009. timer_1s.Enabled = true;
  4010. }
  4011. /// <summary>
  4012. /// 生产模式关机
  4013. /// </summary>
  4014. /// <param name="sender"></param>
  4015. /// <param name="e"></param>
  4016. private void Button_FacModePowerOff_Click(object sender, EventArgs e)
  4017. {
  4018. var Code_Off = new byte[5];
  4019. var Code_Reset = new byte[5];
  4020. Code_Off[0] = (byte)'O';
  4021. Code_Off[1] = (byte)'F';
  4022. Code_Off[2] = (byte)'F';
  4023. Code_Off[3] = (byte)'.';
  4024. Code_Off[4] = (byte)'.';
  4025. Code_Reset[0] = (byte)'R';
  4026. Code_Reset[1] = (byte)'E';
  4027. Code_Reset[2] = (byte)'S';
  4028. Code_Reset[3] = (byte)'E';
  4029. Code_Reset[4] = (byte)'T';
  4030. timer_1s.Enabled = false;
  4031. if (MessageBox.Show("系统将关机", "确认关机?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  4032. {
  4033. timer_1s.Enabled = true;
  4034. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2505, Code_Off);
  4035. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2505, Code_Reset);
  4036. PowerOffAckStatus = false;
  4037. Delay_ms(250);
  4038. int TryCnt = 3;
  4039. while ((TryCnt--) >= 0)
  4040. {
  4041. if (!PowerOffAckStatus)
  4042. {
  4043. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2505, Code_Off);
  4044. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2505, Code_Reset);
  4045. PowerOffAckStatus = false;
  4046. Delay_ms(250);
  4047. }
  4048. else break;
  4049. }
  4050. if (PowerOffAckStatus)
  4051. {
  4052. timer_1s.Enabled = false;
  4053. MessageBox.Show("已关机!", "提示", MessageBoxButtons.OK, MessageBoxIcon.None);
  4054. timer_1s.Enabled = true;
  4055. }
  4056. else
  4057. {
  4058. timer_1s.Enabled = false;
  4059. MessageBox.Show("关机失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  4060. timer_1s.Enabled = true;
  4061. }
  4062. }
  4063. timer_1s.Enabled = true;
  4064. }
  4065. //仪表电源键按下
  4066. private void button_OBC_ButtonPower_Click(object sender, EventArgs e)
  4067. {
  4068. var Code = new byte[2];
  4069. Code[0] = 0;
  4070. Code[1] = 0b00100000;
  4071. mySerialProcess.SendCmd((ushort)0x731, (byte)0x0C, (ushort)0x3A02, Code);
  4072. }
  4073. /// <summary>
  4074. /// 仪表加键按下
  4075. /// </summary>
  4076. /// <param name="sender"></param>
  4077. /// <param name="e"></param>
  4078. private void button_OBC_ButtonAcc_Click(object sender, EventArgs e)
  4079. {
  4080. var Code = new byte[2];
  4081. Code[0] = 0;
  4082. Code[1] = 0b00010000;
  4083. mySerialProcess.SendCmd((ushort)0x731, (byte)0x0C, (ushort)0x3A02, Code);
  4084. }
  4085. /// <summary>
  4086. /// 仪表减键按下
  4087. /// </summary>
  4088. /// <param name="sender"></param>
  4089. /// <param name="e"></param>
  4090. private void button_OBC_ButtonDec_Click(object sender, EventArgs e)
  4091. {
  4092. var Code = new byte[2];
  4093. Code[0] = 0;
  4094. Code[1] = 0b00001000;
  4095. mySerialProcess.SendCmd((ushort)0x731, (byte)0x0C, (ushort)0x3A02, Code);
  4096. }
  4097. /// <summary>
  4098. /// 仪表Walk键按下
  4099. /// </summary>
  4100. /// <param name="sender"></param>
  4101. /// <param name="e"></param>
  4102. private void button_OBC_ButtonWalk_Click(object sender, EventArgs e)
  4103. {
  4104. var Code = new byte[2];
  4105. Code[0] = 0;
  4106. Code[1] = 0b00000100;
  4107. mySerialProcess.SendCmd((ushort)0x731, (byte)0x0C, (ushort)0x3A02, Code);
  4108. }
  4109. /// <summary>
  4110. /// 仪表Light键按下
  4111. /// </summary>
  4112. /// <param name="sender"></param>
  4113. /// <param name="e"></param>
  4114. private void button_OBC_ButtonLight_Click(object sender, EventArgs e)
  4115. {
  4116. var Code = new byte[2];
  4117. Code[0] = 0;
  4118. Code[1] = 0b00000010;
  4119. mySerialProcess.SendCmd((ushort)0x731, (byte)0x0C, (ushort)0x3A02, Code);
  4120. }
  4121. /// <summary>
  4122. /// 仪表Set键按下
  4123. /// </summary>
  4124. /// <param name="sender"></param>
  4125. /// <param name="e"></param>
  4126. private void button_OBC_ButtonSet_Click(object sender, EventArgs e)
  4127. {
  4128. var Code = new byte[2];
  4129. Code[0] = 0;
  4130. Code[1] = 0b00000001;
  4131. mySerialProcess.SendCmd((ushort)0x731, (byte)0x0C, (ushort)0x3A02, Code);
  4132. }
  4133. /// <summary>
  4134. /// 带载类型选择
  4135. /// </summary>
  4136. /// <param name="sender"></param>
  4137. /// <param name="e"></param>
  4138. private void label_Load_Click(object sender, EventArgs e)
  4139. {
  4140. if (label_Load.Text == "带载")
  4141. label_Load.Text = "空载";
  4142. else if (label_Load.Text == "空载")
  4143. label_Load.Text = "带载";
  4144. }
  4145. /// <summary>
  4146. /// 控制模式选择
  4147. /// </summary>
  4148. /// <param name="sender"></param>
  4149. /// <param name="e"></param>
  4150. private void label_MotorSensor_Click(object sender, EventArgs e)
  4151. {
  4152. if (label_MotorSensor.Text == "有感")
  4153. label_MotorSensor.Text = "无感";
  4154. else if (label_MotorSensor.Text == "无感")
  4155. label_MotorSensor.Text = "有感";
  4156. }
  4157. /// <summary>
  4158. /// 导出电机故障日志
  4159. /// </summary>
  4160. /// <param name="sender"></param>
  4161. /// <param name="e"></param>
  4162. private void button_ExportLog_Click(object sender, EventArgs e)
  4163. {
  4164. var Address = new byte[4];
  4165. UInt32 Address_Begin;
  4166. UInt32 Addres_End;
  4167. string textBox_Address_Begin = textBox_LogAddrBegin.Text;
  4168. string textBox_Address_End = textBox_LogAddrEnd.Text;
  4169. richTextBox_Record.Clear();
  4170. textBox_LogAddrBegin.ReadOnly = true;
  4171. textBox_LogAddrEnd.ReadOnly = true;
  4172. if (!mySerialProcess.mySerial.IsOpen)
  4173. {
  4174. timer_1s.Enabled = false;
  4175. MessageBox.Show("串口未连接!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  4176. timer_1s.Enabled = true;
  4177. return;
  4178. }
  4179. if ((textBox_Address_Begin == string.Empty) || (textBox_Address_End == string.Empty))
  4180. {
  4181. timer_1s.Enabled = false;
  4182. MessageBox.Show("请输入日志存储起始地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  4183. timer_1s.Enabled = true;
  4184. return;
  4185. }
  4186. else
  4187. {
  4188. Address = HexStringToBytes(textBox_Address_Begin, false);
  4189. Address_Begin = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
  4190. Address = HexStringToBytes(textBox_Address_End, false);
  4191. Addres_End = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
  4192. if (Addres_End <= Address_Begin)
  4193. {
  4194. timer_1s.Enabled = false;
  4195. MessageBox.Show("结束地址应大于起始地址!", "提示");
  4196. timer_1s.Enabled = true;
  4197. return;
  4198. }
  4199. else if (((Addres_End - Address_Begin + 1) % 128) != 0) //
  4200. {
  4201. timer_1s.Enabled = false;
  4202. MessageBox.Show("(结束地址 - 起始地址 + 1)应为128整数倍!", "提示");
  4203. timer_1s.Enabled = true;
  4204. return;
  4205. }
  4206. else
  4207. {
  4208. UInt32 ReadCount = 0;
  4209. button_ExportLog.Text = "导出中...";
  4210. button_ExportLog.Enabled = false;
  4211. ReadCount = (Addres_End - Address_Begin + 1) / 128;
  4212. for (ushort i = 0; i < ReadCount; i++)
  4213. {
  4214. var SendByte = new byte[8];
  4215. UInt32 Address_Temp_Begin;
  4216. UInt32 Address_Temp_End;
  4217. Address_Temp_Begin = Address_Begin + 128 * (UInt32)i;
  4218. SendByte[0] = (byte)(Address_Temp_Begin >> 24);
  4219. SendByte[1] = (byte)(Address_Temp_Begin >> 16);
  4220. SendByte[2] = (byte)(Address_Temp_Begin >> 8);
  4221. SendByte[3] = (byte)(Address_Temp_Begin);
  4222. Address_Temp_End = Address_Temp_Begin + 127;
  4223. SendByte[4] = (byte)(Address_Temp_End >> 24);
  4224. SendByte[5] = (byte)(Address_Temp_End >> 16);
  4225. SendByte[6] = (byte)(Address_Temp_End >> 8);
  4226. SendByte[7] = (byte)(Address_Temp_End);
  4227. mySerialProcess.SendCmdWithoutNotice((ushort)0x751, (byte)0x11, (ushort)0x2D08, SendByte);
  4228. Delay_ms(200);//Thread.Sleep(200);
  4229. }
  4230. button_ExportLog.Text = "导出日志";
  4231. button_ExportLog.Enabled = true;
  4232. }
  4233. }
  4234. }
  4235. /// <summary>
  4236. /// 保存日志信息
  4237. /// </summary>
  4238. /// <param name="sender"></param>
  4239. /// <param name="e"></param>
  4240. private void button_SaveRecordInfo_Click(object sender, EventArgs e)
  4241. {
  4242. string SaveInfo = "";
  4243. if (richTextBox_Record.Text == string.Empty)
  4244. {
  4245. timer_1s.Enabled = false;
  4246. MessageBox.Show("无内容!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  4247. timer_1s.Enabled = true;
  4248. return;
  4249. }
  4250. //获取保存信息
  4251. SaveInfo += "读取时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n";
  4252. SaveInfo += "电机型号:" + textBox_FacModeName.Text + "\r\n";
  4253. SaveInfo += "电机SN:" + textBox_FacModeNum.Text + "\r\n";
  4254. SaveInfo += "硬件版本:" + textBox_FacModeHW.Text + "\r\n";
  4255. SaveInfo += "软件版本:" + textBox_FacModeFW.Text + "\r\n";
  4256. SaveInfo += "\r\n";
  4257. SaveInfo += richTextBox_Record.Text;
  4258. //设置保存路径和文件名
  4259. SaveFileDialog sf = new SaveFileDialog();
  4260. sf.Title = "数据保存";
  4261. sf.FileName = "MotorLog_" + textBox_FacModeName.Text + "_" + textBox_FacModeNum.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + ".ttlog";
  4262. sf.FileName.Replace(" ", "");
  4263. sf.Filter = "日志|*.ttlog";
  4264. if (sf.ShowDialog() == DialogResult.OK)
  4265. {
  4266. System.IO.File.AppendAllText(sf.FileName, SaveInfo);
  4267. }
  4268. }
  4269. /// <summary>
  4270. /// 双击修改日志起始地址
  4271. /// </summary>
  4272. /// <param name="sender"></param>
  4273. /// <param name="e"></param>
  4274. private void label_LogAddrBegin_DoubleClick_1(object sender, EventArgs e)
  4275. {
  4276. textBox_LogAddrBegin.ReadOnly = false;
  4277. }
  4278. /// <summary>
  4279. /// 双击修改日志结束地址
  4280. /// </summary>
  4281. /// <param name="sender"></param>
  4282. /// <param name="e"></param>
  4283. private void label88_DoubleClick(object sender, EventArgs e)
  4284. {
  4285. textBox_LogAddrEnd.ReadOnly = false;
  4286. }
  4287. /// <summary>
  4288. /// 存储器自动读取选择
  4289. /// </summary>
  4290. /// <param name="sender"></param>
  4291. /// <param name="e"></param>
  4292. private void checkBox_ReadRanFlash_AutoSW_CheckedChanged(object sender, EventArgs e)
  4293. {
  4294. if (checkBox_ReadRanFlash_AutoSW.Checked == true)
  4295. {
  4296. //检查串口是否打开
  4297. if (!mySerialProcess.mySerial.IsOpen)
  4298. {
  4299. timer_1s.Enabled = false;
  4300. MessageBox.Show("串口未连接!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  4301. timer_1s.Enabled = true;
  4302. checkBox_ReadRanFlash_AutoSW.Checked = false;
  4303. return;
  4304. }
  4305. }
  4306. }
  4307. /// <summary>
  4308. /// 系统开机按钮
  4309. /// </summary>
  4310. /// <param name="sender"></param>
  4311. /// <param name="e"></param>
  4312. private void 开机ToolStripMenuItem_Click(object sender, EventArgs e)
  4313. {
  4314. byte[] Data = new byte[1];
  4315. Data[0] = 0xF1;
  4316. if (mySerialProcess.SendCmd(0x7FF, 0x16, 0x2201, Data))
  4317. {
  4318. 开机ToolStripMenuItem.Checked = true;
  4319. 关机ToolStripMenuItem.Checked = false;
  4320. }
  4321. }
  4322. /// <summary>
  4323. /// 系统关机按钮
  4324. /// </summary>
  4325. /// <param name="sender"></param>
  4326. /// <param name="e"></param>
  4327. private void 关机ToolStripMenuItem_Click(object sender, EventArgs e)
  4328. {
  4329. byte[] Data = new byte[1];
  4330. Data[0] = 0xF0;
  4331. if (mySerialProcess.SendCmd(0x7FF, 0x16, 0x2201, Data))
  4332. {
  4333. 开机ToolStripMenuItem.Checked = false;
  4334. 关机ToolStripMenuItem.Checked = true;
  4335. }
  4336. }
  4337. /// <summary>
  4338. /// 连接开机选择
  4339. /// </summary>
  4340. /// <param name="sender"></param>
  4341. /// <param name="e"></param>
  4342. private void 连接开机ToolStripMenuItem_Click(object sender, EventArgs e)
  4343. {
  4344. 连接开机ToolStripMenuItem.Checked = !连接开机ToolStripMenuItem.Checked;
  4345. ConfigFileSave(true, ConfigFileName);
  4346. }
  4347. /// <summary>
  4348. /// 断开关机选择
  4349. /// </summary>
  4350. /// <param name="sender"></param>
  4351. /// <param name="e"></param>
  4352. private void 断开关机ToolStripMenuItem_Click(object sender, EventArgs e)
  4353. {
  4354. 断开关机ToolStripMenuItem.Checked = !断开关机ToolStripMenuItem.Checked;
  4355. ConfigFileSave(true, ConfigFileName);
  4356. }
  4357. /// <summary>
  4358. /// 识别通讯盒选择
  4359. /// </summary>
  4360. /// <param name="sender"></param>
  4361. /// <param name="e"></param>
  4362. private void 识别通讯盒ToolStripMenuItem_Click(object sender, EventArgs e)
  4363. {
  4364. 识别通讯盒ToolStripMenuItem.Checked = !识别通讯盒ToolStripMenuItem.Checked;
  4365. ConfigFileSave(true, ConfigFileName);
  4366. }
  4367. /// <summary>
  4368. /// 将配置信息导出为cfg文件
  4369. /// </summary>
  4370. private void ConfigFileSave(bool DeleteFlag, string FileName)
  4371. {
  4372. if (DeleteFlag)
  4373. {
  4374. if (File.Exists(FileName))
  4375. File.Delete(FileName);
  4376. }
  4377. string CfgFileInfo = "";
  4378. CfgFileInfo += "[PortSet]" + "\r\n";
  4379. CfgFileInfo += "PortNum:" + toolStripComboBox_ComNum.Text + "\r\n";
  4380. CfgFileInfo += "PortBaudrate:" + toolStripComboBox_Baudrate.Text + "\r\n";
  4381. CfgFileInfo += "AutoOn:" + 连接开机ToolStripMenuItem.Checked.ToString() + "\r\n";
  4382. CfgFileInfo += "AutoOff:" + 断开关机ToolStripMenuItem.Checked.ToString() + "\r\n";
  4383. CfgFileInfo += "CheckCDL:" + 识别通讯盒ToolStripMenuItem.Checked.ToString() + "\r\n";
  4384. CfgFileInfo += "\r\n";
  4385. CfgFileInfo += "[Option]" + "\r\n";
  4386. CfgFileInfo += "SaveWhenWrite:" + 写入存储ToolStripMenuItem.Checked.ToString() + "\r\n";
  4387. CfgFileInfo += "OfflineEnable:" + 允许ToolStripMenuItem.Checked + "\r\n";
  4388. CfgFileInfo += "\r\n";
  4389. CfgFileInfo += "[ServerSet]" + "\r\n";
  4390. CfgFileInfo += "IPAddress:" + toolStripTextBox_ServerIP.Text + "\r\n";
  4391. CfgFileInfo += "Port:" + toolStripTextBox_ServerPort.Text + "\r\n";
  4392. CfgFileInfo += "User:" + toolStripTextBox_ServerUser.Text + "\r\n";
  4393. CfgFileInfo += "Password:" + toolStripTextBox_ServerPasswd.Text + "\r\n";
  4394. CfgFileInfo += "PathName:" + toolStripTextBox_ServerPath.Text + "\r\n";
  4395. CfgFileInfo += "\r\n";
  4396. CfgFileInfo += "[MacInfo]" + "\r\n";
  4397. CfgFileInfo += "MacName:" + toolStripTextBox_MAC.Text + "\r\n";
  4398. CfgFileInfo += "MacAddress:" + toolStripTextBoxMACAddr.Text + "\r\n";
  4399. CfgFileInfo += "MacDate:" + toolStripTextBox_MACDate.Text + "\r\n";
  4400. CfgFileInfo += "MacPdInfo:" + toolStripTextBox_MACPD.Text + "\r\n";
  4401. System.IO.File.WriteAllText(FileName, CfgFileInfo);
  4402. }
  4403. /// <summary>
  4404. /// 配置状态更新,点击时保存
  4405. /// </summary>
  4406. private void ConfigFileSave_Click(object sender, EventArgs e)
  4407. {
  4408. ConfigFileSave(true, ConfigFileName);
  4409. }
  4410. /// <summary>
  4411. /// 文本框修改结束,回车保存
  4412. /// </summary>
  4413. /// <param name="sender"></param>
  4414. /// <param name="e"></param>
  4415. private void ConfigFileSave_KeyDown(object sender, KeyEventArgs e)
  4416. {
  4417. if (e.KeyCode == Keys.Enter)
  4418. {
  4419. ConfigFileSave(true, ConfigFileName);
  4420. }
  4421. }
  4422. /// <summary>
  4423. /// 允许离线使用
  4424. /// </summary>
  4425. /// <param name="sender"></param>
  4426. /// <param name="e"></param>
  4427. private void 允许ToolStripMenuItem_Click(object sender, EventArgs e)
  4428. {
  4429. 允许ToolStripMenuItem.Checked = true;
  4430. 不允许ToolStripMenuItem.Checked = false;
  4431. ConfigFileSave(true, ConfigFileName);
  4432. }
  4433. /// <summary>
  4434. /// 不允许离线使用
  4435. /// </summary>
  4436. /// <param name="sender"></param>
  4437. /// <param name="e"></param>
  4438. private void 不允许ToolStripMenuItem_Click(object sender, EventArgs e)
  4439. {
  4440. 允许ToolStripMenuItem.Checked = false;
  4441. 不允许ToolStripMenuItem.Checked = true;
  4442. ConfigFileSave(true, ConfigFileName);
  4443. }
  4444. /// <summary>
  4445. /// 弹出扫码按钮
  4446. /// </summary>
  4447. /// <param name="sender"></param>
  4448. /// <param name="e"></param>
  4449. private void button_ScanCode_Click(object sender, EventArgs e)
  4450. {
  4451. Scan scan_form = new Scan();
  4452. scan_form.ShowDialog();
  4453. do
  4454. {
  4455. if (scan_form.textBox_Code.Text == string.Empty)
  4456. {
  4457. timer_1s.Enabled = false;
  4458. MessageBox.Show("无内容!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  4459. timer_1s.Enabled = true;
  4460. break;
  4461. }
  4462. string ScanInfo = scan_form.textBox_Code.Text;
  4463. string regexInfo = @"[0-9][0-9][V][0-9][0-9][0-9][W]";
  4464. string[] InfoList = Regex.Split(ScanInfo, regexInfo, RegexOptions.IgnoreCase);
  4465. if (InfoList.Length >= 2)
  4466. {
  4467. //写入型号
  4468. textBox_FacModeName.Text = InfoList[0];
  4469. string Mode = textBox_FacModeName.Text;
  4470. var ModeArray = new byte[16];
  4471. for (ushort i = 0; i < Mode.Length; i++)
  4472. {
  4473. ModeArray[i] = (byte)Mode[i];
  4474. }
  4475. if (Mode.Length < 16)
  4476. {
  4477. ModeArray[Mode.Length] = (byte)'.';
  4478. for (ushort i = 0; i < 16 - Mode.Length - 1; i++)
  4479. {
  4480. ModeArray[Mode.Length + 1 + i] = 0x20;
  4481. }
  4482. }
  4483. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2210, ModeArray);
  4484. //写入SN
  4485. textBox_FacModeNum.Text = InfoList[1];
  4486. string SN = textBox_FacModeNum.Text;
  4487. var SNArray = new byte[16];
  4488. for (ushort i = 0; i < SN.Length; i++)
  4489. {
  4490. SNArray[i] = (byte)SN[i];
  4491. }
  4492. if (SN.Length < 16)
  4493. {
  4494. SNArray[SN.Length] = (byte)'.';
  4495. for (ushort i = 0; i < 16 - SN.Length - 1; i++)
  4496. {
  4497. SNArray[SN.Length + 1 + i] = 0x20;
  4498. }
  4499. }
  4500. mySerialProcess.SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2310, SNArray);
  4501. }
  4502. else
  4503. {
  4504. timer_1s.Enabled = false;
  4505. MessageBox.Show("格式错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  4506. timer_1s.Enabled = true;
  4507. break;
  4508. }
  4509. } while (false);
  4510. scan_form.Dispose();
  4511. }
  4512. }
  4513. }