mainForm.cs 237 KB

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