Form1.cs 211 KB

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