Form1.cs 206 KB

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