Form1.cs 211 KB

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