Form1.cs 205 KB

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