mainForm.cs 250 KB

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