mainForm.cs 242 KB

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