mainForm.cs 234 KB

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