12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761 |
- 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;
- 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 MC运行信息超时计数
- bool MC_RunInfo_Refresh = false;
- ushort MC_RunInfo_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 解锁写保护标志
- bool WriteProtectUnlock = false;
- #endregion
- Form2 LoginForm = new Form2();
- #endregion
- public Form1()
- {
- InitializeComponent();
- }
- #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)
- {
- //Form2 LoginForm = new Form2() ;
- //LoginForm.Show();
- 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)
- {
- 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)
- {
- 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 ((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)
- {
- 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 = "---";
- 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故障信息超时清除
- 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;
- }
- }
- }
- 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
- {
- //三力矩传感器不用
- }));
- }
- 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
- {
- //三力矩传感器不用
-
- }));
- }
- 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 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();
- }
- Class_Motor_Ver.Mode = textBox_Firmware_Special.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 * 2) + " 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;
- //计算传感器均值和标准差
- 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);
- }));
- }
- 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);
- //绕组最高温
- 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);
- }));
- }
- 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]);
- 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");
- }));
- }
- 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;
- }
- 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 groupBox3.Controls)
- {
- if (c is TextBox)
- {
- 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)
- {
- 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);
- 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_TailLightMode.Text == "模式1")
- {
- ConfigParam[5] = 0x01;
- }
- 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);
- 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 groupBox3.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 groupBox3.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 groupBox3.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 groupBox3.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();
- }
-
- 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 = "0801F800";
- string textBox_Address_End = "0801FBFF";
- 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);
- //查询用户参数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);
- }
- }
- }
- }
- }
|