1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300 |
- 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:" + "FALSE" + "\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_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 = "";
- }
- }
- }
- 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));
- 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 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;
- }
- }
- }
|