mainForm.cs 240 KB

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