mainForm.cs 263 KB

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