Form1.cs 214 KB

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