123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.IO.Ports;
- using System.Threading;
- using System.Runtime.InteropServices;
- using System.Runtime.CompilerServices;
- using System.Collections;
- using System.IO;
- using static System.Net.Mime.MediaTypeNames;
- using System.Globalization;
- namespace MOTINOVA_MC_Test
- {
- public partial class Form1 : Form
- {
- #region------------------------本地变量-----------------------------
- #region 串口相关
- private List<byte> buffer = new List<byte>(4096);
- private byte[] binary_data_1 = new byte[1024];
- private bool Listening = false;// 侦听串口是否是接收数据标志位
- public static string m_strDataBits = "8";
- public static string m_strStopBits = "1";
- public static string m_strCheckBits = "None";
- public static bool g_blnIsOpen = false;
- #endregion
- #region FTP实例
- public static ftp myFtp = new ftp();
- public static string ServerIP = "";
- public static string ServerPort = "";
- public static string ServerUser = "";
- public static string ServerPasswd = "";
- public static string ServerPath = "";
- #endregion
- #region 离线使用标识
- bool Offline = true;
- #endregion
- #region 本地配置存储数据路径
- public static string LocalPath = "";
- string ConfigFileName = "";
- #endregion
- #region MC运行信息超时计数
- bool MC_RunInfo_Refresh = false;
- ushort MC_RunInfo_Refresh_Cnt = 0;
- #endregion
- #region
- bool MC_DebugInfo_Refresh = false;
- ushort MC_DebugInfo_Refresh_Cnt = 0;
- #endregion
- #region MC故障码超时计数
- bool MC_ErrorCode_Refresh = false;
- ushort MC_ErrorCode_Refresh_Cnt = 0;
- #endregion
- #region 力矩传感器平均值采集
- byte[] TorqueArray = new byte[25];
- byte[] Sensor1Array = new byte[25];
- byte[] Sensor2Array = new byte[25];
- byte[] Sensor3Array = new byte[25];
- ushort TorqueArray_Index = 0;
- #endregion
- #region 自动发送助力档位计时
- ushort AutoSendTimeCnt;
- #endregion
- #region 骑行信息超时计数
- bool MC_RideInfo_Refresh = false;
- ushort MC_RideInfo_Refresh_Cnt = 0;
- #endregion
- #region 姿态信息超时计数
- bool MC_AttitudeAngle_Refresh = false;
- ushort MC_AttitudeAngle_Refresh_Cnt = 0;
- #endregion
- #region 自动发送查询姿态信息计时
- ushort AutoSendReadAttitudeAngleTimeCnt;
- #endregion
- #region 自动发送查询骑行信息计时
- ushort AutoSendReadRideInfoTimeCnt;
- #endregion
- #region 自动存储数据
- string AutoSaveFileName = "";
- #endregion
- #region 故障日志存储相关
- bool ErrorLogSaveFlag = false;
- string ErrorLogSaveFileName = "";
- #endregion
- #region CDL在线检测延时
- ushort CDL_OnlineCheck_Cnt = 0;
- bool CDL_Online_Flag = true;
- #endregion
- #region 截图悬浮窗
- topFormCapture myCapture;
- #endregion
- #region 解锁写保护标志
- bool WriteProtectUnlock = false;
- #endregion
- Form2 LoginForm = new Form2();
- #endregion
- public Form1()
- {
- InitializeComponent();
- //检查许可
- if (!System.IO.File.Exists(Directory.GetCurrentDirectory() + "\\licences.lic")) //未许可
- {
- if (MessageBox.Show("无授权,请导入许可文件!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
- {
- Licences myLicences = new Licences();
- myLicences.ShowDialog();
- //从许可文件读取内容
- string str_lic = aes.DecryptFromFile(myLicences.textBox_LicencePath.Text, "2D820F88F60A39D5", "3687C5216B19D16B");
- string PC_Code = PC_Information.GetCPUSerialNumber();
- if (PC_Code == str_lic) //授权码正确
- {
- aes.EncryptToFile(PC_Code, Directory.GetCurrentDirectory() + "\\licences.lic", "2D820F88F60A39D5", "3687C5216B19D16B");
- string strInfo = "";
- strInfo = "0;" + DateTime.Now.ToString("yyyyMMdd");
- aes.EncryptToFile(strInfo, Directory.GetCurrentDirectory() + "\\offlog", "2D820F88F60A39D5", "3687C5216B19D16B");
- }
- else //授权码错误
- {
- MessageBox.Show("许可文件无效,请联系管理员!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- System.Environment.Exit(0);
- }
- }
- else
- {
- System.Environment.Exit(0);
- }
- }
- else //已授权
- {
- string str_Key = aes.DecryptFromFile(Directory.GetCurrentDirectory() + "\\licences.lic", "2D820F88F60A39D5", "3687C5216B19D16B");
- //判断授权是否正确
- if (PC_Information.GetCPUSerialNumber() == str_Key) //授权正确
- {
- //检查离线使用记录文件
- if (!System.IO.File.Exists(Directory.GetCurrentDirectory() + "\\offlog"))
- {
- MessageBox.Show("许可文件内容缺失,请联系管理员!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- File.Delete(Directory.GetCurrentDirectory() + "\\licences.lic");//缺少离线使用记录文件时,自动删除许可文件,下次打开重新导入许可
- System.Environment.Exit(0);
- }
- //获取离线使用记录并更新
- string UseLog = aes.DecryptFromFile(Directory.GetCurrentDirectory() + "\\offlog", "2D820F88F60A39D5", "3687C5216B19D16B");
- UInt32 UseTimes = Convert.ToUInt32(UseLog.Split(';')[0]);
- UseTimes++;
- string str = UseTimes.ToString() + ";" + UseLog.Split(';')[1];
- aes.EncryptToFile(str, Directory.GetCurrentDirectory() + "\\offlog", "2D820F88F60A39D5", "3687C5216B19D16B");
- //判断授权期限
- DateTime DataOrigin = new DateTime();
- DateTime.TryParseExact(UseLog.Split(';')[1], "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DataOrigin);
- DateTime DataEnd = DataOrigin.AddDays(90);
- DateTime NowDate = DateTime.Now.Date;
- if ((NowDate > DataEnd) || (UseTimes > 1000)) //第一次打开软件90天内,或1000次内
- {
- MessageBox.Show("授权过期,请联系管理员!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- File.Delete(Directory.GetCurrentDirectory() + "\\licences.lic");//许可过期或超出使用次数,自动删除许可文件和使用记录,下次打开重新导入许可
- File.Delete(Directory.GetCurrentDirectory() + "\\offlog");//许可过期或超出使用次数,自动删除许可文件和使用记录,下次打开重新导入许可
- System.Environment.Exit(0);
- }
- }
- else
- {
- MessageBox.Show("许可文件错误,请联系管理员!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- File.Delete(Directory.GetCurrentDirectory() + "\\licences.lic");//许可过期或超出使用次数,自动删除许可文件和使用记录,下次打开重新导入许可
- File.Delete(Directory.GetCurrentDirectory() + "\\offlog");//许可过期或超出使用次数,自动删除许可文件和使用记录,下次打开重新导入许可
- System.Environment.Exit(0);
- }
- }
- //显示截图悬浮窗
- myCapture = new topFormCapture();
- myCapture.Show();
- //检查配置文件
- LocalPath = Directory.GetCurrentDirectory();
- if (Directory.Exists(LocalPath + "\\" + "MotorRunTest") == false)
- Directory.CreateDirectory(LocalPath + "\\" + "MotorRunTest");
- ConfigFileName = Directory.GetCurrentDirectory() + "\\" + "Server";
- if (File.Exists(ConfigFileName)) //存在配置文件,导入配置信息
- {
- //打开文件
- string fileInfo = aes.DecryptFromFile(ConfigFileName, "2D820F88F60A39D5", "3687C5216B19D16B");
- string[] array_CfgInfo = fileInfo.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
- try
- {
- ServerIP = array_CfgInfo[1].ToString().Split(':')[1];
- ServerPort = array_CfgInfo[2].ToString().Split(':')[1];
- ServerUser = array_CfgInfo[3].ToString().Split(':')[1];
- ServerPasswd = array_CfgInfo[4].ToString().Split(':')[1];
- ServerPath = array_CfgInfo[5].ToString().Split(':')[1];
- Offline = (array_CfgInfo[6].ToString().Split(':')[1] == "TRUE") ? true : false;
- }
- catch (System.Exception)
- {
- MessageBox.Show("参数格式错误,写入默认值!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- }
- else //不存在配置文件,生成文件写入默认值
- {
- string CfgFileInfo = "";
- #if true //武汉
- ServerIP = "10.177.130.30";
- ServerPort = "15021";
- ServerUser = "factory_test";
- ServerPasswd = "ttium";
- ServerPath = "MotorRunTest";
- #else //阿里云
- ServerIP = "8.134.109.44";
- ServerPort = "21";
- ServerUser = "ttium";
- ServerPasswd = "ttium123";
- ServerPath = "MotorRunTest";
- #endif
- CfgFileInfo += "[ServerSet]" + "\r\n";
- CfgFileInfo += "IPAddress:" + ServerIP + "\r\n";
- CfgFileInfo += "Port:" + ServerPort + "\r\n";
- CfgFileInfo += "User:" + ServerUser + "\r\n";
- CfgFileInfo += "Password:" + ServerPasswd + "\r\n";
- CfgFileInfo += "PathName:" + ServerPath + "\r\n";
- CfgFileInfo += "Offline:" + "TRUE" + "\r\n";
- aes.EncryptToFile(CfgFileInfo, ConfigFileName, "2D820F88F60A39D5", "3687C5216B19D16B");
- }
- //配置网络FTP服务器
- myFtp.FtpOption(ServerIP, ServerPort, ServerUser, ServerPasswd);
- //创建线程,定时检测网络连接状态
- Thread th = new Thread(NetworkCheck);
- th.IsBackground = true;
- th.Start();
- }
- /// <summary>
- /// 检查网络状态线程
- /// </summary>
- private void NetworkCheck()
- {
- //初始化第一次连接FTP服务器
- bool Result = myFtp.CheckFtp();
- this.Invoke((EventHandler)(delegate
- {
- if (Result == true)//服务器连接成功
- {
- MessageBox.Show("服务器连接成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- myFtp.IsNetConnected = true;
- myCapture.label_ServerStatus.BackColor = Color.Green;
- }
- else//服务器连接失败
- {
- MessageBox.Show("服务器连接失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- myFtp.IsNetConnected = false;
- myCapture.label_ServerStatus.BackColor = Color.Red;
- if (Offline != true)
- this.Dispose();
- }
- }));
- //创建定时器,定时30s检查网络
- System.Timers.Timer timer_CheckNet = new System.Timers.Timer();
- timer_CheckNet.Enabled = true;
- timer_CheckNet.Interval = 30000;
- timer_CheckNet.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_CheckNet_Tick);
- timer_CheckNet.Start();
- while (true)
- {
- Thread.Sleep(1);
- }
- }
- /// <summary>
- /// 线程中定时任务,检查网络状态
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void timer_CheckNet_Tick(object sender, EventArgs e)
- {
- //连接FTP服务器
- bool Result = myFtp.CheckFtp();
- this.Invoke((EventHandler)(delegate
- {
- if (Result == true)//服务器连接成功
- {
- myFtp.IsNetConnected = true;
- myCapture.label_ServerStatus.BackColor = Color.Green;
- }
- else//服务器连接失败
- {
- myFtp.IsNetConnected = false;
- myCapture.label_ServerStatus.BackColor = Color.Red;
- }
- }));
- }
-
- #region 打开或关闭串口
- private void button_ComOpen_Click(object sender, EventArgs e)
- {
- byte[] Data = new byte[1];
- try
- {
- if (!serialPort1.IsOpen)
- {
- serialPort1.PortName = comboBox_ComIndex.Text;
- serialPort1.BaudRate = int.Parse(comboBox_Baudrate.Text);
- serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), m_strCheckBits);
- serialPort1.DataBits = Int32.Parse(m_strDataBits);
- serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), (m_strStopBits == "1.5") ? "3" : m_strStopBits);
- serialPort1.Open();
- comboBox_ComIndex.Enabled = false;
- comboBox_Baudrate.Enabled = false;
- button_ComOpen.Text = "断开(&C)";
- g_blnIsOpen = true;
- timer1.Enabled = true;
- button_RefreshPort.Enabled = false;
- //发送指令检测CDL是否在线
- SendCmd((ushort)0x7FF, (byte)0x11, (ushort)0x1100, null);
- CDL_OnlineCheck_Cnt = 0;
- CDL_Online_Flag = false;
- //开机
- Data[0] = 0xF1;
- SendCmd(0x7FF, 0x16, 0x2201, Data);
- }
- else
- {
- //关机
- Data[0] = 0xF0;
- SendCmd(0x7FF, 0x16, 0x2201, Data);
- //while (Listening) Application.DoEvents();
- //打开时点击,则关闭串口
- serialPort1.Close();
- comboBox_ComIndex.Enabled = true;
- comboBox_Baudrate.Enabled = true;
- button_ComOpen.Text = "连接(&C)";
- g_blnIsOpen = false;
- timer1.Enabled = false;
- button_RefreshPort.Enabled = true;
- CDL_Online_Flag = true;
- }
- }
- catch (System.Exception ex)
- {
- MessageBox.Show(ex.Message);
- return;
- }
- }
- #endregion
- private void Form1_Load(object sender, EventArgs e)
- {
- string[] ports = SerialPort.GetPortNames();
- Array.Sort(ports);
- comboBox_ComIndex.Items.AddRange(ports);
- comboBox_ComIndex.SelectedIndex = comboBox_ComIndex.Items.Count > 0 ? 0 : -1;
- comboBox_Baudrate.SelectedIndex = comboBox_Baudrate.Items.IndexOf("115200");
- MC_Config_Clear();
- Motor_Config_Clear();
- MC_RunInfo_Clear();
- MC_ErrorCode_Clear();
- MC_Config_Ext_Clear();
- MC_AttitudeAngle_Clear();
- MC_RideInfo_Clear();
- MC_RunLog_Clear();
- label_BiuldTime.Text = System.IO.File.GetLastWriteTime(this.GetType().Assembly.Location).ToString();
- }
- #region CRC校验
- static UInt32[] crc32_table = new UInt32[256]
- {
- 0x00000000, 0x04C11DB7, 0x09823B6E, 0x0D4326D9, 0x130476DC, 0x17C56B6B,
- 0x1A864DB2, 0x1E475005, 0x2608EDB8, 0x22C9F00F, 0x2F8AD6D6, 0x2B4BCB61,
- 0x350C9B64, 0x31CD86D3, 0x3C8EA00A, 0x384FBDBD, 0x4C11DB70, 0x48D0C6C7,
- 0x4593E01E, 0x4152FDA9, 0x5F15ADAC, 0x5BD4B01B, 0x569796C2, 0x52568B75,
- 0x6A1936C8, 0x6ED82B7F, 0x639B0DA6, 0x675A1011, 0x791D4014, 0x7DDC5DA3,
- 0x709F7B7A, 0x745E66CD, 0x9823B6E0, 0x9CE2AB57, 0x91A18D8E, 0x95609039,
- 0x8B27C03C, 0x8FE6DD8B, 0x82A5FB52, 0x8664E6E5, 0xBE2B5B58, 0xBAEA46EF,
- 0xB7A96036, 0xB3687D81, 0xAD2F2D84, 0xA9EE3033, 0xA4AD16EA, 0xA06C0B5D,
- 0xD4326D90, 0xD0F37027, 0xDDB056FE, 0xD9714B49, 0xC7361B4C, 0xC3F706FB,
- 0xCEB42022, 0xCA753D95, 0xF23A8028, 0xF6FB9D9F, 0xFBB8BB46, 0xFF79A6F1,
- 0xE13EF6F4, 0xE5FFEB43, 0xE8BCCD9A, 0xEC7DD02D, 0x34867077, 0x30476DC0,
- 0x3D044B19, 0x39C556AE, 0x278206AB, 0x23431B1C, 0x2E003DC5, 0x2AC12072,
- 0x128E9DCF, 0x164F8078, 0x1B0CA6A1, 0x1FCDBB16, 0x018AEB13, 0x054BF6A4,
- 0x0808D07D, 0x0CC9CDCA, 0x7897AB07, 0x7C56B6B0, 0x71159069, 0x75D48DDE,
- 0x6B93DDDB, 0x6F52C06C, 0x6211E6B5, 0x66D0FB02, 0x5E9F46BF, 0x5A5E5B08,
- 0x571D7DD1, 0x53DC6066, 0x4D9B3063, 0x495A2DD4, 0x44190B0D, 0x40D816BA,
- 0xACA5C697, 0xA864DB20, 0xA527FDF9, 0xA1E6E04E, 0xBFA1B04B, 0xBB60ADFC,
- 0xB6238B25, 0xB2E29692, 0x8AAD2B2F, 0x8E6C3698, 0x832F1041, 0x87EE0DF6,
- 0x99A95DF3, 0x9D684044, 0x902B669D, 0x94EA7B2A, 0xE0B41DE7, 0xE4750050,
- 0xE9362689, 0xEDF73B3E, 0xF3B06B3B, 0xF771768C, 0xFA325055, 0xFEF34DE2,
- 0xC6BCF05F, 0xC27DEDE8, 0xCF3ECB31, 0xCBFFD686, 0xD5B88683, 0xD1799B34,
- 0xDC3ABDED, 0xD8FBA05A, 0x690CE0EE, 0x6DCDFD59, 0x608EDB80, 0x644FC637,
- 0x7A089632, 0x7EC98B85, 0x738AAD5C, 0x774BB0EB, 0x4F040D56, 0x4BC510E1,
- 0x46863638, 0x42472B8F, 0x5C007B8A, 0x58C1663D, 0x558240E4, 0x51435D53,
- 0x251D3B9E, 0x21DC2629, 0x2C9F00F0, 0x285E1D47, 0x36194D42, 0x32D850F5,
- 0x3F9B762C, 0x3B5A6B9B, 0x0315D626, 0x07D4CB91, 0x0A97ED48, 0x0E56F0FF,
- 0x1011A0FA, 0x14D0BD4D, 0x19939B94, 0x1D528623, 0xF12F560E, 0xF5EE4BB9,
- 0xF8AD6D60, 0xFC6C70D7, 0xE22B20D2, 0xE6EA3D65, 0xEBA91BBC, 0xEF68060B,
- 0xD727BBB6, 0xD3E6A601, 0xDEA580D8, 0xDA649D6F, 0xC423CD6A, 0xC0E2D0DD,
- 0xCDA1F604, 0xC960EBB3, 0xBD3E8D7E, 0xB9FF90C9, 0xB4BCB610, 0xB07DABA7,
- 0xAE3AFBA2, 0xAAFBE615, 0xA7B8C0CC, 0xA379DD7B, 0x9B3660C6, 0x9FF77D71,
- 0x92B45BA8, 0x9675461F, 0x8832161A, 0x8CF30BAD, 0x81B02D74, 0x857130C3,
- 0x5D8A9099, 0x594B8D2E, 0x5408ABF7, 0x50C9B640, 0x4E8EE645, 0x4A4FFBF2,
- 0x470CDD2B, 0x43CDC09C, 0x7B827D21, 0x7F436096, 0x7200464F, 0x76C15BF8,
- 0x68860BFD, 0x6C47164A, 0x61043093, 0x65C52D24, 0x119B4BE9, 0x155A565E,
- 0x18197087, 0x1CD86D30, 0x029F3D35, 0x065E2082, 0x0B1D065B, 0x0FDC1BEC,
- 0x3793A651, 0x3352BBE6, 0x3E119D3F, 0x3AD08088, 0x2497D08D, 0x2056CD3A,
- 0x2D15EBE3, 0x29D4F654, 0xC5A92679, 0xC1683BCE, 0xCC2B1D17, 0xC8EA00A0,
- 0xD6AD50A5, 0xD26C4D12, 0xDF2F6BCB, 0xDBEE767C, 0xE3A1CBC1, 0xE760D676,
- 0xEA23F0AF, 0xEEE2ED18, 0xF0A5BD1D, 0xF464A0AA, 0xF9278673, 0xFDE69BC4,
- 0x89B8FD09, 0x8D79E0BE, 0x803AC667, 0x84FBDBD0, 0x9ABC8BD5, 0x9E7D9662,
- 0x933EB0BB, 0x97FFAD0C, 0xAFB010B1, 0xAB710D06, 0xA6322BDF, 0xA2F33668,
- 0xBCB4666D, 0xB8757BDA, 0xB5365D03, 0xB1F740B4
- };
- static UInt32 crc32_cal(byte[] pData, UInt16 Length)
- {
- UInt32 nReg;
- UInt32 nTemp = 0;
- UInt16 i, n;
- nReg = 0xFFFFFFFF;
- for (n = 0; n < Length; n++)
- {
- nReg ^= (UInt32)pData[n];
- for (i = 0; i < 4; i++)
- {
- nTemp = crc32_table[((nReg >> 24) & 0xFF)];
- nReg <<= 8;
- nReg ^= nTemp;
- }
- }
- return nReg;
- }
- #endregion
- #region 平均值计算
- private ushort GetAvg(byte[] pData, UInt16 Length)
- {
- UInt32 Sum = 0;
- for(ushort i=0; i<Length; i++)
- {
- Sum += pData[i];
- }
- return (ushort)(Sum/Length);
- }
- #endregion
- #region 标准差计算
- private ushort GetStDev(byte[] pData, UInt16 Length)
- {
- double Result = 0;
- //计算平均值
- UInt32 Avg = GetAvg(pData, Length);
- //计算各个数值与平均值的差值平方和
- UInt32 Sum = 0;
- for (ushort i = 0; i < Length; i++)
- {
- Sum += (pData[i] - Avg) * (pData[i] - Avg);
- }
- //计算标准差
- Result = Math.Sqrt((double)Sum / Length);
- return (ushort)(Result);
- }
- #endregion
- #region 发送指令函数
- private bool SendCmd(ushort ID, byte Mode, ushort Cmd, byte[] Data)
- {
- if (!serialPort1.IsOpen)
- {
- MessageBox.Show("串口未连接");
- return false;
- }
- ushort DataLength = (ushort)(Cmd & 0xFF);
- var SendCmdTmp = new byte[DataLength + 13];
- //帧头
- SendCmdTmp[0] = 0x55;
- SendCmdTmp[1] = 0xAA;
- //ID
- SendCmdTmp[2] = (byte)(ID >> 8);
- SendCmdTmp[3] = (byte)(ID & 0xFF);
- //MODE
- SendCmdTmp[4] = Mode;
- //长度
- SendCmdTmp[5] = (byte)((Cmd & 0xFF) + 2);
- //命令字
- SendCmdTmp[6] = (byte)(Cmd >> 8);
- SendCmdTmp[7] = (byte)(Cmd & 0xFF);
- //数据段
- for (UInt16 i = 0; i < (Cmd & 0xFF); i++)
- {
- SendCmdTmp[8 + i] = Data[i];
- }
- //校验
- UInt32 CRC_Result = crc32_cal(SendCmdTmp, (ushort)(DataLength + 8));
- SendCmdTmp[DataLength + 8] = Convert.ToByte(CRC_Result >> 24);
- SendCmdTmp[DataLength + 9] = Convert.ToByte((CRC_Result >> 16) % 256);
- SendCmdTmp[DataLength + 10] = Convert.ToByte((CRC_Result >> 8) % 256);
- SendCmdTmp[DataLength + 11] = Convert.ToByte(CRC_Result % 256);
- //帧尾
- SendCmdTmp[DataLength + 12] = 0xF0;
- //发送数据
- serialPort1.Write(SendCmdTmp, 0, SendCmdTmp.Length);
- return true;
- }
- #endregion
- #region 力矩传感器查询和写入相关
- //发送力矩传感器校正系数
- private void button_WriteK_Click(object sender, EventArgs e)
- {
- if (radioButton_SingleSensor.Checked)
- {
- var K = new byte[1];
- ushort DataTemp;
- DataTemp = (ushort)(decimal.Parse(textBox_Sensor1_K.Text) * 100);
- K[0] = Convert.ToByte(DataTemp);
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2A01, K);
- }
- else if (radioButton_ThreeSensor.Checked)
- {
- var K = new byte[2];
- ushort DataTemp;
- if ((checkBox_Sensor1.Checked == false) && (checkBox_Sensor2.Checked == false) && (checkBox_Sensor3.Checked == false))
- {
- timer1.Enabled = false;
- MessageBox.Show("请选择传感器", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- timer1.Enabled = true;
- return;
- }
- else
- {
- if (MessageBox.Show("确认写入", "确认写入?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- {
- if (checkBox_Sensor1.Checked == true)
- {
- if (textBox_Sensor1_K.Text != string.Empty)
- {
- K[0] = 1;
- DataTemp = (ushort)(decimal.Parse(textBox_Sensor1_K.Text) * 100);
- K[1] = Convert.ToByte(DataTemp);
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3402, K);
- Thread.Sleep(20);
- }
- }
- if (checkBox_Sensor2.Checked == true)
- {
- if (textBox_Sensor2_K.Text != string.Empty)
- {
- K[0] = 2;
- DataTemp = (ushort)(decimal.Parse(textBox_Sensor2_K.Text) * 100);
- K[1] = Convert.ToByte(DataTemp);
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3402, K);
- Thread.Sleep(20);
- }
- }
- if (checkBox_Sensor3.Checked == true)
- {
- if (textBox_Sensor3_K.Text != string.Empty)
- {
- K[0] = 3;
- DataTemp = (ushort)(decimal.Parse(textBox_Sensor3_K.Text) * 100);
- K[1] = Convert.ToByte(DataTemp);
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3402, K);
- Thread.Sleep(20);
- }
- }
- }
- }
- }
- }
- //查询传感器零点
- private void button_ReadZero_Click(object sender, EventArgs e)
- {
- if (radioButton_SingleSensor.Checked)
- {
- textBox_Sensor1_Zero.Text = "---";
- textBox_Sensor1_K.Text = "---";
- textBox_Torque_Start.Text = "---";
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2000, null); //单力矩零点值
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2900, null); //单力矩校正系数和启动值
- }
- else if (radioButton_ThreeSensor.Checked)
- {
- var K = new byte[1];
- textBox_Sensor1_Zero.Text = "---";
- textBox_Sensor2_Zero.Text = "---";
- textBox_Sensor3_Zero.Text = "---";
- textBox_Sensor1_K.Text = "---";
- textBox_Sensor2_K.Text = "---";
- textBox_Sensor3_K.Text = "---";
- //轮询零点
- for (ushort i = 1; i <= 3; i++)
- {
- K[0] = (byte)i;
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3201, K);
- }
- //轮询校正系数
- for (ushort i = 1; i <= 3; i++)
- {
- K[0] = (byte)i;
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3301, K);
- }
- }
-
- }
- //计算校正系数
- private void label119_DoubleClick(object sender, EventArgs e)
- {
- float K;
- string DataTemp;
- if (radioButton_SingleSensor.Checked)
- {
- if (checkBox_Sensor1.Checked == false)
- {
- timer1.Enabled = false;
- MessageBox.Show("请选择传感器", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- timer1.Enabled = true;
- return;
- }
- else
- {
- if ((textBox_TorqueData.Text != "---") && (textBox_TorqueData.Text != string.Empty))
- {
- DataTemp = textBox_TorqueData.Text;
- DataTemp = System.Text.RegularExpressions.Regex.Replace(DataTemp, @"[^0-9]+", "");
- K = float.Parse(DataTemp) / 75f;
- textBox_Sensor1_K.Text = K.ToString("#0.00");
- }
- else
- {
- textBox_Sensor1_K.Text = "";
- }
- }
- }
- else if (radioButton_ThreeSensor.Checked)
- {
- if ((checkBox_Sensor1.Checked == false) && (checkBox_Sensor2.Checked == false) && (checkBox_Sensor3.Checked == false))
- {
- timer1.Enabled = false;
- MessageBox.Show("请选择传感器", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- timer1.Enabled = true;
- return;
- }
- else
- {
- if (checkBox_Sensor1.Checked == true)
- {
- if ((textBox_Sensor1_Avg.Text != "---") && (textBox_Sensor1_Avg.Text != string.Empty))
- {
- DataTemp = textBox_Sensor1_Avg.Text;
- DataTemp = System.Text.RegularExpressions.Regex.Replace(DataTemp, @"[^0-9]+", "");
- K = float.Parse(DataTemp) / 75f;
- textBox_Sensor1_K.Text = K.ToString("#0.00");
- }
- else
- {
- textBox_Sensor1_K.Text = "";
- }
- }
- if (checkBox_Sensor2.Checked == true)
- {
- if ((textBox_Sensor2_Avg.Text != "---") && (textBox_Sensor2_Avg.Text != string.Empty))
- {
- DataTemp = textBox_Sensor2_Avg.Text;
- DataTemp = System.Text.RegularExpressions.Regex.Replace(DataTemp, @"[^0-9]+", "");
- K = float.Parse(DataTemp) / 75f;
- textBox_Sensor2_K.Text = K.ToString("#0.00");
- }
- else
- {
- textBox_Sensor2_K.Text = "";
- }
- }
- if (checkBox_Sensor3.Checked == true)
- {
- if ((textBox_Sensor3_Avg.Text != "---") && (textBox_Sensor3_Avg.Text != string.Empty))
- {
- DataTemp = textBox_Sensor3_Avg.Text;
- DataTemp = System.Text.RegularExpressions.Regex.Replace(DataTemp, @"[^0-9]+", "");
- K = float.Parse(DataTemp) / 75f;
- textBox_Sensor3_K.Text = K.ToString("#0.00");
- }
- else
- {
- textBox_Sensor3_K.Text = "";
- }
- }
- }
- }
-
- }
- //发送力矩传感器启动值
- private void button_Write_Torque_Start_Click(object sender, EventArgs e)
- {
- ushort StarData = Convert.ToUInt16(textBox_Torque_Start.Text);
- var StarData_Array = new byte[2];
- StarData_Array[0] = (byte)(StarData & 0xFF);
- StarData_Array[1] = (byte)(StarData >> 8);
- if (MessageBox.Show("确认写入", "确认写入?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- {
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2B02, StarData_Array);
- }
- }
- //查询力矩传感器启动值
- private void button_ReadStartData_Click(object sender, EventArgs e)
- {
- if (radioButton_SingleSensor.Checked)
- {
- textBox_Sensor1_Zero.Text = "---";
- textBox_Sensor1_K.Text = "---";
- textBox_Torque_Start.Text = "---";
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2900, null);
- }
- else if (radioButton_ThreeSensor.Checked)
- {
- textBox_Torque_Start.Text = "---";
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3500, null);
- }
- }
- //根据采集值计算启动值
- private void label9_DoubleClick(object sender, EventArgs e)
- {
- ushort StartData;
- string DataTemp;
- if ((textBox_TorqueData.Text == "---") || (textBox_TorqueData.Text == string.Empty))
- {
- return;
- }
- DataTemp = textBox_TorqueData.Text;
- DataTemp = System.Text.RegularExpressions.Regex.Replace(DataTemp, @"[^0-9]+", "");
- StartData = (ushort)(Convert.ToUInt16(DataTemp) * 28);
- textBox_Torque_Start.Text = StartData.ToString();
- }
- #endregion
- #region 工作模式设置
- //进入运行模式
- private void radioButton_WorkMode_Run_Click(object sender, EventArgs e)
- {
- var RunMode = new byte[1];
- RunMode[0] = 0x00;
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, RunMode);
- checkBox_ReadTorqueData.Checked = false;
- }
- //进入配置模式
- private void radioButton_WorkMode_Confg_Click(object sender, EventArgs e)
- {
- var RunMode = new byte[1];
- RunMode[0] = 0x01;
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, RunMode);
- checkBox_AutoSend.Checked = false;
- }
- #endregion
- #region 助力档位设置
- //设置OFF档位
- private void radioButton_Gear_OFF_Click(object sender, EventArgs e)
- {
- var CtrlCode = new byte[2];
- CtrlCode[0] = 0x00;
- if (radioButton_Light_OFF.Checked == true)
- {
- CtrlCode[1] = 0xF0;
- }
- else if (radioButton_Light_ON.Checked == true)
- {
- CtrlCode[1] = 0xF1;
- }
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
- }
- //设置ECO档位
- private void radioButton_Gear_ECO_Click(object sender, EventArgs e)
- {
- var CtrlCode = new byte[2];
- CtrlCode[0] = 0x01;
- if (radioButton_Light_OFF.Checked == true)
- {
- CtrlCode[1] = 0xF0;
- }
- else if (radioButton_Light_ON.Checked == true)
- {
- CtrlCode[1] = 0xF1;
- }
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
- }
- //设置NORM档位
- private void radioButton_Gear_NORM_Click(object sender, EventArgs e)
- {
- var CtrlCode = new byte[2];
- CtrlCode[0] = 0x02;
- if (radioButton_Light_OFF.Checked == true)
- {
- CtrlCode[1] = 0xF0;
- }
- else if (radioButton_Light_ON.Checked == true)
- {
- CtrlCode[1] = 0xF1;
- }
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
- }
- //设置SPORT档位
- private void radioButton_Gear_SPORT_Click(object sender, EventArgs e)
- {
- var CtrlCode = new byte[2];
- CtrlCode[0] = 0x03;
- if (radioButton_Light_OFF.Checked == true)
- {
- CtrlCode[1] = 0xF0;
- }
- else if (radioButton_Light_ON.Checked == true)
- {
- CtrlCode[1] = 0xF1;
- }
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
- }
- //设置TURBO档位
- private void radioButton_Gear_TURBO_Click(object sender, EventArgs e)
- {
- var CtrlCode = new byte[2];
- CtrlCode[0] = 0x04;
- if (radioButton_Light_OFF.Checked == true)
- {
- CtrlCode[1] = 0xF0;
- }
- else if (radioButton_Light_ON.Checked == true)
- {
- CtrlCode[1] = 0xF1;
- }
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
- }
- //设置SMART档位
- private void radioButton_Gear_SMART_Click(object sender, EventArgs e)
- {
- var CtrlCode = new byte[2];
- CtrlCode[0] = 0x33;
- if (radioButton_Light_OFF.Checked == true)
- {
- CtrlCode[1] = 0xF0;
- }
- else if (radioButton_Light_ON.Checked == true)
- {
- CtrlCode[1] = 0xF1;
- }
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
- }
- //设置WALK档位
- private void radioButton_Gear_WALK_Click(object sender, EventArgs e)
- {
- var CtrlCode = new byte[2];
- CtrlCode[0] = 0x22;
- if (radioButton_Light_OFF.Checked == true)
- {
- CtrlCode[1] = 0xF0;
- }
- else if (radioButton_Light_ON.Checked == true)
- {
- CtrlCode[1] = 0xF1;
- }
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
- }
- #endregion
- #region 大灯开关
- //大灯关
- private void radioButton_Light_OFF_Click(object sender, EventArgs e)
- {
- var CtrlCode = new byte[2];
- if (radioButton_Gear_OFF.Checked == true)
- {
- CtrlCode[0] = 0x00;
- }
- else if (radioButton_Gear_ECO.Checked == true)
- {
- CtrlCode[0] = 0x01;
- }
- else if (radioButton_Gear_NORM.Checked == true)
- {
- CtrlCode[0] = 0x02;
- }
- else if (radioButton_Gear_SPORT.Checked == true)
- {
- CtrlCode[0] = 0x03;
- }
- else if (radioButton_Gear_TURBO.Checked == true)
- {
- CtrlCode[0] = 0x04;
- }
- else if (radioButton_Gear_SMART.Checked == true)
- {
- CtrlCode[0] = 0x33;
- }
- else if (radioButton_Gear_WALK.Checked == true)
- {
- CtrlCode[0] = 0x22;
- }
- CtrlCode[1] = 0xF0;
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
- }
- //大灯开
- private void radioButton_Light_ON_Click(object sender, EventArgs e)
- {
- var CtrlCode = new byte[2];
- if (radioButton_Gear_OFF.Checked == true)
- {
- CtrlCode[0] = 0x00;
- }
- else if (radioButton_Gear_ECO.Checked == true)
- {
- CtrlCode[0] = 0x01;
- }
- else if (radioButton_Gear_NORM.Checked == true)
- {
- CtrlCode[0] = 0x02;
- }
- else if (radioButton_Gear_SPORT.Checked == true)
- {
- CtrlCode[0] = 0x03;
- }
- else if (radioButton_Gear_TURBO.Checked == true)
- {
- CtrlCode[0] = 0x04;
- }
- else if (radioButton_Gear_SMART.Checked == true)
- {
- CtrlCode[0] = 0x33;
- }
- else if (radioButton_Gear_WALK.Checked == true)
- {
- CtrlCode[0] = 0x22;
- }
- CtrlCode[1] = 0xF1;
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
- }
- #endregion
- //数据清除指令
- private void button_ClearData_Click(object sender, EventArgs e)
- {
- var Code = new byte[5];
- Code[0] = (byte)'C';
- Code[1] = (byte)'L';
- Code[2] = (byte)'E';
- Code[3] = (byte)'A';
- Code[4] = (byte)'R';
- timer1.Enabled = false;
- if (MessageBox.Show("系统清除后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- {
- if (MessageBox.Show("请再次确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- {
- if (MessageBox.Show("请最后确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- {
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2605, Code);
- }
- }
- }
- timer1.Enabled = true;
- }
- //查询版本信息
- private void button_Read_VerInfo_Click(object sender, EventArgs e)
- {
- textBox_Mode.Text = "---";
- textBox_SN.Text = "---";
- textBox_HW.Text = "---";
- textBox_FW.Text = "---";
- textBox_Firmware_Special.Text = "---";
- textBox_BuildTime.Text = "---";
- Class_Motor_Ver.Mode = "---";
- Class_Motor_Ver.SN = "---";
- Class_Motor_Ver.HW = "---";
- Class_Motor_Ver.FW = "---";
- Class_Motor_Ver.Special = "---";
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
- textBox_TE_FW.Text = "---";
- SendCmd((ushort)0x651, (byte)0x11, (ushort)0x1200, null);
- }
- //串口数据接收
- private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
- {
- try
- {
- Listening = true;
- int n = serialPort1.BytesToRead;
- byte[] buf = new byte[n];//将一次串口事件中接收到的数据暂存于buf中(注:对于超过8个字节的数据,C#的
- //串口接收事件将按每次处理8个字节的方式多次处理
- serialPort1.Read(buf, 0, n);//将接收到的数据读入buf
- this.Invoke((EventHandler)delegate
- {
- });
- buffer.AddRange(buf);//将读取的数据放入buffer中
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- return;
- }
- finally
- {
- Listening = false;
- }
- }
- //time1定时任务
- private void timer1_Tick(object sender, EventArgs e)
- {
- //MC_RunInfo超时清除
- if (MC_RunInfo_Refresh == false)
- {
- MC_RunInfo_Refresh_Cnt++;
- if (MC_RunInfo_Refresh_Cnt > 50)
- {
- //运行信息清除
- MC_RunInfo_Clear();
- //传感器数值清除
- textBox_TorqueData.Text = "---";
- textBox_Torque_Stdev.Text = "---";
- textBox_Sensor1_Avg.Text = "---";
- textBox_Sensor1_Std.Text = "---";
- textBox_Sensor2_Avg.Text = "---";
- textBox_Sensor2_Std.Text = "---";
- textBox_Sensor3_Avg.Text = "---";
- textBox_Sensor3_Std.Text = "---";
- MC_RunInfo_Refresh_Cnt = 0;
- TorqueArray_Index = 0;
- }
- }
- else
- {
- MC_RunInfo_Refresh_Cnt = 0;
- }
- MC_RunInfo_Refresh = false;
- //MC_DebugInfo超时清除
- if (MC_DebugInfo_Refresh == false)
- {
- MC_DebugInfo_Refresh_Cnt++;
- if (MC_DebugInfo_Refresh_Cnt > 10)//5s
- {
- MC_DebugInfo_Clear();
- MC_DebugInfo_Refresh_Cnt = 0;
- }
- }
- else
- {
- MC_DebugInfo_Refresh_Cnt = 0;
- }
- MC_DebugInfo_Refresh = false;
- //MC故障信息超时清除
- if (MC_ErrorCode_Refresh == false)
- {
- MC_ErrorCode_Refresh_Cnt++;
- if (MC_ErrorCode_Refresh_Cnt > 50)
- {
- MC_ErrorCode_Clear();
- MC_ErrorCode_Refresh_Cnt = 0;
- }
- }
- else
- {
- MC_ErrorCode_Refresh_Cnt = 0;
- }
- MC_ErrorCode_Refresh = false;
- //MC骑行信息超时清除
- if (MC_RideInfo_Refresh == false)
- {
- MC_RideInfo_Refresh_Cnt++;
- if (MC_RideInfo_Refresh_Cnt > 50)
- {
- MC_RideInfo_Clear();
- MC_RideInfo_Refresh_Cnt = 0;
- }
- }
- else
- {
- MC_RideInfo_Refresh_Cnt = 0;
- }
- MC_RideInfo_Refresh = false;
- //MC姿态信息超时清除
- if (MC_AttitudeAngle_Refresh == false)
- {
- MC_AttitudeAngle_Refresh_Cnt++;
- if (MC_AttitudeAngle_Refresh_Cnt > 50)
- {
- MC_AttitudeAngle_Clear();
- MC_AttitudeAngle_Refresh_Cnt = 0;
- }
- }
- else
- {
- MC_AttitudeAngle_Refresh_Cnt = 0;
- }
- MC_AttitudeAngle_Refresh = false;
- //定时发送助力档位
- if (checkBox_AutoSend.Checked == true)
- {
- var CtrlCode = new byte[2];
- AutoSendTimeCnt++;
- if (AutoSendTimeCnt >= 10)
- {
- AutoSendTimeCnt = 0;
- //助力档位
- if (radioButton_Gear_OFF.Checked == true)
- {
- CtrlCode[0] = 0x00;
- }
- else if (radioButton_Gear_ECO.Checked == true)
- {
- CtrlCode[0] = 0x01;
- }
- else if (radioButton_Gear_NORM.Checked == true)
- {
- CtrlCode[0] = 0x02;
- }
- else if (radioButton_Gear_SPORT.Checked == true)
- {
- CtrlCode[0] = 0x03;
- }
- else if (radioButton_Gear_TURBO.Checked == true)
- {
- CtrlCode[0] = 0x04;
- }
- else if (radioButton_Gear_SMART.Checked == true)
- {
- CtrlCode[0] = 0x33;
- }
- else if (radioButton_Gear_WALK.Checked == true)
- {
- CtrlCode[0] = 0x22;
- }
- //大灯状态
- if (radioButton_Light_OFF.Checked == true)
- {
- CtrlCode[1] = 0xF0;
- }
- else if (radioButton_Light_ON.Checked == true)
- {
- CtrlCode[1] = 0xF1;
- }
- if (serialPort1.IsOpen)
- {
- if (!SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode))
- checkBox_AutoSend.Checked = false;
- }
- }
- }
- else
- {
- AutoSendTimeCnt = 0;
- }
- //定时查询姿态信息
- if (checkBox_ReadAttitudeAngleAutoSend.Checked == true)
- {
- AutoSendReadAttitudeAngleTimeCnt++;
- if (AutoSendReadAttitudeAngleTimeCnt >= 20)
- {
- AutoSendReadAttitudeAngleTimeCnt = 0;
- if (serialPort1.IsOpen)
- {
- if (!SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2F00, null))
- checkBox_ReadAttitudeAngleAutoSend.Checked = false;
- }
- }
- }
- else
- {
- AutoSendReadAttitudeAngleTimeCnt = 0;
- }
- //定时查询骑行信息
- if ( checkBox_ReadRideInfo.Checked == true)
- {
- AutoSendReadRideInfoTimeCnt++;
- if (AutoSendReadRideInfoTimeCnt >= 20)
- {
- AutoSendReadRideInfoTimeCnt = 0;
- if (serialPort1.IsOpen)
- {
- if (!SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2E00, null))
- checkBox_ReadRideInfo.Checked = false;
- }
-
- }
- }
- else
- {
- AutoSendReadRideInfoTimeCnt = 0;
- }
- //CDL在线检测超时判断
- if (CDL_Online_Flag == false)
- {
- CDL_OnlineCheck_Cnt++;
- if (CDL_OnlineCheck_Cnt > 10)
- {
- timer1.Enabled = false;
- MessageBox.Show("连接失败!", "提示");
- timer1.Enabled = true;
- //关闭串口
- serialPort1.Close();
- comboBox_ComIndex.Enabled = true;
- comboBox_Baudrate.Enabled = true;
- button_ComOpen.Text = "连接(&C)";
- g_blnIsOpen = false;
- timer1.Enabled = false;
- button_RefreshPort.Enabled = true;
- }
- }
- //...
-
- //串口解析
- byte DataNum = 0; //记录每条命令数据段的长度
- while (buffer.Count >= 13)//当buffer中的数据的个数大于等于4个时,即每一帧的数据长度所在位(第4位)出现时
- {
- if (buffer[0] == 0x55 && buffer[1] == 0xAA)//判断帧头
- {
- ushort PackageID;
- PackageID = (ushort)(buffer[2] * 256 + buffer[3]);
- if ((PackageID == 0x0710) || (PackageID == 0x0715) || (PackageID == 0x0610) || (PackageID == 0x0615) || (PackageID == 0x07FF))
- {
- int CmdLen = buffer[5];
- if (buffer.Count < CmdLen + 11) break;//如果接收到的数据没有达到一帧数据的指定长度, 则执行下次循环
- buffer.RemoveRange(0, 6);//数据达到要求长度后删去帧头,ID,模式,和命令长度
- while (CmdLen > 0) //读取命令段
- {
- DataNum = (byte)(buffer[1]);//命令字的第二个字节的表示数据位的长度
- if (DataNum <= (CmdLen - 2))
- {
- for (int i = 0; i < DataNum + 2; i++)
- {
- binary_data_1[i] = buffer[i];
- }
- DataCmdProcess(PackageID);//对于不同的命令段,做不同的处理
- buffer.RemoveRange(0, DataNum + 2);//移除处理过的命令段与数据段
- CmdLen -= (DataNum + 2);
- if (CmdLen < 2)
- {
- buffer.RemoveRange(0, CmdLen + 2);
- return;
- }
- }
- else
- {
- buffer.RemoveRange(0, CmdLen + 11);
- }
- }
- }
- else
- {
- buffer.RemoveAt(0);//ID不对
- }
- }
- else
- {
- buffer.RemoveAt(0);//帧头不对,删除帧头高字节
- }
- }
- }
- //故障转换和显示
- List<string> ErrorInfo = new List<string>{ "过流保护", "低压保护", "过压保护", "堵转保护","过热保护","速度传感器故障","力矩传感器故障","霍尔故障",
- "马达缺相","NTC故障","","","","MCU故障","踏频传感器故障","指拨故障","MOS短路","电压异常","电路故障","TE故障","TE电路故障","","","","","","","","","",
- "",""};
- private string ErrorInfoDisplay(UInt32 Code)
- {
- string Result = "";
- if (Code == 0)
- {
- Result = "无故障";
- }
- else
- {
- for (int i = 0; i < 32; i++)
- {
- if ((Code & 0x01) == 0x01)
- {
- Result += ErrorInfo[i] + " ";
- }
- Code >>= 1;
- }
- }
- return Result;
- }
- #region 命令段处理函数
- private void DataCmdProcess(ushort ID)
- {
- ushort CmdTemp = (ushort)(binary_data_1[0] * 256 + binary_data_1[1]);
- ushort DataTemp;
- short DataTemp_int16;
- textBox_RevCnt.Text = Convert.ToString(Convert.ToInt32(textBox_RevCnt.Text) + 1);
- #region 解析发送给CDL的指令
- if (ID == 0x7FF)
- {
- switch (CmdTemp)
- {
- case 0x1100://TE APP版本信息
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- //CDL连接成功
- CDL_Online_Flag = true;
- timer1.Enabled = false;
- MessageBox.Show("连接成功!", "提示");
- timer1.Enabled = true;
- //显示其它界面
- foreach (Control c in this.Controls)
- {
- if (c is GroupBox)
- {
- if (c != groupBox1)
- {
- c.Visible = true;
- }
- }
- if (c is TabControl)
- c.Visible = true;
- }
- button_ReadAll.Visible = true;
- label1_Welcom.Visible = false;
- }));
- }
- break;
- }
- default: break;
- }
- }
- #endregion
- #region 解析主控的命令
- else if ((ID == 0x715) || (ID == 0x710))
- {
- switch (CmdTemp)
- {
- case 0xA318://力矩传感器零点值,单力矩
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- DataTemp = (ushort)(binary_data_1[25] * 256 + binary_data_1[24]);
- textBox_Sensor1_Zero.Text = Convert.ToString(DataTemp);
- }));
- }
- break;
- }
- case 0xAE1A://力矩传感器零点值,针对3力矩传感器使用
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- if (binary_data_1[2] == 1)
- {
- DataTemp = (ushort)(binary_data_1[26] * 256 + binary_data_1[25]);
- textBox_Sensor1_Zero.Text = Convert.ToString(DataTemp);
- }
- else if (binary_data_1[2] == 2)
- {
- DataTemp = (ushort)(binary_data_1[26] * 256 + binary_data_1[25]);
- textBox_Sensor2_Zero.Text = Convert.ToString(DataTemp);
- }
- else if (binary_data_1[2] == 3)
- {
- DataTemp = (ushort)(binary_data_1[26] * 256 + binary_data_1[25]);
- textBox_Sensor3_Zero.Text = Convert.ToString(DataTemp);
- }
- }));
- }
- break;
- }
- case 0xAA04://力矩传感器值校正参数和启动值,单力矩
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- //启动值
- DataTemp = (ushort)(binary_data_1[3] * 256 + binary_data_1[2]);
- textBox_Torque_Start.Text = Convert.ToString(DataTemp);
- //标定系数
- DataTemp = (ushort)(binary_data_1[4]);
- float K = (float)DataTemp / 100f;
- textBox_Sensor1_K.Text = K.ToString("#0.00");
- }));
- }
- break;
- }
- case 0xAF02://力矩传感器值校正参数,针对3力矩传感器使用
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- if (binary_data_1[2] == 1)
- {
- DataTemp = (ushort)(binary_data_1[3]);
- float K = (float)DataTemp / 100f;
- textBox_Sensor1_K.Text = K.ToString("#0.00");
- }
- else if (binary_data_1[2] == 2)
- {
- DataTemp = (ushort)(binary_data_1[3]);
- float K = (float)DataTemp / 100f;
- textBox_Sensor2_K.Text = K.ToString("#0.00");
- }
- else if (binary_data_1[2] == 3)
- {
- DataTemp = (ushort)(binary_data_1[3]);
- float K = (float)DataTemp / 100f;
- textBox_Sensor3_K.Text = K.ToString("#0.00");
- }
- }));
- }
- break;
- }
- case 0xB002://力矩传感器值启动值
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- DataTemp = (ushort)(binary_data_1[3] * 256 + binary_data_1[2]);
- textBox_Torque_Start.Text = Convert.ToString(DataTemp);
- }));
- }
- break;
- }
- case 0x1240://电机版本信息
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- //电机型号
- textBox_Mode.Text = "";
- for (ushort i = 0; i < 16; i++)
- {
- if (binary_data_1[2 + i] == 0x2E)
- {
- break;
- }
- textBox_Mode.Text += ((char)binary_data_1[2 + i]).ToString();
- }
- Class_Motor_Ver.Mode = textBox_Mode.Text;
-
- //电机SN
- textBox_SN.Text = "";
- for (ushort i = 0; i < 16; i++)
- {
- if (binary_data_1[18 + i] == 0x2E)
- {
- break;
- }
- textBox_SN.Text += ((char)binary_data_1[18 + i]).ToString();
- }
- Class_Motor_Ver.SN = textBox_SN.Text;
- //电机HW
- textBox_HW.Text = "";
- for (ushort i = 0; i < 10; i++)//前10位是PCB版号,以'.'结束
- {
- if (binary_data_1[34 + i] == 0x2E)
- {
- break;
- }
- if (binary_data_1[34 + i] == 'r')
- {
- binary_data_1[34 + i] = (byte)'.';
- }
- textBox_HW.Text += ((char)binary_data_1[34 + i]).ToString();
- }
- switch (binary_data_1[34 + 10])//第10位代表MCU品牌
- {
- case (byte)'1':
- textBox_HW.Text += " " + "ST" + " "; break;
- case (byte)'2':
- textBox_HW.Text += " " + "APM" + " "; break;
- case (byte)'3':
- textBox_HW.Text += " " + "HK" + " "; break;
- case (byte)'4':
- textBox_HW.Text += " " + "GD" + " "; break;
- default:
- textBox_HW.Text += " " + "NULL" + " "; break;
- }
- for (ushort i = 0; i < 4; i++)//后4位是MCU的ID校验码,用于识别板子的ID
- {
- textBox_HW.Text += Convert.ToString(binary_data_1[46 + i], 16).PadLeft(2, '0').ToUpper();//MCU ID 的CRC32值
- }
- Class_Motor_Ver.HW = textBox_HW.Text;
- //电机FW
- textBox_FW.Text = "";
- for (ushort i = 0; i < 16; i++)
- {
- if (binary_data_1[50 + i] == 0x2E)
- {
- break;
- }
- if (binary_data_1[50 + i] == 'r')
- {
- binary_data_1[50 + i] = (byte)'.';
- }
- textBox_FW.Text += ((char)binary_data_1[50 + i]).ToString();
- }
- Class_Motor_Ver.FW = textBox_FW.Text;
- }));
- }
- break;
- }
- case 0x1020://电机运行信息
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- MC_RunInfo_Refresh = true;
- //车速
- DataTemp = (ushort)(binary_data_1[3] * 256 + binary_data_1[2]);
- textBox_RunInfo_BikeSpeed.Text = ((float)DataTemp / 10f).ToString("0.0") + " km/h";
- //输出转速
- DataTemp = (ushort)(binary_data_1[5] * 256 + binary_data_1[4]);
- textBox_RunInfo_OutSpeed.Text = Convert.ToString(DataTemp) + " rpm";
- Class_MotorSpeed.String = textBox_RunInfo_OutSpeed.Text;
- Class_MotorSpeed.Data = DataTemp;
- //电功率
- DataTemp = (ushort)(binary_data_1[7] * 256 + binary_data_1[6]);
- textBox_RunInfo_Power.Text = Convert.ToString(DataTemp) + " W";
- //母线电压
- DataTemp = (ushort)(binary_data_1[9] * 256 + binary_data_1[8]);
- textBox_RunInfo_Voltage.Text = Convert.ToString(DataTemp) + " mV";
- //母线电流
- DataTemp = (ushort)(binary_data_1[11] * 256 + binary_data_1[10]);
- textBox_RunInfo_Current.Text = Convert.ToString(DataTemp) + " mA";
- Class_CurrentData.String = textBox_RunInfo_Current.Text;
- Class_CurrentData.Data = DataTemp;
- //踏频
- DataTemp = (ushort)(binary_data_1[12]);
- textBox_RunInfo_Cadence.Text = Convert.ToString(DataTemp) + " rpm";
- //踩踏力矩
- DataTemp = (ushort)(binary_data_1[13]);
- textBox_RunInfo_Torque.Text = Convert.ToString(DataTemp) + " N.m";
- Class_TorqueData.String = textBox_RunInfo_Torque.Text;
- Class_TorqueData.Data = DataTemp;
- //踩踏方向
- DataTemp = (ushort)(binary_data_1[14]);
- if (DataTemp == 0)
- {
- textBox_RunInfo_Dir.Text = "正向";
- }
- else if (DataTemp == 1)
- {
- textBox_RunInfo_Dir.Text = "反向";
- }
- else if (DataTemp == 2)
- {
- textBox_RunInfo_Dir.Text = "停止";
- }
- //助力档位
- DataTemp = (ushort)(binary_data_1[15]);
- if (DataTemp == 0x00)
- {
- textBox_RunInfo_Gear.Text = "OFF";
- radioButton_Gear_OFF.Checked = true;
- }
- else if (DataTemp == 0x01)
- {
- textBox_RunInfo_Gear.Text = "ECO";
- radioButton_Gear_ECO.Checked = true;
- }
- else if (DataTemp == 0x02)
- {
- textBox_RunInfo_Gear.Text = "NORM";
- radioButton_Gear_NORM.Checked = true;
- }
- else if (DataTemp == 0x03)
- {
- textBox_RunInfo_Gear.Text = "SPORT";
- radioButton_Gear_SPORT.Checked = true;
- }
- else if (DataTemp == 0x04)
- {
- textBox_RunInfo_Gear.Text = "TURBO";
- radioButton_Gear_TURBO.Checked = true;
- }
- else if (DataTemp == 0x33)
- {
- textBox_RunInfo_Gear.Text = "SMART";
- radioButton_Gear_SMART.Checked = true;
- }
- else if (DataTemp == 0x22)
- {
- textBox_RunInfo_Gear.Text = "WALK";
- radioButton_Gear_WALK.Checked = true;
- }
- //大灯状态
- DataTemp = (ushort)(binary_data_1[16]);
- if (DataTemp == 0xF0)
- {
- textBox_RunInfo_Light.Text = "OFF";
- radioButton_Light_OFF.Checked = true;
- }
- else if (DataTemp == 0xF1)
- {
- textBox_RunInfo_Light.Text = "ON";
- radioButton_Light_ON.Checked = true;
- }
- //剩余电量
- DataTemp = (ushort)(binary_data_1[17]);
- if (DataTemp == 0xFF)
- {
- textBox_RunInfo_SOC.Text = "---";
- }
- else
- {
- textBox_RunInfo_SOC.Text = Convert.ToString(DataTemp) + " %";
- }
- //剩余续航里程
- DataTemp = (ushort)(binary_data_1[19] * 256 + binary_data_1[18]);
- if ((DataTemp == 0xEEEE) || (DataTemp == 0xFFFF))
- {
- textBox_RunInfo_Range.Text = "---";
- }
- else
- {
- textBox_RunInfo_Range.Text = Convert.ToString(DataTemp) + " km";
- }
- //ODO里程
- DataTemp = (ushort)(binary_data_1[21] * 256 + binary_data_1[20]);
- textBox_RunInfo_ODO_Km.Text = Convert.ToString(DataTemp) + " km";
- //平均功耗
- DataTemp = (ushort)(binary_data_1[22]);
- textBox_RunInfo_PowerAvg.Text = Convert.ToString(DataTemp * 10) + " mAh/km";
- //PCB温度
- DataTemp = (ushort)(binary_data_1[23]);
- textBox_RunInfo_T_PCB.Text = Convert.ToString((int)(DataTemp - 40));
- //绕组温度
- DataTemp = (ushort)(binary_data_1[24]);
- textBox_RunInfo_T_Roil.Text = Convert.ToString((int)(DataTemp - 40));
- //MCU温度
- DataTemp = (ushort)(binary_data_1[25]);
- textBox_RunInfo_T_MCU.Text = Convert.ToString((int)(DataTemp - 40));
- //单次里程
- DataTemp = (ushort)(binary_data_1[27] * 256 + binary_data_1[26]);
- textBox_RunInfo_Distance.Text = ((float)DataTemp / 10f).ToString("0.0") + " km";
- //单次时间
- DataTemp = (ushort)(binary_data_1[29] * 256 + binary_data_1[28]);
- textBox_RunInfo_Time.Text = Convert.ToString(DataTemp) + " s";
- //传感器1
- DataTemp = (ushort)(binary_data_1[30]);
- textBox_Sensor1.Text = Convert.ToString(DataTemp) + " N.m";
- Class_Sensor1Data.String = textBox_Sensor1.Text;
- Class_Sensor1Data.Data = DataTemp;
- //传感器2
- DataTemp = (ushort)(binary_data_1[31]);
- textBox_Sensor2.Text = Convert.ToString(DataTemp) + " N.m";
- Class_Sensor2Data.String = textBox_Sensor2.Text;
- Class_Sensor2Data.Data = DataTemp;
- //传感器3
- DataTemp = (ushort)(binary_data_1[32]);
- textBox_Sensor3.Text = Convert.ToString(DataTemp) + " N.m";
- Class_Sensor3Data.String = textBox_Sensor3.Text;
- Class_Sensor3Data.Data = DataTemp;
- //变速器状态
- DataTemp = (ushort)(binary_data_1[33]);
- textBox_GearBox.Text = Convert.ToString(DataTemp, 16).PadLeft(2, '0').ToUpper();
- //计算传感器均值和标准差
- TorqueArray[TorqueArray_Index] = binary_data_1[13];
- Sensor1Array[TorqueArray_Index] = binary_data_1[30];
- Sensor2Array[TorqueArray_Index] = binary_data_1[31];
- Sensor3Array[TorqueArray_Index] = binary_data_1[32];
- TorqueArray_Index++;
- if (TorqueArray_Index >= 25)
- {
- textBox_TorqueData.Text = Convert.ToString(GetAvg(TorqueArray, 25)) + " N.m";
- textBox_Torque_Stdev.Text = Convert.ToString(GetStDev(TorqueArray, 25));
- if (radioButton_SingleSensor.Checked) //单力矩
- {
- textBox_Sensor1_Avg.Text = Convert.ToString(GetAvg(TorqueArray, 25)) + " N.m";
- textBox_Sensor1_Std.Text = Convert.ToString(GetStDev(TorqueArray, 25));
- }
- else if (radioButton_ThreeSensor.Checked) //三力矩
- {
- textBox_Sensor1_Avg.Text = Convert.ToString(GetAvg(Sensor1Array, 25)) + " N.m";
- textBox_Sensor1_Std.Text = Convert.ToString(GetStDev(Sensor1Array, 25));
- textBox_Sensor2_Avg.Text = Convert.ToString(GetAvg(Sensor2Array, 25)) + " N.m";
- textBox_Sensor2_Std.Text = Convert.ToString(GetStDev(Sensor2Array, 25));
- textBox_Sensor3_Avg.Text = Convert.ToString(GetAvg(Sensor3Array, 25)) + " N.m";
- textBox_Sensor3_Std.Text = Convert.ToString(GetStDev(Sensor3Array, 25));
- }
- TorqueArray_Index = 0;
- }
- //数据自动保存
- DateAutoSave();
- }));
- }
- break;
- }
- case 0xA020://用户参数
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- //指拨模式
- DataTemp = (ushort)(binary_data_1[2]);
- textBox_Config_GasMode.Text = Convert.ToString(DataTemp, 16).PadLeft(2, '0').ToUpper();
- //启动模式
- DataTemp = (ushort)(binary_data_1[3]);
- if (DataTemp == 0x01)
- comboBox_StartMode.Text = "柔和";
- else if (DataTemp == 0x02)
- comboBox_StartMode.Text = "正常";
- else if (DataTemp == 0x03)
- comboBox_StartMode.Text = "强劲";
- //停机时间
- DataTemp = (ushort)(binary_data_1[5] * 256 + binary_data_1[4]);
- textBox_Config_StopTime.Text = Convert.ToString(DataTemp);
- //限速值
- DataTemp = (ushort)(binary_data_1[6]);
- textBox_Config_MaxSpeed.Text = Convert.ToString(DataTemp);
- //下降速度
- DataTemp = (ushort)(binary_data_1[7]);
- if (DataTemp == 0x01)
- comboBox_DownDec.Text = "超慢";
- else if (DataTemp == 0x02)
- comboBox_DownDec.Text = "慢";
- else if (DataTemp == 0x03)
- comboBox_DownDec.Text = "正常";
- else if (DataTemp == 0x02)
- comboBox_DownDec.Text = "快";
- else if (DataTemp == 0x03)
- comboBox_DownDec.Text = "超快";
- //前飞
- DataTemp = (ushort)(binary_data_1[8]);
- textBox_Config_Teeth_F.Text = Convert.ToString(DataTemp);
- //后飞
- DataTemp = (ushort)(binary_data_1[9]);
- textBox_Config_Teeth_B.Text = Convert.ToString(DataTemp);
- //限流
- DataTemp = (ushort)(binary_data_1[10]);
- textBox_Config_MaxCurrent.Text = Convert.ToString(DataTemp);
- //温度预警
- DataTemp = (ushort)(binary_data_1[11]);
- textBox_Config_T_Alarm.Text = Convert.ToString((Int16)(DataTemp - 40));
- //温度保护
- DataTemp = (ushort)(binary_data_1[12]);
- textBox_Config_T_Protect.Text = Convert.ToString((Int16)(DataTemp - 40));
- //无PBU标志
- DataTemp = (ushort)(binary_data_1[13]);
- if (DataTemp == 0x55)
- {
- comboBox_NoWatch.Text = "有";
- }
- else if (DataTemp == 0xAA)
- {
- comboBox_NoWatch.Text = "无";
- }
- //轮胎周长
- DataTemp = (ushort)(binary_data_1[14]);
- textBox_Config_WheelSize.Text = Convert.ToString(DataTemp);
- //电机系列号
- DataTemp = (ushort)(binary_data_1[15]);
- comboBox_SerNum.Text = Convert.ToString(DataTemp);
- //ECO助力增益
- DataTemp = (ushort)(binary_data_1[16]);
- textBox_Config_ECO_K.Text = Convert.ToString(DataTemp);
- //ECO加速
- DataTemp = (ushort)(binary_data_1[17]);
- textBox_Config_ECO_Acc.Text = Convert.ToString(DataTemp);
- //NORM助力增益
- DataTemp = (ushort)(binary_data_1[18]);
- textBox_Config_NORM_K.Text = Convert.ToString(DataTemp);
- //NORM加速
- DataTemp = (ushort)(binary_data_1[19]);
- textBox_Config_NORM_Acc.Text = Convert.ToString(DataTemp);
- //SPORT助力增益
- DataTemp = (ushort)(binary_data_1[20]);
- textBox_Config_SPORT_K.Text = Convert.ToString(DataTemp);
- //SPORT加速
- DataTemp = (ushort)(binary_data_1[21]);
- textBox_Config_SPORT_Acc.Text = Convert.ToString(DataTemp);
- //TURBO助力增益
- DataTemp = (ushort)(binary_data_1[22]);
- textBox_Config_TURBO_K.Text = Convert.ToString(DataTemp);
- //TURBO加速
- DataTemp = (ushort)(binary_data_1[23]);
- textBox_Config_TURBO_Acc.Text = Convert.ToString(DataTemp);
- //SMART助力增益
- DataTemp = (ushort)(binary_data_1[24]);
- textBox_Config_SMART_K.Text = Convert.ToString(DataTemp);
- //SMART加速
- DataTemp = (ushort)(binary_data_1[25]);
- textBox_Config_SMART_Acc.Text = Convert.ToString(DataTemp);
- //速度传感器信号个数
- DataTemp = (ushort)(binary_data_1[26]);
- textBox_Config_SpeedNum.Text = Convert.ToString(DataTemp);
- //踏频启动信号个数
- DataTemp = (ushort)(binary_data_1[27]);
- textBox_Config_CadenceNum.Text = Convert.ToString(DataTemp);
- //无速度传感器
- DataTemp = (ushort)(binary_data_1[28]);
- if (DataTemp == 0x55)
- {
- comboBox_SpeedSing.Text = "传感器";
- }
- else if (DataTemp == 0xAA)
- {
- comboBox_SpeedSing.Text = "踏频";
- }
- else if (DataTemp == 0xEE)
- {
- comboBox_SpeedSing.Text = "通信";
- }
- //周长微调
- DataTemp_int16 = (short)binary_data_1[29];
- textBox_Config_WheelAdj.Text = Convert.ToString((DataTemp_int16 >= 128) ? (DataTemp_int16 - 256) : DataTemp_int16);
- //低压保护阈值
- DataTemp = (ushort)(binary_data_1[31] * 256 + binary_data_1[30]);
- textBox_Config_UV_Protect.Text = Convert.ToString(DataTemp);
- //推行限速值
- DataTemp = (ushort)(binary_data_1[32]);
- textBox_Config_WalkSpeedLimit.Text = ((float)DataTemp / 10f).ToString("#0.0");
- //推行马达转速
- DataTemp = (ushort)(binary_data_1[33]);
- textBox_Config_WalkMotorSpeed.Text = Convert.ToString(DataTemp);
- }));
- }
- break;
- }
- case 0xA110://马达参数
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- //额定功率
- DataTemp = (ushort)(binary_data_1[3] * 256 + binary_data_1[2]);
- textBox_Motor_Power.Text = Convert.ToString(DataTemp);
- //额定转速
- DataTemp = (ushort)(binary_data_1[5] * 256 + binary_data_1[4]);
- textBox_Motor_Speed.Text = Convert.ToString(DataTemp);
- //定子电阻
- DataTemp = (ushort)(binary_data_1[7] * 256 + binary_data_1[6]);
- textBox_Motor_R.Text = Convert.ToString(DataTemp);
- //Lq
- DataTemp = (ushort)(binary_data_1[9] * 256 + binary_data_1[8]);
- textBox_Motor_Lq.Text = Convert.ToString(DataTemp);
- //Ld
- DataTemp = (ushort)(binary_data_1[11] * 256 + binary_data_1[10]);
- textBox_Motor_Ld.Text = Convert.ToString(DataTemp);
- //反电动势
- DataTemp = (ushort)(binary_data_1[13] * 256 + binary_data_1[12]);
- textBox_Motor_E.Text = Convert.ToString(DataTemp);
- //额定电压
- DataTemp = (ushort)(binary_data_1[14]);
- textBox_Motor_Voltage.Text = Convert.ToString(DataTemp);
- //极对数
- DataTemp = (ushort)(binary_data_1[15]);
- textBox_Motor_Pole.Text = Convert.ToString(DataTemp);
- }));
- }
- break;
- }
- case 0xA230://历史信息1
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- UInt32 DataTemp_Uint32;
- ushort DataTemp_Uint16;
- short DataTemp_Int16;
- //开机次数
- DataTemp_Uint32 = (UInt32)((binary_data_1[5] << 24) + (binary_data_1[4] << 16) + (binary_data_1[3] << 8) + binary_data_1[2]);
- textBox_Runlog1_PowerCount.Text = Convert.ToString(DataTemp_Uint32);
- //使用时间
- DataTemp_Uint32 = (UInt32)((binary_data_1[9] << 24) + (binary_data_1[8] << 16) + (binary_data_1[7] << 8) + binary_data_1[6]);
- textBox_Runlog1_RunTime.Text = Convert.ToString(DataTemp_Uint32);
- //过流保护
- DataTemp_Uint16 = (ushort)(binary_data_1[11] * 256 + binary_data_1[10]);
- textBox_Runlog1_OC.Text = Convert.ToString(DataTemp_Uint16);
- //低压保护
- DataTemp_Uint16 = (ushort)(binary_data_1[13] * 256 + binary_data_1[12]);
- textBox_Runlog1_UV.Text = Convert.ToString(DataTemp_Uint16);
- //过压保护
- DataTemp_Uint16 = (ushort)(binary_data_1[15] * 256 + binary_data_1[14]);
- textBox_Runlog1_OV.Text = Convert.ToString(DataTemp_Uint16);
- //堵转保护
- DataTemp_Uint16 = (ushort)(binary_data_1[17] * 256 + binary_data_1[16]);
- textBox_Runlog1_Lock.Text = Convert.ToString(DataTemp_Uint16);
- //过热保护
- DataTemp_Uint16 = (ushort)(binary_data_1[19] * 256 + binary_data_1[18]);
- textBox_Runlog1_OT.Text = Convert.ToString(DataTemp_Uint16);
- //速度传感器故障
- DataTemp_Uint16 = (ushort)(binary_data_1[21] * 256 + binary_data_1[20]);
- textBox_Runlog1_SPSFault.Text = Convert.ToString(DataTemp_Uint16);
- //力矩传感器
- DataTemp_Uint16 = (ushort)(binary_data_1[23] * 256 + binary_data_1[22]);
- textBox_Runlog1_TQSFault.Text = Convert.ToString(DataTemp_Uint16);
- //霍尔故障
- DataTemp_Uint16 = (ushort)(binary_data_1[25] * 256 + binary_data_1[24]);
- textBox_Runlog1_HallFault.Text = Convert.ToString(DataTemp_Uint16);
- //马达缺相
- DataTemp_Uint16 = (ushort)(binary_data_1[27] * 256 + binary_data_1[26]);
- textBox_Runlog1_Phase.Text = Convert.ToString(DataTemp_Uint16);
- //NTC故障
- DataTemp_Uint16 = (ushort)(binary_data_1[29] * 256 + binary_data_1[28]);
- textBox_Runlog1_NTCFault.Text = Convert.ToString(DataTemp_Uint16);
- //BMS校验失败
- DataTemp_Uint16 = (ushort)(binary_data_1[31] * 256 + binary_data_1[30]);
- textBox_Runlog1_BMSFault.Text = Convert.ToString(DataTemp_Uint16);
- //HMI校验失败
- DataTemp_Uint16 = (ushort)(binary_data_1[33] * 256 + binary_data_1[32]);
- textBox_Runlog1_HMIFault.Text = Convert.ToString(DataTemp_Uint16);
- //PBU校验失败
- DataTemp_Uint16 = (ushort)(binary_data_1[35] * 256 + binary_data_1[34]);
- textBox_Runlog1_PBUFault.Text = Convert.ToString(DataTemp_Uint16);
- //PCB最高温
- DataTemp_Int16 = (short)(ushort)binary_data_1[36];
- textBox_Runlog1_PCB_MAX.Text = Convert.ToString(DataTemp_Int16 - 40);
- //PCB最低温
- DataTemp_Int16 = (short)(ushort)binary_data_1[37];
- textBox_Runlog1_PCB_MIN.Text = Convert.ToString(DataTemp_Int16 - 40);
- //ODO里程
- DataTemp_Uint32 = (UInt32)((binary_data_1[41] << 24) + (binary_data_1[40] << 16) + (binary_data_1[39] << 8) + binary_data_1[38]);
- textBox_ODO_Km.Text = ((float)DataTemp_Uint32 / 10f).ToString("0.0");
- //绕组最高温
- DataTemp_Int16 = (short)(ushort)binary_data_1[42];
- textBox_Runlog1_Coil_MAX.Text = Convert.ToString(DataTemp_Int16 - 40);
- //绕组最低温
- DataTemp_Int16 = (short)(ushort)binary_data_1[43];
- textBox_Runlog1_Coil_MIN.Text = Convert.ToString(DataTemp_Int16 - 40);
- //MCU最高温
- DataTemp_Int16 = (short)(ushort)binary_data_1[44];
- textBox_Runlog1_MCU_MAX.Text = Convert.ToString(DataTemp_Int16 - 40);
- //MCU最低温
- DataTemp_Int16 = (short)(ushort)binary_data_1[45];
- textBox_Runlog1_MCU_MIN.Text = Convert.ToString(DataTemp_Int16 - 40);
- //ODO时间
- DataTemp_Uint32 = (UInt32)((binary_data_1[49] << 24) + (binary_data_1[48] << 16) + (binary_data_1[47] << 8) + binary_data_1[46]);
- textBox_ODO_Time.Text = Convert.ToString(DataTemp_Uint32);
- }));
- }
- break;
- }
- case 0xAB09://存储器数据
- case 0xAB0A:
- case 0xAB0B:
- case 0xAB0C:
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- long ByteNum = 0;
- ByteNum = ((binary_data_1[6] << 24) + (binary_data_1[7] << 16) + (binary_data_1[8] << 8) + binary_data_1[9]) - ((binary_data_1[2] << 24) + (binary_data_1[3] << 16) + (binary_data_1[4] << 8) + binary_data_1[5]) + 1;
- if (ByteNum > 4) //数据长度超过4Bytes,按照16进制显示字节流
- {
- for (int i = 0; i < ByteNum; i++)
- {
- richTextBox_DebugInfo.AppendText(Convert.ToString(binary_data_1[10 + i], 16).PadLeft(2, '0').ToUpper() + " ");
- }
- }
- else //根据设置自动转换10进制显示
- {
- if (radioButton_DebugDec.Checked == true) //10进制显示
- {
- switch (ByteNum)
- {
- case 1:
- {
- richTextBox_DebugInfo.AppendText(Convert.ToString(binary_data_1[10]) + ", ");
- break;
- }
- case 2:
- {
- richTextBox_DebugInfo.AppendText(Convert.ToString(binary_data_1[10] + (binary_data_1[11] << 8)) + ", ");
- break;
- }
- case 3:
- {
- richTextBox_DebugInfo.AppendText(Convert.ToString(binary_data_1[10] + (binary_data_1[11] << 8) + (binary_data_1[12] << 16)) + ", ");
- break;
- }
- case 4:
- {
- richTextBox_DebugInfo.AppendText(Convert.ToString(binary_data_1[10] + (binary_data_1[11] << 8) + (binary_data_1[12] << 16) + (binary_data_1[13] << 24)) + ", ");
- break;
- }
- default:
- {
- richTextBox_DebugInfo.AppendText("无效数据" + ", ");
- break;
- }
- }
- }
- else if (radioButton_DebugHex.Checked == true)//16进制显示
- {
- switch (ByteNum)
- {
- case 1:
- {
- richTextBox_DebugInfo.AppendText("0x" + Convert.ToString(binary_data_1[10], 16).PadLeft(2, '0').ToUpper() + ", ");
- break;
- }
- case 2:
- {
- richTextBox_DebugInfo.AppendText("0x" + Convert.ToString(binary_data_1[10] + (binary_data_1[11] << 8), 16).PadLeft(4, '0').ToUpper() + ", ");
- break;
- }
- case 3:
- {
- richTextBox_DebugInfo.AppendText("0x" + Convert.ToString(binary_data_1[10] + (binary_data_1[11] << 8) + (binary_data_1[12] << 16), 16).PadLeft(6, '0').ToUpper() + ", ");
- break;
- }
- case 4:
- {
- richTextBox_DebugInfo.AppendText("0x" + Convert.ToString(binary_data_1[10] + (binary_data_1[11] << 8) + (binary_data_1[12] << 16) + (binary_data_1[13] << 24), 16).PadLeft(8, '0').ToUpper() + ", ");
- break;
- }
- default:
- {
- richTextBox_DebugInfo.AppendText("无效数据" + ", ");
- break;
- }
- }
- }
- }
- }));
- }
- break;
- }
- case 0xAB88://故障日志
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- //电机存储的故障日志,一包128字节包含2条故障日志
- var ErrorLogSaveInfo1 = new MC_ErrorLogSaveInfo_Struct_t();
- var ErrorLogSaveInfo2 = new MC_ErrorLogSaveInfo_Struct_t();
- ErrorLogSaveInfo1 = MyConverter.ByteToStructure<MC_ErrorLogSaveInfo_Struct_t>(binary_data_1, 10);
- ErrorLogSaveInfo2 = MyConverter.ByteToStructure<MC_ErrorLogSaveInfo_Struct_t>(binary_data_1, 74);
- //数据存储
- ErrorLogDateSave(ErrorLogSaveInfo1, ErrorLogSaveInfo2);
- }));
- }
- break;
- }
- case 0xAC10://运行历史2
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- ushort DataTemp_Uint16;
- //MCU故障
- DataTemp_Uint16 = (ushort)(binary_data_1[3] * 256 + binary_data_1[2]);
- textBox_Runlog2_MCUFault.Text = Convert.ToString(DataTemp_Uint16);
- //踏频传感器故障
- DataTemp_Uint16 = (ushort)(binary_data_1[5] * 256 + binary_data_1[4]);
- textBox_Runlog2_CDSFault.Text = Convert.ToString(DataTemp_Uint16);
- //指拨故障
- DataTemp_Uint16 = (ushort)(binary_data_1[7] * 256 + binary_data_1[6]);
- textBox_Runlog2_GasFault.Text = Convert.ToString(DataTemp_Uint16);
- //MOS短路
- DataTemp_Uint16 = (ushort)(binary_data_1[9] * 256 + binary_data_1[8]);
- textBox_Runlog2_MOS_SC.Text = Convert.ToString(DataTemp_Uint16);
- //电压异常
- DataTemp_Uint16 = (ushort)(binary_data_1[11] * 256 + binary_data_1[10]);
- textBox_Runlog2_VolFault.Text = Convert.ToString(DataTemp_Uint16);
- //电路异常
- DataTemp_Uint16 = (ushort)(binary_data_1[13] * 256 + binary_data_1[12]);
- textBox_Runlog2_CircuitFault.Text = Convert.ToString(DataTemp_Uint16);
- //TE MCU异常
- DataTemp_Uint16 = (ushort)(binary_data_1[15] * 256 + binary_data_1[14]);
- textBox_Runlog2_TE_MCUFault.Text = Convert.ToString(DataTemp_Uint16);
- //TE电路故障
- DataTemp_Uint16 = (ushort)(binary_data_1[17] * 256 + binary_data_1[16]);
- textBox_Runlog2_TE_CircuitFault.Text = Convert.ToString(DataTemp_Uint16);
- }));
- }
- break;
- }
- case 0x1104://故障码
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- MC_ErrorCode_Refresh = true;
- textBox_RunInfo_ErrorCode.Text =
- Convert.ToString(binary_data_1[5], 16).PadLeft(2, '0').ToUpper() +
- Convert.ToString(binary_data_1[4], 16).PadLeft(2, '0').ToUpper() +
- Convert.ToString(binary_data_1[3], 16).PadLeft(2, '0').ToUpper() +
- Convert.ToString(binary_data_1[2], 16).PadLeft(2, '0').ToUpper() +
- "H";
- }));
- }
- break;
- }
- case 0xA903://反馈指令
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- timer1.Enabled = false;
- MessageBox.Show("OK", "反馈指令", MessageBoxButtons.OK, MessageBoxIcon.Information);
- timer1.Enabled = true;
- }));
- }
- break;
- }
- case 0xAD20://用户参数2
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- short DataTemp_Int16;
- sbyte DataTemp_byte;
- //俯仰角零偏
- DataTemp_Int16 = (short)(binary_data_1[3] * 256 + binary_data_1[2]);
- textBox_Config_Angle_Pitch.Text = ((float)DataTemp_Int16 / 10f).ToString("0.0");
- //横滚角零偏
- DataTemp_Int16 = (short)(binary_data_1[5] * 256 + binary_data_1[4]);
- textBox_Config_Angle_Roll.Text = ((float)DataTemp_Int16 / 10f).ToString("0.0");
- //支持姿态传感器
- DataTemp = (ushort)(binary_data_1[6]);
- if (DataTemp == 0x55)
- {
- comboBox_AttiSensor.Text = "不支持";
- }
- else if (DataTemp == 0xAA)
- {
- comboBox_AttiSensor.Text = "支持";
- }
- //前灯模式
- DataTemp = (ushort)(binary_data_1[7] & 0xF0);
- if (DataTemp == 0x10)
- {
- comboBox_FrontLightMode.Text = "模式1";
- }
- else if (DataTemp == 0x20)
- {
- comboBox_FrontLightMode.Text = "模式2";
- }
- else
- {
- comboBox_FrontLightMode.Text = "0";
- }
- //尾灯模式
- DataTemp = (ushort)(binary_data_1[7] & 0x0F);
- if (DataTemp == 0x01)
- {
- comboBox_TailLightMode.Text = "模式1";
- }
- else if (DataTemp == 0x02)
- {
- comboBox_TailLightMode.Text = "模式2";
- }
- else if (DataTemp == 0x03)
- {
- comboBox_TailLightMode.Text = "模式3";
- }
- else if (DataTemp == 0x04)
- {
- comboBox_TailLightMode.Text = "模式4";
- }
- else if (DataTemp == 0x05)
- {
- comboBox_TailLightMode.Text = "模式5";
- }
- else if (DataTemp == 0x06)
- {
- comboBox_TailLightMode.Text = "模式6";
- }
- else
- {
- comboBox_TailLightMode.Text = "0";
- }
- //前灯电压
- DataTemp = (ushort)(binary_data_1[8]);
- if (DataTemp == 0x06)
- {
- comboBox_FrontLigthVol.Text = "6V";
- }
- else if (DataTemp == 0x0C)
- {
- comboBox_FrontLigthVol.Text = "12V";
- }
- else if (DataTemp == 0xFF)
- {
- comboBox_FrontLigthVol.Text = "随电池";
- }
- else if (DataTemp == 0x00)
- {
- comboBox_FrontLigthVol.Text = "硬件";
- }
- else
- {
- comboBox_FrontLigthVol.Text = "无效";
- }
- //尾灯电压
- DataTemp = (ushort)(binary_data_1[9]);
- if (DataTemp == 0x06)
- {
- comboBox_TailLightVol.Text = "6V";
- }
- else if (DataTemp == 0x0C)
- {
- comboBox_TailLightVol.Text = "12V";
- }
- else if (DataTemp == 0xFF)
- {
- comboBox_TailLightVol.Text = "随电池";
- }
- else if (DataTemp == 0x00)
- {
- comboBox_TailLightVol.Text = "硬件";
- }
- else
- {
- comboBox_TailLightVol.Text = "无效";
- }
- //超速报警阈值
- DataTemp = (ushort)(binary_data_1[11] * 256 + binary_data_1[10]);
- textBox_Config_SpeedAlarm.Text = ((float)DataTemp / 10f).ToString("#0.0");
- //尾灯脉宽
- DataTemp = (ushort)(binary_data_1[13] * 256 + binary_data_1[12]);
- textBox_Config_Pulse.Text = Convert.ToString(DataTemp);
- //尾灯周期
- DataTemp = (ushort)(binary_data_1[15] * 256 + binary_data_1[14]);
- textBox_Config_Period.Text = Convert.ToString(DataTemp);
- //起始限速
- DataTemp_byte = (sbyte)(binary_data_1[16]);
- textBox_Config_SpeedlimitStart.Text = ((float)(DataTemp_byte) / 10f).ToString("#0.0");
- //结束限速
- DataTemp_byte = (sbyte)(binary_data_1[17]);
- textBox_Config_SpeedlimitStop.Text = ((float)DataTemp_byte / 10f).ToString("#0.0");
- //限速微调
- DataTemp_int16 = (short)binary_data_1[18];
- textBox_Config_SpeedlimitAdj.Text = Convert.ToString((DataTemp_int16 >= 128) ? (DataTemp_int16 - 256) : DataTemp_int16);
- }));
- }
- break;
- }
- case 0x1510:
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- long DataTemp32;
- MC_RideInfo_Refresh = true;
- //ODO里程
- DataTemp32 = (long)((binary_data_1[5] << 24) + (binary_data_1[4] << 16) + (binary_data_1[3] << 8) + binary_data_1[2]);
- textBox_RideInfo_ODO_Km.Text = ((float)DataTemp32 / 10f).ToString("0.0") + " km";
- //ODO时间
- DataTemp32 = (long)((binary_data_1[9] << 24) + (binary_data_1[8] << 16) + (binary_data_1[7] << 8) + binary_data_1[6]);
- if (DataTemp32 >= 60 * 1000)
- textBox_RideInfo_ODO_Time.Text = Convert.ToString(DataTemp32 / 60) + " h";
- else
- textBox_RideInfo_ODO_Time.Text = Convert.ToString(DataTemp32 / 60) + " h " + Convert.ToString(DataTemp32 % 60) + " m";
- //TRIP里程
- DataTemp32 = (long)((binary_data_1[13] << 24) + (binary_data_1[12] << 16) + (binary_data_1[11] << 8) + binary_data_1[10]);
- textBox_RideInfo_Trip_Km.Text = ((float)DataTemp32 / 10f).ToString("0.0") + " km";
- //TRIP时间
- DataTemp32 = (long)((binary_data_1[17] << 24) + (binary_data_1[16] << 16) + (binary_data_1[15] << 8) + binary_data_1[14]);
- if (DataTemp32 >= 60 * 1000)
- textBox_RideInfo_Trip_Time.Text = Convert.ToString(DataTemp32 / 60) + " h";
- else
- textBox_RideInfo_Trip_Time.Text = Convert.ToString(DataTemp32 / 60) + " h " + Convert.ToString(DataTemp32 % 60) + " m";
-
- }));
- }
- break;
- }
- case 0x1608://姿态信息
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- short DataTemp_Int16;
- MC_AttitudeAngle_Refresh = true;
- //俯仰角度绝对
- DataTemp_Int16 = (short)(binary_data_1[3] * 256 + binary_data_1[2]);
- textBox_Attitude_PitchAngle_Abs.Text = ((float)DataTemp_Int16 / 10f).ToString("0.0");
- //横滚角度绝对
- DataTemp_Int16 = (short)(binary_data_1[5] * 256 + binary_data_1[4]);
- textBox_Attitude_RollAngle_Abs.Text = ((float)DataTemp_Int16 / 10f).ToString("0.0");
- //俯仰角度相对
- DataTemp_Int16 = (short)(binary_data_1[7] * 256 + binary_data_1[6]);
- textBox_Attitude_PitchAngle_Rel.Text = ((float)DataTemp_Int16 / 10f).ToString("0.0");
- //横滚角度相对
- DataTemp_Int16 = (short)(binary_data_1[9] * 256 + binary_data_1[8]);
- textBox_Attitude_RollAngle_Rel.Text = ((float)DataTemp_Int16 / 10f).ToString("0.0");
- }));
- }
- break;
- }
- case 0x1720://电机程序特性信息
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- //程序特性
- textBox_Firmware_Special.Text = "";
- for (ushort i = 0; i < 32; i++)
- {
- if (binary_data_1[2 + i] == 0x2E)
- {
- break;
- }
- textBox_Firmware_Special.Text += ((char)binary_data_1[2 + i]).ToString();
- }
- //编译时间
- textBox_BuildTime.Text = "";
- do
- {
- ushort[] DateTime = new ushort[6];
- DateTime[0] = (ushort)(binary_data_1[2 + 26] + 2000);
- DateTime[1] = (ushort)(binary_data_1[2 + 27]);
- DateTime[2] = (ushort)(binary_data_1[2 + 28]);
- DateTime[3] = (ushort)(binary_data_1[2 + 29]);
- DateTime[4] = (ushort)(binary_data_1[2 + 30]);
- DateTime[5] = (ushort)(binary_data_1[2 + 31]);
- //年
- textBox_BuildTime.Text += Convert.ToString(DateTime[0]).PadLeft(4, '0');
- //月
- textBox_BuildTime.Text += Convert.ToString(DateTime[1]).PadLeft(2, '0');
- //日
- textBox_BuildTime.Text += Convert.ToString(DateTime[2]).PadLeft(2, '0');
- textBox_BuildTime.Text += " ";
- //时
- textBox_BuildTime.Text += Convert.ToString(DateTime[3]).PadLeft(2, '0');
- textBox_BuildTime.Text += ":";
- //分
- textBox_BuildTime.Text += Convert.ToString(DateTime[4]).PadLeft(2, '0');
- textBox_BuildTime.Text += ":";
- //秒
- textBox_BuildTime.Text += Convert.ToString(DateTime[5]).PadLeft(2, '0');
- } while (false);
- }));
- }
- break;
- }
- case 0x1A10:
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- long DataTemp32;
- MC_RideInfo_Refresh = true;
- //平均车速
- DataTemp = (ushort)(binary_data_1[3] * 256 + binary_data_1[2]);
- textBox_RideInfo_SpeedAvg.Text = ((float)DataTemp / 10f).ToString("0.0") + " km/h";
- //最高车速
- DataTemp = (ushort)(binary_data_1[5] * 256 + binary_data_1[4]);
- textBox_RideInfo_SpeedMax.Text = ((float)DataTemp / 10f).ToString("0.0") + " km/h";
- //卡路里Trip
- DataTemp32 = (long)((binary_data_1[9] << 24) + (binary_data_1[8] << 16) + (binary_data_1[7] << 8) + binary_data_1[6]);
- textBox_RideInfo_CalTrip.Text = DataTemp32.ToString() + " kcal";
- //卡路里Once
- DataTemp = (ushort)(binary_data_1[11] * 256 + binary_data_1[10]);
- textBox_RideInfo_CalOnce.Text = DataTemp.ToString() + " kcal";
- }));
- }
- break;
- }
- case 0xBA20: //调试信息
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- MC_DebugInfo_Refresh = true;
- //系统状态机
- DataTemp_int16 = (short)(binary_data_1[2]);
- textBox_DebugInfo_SysFun.Text = Convert.ToString(DataTemp_int16);
- //二层状态机
- DataTemp_int16 = (short)(binary_data_1[3]);
- textBox_DebugInfo_SecondFun.Text = Convert.ToString(DataTemp_int16);
- //电机状态机
- DataTemp_int16 = (short)(binary_data_1[4]);
- textBox_DebugInfo_MotorFun.Text = Convert.ToString(DataTemp_int16);
- //助力状态机
- DataTemp_int16 = (short)(binary_data_1[5]);
- textBox_DebugInfo_AssistFun.Text = Convert.ToString(DataTemp_int16);
- //IdRef
- DataTemp_int16 = (short)(binary_data_1[7] * 256 + binary_data_1[6]);
- textBox_DebugInfo_IdRef.Text = Convert.ToString(DataTemp_int16);
- //IqRef
- DataTemp_int16 = (short)(binary_data_1[9] * 256 + binary_data_1[8]);
- textBox_DebugInfo_IqRef.Text = Convert.ToString(DataTemp_int16);
- //UdRef
- DataTemp_int16 = (short)(binary_data_1[11] * 256 + binary_data_1[10]);
- textBox_DebugInfo_UdRef.Text = Convert.ToString(DataTemp_int16);
- //UqRef
- DataTemp_int16 = (short)(binary_data_1[13] * 256 + binary_data_1[12]);
- textBox_DebugInfo_UqRef.Text = Convert.ToString(DataTemp_int16);
- //瞬时力矩
- DataTemp_int16 = (short)(binary_data_1[15] * 256 + binary_data_1[14]);
- textBox_DebugInfo_TorquePu.Text = Convert.ToString(DataTemp_int16);
- //时间滤波力矩
- DataTemp_int16 = (short)(binary_data_1[17] * 256 + binary_data_1[16]);
- textBox_DebugInfo_TorqueFil.Text = Convert.ToString(DataTemp_int16);
- //踏频滤波力矩
- DataTemp_int16 = (short)(binary_data_1[19] * 256 + binary_data_1[18]);
- textBox_DebugInfo_TorqueCadence.Text = Convert.ToString(DataTemp_int16);
- //IdFdb
- DataTemp_int16 = (short)(binary_data_1[21] * 256 + binary_data_1[20]);
- textBox_DebugInfo_IdFdb.Text = Convert.ToString(DataTemp_int16);
- //IqFdb
- DataTemp_int16 = (short)(binary_data_1[23] * 256 + binary_data_1[22]);
- textBox_DebugInfo_IqFdb.Text = Convert.ToString(DataTemp_int16);
- //助力输出
- DataTemp_int16 = (short)(binary_data_1[25] * 256 + binary_data_1[24]);
- textBox_DebugInfo_AssistOut.Text = Convert.ToString(DataTemp_int16);
- //数据保存
- DateAutoSave();
- }));
- }
- break;
- }
- default: break;
- }
- //存储器导出数据显示
- if ((CmdTemp & 0xFF00) == 0xAB00)
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- long ByteNum = 0;
- ByteNum = ((binary_data_1[6] << 24) + (binary_data_1[7] << 16) + (binary_data_1[8] << 8) + binary_data_1[9]) - ((binary_data_1[2] << 24) + (binary_data_1[3] << 16) + (binary_data_1[4] << 8) + binary_data_1[5]) + 1;
- if (ByteNum > 4) //数据长度超过4Bytes,按照16进制显示字节流
- {
- for (int i = 0; i < ByteNum; i++)
- {
- richTextBox_DebugInfo.AppendText(Convert.ToString(binary_data_1[10 + i], 16).PadLeft(2, '0').ToUpper() + " ");
- }
- }
- }));
- }
- }
- }
- #endregion
- #region 解析MC TE的命令
- else if ((ID == 0x615) || (ID == 0x610))
- {
- switch (CmdTemp)
- {
- case 0x1240://TE APP版本信息
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- //电机FW
- textBox_TE_FW.Text = "";
- for (ushort i = 0; i < 16; i++)
- {
- if (binary_data_1[50 + i] == 0x2E)
- {
- break;
- }
- if (binary_data_1[50 + i] == 'r')
- {
- binary_data_1[50 + i] = (byte)'.';
- }
- textBox_TE_FW.Text += ((char)binary_data_1[50 + i]).ToString();
- }
- }));
- }
- break;
- }
- default: break;
- }
- #endregion
- }
- }
- #endregion
- //用户参数显示清除
- private void MC_Config_Clear()
- {
- foreach (Control c in groupBox10.Controls)
- {
- if ((c is TextBox) || (c is ComboBox))
- {
- c.Text = "---";
- }
- }
- }
- //用户参数2清除
- private void MC_Config_Ext_Clear()
- {
- foreach (Control c in groupBox4.Controls)
- {
- if ((c is TextBox) || (c is ComboBox))
- {
- c.Text = "---";
- }
- }
- }
- //马达参数显示清除
- private void Motor_Config_Clear()
- {
- foreach (Control c in groupBox11.Controls)
- {
- if (c is TextBox)
- {
- c.Text = "---";
- }
- }
- }
- //电机运行信息清除
- private void MC_RunInfo_Clear()
- {
- foreach (Control c in tabPage1.Controls)
- {
- if (c is TextBox)
- {
- c.Text = "---";
- }
- }
- }
- //电机调试信息清除
- private void MC_DebugInfo_Clear()
- {
- foreach (Control c in tabPage2.Controls)
- {
- if ((c is TextBox))
- {
- if (c.Name != tabPage2.ToString())
- c.Text = "---";
- }
- }
- }
- //姿态传感器信息清除
- private void MC_AttitudeAngle_Clear()
- {
- foreach (Control c in groupBox13.Controls)
- {
- if (c is TextBox)
- {
- c.Text = "---";
- }
- }
- }
- //骑行信息清除
- private void MC_RideInfo_Clear()
- {
- foreach (Control c in groupBox12.Controls)
- {
- if (c is TextBox)
- {
- c.Text = "---";
- }
- }
- }
- //历史信息清除
- private void MC_RunLog_Clear()
- {
- foreach (Control c in groupBox14.Controls)
- {
- if (c is TextBox)
- {
- if ((c == textBox_LogAddrBegin) || (c == textBox_LogAddrEnd))
- continue;
- else
- c.Text = "---";
- }
- }
- }
- //电机故障码显示清除
- private void MC_ErrorCode_Clear()
- {
- //故障码
- textBox_RunInfo_ErrorCode.Text = "---";
- }
- //接收计数清零
- private void label58_Click(object sender, EventArgs e)
- {
- textBox_RevCnt.Text = "0";
- }
- //读取用户参数
- private void button_Read_Config_Click(object sender, EventArgs e)
- {
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1A00, null);
- MC_Config_Clear();
- }
- //写入用户参数
- private void button_Write_Config_Click(object sender, EventArgs e)
- {
- var ConfigParam = new byte[32];
- for (int i = 0; i < 32; i++)
- ConfigParam[i] = 0;
- ushort DataTemp;
- //指拨参数
- ConfigParam[0] = byte.Parse(textBox_Config_GasMode.Text, System.Globalization.NumberStyles.HexNumber);
- //启动模式
- if (comboBox_StartMode.Text == "柔和")
- ConfigParam[1] = 0x01;
- else if (comboBox_StartMode.Text == "强劲")
- ConfigParam[1] = 0x03;
- else
- ConfigParam[1] = 0x02;
- //停机时间
- DataTemp = Convert.ToUInt16(textBox_Config_StopTime.Text);
- ConfigParam[2] = (byte)(DataTemp & 0xFF);
- ConfigParam[3] = (byte)(DataTemp >> 8);
- //限速
- ConfigParam[4] = Convert.ToByte(textBox_Config_MaxSpeed.Text);
- //下降速度
- if (comboBox_DownDec.Text == "超慢")
- ConfigParam[5] = 0x01;
- else if (comboBox_DownDec.Text == "慢")
- ConfigParam[5] = 0x02;
- else if (comboBox_DownDec.Text == "快")
- ConfigParam[5] = 0x04;
- else if (comboBox_DownDec.Text == "超快")
- ConfigParam[5] = 0x05;
- else
- ConfigParam[5] = 0x03;
- //前飞
- ConfigParam[6] = Convert.ToByte(textBox_Config_Teeth_F.Text);
- //后飞
- ConfigParam[7] = Convert.ToByte(textBox_Config_Teeth_B.Text);
- //限流
- ConfigParam[8] = Convert.ToByte(textBox_Config_MaxCurrent.Text);
- //温度预警
- ConfigParam[9] = (byte)(Convert.ToByte(textBox_Config_T_Alarm.Text) + 40);
- //温度保护
- ConfigParam[10] = (byte)(Convert.ToByte(textBox_Config_T_Protect.Text) + 40);
- //无PBU
- if (comboBox_NoWatch.Text == "无")
- ConfigParam[11] = 0xAA;
- else
- ConfigParam[11] = 0x55;
- //轮胎周长
- ConfigParam[12] = Convert.ToByte(textBox_Config_WheelSize.Text);
- //电机系列号
- ConfigParam[13] = Convert.ToByte(comboBox_SerNum.Text);
- //ECO助力增益
- ConfigParam[14] = Convert.ToByte(textBox_Config_ECO_K.Text);
- //ECO加速
- ConfigParam[15] = Convert.ToByte(textBox_Config_ECO_Acc.Text);
- //NORM助力增益
- ConfigParam[16] = Convert.ToByte(textBox_Config_NORM_K.Text);
- //NORM加速
- ConfigParam[17] = Convert.ToByte(textBox_Config_NORM_Acc.Text);
- //SPORT助力增益
- ConfigParam[18] = Convert.ToByte(textBox_Config_SPORT_K.Text);
- //SPORT加速
- ConfigParam[19] = Convert.ToByte(textBox_Config_SPORT_Acc.Text);
- //TURBO助力增益
- ConfigParam[20] = Convert.ToByte(textBox_Config_TURBO_K.Text);
- //TURBO加速
- ConfigParam[21] = Convert.ToByte(textBox_Config_TURBO_Acc.Text);
- //SMART助力增益
- ConfigParam[22] = Convert.ToByte(textBox_Config_SMART_K.Text);
- //SMART加速
- ConfigParam[23] = Convert.ToByte(textBox_Config_SMART_Acc.Text);
- //速度传感器信号个数
- ConfigParam[24] = Convert.ToByte(textBox_Config_SpeedNum.Text);
- //踏频启动信号个数
- ConfigParam[25] = Convert.ToByte(textBox_Config_CadenceNum.Text);
- //速度传感器
- if (comboBox_SpeedSing.Text == "踏频")
- ConfigParam[26] = 0xAA;
- else if (comboBox_SpeedSing.Text == "通信")
- ConfigParam[26] = 0xEE;
- else
- ConfigParam[26] = 0x55;
- //周长微调
- ConfigParam[27] = (byte)Convert.ToInt16(textBox_Config_WheelAdj.Text);
- //低压保护阈值
- DataTemp = Convert.ToUInt16(textBox_Config_UV_Protect.Text);
- ConfigParam[28] = (byte)(DataTemp & 0xFF);
- ConfigParam[29] = (byte)(DataTemp >> 8);
- //推行限速
- DataTemp = (ushort)(decimal.Parse(textBox_Config_WalkSpeedLimit.Text) * 10);
- ConfigParam[30] = (byte)Convert.ToByte(DataTemp);
- //推行马达转速
- ConfigParam[31] = (byte)Convert.ToByte(textBox_Config_WalkMotorSpeed.Text);
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1B20, ConfigParam);
- }
- //读取马达参数
- private void button_Read_MortoConfig_Click(object sender, EventArgs e)
- {
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1C00, null);
- Motor_Config_Clear();
- }
- //写入马达参数
- private void button_Write_MotorConfig_Click(object sender, EventArgs e)
- {
- var ConfigParam = new byte[16];
- for (int i = 0; i < 16; i++)
- ConfigParam[i] = 0;
- ushort DataTemp;
- //额定功率
- DataTemp = Convert.ToUInt16(textBox_Motor_Power.Text);
- ConfigParam[0] = (byte)(DataTemp & 0xFF);
- ConfigParam[1] = (byte)(DataTemp >> 8);
- //额定转速
- DataTemp = Convert.ToUInt16(textBox_Motor_Speed.Text);
- ConfigParam[2] = (byte)(DataTemp & 0xFF);
- ConfigParam[3] = (byte)(DataTemp >> 8);
- //定子电阻
- DataTemp = Convert.ToUInt16(textBox_Motor_R.Text);
- ConfigParam[4] = (byte)(DataTemp & 0xFF);
- ConfigParam[5] = (byte)(DataTemp >> 8);
- //Lq
- DataTemp = Convert.ToUInt16(textBox_Motor_Lq.Text);
- ConfigParam[6] = (byte)(DataTemp & 0xFF);
- ConfigParam[7] = (byte)(DataTemp >> 8);
- //Ld
- DataTemp = Convert.ToUInt16(textBox_Motor_Ld.Text);
- ConfigParam[8] = (byte)(DataTemp & 0xFF);
- ConfigParam[9] = (byte)(DataTemp >> 8);
- //反电动势
- DataTemp = Convert.ToUInt16(textBox_Motor_E.Text);
- ConfigParam[10] = (byte)(DataTemp & 0xFF);
- ConfigParam[11] = (byte)(DataTemp >> 8);
- //额定电压
- ConfigParam[12] = Convert.ToByte(textBox_Motor_Voltage.Text);
- //极对数
- ConfigParam[13] = Convert.ToByte(textBox_Motor_Pole.Text);
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1D10, ConfigParam);
- }
- //写入电机型号
- private void button_Write_Mode_Click(object sender, EventArgs e)
- {
- string Mode = textBox_Mode.Text;
- var ModeArray = new byte[16];
- for (ushort i = 0; i < Mode.Length; i++)
- {
- ModeArray[i] = (byte)Mode[i];
- }
- if (Mode.Length < 16)
- {
- ModeArray[Mode.Length] = (byte)'.';
- for (ushort i = 0; i < 16 - Mode.Length - 1; i++)
- {
- ModeArray[Mode.Length + 1 + i] = 0x20;
- }
- }
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2210, ModeArray);
- }
- //写入电机SN
- private void button_Write_SN_Click(object sender, EventArgs e)
- {
- string SN = textBox_SN.Text;
- var SNArray = new byte[16];
- for (ushort i = 0; i < SN.Length; i++)
- {
- SNArray[i] = (byte)SN[i];
- }
- if (SN.Length < 16)
- {
- SNArray[SN.Length] = (byte)'.';
- for (ushort i = 0; i < 16 - SN.Length - 1; i++)
- {
- SNArray[SN.Length + 1 + i] = 0x20;
- }
- }
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2310, SNArray);
- }
- //系统复位
- private void button_Reset_Click(object sender, EventArgs e)
- {
- var Code = new byte[5];
- Code[0] = (byte)'R';
- Code[1] = (byte)'E';
- Code[2] = (byte)'S';
- Code[3] = (byte)'E';
- Code[4] = (byte)'T';
- timer1.Enabled = false;
- if (MessageBox.Show("系统将重启", "确认重启?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- {
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2505, Code);
- }
- timer1.Enabled = true;
- }
- private void button_RefreshPort_Click(object sender, EventArgs e)
- {
- string[] ports = SerialPort.GetPortNames();
- Array.Sort(ports);
- comboBox_ComIndex.Items.Clear();
- comboBox_ComIndex.Items.AddRange(ports);
- comboBox_ComIndex.SelectedIndex = comboBox_ComIndex.Items.Count > 0 ? 0 : -1;
- }
- private void checkBox_AutoSend_CheckedChanged(object sender, EventArgs e)
- {
- if (checkBox_AutoSend.Checked == false)
- {
- radioButton_Gear_ECO.Enabled = true;
- radioButton_Gear_NORM.Enabled = true;
- radioButton_Gear_SPORT.Enabled = true;
- radioButton_Gear_TURBO.Enabled = true;
- radioButton_Gear_WALK.Enabled = true;
- radioButton_Gear_SMART.Enabled = true;
- radioButton_Gear_OFF.Enabled = true;
- }
- else
- {
- radioButton_Gear_ECO.Enabled = false;
- radioButton_Gear_NORM.Enabled = false;
- radioButton_Gear_SPORT.Enabled = false;
- radioButton_Gear_TURBO.Enabled = false;
- radioButton_Gear_WALK.Enabled = false;
- radioButton_Gear_SMART.Enabled = false;
- radioButton_Gear_OFF.Enabled = false;
- }
- }
- private void button_Read_Config_Ext_Click(object sender, EventArgs e)
- {
- MC_Config_Ext_Clear();
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3000, null);
- }
- private void button_Write_Config_Ext_Click(object sender, EventArgs e)
- {
- var ConfigParam = new byte[32];
- for (int i = 0; i < 32; i++)
- ConfigParam[i] = 0;
- short DataTemp_Int16;
- ushort DataTemp;
- //俯仰角零偏
- DataTemp_Int16 = (short)(decimal.Parse(textBox_Config_Angle_Pitch.Text) * 10);
- ConfigParam[0] = (byte)(DataTemp_Int16 & 0xFF);
- ConfigParam[1] = (byte)(DataTemp_Int16 >> 8);
- //横滚角零偏
- DataTemp_Int16 = (short)(decimal.Parse(textBox_Config_Angle_Roll.Text) * 10);
- ConfigParam[2] = (byte)(DataTemp_Int16 & 0xFF);
- ConfigParam[3] = (byte)(DataTemp_Int16 >> 8);
- //支持姿态传感器
- if (comboBox_AttiSensor.Text == "支持")
- ConfigParam[4] = 0xAA;
- else
- ConfigParam[4] = 0x55;
- //前灯模式
- if (comboBox_FrontLightMode.Text == "模式1")
- {
- ConfigParam[5] = 0x10;
- }
- else if (comboBox_FrontLightMode.Text == "模式2")
- {
- ConfigParam[5] = 0x20;
- }
- else
- {
- ConfigParam[5] = 0x20;
- }
- //尾灯模式
- if (comboBox_TailLightMode.Text == "模式1")
- {
- ConfigParam[5] |= 0x01;
- }
- else if (comboBox_TailLightMode.Text == "模式2")
- {
- ConfigParam[5] |= 0x02;
- }
- else if (comboBox_TailLightMode.Text == "模式3")
- {
- ConfigParam[5] |= 0x03;
- }
- else if (comboBox_TailLightMode.Text == "模式4")
- {
- ConfigParam[5] |= 0x04;
- }
- else if (comboBox_TailLightMode.Text == "模式5")
- {
- ConfigParam[5] |= 0x05;
- }
- else if (comboBox_TailLightMode.Text == "模式6")
- {
- ConfigParam[5] |= 0x06;
- }
- else
- ConfigParam[5] |= 0x02;
- //前灯电压
- if (comboBox_FrontLigthVol.Text == "6V")
- ConfigParam[6] = 0x06;
- else if (comboBox_FrontLigthVol.Text == "12V")
- ConfigParam[6] = 0x0C;
- else if (comboBox_FrontLigthVol.Text == "随电池")
- ConfigParam[6] = 0xFF;
- else
- ConfigParam[6] = 0x00;
- //尾灯电压
- if (comboBox_TailLightVol.Text == "6V")
- ConfigParam[7] = 0x06;
- else if (comboBox_TailLightVol.Text == "12V")
- ConfigParam[7] = 0x0C;
- else if (comboBox_TailLightVol.Text == "随电池")
- ConfigParam[7] = 0xFF;
- else
- ConfigParam[7] = 0x00;
- //超速报警阈值
- DataTemp = (ushort)(decimal.Parse(textBox_Config_SpeedAlarm.Text) * 10);
- ConfigParam[8] = (byte)(DataTemp & 0xFF);
- ConfigParam[9] = (byte)(DataTemp >> 8);
- //尾灯脉宽
- DataTemp = Convert.ToUInt16(textBox_Config_Pulse.Text);
- ConfigParam[10] = (byte)(DataTemp & 0xFF);
- ConfigParam[11] = (byte)(DataTemp >> 8);
- //尾灯周期
- DataTemp = Convert.ToUInt16(textBox_Config_Period.Text);
- ConfigParam[12] = (byte)(DataTemp & 0xFF);
- ConfigParam[13] = (byte)(DataTemp >> 8);
- //起始限速
- DataTemp_Int16 = (short)(decimal.Parse(textBox_Config_SpeedlimitStart.Text) * 10);
- ConfigParam[14] = (byte)(DataTemp_Int16 & 0xFF);
- //结束限速
- DataTemp_Int16 = (short)(decimal.Parse(textBox_Config_SpeedlimitStop.Text) * 10);
- ConfigParam[15] = (byte)(DataTemp_Int16 & 0xFF);
- //限速微调
- ConfigParam[16] = (byte)Convert.ToInt16(textBox_Config_SpeedlimitAdj.Text);
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3120, ConfigParam);
- }
- private void button_Read_AttitudeAngle_Click(object sender, EventArgs e)
- {
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2F00, null);
- MC_AttitudeAngle_Clear();
- }
- private void button_Read_RidedInfo_Click(object sender, EventArgs e)
- {
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2E00, null);
- MC_RideInfo_Clear();
- }
- private void button_ReadRunLog_Click(object sender, EventArgs e)
- {
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1E00, null);
- MC_RunLog_Clear();
- }
- private void timer2_Tick(object sender, EventArgs e)
- {
- //定时显示系统时钟
- label_DateTime.Text = DateTime.Now.ToString();
- //定时读取存储器
- if (checkBox_DebugAuto.Checked == true)
- {
- var Address = new byte[4];
- UInt32 Address_Begin;
- UInt32 Addres_End;
- if (!serialPort1.IsOpen)
- {
- checkBox_DebugAuto.Checked = false;
- return;
- }
-
- if (checkBox_DebugReset.Checked == true)
- richTextBox_DebugInfo.Clear();
- do
- {
- if ((textBox_DebugAddrBegin.Text == string.Empty) || (textBox_DebugAddrEnd.Text == string.Empty))
- {
- checkBox_DebugAuto.Checked = false;
- MessageBox.Show("请输入起始和结束地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- continue;
- }
- else if ((textBox_DebugAddrBegin.Text.Length != 8) || (textBox_DebugAddrEnd.Text.Length != 8))
- {
- checkBox_DebugAuto.Checked = false;
- MessageBox.Show("请检查地址长度!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- continue;
- }
- else
- {
- Address = HexStringToBytes(textBox_DebugAddrBegin.Text, false);
- Address_Begin = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
- Address = HexStringToBytes(textBox_DebugAddrEnd.Text, false);
- Addres_End = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
- if (Addres_End < Address_Begin)
- {
- checkBox_DebugAuto.Checked = false;
- MessageBox.Show("结束地址必须大于或等于起始地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- continue;
- }
- else
- {
- var SendByte = new byte[8];
- SendByte[0] = (byte)(Address_Begin >> 24);
- SendByte[1] = (byte)(Address_Begin >> 16);
- SendByte[2] = (byte)(Address_Begin >> 8);
- SendByte[3] = (byte)(Address_Begin);
- SendByte[4] = (byte)(Addres_End >> 24);
- SendByte[5] = (byte)(Addres_End >> 16);
- SendByte[6] = (byte)(Addres_End >> 8);
- SendByte[7] = (byte)(Addres_End);
- if (!SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2D08, SendByte))
- checkBox_DebugAuto.Checked = false;
- }
- }
- } while (false);
- }
- }
- private void checkBox_ReadTorqueData_CheckedChanged(object sender, EventArgs e)
- {
- if (checkBox_ReadTorqueData.Checked == true)
- {
- #if true
- foreach (Control c in tabPage1.Controls)
- {
- if (c is TextBox)
- {
- if ((c != textBox_RunInfo_Range) && (c != textBox_RunInfo_ErrorCode))
- {
- if ((c.Text == string.Empty) || (c.Text == "---"))
- {
- timer1.Enabled = false;
- MessageBox.Show("请确认进入配置模式!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- timer1.Enabled = true;
- checkBox_Savedata.Checked = false;
- checkBox_ReadTorqueData.Checked = false;
- return;
- }
- else
- {
- LoginForm.Show();
- }
- }
- }
- }
- #else
- if (radioButton_WorkMode_Confg.Checked == false)
- {
- timer1.Enabled = false;
- MessageBox.Show("请确认进入配置模式", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- timer1.Enabled = true;
- checkBox_ReadTorqueData.Checked = false;
- }
- else
- {
- LoginForm.Show();
- }
- #endif
- }
- else
- {
- LoginForm.Hide();
- }
- }
- private void checkBox_Savedata_CheckedChanged(object sender, EventArgs e)
- {
- if (checkBox_Savedata.Checked == true)
- {
- //判断是否显示运行信息
- foreach (Control c in tabPage1.Controls)
- {
- if (c is TextBox)
- {
- if ((c != textBox_RunInfo_Range) && (c != textBox_RunInfo_ErrorCode))
- {
- if ((c.Text == string.Empty) || (c.Text == "---"))
- {
- timer1.Enabled = false;
- MessageBox.Show("请设置电机进入配置模式!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- timer1.Enabled = true;
- checkBox_Savedata.Checked = false;
- return;
- }
- }
- }
- }
- //判断是否查询版本信息
- foreach (Control c in groupBox9.Controls)
- {
- if (c is TextBox)
- {
- if ((c.Text == string.Empty) || (c.Text == "---"))
- {
- if ((c == textBox_Mode) || (c == textBox_SN))
- {
- timer1.Enabled = false;
- MessageBox.Show("请查询版本信息!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- timer1.Enabled = true;
- checkBox_Savedata.Checked = false;
- return;
- }
- }
- }
- }
- AutoSaveFileName = textBox_Mode.Text + "_" + textBox_SN.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_实时数据" + ".txt"; ;
- timer1.Enabled = false;
- MessageBox.Show("开始记录数据!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- timer1.Enabled = true;
- }
- else
- {
- AutoSaveFileName = "";
- timer1.Enabled = false;
- MessageBox.Show("停止记录数据!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- timer1.Enabled = true;
- }
- }
- private void DateAutoSave()
- {
- if (checkBox_Savedata.Checked == true)
- {
- if (AutoSaveFileName == string.Empty)
- {
- return;
- }
- string SaveData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace(" ", "_")+" ";
- foreach (Control c in tabPage1.Controls)
- {
- if (c is TextBox)
- {
- SaveData += c.Text + " , ";
- }
- }
- foreach (Control c in tabPage2.Controls)
- {
- if (c is TextBox)
- {
- SaveData += c.Text + " , ";
- }
- }
- SaveData += "\r\n";
- System.IO.File.AppendAllText(AutoSaveFileName, SaveData);//sb.ToString());
- }
-
- }
- private void button_DataExport_Click(object sender, EventArgs e)
- {
- //增加控件查空处理,存在没有数据时提示
- //判断是否查询力矩传感器
- foreach (Control c in groupBox2.Controls)
- {
- if (c is TextBox)
- {
- if ((c != textBox_TorqueData) && (c != textBox_Torque_Stdev))
- {
- if ((c.Text == string.Empty) || (c.Text == "---"))
- {
- timer1.Enabled = false;
- MessageBox.Show("请查询力矩传感器!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- timer1.Enabled = true;
- return;
- }
- }
-
- }
- }
- //判断是否显示运行信息
- foreach (Control c in tabPage1.Controls)
- {
- if (c is TextBox)
- {
- if ((c != textBox_RunInfo_Range) && (c != textBox_RunInfo_ErrorCode))
- {
- if ((c.Text == string.Empty) || (c.Text == "---"))
- {
- timer1.Enabled = false;
- MessageBox.Show("请设置电机进入配置模式!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- timer1.Enabled = true;
- return;
- }
- }
-
- }
- }
- //判断是否查询用户参数1
- foreach (Control c in groupBox10.Controls)
- {
- if (c is TextBox)
- {
- if (c.Text == string.Empty)
- {
- timer1.Enabled = false;
- MessageBox.Show("请查询用户参数1!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- timer1.Enabled = true;
- return;
- }
- }
- }
- //判断是否查询用户参数2
- foreach (Control c in groupBox4.Controls)
- {
- if (c is TextBox)
- {
- if (c.Text == string.Empty)
- {
- timer1.Enabled = false;
- MessageBox.Show("请查询用户参数2!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- timer1.Enabled = true;
- return;
- }
- }
- }
- #if false
- //判断是否显示骑行信息
- foreach (Control c in groupBox12.Controls)
- {
- if (c is TextBox)
- {
- if ((c.Text == string.Empty) || (c.Text == "---"))
- {
- timer1.Enabled = false;
- MessageBox.Show("请自动查询骑行信息!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- timer1.Enabled = true;
- return;
- }
- }
- }
- #endif
- #if false
- //判断是否查询姿态信息
- foreach (Control c in groupBox13.Controls)
- {
- if (c is TextBox)
- {
- if ((c.Text == string.Empty) || (c.Text == "---"))
- {
- timer1.Enabled = false;
- MessageBox.Show("请自动查询姿态信息!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- timer1.Enabled = true;
- return;
- }
- }
- }
- #endif
- //判断是否查询马达参数信息
- foreach (Control c in groupBox11.Controls)
- {
- if (c is TextBox)
- {
- if ((c.Text == string.Empty) || (c.Text == "---"))
- {
- timer1.Enabled = false;
- MessageBox.Show("请查询马达参数!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- timer1.Enabled = true ;
- return;
- }
- }
- }
- //判断是否查询版本信息
- foreach (Control c in groupBox9.Controls)
- {
- if (c is TextBox)
- {
- if ((c.Text == string.Empty) || (c.Text == "---"))
- {
- if (c == textBox_TE_FW)//忽略TE版本和软件标识
- {
- continue;
- }
- else
- {
- timer1.Enabled = false;
- MessageBox.Show("请查询版本信息!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- timer1.Enabled = true;
- return;
- }
-
- }
- }
- }
- //判断是否查询历史信息
- foreach (Control c in groupBox14.Controls)
- {
- if (c is TextBox)
- {
- if ((c.Text == string.Empty) || (c.Text == "---"))
- {
- timer1.Enabled = false;
- MessageBox.Show("请查询历史信息!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- timer1.Enabled = true;
- return;
- }
- }
- }
- //...
-
- Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap
- Graphics g = Graphics.FromImage(bit);
- g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片
- SaveFileDialog sf = new SaveFileDialog();
- sf.Title = "数据保存";
- sf.FileName = textBox_Mode.Text + "_" + textBox_SN.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_页面存储" + ".png";
- sf.Filter = "图片|*.png";
- if (sf.ShowDialog() == DialogResult.OK)
- {
- bit.Save(sf.FileName);//默认保存格式为PNG,保存成jpg格式质量不是很好
- }
- }
- private void Form1_FormClosed(object sender, FormClosedEventArgs e)
- {
-
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- timer1.Enabled = false;
- DialogResult result = MessageBox.Show("确定关闭吗!", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
- if (result == DialogResult.OK)
- {
- if (serialPort1.IsOpen == true)
- {
- //发送指令退出配置模式
- var RunMode = new byte[1];
- RunMode[0] = 0x00;
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, RunMode);
- serialPort1.Close();
- myCapture.Dispose();
- }
-
- e.Cancel = false; //点击OK
- }
- else
- {
- e.Cancel = true;
- timer1.Enabled = true;
- }
-
- }
- private void trackBar_MotorSpeedSet_Scroll(object sender, EventArgs e)
- {
- var Data = new byte[1];
- label_MotorSpeedSet.Text = Convert.ToString(trackBar_MotorSpeedSet.Value) + "%";
- Data[0] = (byte)trackBar_MotorSpeedSet.Value;
-
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2C01, Data);
- }
- private void button_Recovery_Click(object sender, EventArgs e)
- {
- var Code = new byte[8];
- Code[0] = (byte)'R';
- Code[1] = (byte)'E';
- Code[2] = (byte)'C';
- Code[3] = (byte)'O';
- Code[4] = (byte)'V';
- Code[5] = (byte)'E';
- Code[6] = (byte)'R';
- Code[7] = (byte)'Y';
- timer1.Enabled = false;
- if (MessageBox.Show("参数还原后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- {
- if (MessageBox.Show("请再次确认", "确认还原?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- {
- if (MessageBox.Show("请最后确认", "确认还原?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- {
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2708, Code);
- }
- }
- }
- timer1.Enabled = true;
- }
- #region 字符串转换为字节流
- public static byte[] HexStringToBytes(string hexStr, bool Flag_Space)
- {
- if (string.IsNullOrEmpty(hexStr))
- {
- return new byte[0];
- }
- if (hexStr.StartsWith("0x"))
- {
- hexStr = hexStr.Remove(0, 2);
- }
- var count = hexStr.Length;
- var byteCount = 0;
- if (Flag_Space == true)//相邻字节含空格
- {
- if ((count % 3) == 0)//最后字节含空格
- {
- byteCount = count / 3;
- }
- else if ((count % 3) == 2)//最后字节不含空格
- {
- byteCount = (count + 1) / 3;
- }
- else//数据无效
- {
- MessageBox.Show("相邻字节请插入空格!", "提示");
- }
- var result = new byte[byteCount];
- for (int ii = 0; ii < byteCount; ++ii)
- {
- var tempBytes = Byte.Parse(hexStr.Substring(3 * ii, 2), System.Globalization.NumberStyles.HexNumber);
- result[ii] = tempBytes;
- }
- return result;
- }
- else if (Flag_Space == false)//相邻字节不含空格
- {
- if (count % 2 == 1)
- {
- MessageBox.Show("请删除相邻字节之间空格!", "提示");
- }
- byteCount = count / 2;
- var result = new byte[byteCount];
- for (int ii = 0; ii < byteCount; ++ii)
- {
- var tempBytes = Byte.Parse(hexStr.Substring(2 * ii, 2), System.Globalization.NumberStyles.HexNumber);
- result[ii] = tempBytes;
- }
- return result;
- }
- else
- {
- return new byte[0];
- }
- }
- #endregion
- #region 故障日志结构体定义
- //故障日志记录信息 60 bytes,1K空间可以保存最近16条故障记录
- [StructLayout(LayoutKind.Sequential)]
- public struct MC_ErrorLogSaveInfo_Struct_t
- {
- public UInt16 Error_Index; //故障列表索引 2 bytes, 地址偏移 0
- public UInt16 NotesInfo1; //备注信息1,地址偏移 2
- public UInt16 NotesInfo2; //备注信息2,地址偏移 4
- public UInt16 NotesInfo3; //备注信息3,地址偏移 6
- public UInt32 ErrorCode; //故障码 4 bytes, 地址偏移 8
- public UInt32 RunTime; //运行时间 4bytes, 地址偏移 12
- //电机运行信息,占用空间32bytes
- [StructLayout(LayoutKind.Sequential)]
- public struct MC_RunInfo_Struct_t
- {
- public ushort BikeSpeed; //车速 0.1km/h,地址偏移0
- public ushort MotorSpeed; //输出转速 1rpm,地址偏移2
- public ushort Power; //电功率 1W,地址偏移4
- public ushort BusVoltage; //母线电压 1mV,地址偏移6
- public ushort BusCurrent; //母线电流 1mA,地址偏移8
- public byte Cadence; //踏频 1rpm,地址偏移10
- public byte Torque; //踩踏力矩 1Nm,地址偏移11
- public byte CadenceDir; //踩踏方向 0-正,1-反,2-停止,地址偏移12
- public byte GearSt; //助力档位,地址偏移13
- public byte LightSwitch; //灯开关 0xF0-关,0xF1-开,地址偏移14
- public byte SOC; //剩余电量 1%,地址偏移15
- public ushort RemainDistance; //续航里程 1km,地址偏移16
- public ushort ODO_Km; //总里程 1km,地址偏移18
- public byte PowerPerKm; //平均功耗 0.01Ah/km ,地址偏移20
- public byte T_PCB; //PCB温度 +40℃,地址偏移21
- public byte T_Coil; //绕组温度 +40℃,地址偏移22
- public byte T_MCU; //MCU温度 +40℃,地址偏移23
- public ushort Ride_Km; //开机后骑行里程 0.1km, 地址偏移24
- public ushort Ride_Time; //开机后骑行时间 1s,地址偏移26
- public UInt32 RS; //预留 4bytes
- };
- [MarshalAs(UnmanagedType.Struct)]
- public MC_RunInfo_Struct_t RunInfo; //运行信息 32bytes, 地址偏移 16
- //姿态传感器数值
- [StructLayout(LayoutKind.Sequential)]
- public struct MC_AttitudeAngle_Struct_t
- {
- public Int16 Angle_Pitch_Absolute; //俯仰角绝对角度
- public Int16 Angle_Roll_Absolute; //横滚角绝对角度
- public Int16 Angle_Pitch_Relative; //俯仰角相对角度
- public Int16 Angle_Roll_Relative; //横滚角相对角度
- };
- [MarshalAs(UnmanagedType.Struct)]
- public MC_AttitudeAngle_Struct_t AttitudeAngle; //车辆姿态角度 8bytes, 地址偏移 48
- //电机控制参数
- [StructLayout(LayoutKind.Sequential)]
- public struct MC_CalParam_Struct_t
- {
- public byte AssistRunMode; //助力模式
- public Int16 Ref_Torque; //力矩模式输入FOC值
- public Int16 Ref_Speed; //速度模式输入FOC值
- public byte Foc_Flag; //FCO运算标志
- };
- [MarshalAs(UnmanagedType.Struct)]
- public MC_CalParam_Struct_t MC_CalParam; //马达控制参数 8bytes , 地址偏移 56
- };
- #endregion
- public static class MyConverter
- {
- /// <summary>
- /// 由结构体转换为byte数组
- /// </summary>
- public static byte[] StructureToByte<T>(T structure)
- {
- int size = Marshal.SizeOf(typeof(T));
- byte[] buffer = new byte[size];
- IntPtr bufferIntPtr = Marshal.AllocHGlobal(size);
- try
- {
- Marshal.StructureToPtr(structure, bufferIntPtr, true);
- Marshal.Copy(bufferIntPtr, buffer, 0, size);
- }
- finally
- {
- Marshal.FreeHGlobal(bufferIntPtr);
- }
- return buffer;
- }
- /// <summary>
- /// 由byte数组转换为结构体
- /// </summary>
- public static T ByteToStructure<T>(byte[] dataBuffer, ushort StarIndex)
- {
- object structure = null;
- int size = Marshal.SizeOf(typeof(T));
- IntPtr allocIntPtr = Marshal.AllocHGlobal(size);
- try
- {
- Marshal.Copy(dataBuffer, StarIndex, allocIntPtr, size);
- structure = Marshal.PtrToStructure(allocIntPtr, typeof(T));
- }
- finally
- {
- Marshal.FreeHGlobal(allocIntPtr);
- }
- return (T)structure;
- }
- }
- private void button_ReadErrorLog_Click(object sender, EventArgs e)
- {
- var Address = new byte[4];
- UInt32 Address_Begin;
- UInt32 Addres_End;
- string textBox_Address_Begin = textBox_LogAddrBegin.Text;
- string textBox_Address_End = textBox_LogAddrEnd.Text;
- if (!serialPort1.IsOpen)
- {
- timer1.Enabled = false;
- MessageBox.Show("串口未连接");
- timer1.Enabled = true;
- return;
- }
- if ((textBox_Address_Begin == string.Empty) || (textBox_Address_End == string.Empty))
- {
- timer1.Enabled = false;
- MessageBox.Show("请输入地址!", "提示");
- timer1.Enabled = true;
- return;
- }
- else
- {
- Address = HexStringToBytes(textBox_Address_Begin, false);
- Address_Begin = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
- Address = HexStringToBytes(textBox_Address_End, false);
- Addres_End = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
- if (Addres_End <= Address_Begin)
- {
- timer1.Enabled = false;
- MessageBox.Show("结束地址应大于起始地址!", "提示");
- timer1.Enabled = true;
- return;
- }
- else if (((Addres_End - Address_Begin + 1) % 128) != 0) //
- {
- timer1.Enabled = false;
- MessageBox.Show("(结束地址 - 起始地址 + 1)应为128整数倍!", "提示");
- timer1.Enabled = true;
- return;
- }
- else
- {
- button_ReadErrorLog.Text = "读取中";
- button_ReadErrorLog.Enabled = false;
- //设置保存路经
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- saveFileDialog1.Title = "设置保存路经";
- saveFileDialog1.RestoreDirectory = true;
- saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
- if (((textBox_Mode.Text == string.Empty) || (textBox_Mode.Text == "---")) && ((textBox_SN.Text == string.Empty) || (textBox_SN.Text == "---")))
- {
- timer1.Enabled = false;
- MessageBox.Show("未查询电机版本信息!", "提示");
- timer1.Enabled = true;
- saveFileDialog1.FileName = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_故障日志"; ;
- }
- else
- {
- saveFileDialog1.FileName = textBox_Mode.Text + "_" + textBox_SN.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_故障日志";
- }
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- ErrorLogSaveFileName = saveFileDialog1.FileName.ToString();
- }
- else
- {
- button_ReadErrorLog.Enabled = true;
- button_ReadErrorLog.Text = "读取";
- return;
- }
- string SaveInfo = "读取时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n\r\n";
- SaveInfo += "电机型号:" + textBox_Mode.Text + "\r\n";
- SaveInfo += "序列号:" + textBox_SN.Text + "\r\n";
- SaveInfo += "硬件版本:" + textBox_HW.Text + "\r\n";
- SaveInfo += "软件版本:" + textBox_FW.Text + "\r\n";
- SaveInfo += "TE软件版本:" + textBox_TE_FW.Text + "\r\n\r\n";
- System.IO.File.WriteAllText(ErrorLogSaveFileName, SaveInfo + "无日志记录");
- ErrorLogSaveFlag = false;
- UInt32 ReadCount = 0;
- ReadCount = (Addres_End - Address_Begin + 1) / 128;
- for (ushort i = 0; i < ReadCount; i++)
- {
- var SendByte = new byte[8];
- UInt32 Address_Temp_Begin;
- UInt32 Address_Temp_End;
- Address_Temp_Begin = Address_Begin + 128 * (UInt32)i;
- SendByte[0] = (byte)(Address_Temp_Begin >> 24);
- SendByte[1] = (byte)(Address_Temp_Begin >> 16);
- SendByte[2] = (byte)(Address_Temp_Begin >> 8);
- SendByte[3] = (byte)(Address_Temp_Begin);
- Address_Temp_End = Address_Temp_Begin + 127;
- SendByte[4] = (byte)(Address_Temp_End >> 24);
- SendByte[5] = (byte)(Address_Temp_End >> 16);
- SendByte[6] = (byte)(Address_Temp_End >> 8);
- SendByte[7] = (byte)(Address_Temp_End);
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2D08, SendByte);
- Thread.Sleep(200);
- }
- timer1.Enabled = false;
- MessageBox.Show("读取完成,请查看日志!", "提示");
- timer1.Enabled = true;
- button_ReadErrorLog.Enabled = true;
- button_ReadErrorLog.Text = "读取";
- }
- }
- }
- private void ErrorLogDateSave(MC_ErrorLogSaveInfo_Struct_t Log1, MC_ErrorLogSaveInfo_Struct_t Log2)
- {
- if (ErrorLogSaveFileName.Substring(ErrorLogSaveFileName.Length - 4, 4) != ".txt")
- {
- timer1.Enabled = false;
- MessageBox.Show("日志存储失败!", "提示");
- timer1.Enabled = true;
-
- return;
- }
- string SaveData = "";
- //Log1
- if (Log1.Error_Index != 0xFFFF)
- {
- if (ErrorLogSaveFlag == false)
- {
- ErrorLogSaveFlag = true;
- string SaveInfo = "读取时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n\r\n";
- SaveInfo += "电机型号:" + textBox_Mode.Text + "\r\n";
- SaveInfo += "序列号:" + textBox_SN.Text + "\r\n";
- SaveInfo += "硬件版本:" + textBox_HW.Text + "\r\n";
- SaveInfo += "软件版本:" + textBox_FW.Text + "\r\n";
- SaveInfo += "TE软件版本:" + textBox_TE_FW.Text + "\r\n\r\n";
- System.IO.File.WriteAllText(ErrorLogSaveFileName, SaveInfo);
- }
- SaveData += "故障序号:" + Convert.ToString(Log1.Error_Index) + ", ";
- SaveData += "备注1:" + Convert.ToString(Log1.NotesInfo1) + ", ";
- SaveData += "备注2:" + Convert.ToString(Log1.NotesInfo2) + ", ";
- SaveData += "备注3:" + Convert.ToString(Log1.NotesInfo3) + ", ";
- SaveData += "故障码:" + "0x" + Convert.ToString(Log1.ErrorCode, 16).PadLeft(8, '0').ToUpper() + ", ";
- SaveData += "运行时间:" + Convert.ToString(Log1.RunTime) + "min" + ", ";
- SaveData += "车速:" + ((float)Log1.RunInfo.BikeSpeed / 10f).ToString("0.0") + "km/h" + ", ";
- SaveData += "电机转速:" + Convert.ToString(Log1.RunInfo.MotorSpeed) + "rpm" + ", ";
- SaveData += "功率:" + Convert.ToString(Log1.RunInfo.Power * 2) + "w" + ", ";
- SaveData += "电压:" + Convert.ToString(Log1.RunInfo.BusVoltage) + "mV" + ", ";
- SaveData += "电流:" + Convert.ToString(Log1.RunInfo.BusCurrent) + "mA" + ", ";
- SaveData += "踏频:" + Convert.ToString(Log1.RunInfo.Cadence) + "rpm" + ", ";
- SaveData += "力矩:" + Convert.ToString(Log1.RunInfo.Torque) + "N.m" + ", ";
- SaveData += "方向:" + ((Log1.RunInfo.CadenceDir == 0) ? "正" : ((Log1.RunInfo.CadenceDir == 1) ? "反" : "停止")) + ", ";
- SaveData += "档位:" + "0x" + Convert.ToString(Log1.RunInfo.GearSt, 16).PadLeft(2, '0').ToUpper() + ", ";
- SaveData += "灯开关:" + ((Log1.RunInfo.LightSwitch == 0xF1) ? "开" : "关") + ", ";
- SaveData += "电量:" + Convert.ToString(Log1.RunInfo.SOC) + "%" + ", ";
- if ((Log1.RunInfo.RemainDistance == 0xEEEE) || (Log1.RunInfo.RemainDistance == 0xFFFF))
- {
- SaveData += "续航:" + "---km" + ", ";
- }
- else
- {
- SaveData += "续航:" + Convert.ToString(Log1.RunInfo.RemainDistance) + "km" + ", ";
- }
- SaveData += "总里程:" + Convert.ToString(Log1.RunInfo.ODO_Km) + "km" + ", ";
- SaveData += "功耗:" + Convert.ToString(Log1.RunInfo.PowerPerKm * 10) + "mAh/km" + ", ";
- SaveData += "PCB温度:" + Convert.ToString((int)(Log1.RunInfo.T_PCB - 40)) + "DegreeC" + ", ";
- SaveData += "绕组温度:" + Convert.ToString((int)(Log1.RunInfo.T_Coil - 40)) + "DegreeC" + ", ";
- SaveData += "MCU温度:" + Convert.ToString((int)(Log1.RunInfo.T_MCU - 40)) + "DegreeC" + ", ";
- SaveData += "单次里程:" + Convert.ToString((float)Log1.RunInfo.Ride_Km / 10f) + "km" + ", ";
- SaveData += "单次时间:" + Convert.ToString(Log1.RunInfo.Ride_Time) + "s" + ", ";
- SaveData += "俯仰绝对角度:" + ((float)Log1.AttitudeAngle.Angle_Pitch_Absolute / 10f).ToString("0.0") + "Degree" + ", ";
- SaveData += "横滚绝对角度:" + ((float)Log1.AttitudeAngle.Angle_Roll_Absolute / 10f).ToString("0.0") + "Degree" + ", ";
- SaveData += "俯仰相对角度:" + ((float)Log1.AttitudeAngle.Angle_Pitch_Relative / 10f).ToString("0.0") + "Degree" + ", ";
- SaveData += "横滚相对角度:" + ((float)Log1.AttitudeAngle.Angle_Roll_Relative / 10f).ToString("0.0") + "Degree" + ", ";
- SaveData += "运行模式:" + Convert.ToString(Log1.MC_CalParam.AssistRunMode) + ", ";
- SaveData += "力矩环给定:" + Convert.ToString(Log1.MC_CalParam.Ref_Torque) + ", ";
- SaveData += "速度环给定:" + Convert.ToString(Log1.MC_CalParam.Ref_Speed) + ", ";
- SaveData += "FOC_Flag:" + Convert.ToString(Log1.MC_CalParam.Foc_Flag) + ", ";
- SaveData += "\r\n\r\n";
- System.IO.File.AppendAllText(ErrorLogSaveFileName, SaveData);
- SaveData = "";
- }
- //Log2
- if (Log2.Error_Index != 0xFFFF)
- {
- if (ErrorLogSaveFlag == false)
- {
- ErrorLogSaveFlag = true;
- string SaveInfo = "读取时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n\r\n";
- SaveInfo += "电机型号:" + textBox_Mode.Text + "\r\n";
- SaveInfo += "序列号:" + textBox_SN.Text + "\r\n";
- SaveInfo += "硬件版本:" + textBox_HW.Text + "\r\n";
- SaveInfo += "软件版本:" + textBox_FW.Text + "\r\n";
- SaveInfo += "TE软件版本:" + textBox_TE_FW.Text + "\r\n\r\n";
- System.IO.File.WriteAllText(ErrorLogSaveFileName, SaveInfo);
- }
- SaveData += "故障序号:" + Convert.ToString(Log2.Error_Index) + ", ";
- SaveData += "备注1:" + Convert.ToString(Log2.NotesInfo1) + ", ";
- SaveData += "备注2:" + Convert.ToString(Log2.NotesInfo2) + ", ";
- SaveData += "备注3:" + Convert.ToString(Log2.NotesInfo3) + ", ";
- SaveData += "故障码:" + "0x" + Convert.ToString(Log2.ErrorCode, 16).PadLeft(8, '0').ToUpper() + ", ";
- SaveData += "运行时间:" + Convert.ToString(Log2.RunTime) + "min" + ", ";
- SaveData += "车速:" + ((float)Log2.RunInfo.BikeSpeed / 10f).ToString("0.0") + "km/h" + ", ";
- SaveData += "电机转速:" + Convert.ToString(Log2.RunInfo.MotorSpeed) + "rpm" + ", ";
- SaveData += "功率:" + Convert.ToString(Log2.RunInfo.Power * 2) + "w" + ", ";
- SaveData += "电压:" + Convert.ToString(Log2.RunInfo.BusVoltage) + "mV" + ", ";
- SaveData += "电流:" + Convert.ToString(Log2.RunInfo.BusCurrent) + "mA" + ", ";
- SaveData += "踏频:" + Convert.ToString(Log2.RunInfo.Cadence) + "rpm" + ", ";
- SaveData += "力矩:" + Convert.ToString(Log2.RunInfo.Torque) + "N.m" + ", ";
- SaveData += "方向:" + ((Log2.RunInfo.CadenceDir == 0) ? "正" : ((Log2.RunInfo.CadenceDir == 1) ? "反" : "停止")) + ", ";
- SaveData += "档位:" + "0x" + Convert.ToString(Log2.RunInfo.GearSt, 16).PadLeft(2, '0').ToUpper() + ", ";
- SaveData += "灯开关:" + ((Log2.RunInfo.LightSwitch == 0xF1) ? "开" : "关") + ", ";
- SaveData += "电量:" + Convert.ToString(Log2.RunInfo.SOC) + "%" + ", ";
- if ((Log1.RunInfo.RemainDistance == 0xEEEE) || (Log1.RunInfo.RemainDistance == 0xFFFF))
- {
- SaveData += "续航:" + "---km" + ", ";
- }
- else
- {
- SaveData += "续航:" + Convert.ToString(Log1.RunInfo.RemainDistance) + "km" + ", ";
- }
- SaveData += "总里程:" + Convert.ToString(Log2.RunInfo.ODO_Km) + "km" + ", ";
- SaveData += "功耗:" + Convert.ToString(Log2.RunInfo.PowerPerKm * 10) + "mAh/km" + ", ";
- SaveData += "PCB温度:" + Convert.ToString((int)(Log2.RunInfo.T_PCB - 40)) + "DegreeC" + ", ";
- SaveData += "绕组温度:" + Convert.ToString((int)(Log2.RunInfo.T_Coil - 40)) + "DegreeC" + ", ";
- SaveData += "MCU温度:" + Convert.ToString((int)(Log2.RunInfo.T_MCU - 40)) + "DegreeC" + ", ";
- SaveData += "单次里程:" + Convert.ToString((float)Log2.RunInfo.Ride_Km / 10f) + "km" + ", ";
- SaveData += "单次时间:" + Convert.ToString(Log2.RunInfo.Ride_Time) + "s" + ", ";
- SaveData += "俯仰绝对角度:" + ((float)Log2.AttitudeAngle.Angle_Pitch_Absolute / 10f).ToString("0.0") + "Degree" + ", ";
- SaveData += "横滚绝对角度:" + ((float)Log2.AttitudeAngle.Angle_Roll_Absolute / 10f).ToString("0.0") + "Degree" + ", ";
- SaveData += "俯仰相对角度:" + ((float)Log2.AttitudeAngle.Angle_Pitch_Relative / 10f).ToString("0.0") + "Degree" + ", ";
- SaveData += "横滚相对角度:" + ((float)Log2.AttitudeAngle.Angle_Roll_Relative / 10f).ToString("0.0") + "Degree" + ", ";
- SaveData += "运行模式:" + Convert.ToString(Log2.MC_CalParam.AssistRunMode) + ", ";
- SaveData += "力矩环给定:" + Convert.ToString(Log2.MC_CalParam.Ref_Torque) + ", ";
- SaveData += "速度环给定:" + Convert.ToString(Log2.MC_CalParam.Ref_Speed) + ", ";
- SaveData += "FOC_Flag:" + Convert.ToString(Log2.MC_CalParam.Foc_Flag) + ", ";
- SaveData += "\r\n\r\n";
- System.IO.File.AppendAllText(ErrorLogSaveFileName, SaveData);
- SaveData = "";
- }
- }
- private void button_ClearData_MouseHover(object sender, EventArgs e)
- {
- // 创建the ToolTip
- ToolTip toolTip1 = new ToolTip();
- // 设置显示样式
- toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
- toolTip1.InitialDelay = 50;//事件触发多久后出现提示
- toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
- toolTip1.ShowAlways = true;//是否显示提示框
- // 设置伴随的对象.
- toolTip1.SetToolTip(this.button_ClearData, "清除电机内部所有数据,包括配置参数、历史记录等。");//设置提示按钮和提示内容
- }
- private void button_Recovery_MouseHover(object sender, EventArgs e)
- {
- // 创建the ToolTip
- ToolTip toolTip1 = new ToolTip();
- // 设置显示样式
- toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
- toolTip1.InitialDelay = 50;//事件触发多久后出现提示
- toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
- toolTip1.ShowAlways = true;//是否显示提示框
- // 设置伴随的对象.
- toolTip1.SetToolTip(this.button_Recovery, "还原配置参数为初始值。");//设置提示按钮和提示内容
- }
- private void button_DataExport_MouseHover(object sender, EventArgs e)
- {
- // 创建the ToolTip
- ToolTip toolTip1 = new ToolTip();
- // 设置显示样式
- toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
- toolTip1.InitialDelay = 50;//事件触发多久后出现提示
- toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
- toolTip1.ShowAlways = true;//是否显示提示框
- // 设置伴随的对象.
- toolTip1.SetToolTip(this.button_DataExport, "存储窗体页面,请先查询相关信息后操作。");//设置提示按钮和提示内容
- }
- private void label9_MouseHover(object sender, EventArgs e)
- {
- // 创建the ToolTip
- ToolTip toolTip1 = new ToolTip();
- // 设置显示样式
- toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
- toolTip1.InitialDelay = 50;//事件触发多久后出现提示
- toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
- toolTip1.ShowAlways = true;//是否显示提示框
- // 设置伴随的对象.
- toolTip1.SetToolTip(this.label9, "双击自动计算启动值。");//设置提示按钮和提示内容
- }
- private void button_ReadErrorLog_MouseHover(object sender, EventArgs e)
- {
- // 创建the ToolTip
- ToolTip toolTip1 = new ToolTip();
- // 设置显示样式
- toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
- toolTip1.InitialDelay = 50;//事件触发多久后出现提示
- toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
- toolTip1.ShowAlways = true;//是否显示提示框
- // 设置伴随的对象.
- toolTip1.SetToolTip(this.button_ReadErrorLog, "导出电机内部存储的故障日志,读取完后自动保存。");//设置提示按钮和提示内容
- }
- private void label119_MouseHover(object sender, EventArgs e)
- {
- // 创建the ToolTip
- ToolTip toolTip1 = new ToolTip();
- // 设置显示样式
- toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
- toolTip1.InitialDelay = 50;//事件触发多久后出现提示
- toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
- toolTip1.ShowAlways = true;//是否显示提示框
- // 设置伴随的对象.
- toolTip1.SetToolTip(this.label119, "双击自动计算校正系数。");//设置提示按钮和提示内容
- }
- private void button_LogClear_Click(object sender, EventArgs e)
- {
- var Code = new byte[9];
- Code[0] = (byte)'L';
- Code[1] = (byte)'O';
- Code[2] = (byte)'G';
- Code[3] = (byte)' ';
- Code[4] = (byte)'C';
- Code[5] = (byte)'L';
- Code[6] = (byte)'E';
- Code[7] = (byte)'A';
- Code[8] = (byte)'R';
- timer1.Enabled = false;
- if (MessageBox.Show("历史记录信息清除后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- {
- if (MessageBox.Show("请再次确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- {
- if (MessageBox.Show("请最后确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- {
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3909, Code);
- }
- }
- }
- timer1.Enabled = true;
- }
- private void textBox_RunInfo_ErrorCode_MouseHover(object sender, EventArgs e)
- {
- // 创建the ToolTip
- ToolTip toolTip1 = new ToolTip();
- // 设置显示样式
- toolTip1.AutoPopDelay = 30000;//提示信息的可见时间
- toolTip1.InitialDelay = 50;//事件触发多久后出现提示
- toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
- toolTip1.ShowAlways = true;//是否显示提示框
- // 设置伴随的对象.
- string ErrorCode = this.textBox_RunInfo_ErrorCode.Text;
- UInt32 Code = ErrorCode.Contains("H") ? Convert.ToUInt32(ErrorCode.Split('H')[0], 16) : 0;
- toolTip1.SetToolTip(this.textBox_RunInfo_ErrorCode, ErrorInfoDisplay(Code));//设置故障内容显示
- }
- private void button_LogClear_MouseHover(object sender, EventArgs e)
- {
- // 创建the ToolTip
- ToolTip toolTip1 = new ToolTip();
- // 设置显示样式
- toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
- toolTip1.InitialDelay = 50;//事件触发多久后出现提示
- toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
- toolTip1.ShowAlways = true;//是否显示提示框
- // 设置伴随的对象.
- toolTip1.SetToolTip(this.button_LogClear, "清除电机历史记录信息");//设置提示按钮和提示内容
- }
- private void button_ReadAll_Click(object sender, EventArgs e)
- {
- if (!serialPort1.IsOpen)
- {
- MessageBox.Show("串口未连接");
- return;
- }
- //查询力矩传感器
- var K = new byte[1];
- textBox_Sensor1_Zero.Text = "---";
- textBox_Sensor2_Zero.Text = "---";
- textBox_Sensor3_Zero.Text = "---";
- textBox_Sensor1_K.Text = "---";
- textBox_Sensor2_K.Text = "---";
- textBox_Sensor3_K.Text = "---";
- //轮询零点
- for (ushort i = 1; i <= 3; i++)
- {
- K[0] = (byte)i;
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3201, K);
- }
- //轮询校正系数
- for (ushort i = 1; i <= 3; i++)
- {
- K[0] = (byte)i;
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3301, K);
- }
- textBox_Torque_Start.Text = "---";
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3500, null);
- //查询单力矩零点和校正系数
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2000, null); //单力矩零点值
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2900, null); //单力矩校正系数和启动值
- //查询用户参数1
- MC_Config_Clear();
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1A00, null);
- //查询用户参数2
- MC_Config_Ext_Clear();
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3000, null);
- //查询姿态传感器参数
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2F00, null);
- MC_AttitudeAngle_Clear();
- //查询马达参数
- Motor_Config_Clear();
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1C00, null);
- //查询版本信息
- textBox_Mode.Text = "---";
- textBox_SN.Text = "---";
- textBox_HW.Text = "---";
- textBox_FW.Text = "---";
- textBox_Firmware_Special.Text = "---";
- Class_Motor_Ver.Mode = "---";
- Class_Motor_Ver.SN = "---";
- Class_Motor_Ver.HW = "---";
- Class_Motor_Ver.FW = "---";
- Class_Motor_Ver.Special = "---";
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
- textBox_TE_FW.Text = "---";
- SendCmd((ushort)0x651, (byte)0x11, (ushort)0x1200, null);
- //查询历史信息
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1E00, null);
- MC_RunLog_Clear();
- //查询骑行历史
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2E00, null);
- MC_RideInfo_Clear();
- }
- //双击时允许解锁写保护
- private void label127_DoubleClick(object sender, EventArgs e)
- {
- WriteProtectUnlock = true;
- }
- //双击解锁写保护
- private void label1_Welcom_DoubleClick(object sender, EventArgs e)
- {
- if (WriteProtectUnlock == false)
- return;
- button_ClearData.Enabled = true;
- button_Recovery.Enabled = true;
- button_Write_Config.Enabled = true;
- button_Write_Config_Ext.Enabled = true;
- button_Write_MotorConfig.Enabled = true;
- button_Write_Mode.Enabled = true;
- button_Write_SN.Enabled = true;
- }
- //读取指定地址的数据
- private void button_DebugRead_Click(object sender, EventArgs e)
- {
- var Address = new byte[4];
- UInt32 Address_Begin;
- UInt32 Addres_End;
- if (checkBox_DebugReset.Checked == true)
- richTextBox_DebugInfo.Clear();
- if ((textBox_DebugAddrBegin.Text == string.Empty) || (textBox_DebugAddrEnd.Text == string.Empty))
- {
- MessageBox.Show("请输入起始和结束地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- else if ((textBox_DebugAddrBegin.Text.Length != 8) || (textBox_DebugAddrEnd.Text.Length != 8))
- {
- MessageBox.Show("请检查地址长度!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- else
- {
- Address = HexStringToBytes(textBox_DebugAddrBegin.Text, false);
- Address_Begin = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
- Address = HexStringToBytes(textBox_DebugAddrEnd.Text, false);
- Addres_End = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
- if (Addres_End < Address_Begin)
- {
- MessageBox.Show("结束地址必须大于或等于起始地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- else
- {
- var SendByte = new byte[8];
- SendByte[0] = (byte)(Address_Begin >> 24);
- SendByte[1] = (byte)(Address_Begin >> 16);
- SendByte[2] = (byte)(Address_Begin >> 8);
- SendByte[3] = (byte)(Address_Begin);
- SendByte[4] = (byte)(Addres_End >> 24);
- SendByte[5] = (byte)(Addres_End >> 16);
- SendByte[6] = (byte)(Addres_End >> 8);
- SendByte[7] = (byte)(Addres_End);
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2D08, SendByte);
- }
- }
- }
- //选择单力矩
- private void radioButton_SingleSensor_CheckedChanged(object sender, EventArgs e)
- {
- checkBox_Sensor1.Checked = true;
- label5.Visible = false;
- textBox_Sensor2_Avg.Visible = false;
- textBox_Sensor2_Std.Visible = false;
- textBox_Sensor2_Zero.Visible = false;
- textBox_Sensor2_K.Visible = false;
- checkBox_Sensor2.Visible = false;
- checkBox_Sensor2.Checked = false;
- label115.Visible = false;
- textBox_Sensor3_Avg.Visible = false;
- textBox_Sensor3_Std.Visible = false;
- textBox_Sensor3_Zero.Visible = false;
- textBox_Sensor3_K.Visible = false;
- checkBox_Sensor3.Visible = false;
- checkBox_Sensor3.Checked = false;
- this.BackColor = System.Drawing.Color.FromArgb((int)0, (int)192, (int)192);
- tabPage1.BackColor = System.Drawing.Color.FromArgb((int)0, (int)192, (int)192);
- tabPage2.BackColor = System.Drawing.Color.FromArgb((int)0, (int)192, (int)192);
- textBox_RevCnt.BackColor = System.Drawing.Color.FromArgb((int)0, (int)192, (int)192);
- }
- //选择三力矩
- private void radioButton_ThreeSensor_CheckedChanged(object sender, EventArgs e)
- {
- checkBox_Sensor1.Checked = true;
- label5.Visible = true;
- textBox_Sensor2_Avg.Visible = true;
- textBox_Sensor2_Std.Visible = true;
- textBox_Sensor2_Zero.Visible = true;
- textBox_Sensor2_K.Visible = true;
- checkBox_Sensor2.Visible = true;
- checkBox_Sensor2.Checked = true;
- label115.Visible = true;
- textBox_Sensor3_Avg.Visible = true;
- textBox_Sensor3_Std.Visible = true;
- textBox_Sensor3_Zero.Visible = true;
- textBox_Sensor3_K.Visible = true;
- checkBox_Sensor3.Visible = true;
- checkBox_Sensor3.Checked = true;
- this.BackColor = System.Drawing.Color.FromArgb((int)46, (int)193, (int)144);
- tabPage1.BackColor = System.Drawing.Color.FromArgb((int)46, (int)193, (int)144);
- tabPage2.BackColor = System.Drawing.Color.FromArgb((int)46, (int)193, (int)144);
- textBox_RevCnt.BackColor = System.Drawing.Color.FromArgb((int)46, (int)193, (int)144);
- }
- private void radioButton_DebugMode_Confg_Click(object sender, EventArgs e)
- {
- var RunMode = new byte[1];
- RunMode[0] = 0x02;
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, RunMode);
- checkBox_AutoSend.Checked = false;
- }
- }
- }
|