Form1.cs 173 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO.Ports;
  9. using System.Threading;
  10. using System.Runtime.InteropServices;
  11. using System.Runtime.CompilerServices;
  12. namespace MOTINOVA_MC_Test
  13. {
  14. public partial class Form1 : Form
  15. {
  16. #region------------------------本地变量-----------------------------
  17. #region 串口相关
  18. private List<byte> buffer = new List<byte>(4096);
  19. private byte[] binary_data_1 = new byte[1024];
  20. private bool Listening = false;// 侦听串口是否是接收数据标志位
  21. public static string m_strDataBits = "8";
  22. public static string m_strStopBits = "1";
  23. public static string m_strCheckBits = "None";
  24. public static bool g_blnIsOpen = false;
  25. #endregion
  26. #region MC运行信息超时计数
  27. bool MC_RunInfo_Refresh = false;
  28. ushort MC_RunInfo_Refresh_Cnt = 0;
  29. #endregion
  30. #region MC故障码超时计数
  31. bool MC_ErrorCode_Refresh = false;
  32. ushort MC_ErrorCode_Refresh_Cnt = 0;
  33. #endregion
  34. #region 力矩传感器平均值采集
  35. byte[] TorqueArray = new byte[25];
  36. byte[] Sensor1Array = new byte[25];
  37. byte[] Sensor2Array = new byte[25];
  38. byte[] Sensor3Array = new byte[25];
  39. ushort TorqueArray_Index = 0;
  40. #endregion
  41. #region 自动发送助力档位计时
  42. ushort AutoSendTimeCnt;
  43. #endregion
  44. #region 骑行信息超时计数
  45. bool MC_RideInfo_Refresh = false;
  46. ushort MC_RideInfo_Refresh_Cnt = 0;
  47. #endregion
  48. #region 姿态信息超时计数
  49. bool MC_AttitudeAngle_Refresh = false;
  50. ushort MC_AttitudeAngle_Refresh_Cnt = 0;
  51. #endregion
  52. #region 自动发送查询姿态信息计时
  53. ushort AutoSendReadAttitudeAngleTimeCnt;
  54. #endregion
  55. #region 自动发送查询骑行信息计时
  56. ushort AutoSendReadRideInfoTimeCnt;
  57. #endregion
  58. #region 自动存储数据
  59. string AutoSaveFileName = "";
  60. #endregion
  61. #region 故障日志存储相关
  62. bool ErrorLogSaveFlag = false;
  63. string ErrorLogSaveFileName = "";
  64. #endregion
  65. #region CDL在线检测延时
  66. ushort CDL_OnlineCheck_Cnt = 0;
  67. bool CDL_Online_Flag = true;
  68. #endregion
  69. #region 解锁写保护标志
  70. bool WriteProtectUnlock = false;
  71. #endregion
  72. Form2 LoginForm = new Form2();
  73. #endregion
  74. public Form1()
  75. {
  76. InitializeComponent();
  77. }
  78. #region 打开或关闭串口
  79. private void button_ComOpen_Click(object sender, EventArgs e)
  80. {
  81. byte[] Data = new byte[1];
  82. try
  83. {
  84. if (!serialPort1.IsOpen)
  85. {
  86. serialPort1.PortName = comboBox_ComIndex.Text;
  87. serialPort1.BaudRate = int.Parse(comboBox_Baudrate.Text);
  88. serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), m_strCheckBits);
  89. serialPort1.DataBits = Int32.Parse(m_strDataBits);
  90. serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), (m_strStopBits == "1.5") ? "3" : m_strStopBits);
  91. serialPort1.Open();
  92. comboBox_ComIndex.Enabled = false;
  93. comboBox_Baudrate.Enabled = false;
  94. button_ComOpen.Text = "断开(&C)";
  95. g_blnIsOpen = true;
  96. timer1.Enabled = true;
  97. button_RefreshPort.Enabled = false;
  98. //发送指令检测CDL是否在线
  99. SendCmd((ushort)0x7FF, (byte)0x11, (ushort)0x1100, null);
  100. CDL_OnlineCheck_Cnt = 0;
  101. CDL_Online_Flag = false;
  102. //开机
  103. Data[0] = 0xF1;
  104. SendCmd(0x7FF, 0x16, 0x2201, Data);
  105. }
  106. else
  107. {
  108. //关机
  109. Data[0] = 0xF0;
  110. SendCmd(0x7FF, 0x16, 0x2201, Data);
  111. while (Listening) Application.DoEvents();
  112. //打开时点击,则关闭串口
  113. serialPort1.Close();
  114. comboBox_ComIndex.Enabled = true;
  115. comboBox_Baudrate.Enabled = true;
  116. button_ComOpen.Text = "连接(&C)";
  117. g_blnIsOpen = false;
  118. timer1.Enabled = false;
  119. button_RefreshPort.Enabled = true;
  120. CDL_Online_Flag = true;
  121. }
  122. }
  123. catch (System.Exception ex)
  124. {
  125. MessageBox.Show(ex.Message);
  126. return;
  127. }
  128. }
  129. #endregion
  130. private void Form1_Load(object sender, EventArgs e)
  131. {
  132. //Form2 LoginForm = new Form2() ;
  133. //LoginForm.Show();
  134. string[] ports = SerialPort.GetPortNames();
  135. Array.Sort(ports);
  136. comboBox_ComIndex.Items.AddRange(ports);
  137. comboBox_ComIndex.SelectedIndex = comboBox_ComIndex.Items.Count > 0 ? 0 : -1;
  138. comboBox_Baudrate.SelectedIndex = comboBox_Baudrate.Items.IndexOf("115200");
  139. MC_Config_Clear();
  140. Motor_Config_Clear();
  141. MC_RunInfo_Clear();
  142. MC_ErrorCode_Clear();
  143. MC_Config_Ext_Clear();
  144. MC_AttitudeAngle_Clear();
  145. MC_RideInfo_Clear();
  146. MC_RunLog_Clear();
  147. label_BiuldTime.Text = System.IO.File.GetLastWriteTime(this.GetType().Assembly.Location).ToString();
  148. }
  149. #region CRC校验
  150. static UInt32[] crc32_table = new UInt32[256]
  151. {
  152. 0x00000000, 0x04C11DB7, 0x09823B6E, 0x0D4326D9, 0x130476DC, 0x17C56B6B,
  153. 0x1A864DB2, 0x1E475005, 0x2608EDB8, 0x22C9F00F, 0x2F8AD6D6, 0x2B4BCB61,
  154. 0x350C9B64, 0x31CD86D3, 0x3C8EA00A, 0x384FBDBD, 0x4C11DB70, 0x48D0C6C7,
  155. 0x4593E01E, 0x4152FDA9, 0x5F15ADAC, 0x5BD4B01B, 0x569796C2, 0x52568B75,
  156. 0x6A1936C8, 0x6ED82B7F, 0x639B0DA6, 0x675A1011, 0x791D4014, 0x7DDC5DA3,
  157. 0x709F7B7A, 0x745E66CD, 0x9823B6E0, 0x9CE2AB57, 0x91A18D8E, 0x95609039,
  158. 0x8B27C03C, 0x8FE6DD8B, 0x82A5FB52, 0x8664E6E5, 0xBE2B5B58, 0xBAEA46EF,
  159. 0xB7A96036, 0xB3687D81, 0xAD2F2D84, 0xA9EE3033, 0xA4AD16EA, 0xA06C0B5D,
  160. 0xD4326D90, 0xD0F37027, 0xDDB056FE, 0xD9714B49, 0xC7361B4C, 0xC3F706FB,
  161. 0xCEB42022, 0xCA753D95, 0xF23A8028, 0xF6FB9D9F, 0xFBB8BB46, 0xFF79A6F1,
  162. 0xE13EF6F4, 0xE5FFEB43, 0xE8BCCD9A, 0xEC7DD02D, 0x34867077, 0x30476DC0,
  163. 0x3D044B19, 0x39C556AE, 0x278206AB, 0x23431B1C, 0x2E003DC5, 0x2AC12072,
  164. 0x128E9DCF, 0x164F8078, 0x1B0CA6A1, 0x1FCDBB16, 0x018AEB13, 0x054BF6A4,
  165. 0x0808D07D, 0x0CC9CDCA, 0x7897AB07, 0x7C56B6B0, 0x71159069, 0x75D48DDE,
  166. 0x6B93DDDB, 0x6F52C06C, 0x6211E6B5, 0x66D0FB02, 0x5E9F46BF, 0x5A5E5B08,
  167. 0x571D7DD1, 0x53DC6066, 0x4D9B3063, 0x495A2DD4, 0x44190B0D, 0x40D816BA,
  168. 0xACA5C697, 0xA864DB20, 0xA527FDF9, 0xA1E6E04E, 0xBFA1B04B, 0xBB60ADFC,
  169. 0xB6238B25, 0xB2E29692, 0x8AAD2B2F, 0x8E6C3698, 0x832F1041, 0x87EE0DF6,
  170. 0x99A95DF3, 0x9D684044, 0x902B669D, 0x94EA7B2A, 0xE0B41DE7, 0xE4750050,
  171. 0xE9362689, 0xEDF73B3E, 0xF3B06B3B, 0xF771768C, 0xFA325055, 0xFEF34DE2,
  172. 0xC6BCF05F, 0xC27DEDE8, 0xCF3ECB31, 0xCBFFD686, 0xD5B88683, 0xD1799B34,
  173. 0xDC3ABDED, 0xD8FBA05A, 0x690CE0EE, 0x6DCDFD59, 0x608EDB80, 0x644FC637,
  174. 0x7A089632, 0x7EC98B85, 0x738AAD5C, 0x774BB0EB, 0x4F040D56, 0x4BC510E1,
  175. 0x46863638, 0x42472B8F, 0x5C007B8A, 0x58C1663D, 0x558240E4, 0x51435D53,
  176. 0x251D3B9E, 0x21DC2629, 0x2C9F00F0, 0x285E1D47, 0x36194D42, 0x32D850F5,
  177. 0x3F9B762C, 0x3B5A6B9B, 0x0315D626, 0x07D4CB91, 0x0A97ED48, 0x0E56F0FF,
  178. 0x1011A0FA, 0x14D0BD4D, 0x19939B94, 0x1D528623, 0xF12F560E, 0xF5EE4BB9,
  179. 0xF8AD6D60, 0xFC6C70D7, 0xE22B20D2, 0xE6EA3D65, 0xEBA91BBC, 0xEF68060B,
  180. 0xD727BBB6, 0xD3E6A601, 0xDEA580D8, 0xDA649D6F, 0xC423CD6A, 0xC0E2D0DD,
  181. 0xCDA1F604, 0xC960EBB3, 0xBD3E8D7E, 0xB9FF90C9, 0xB4BCB610, 0xB07DABA7,
  182. 0xAE3AFBA2, 0xAAFBE615, 0xA7B8C0CC, 0xA379DD7B, 0x9B3660C6, 0x9FF77D71,
  183. 0x92B45BA8, 0x9675461F, 0x8832161A, 0x8CF30BAD, 0x81B02D74, 0x857130C3,
  184. 0x5D8A9099, 0x594B8D2E, 0x5408ABF7, 0x50C9B640, 0x4E8EE645, 0x4A4FFBF2,
  185. 0x470CDD2B, 0x43CDC09C, 0x7B827D21, 0x7F436096, 0x7200464F, 0x76C15BF8,
  186. 0x68860BFD, 0x6C47164A, 0x61043093, 0x65C52D24, 0x119B4BE9, 0x155A565E,
  187. 0x18197087, 0x1CD86D30, 0x029F3D35, 0x065E2082, 0x0B1D065B, 0x0FDC1BEC,
  188. 0x3793A651, 0x3352BBE6, 0x3E119D3F, 0x3AD08088, 0x2497D08D, 0x2056CD3A,
  189. 0x2D15EBE3, 0x29D4F654, 0xC5A92679, 0xC1683BCE, 0xCC2B1D17, 0xC8EA00A0,
  190. 0xD6AD50A5, 0xD26C4D12, 0xDF2F6BCB, 0xDBEE767C, 0xE3A1CBC1, 0xE760D676,
  191. 0xEA23F0AF, 0xEEE2ED18, 0xF0A5BD1D, 0xF464A0AA, 0xF9278673, 0xFDE69BC4,
  192. 0x89B8FD09, 0x8D79E0BE, 0x803AC667, 0x84FBDBD0, 0x9ABC8BD5, 0x9E7D9662,
  193. 0x933EB0BB, 0x97FFAD0C, 0xAFB010B1, 0xAB710D06, 0xA6322BDF, 0xA2F33668,
  194. 0xBCB4666D, 0xB8757BDA, 0xB5365D03, 0xB1F740B4
  195. };
  196. static UInt32 crc32_cal(byte[] pData, UInt16 Length)
  197. {
  198. UInt32 nReg;
  199. UInt32 nTemp = 0;
  200. UInt16 i, n;
  201. nReg = 0xFFFFFFFF;
  202. for (n = 0; n < Length; n++)
  203. {
  204. nReg ^= (UInt32)pData[n];
  205. for (i = 0; i < 4; i++)
  206. {
  207. nTemp = crc32_table[((nReg >> 24) & 0xFF)];
  208. nReg <<= 8;
  209. nReg ^= nTemp;
  210. }
  211. }
  212. return nReg;
  213. }
  214. #endregion
  215. #region 平均值计算
  216. private ushort GetAvg(byte[] pData, UInt16 Length)
  217. {
  218. UInt32 Sum = 0;
  219. for(ushort i=0; i<Length; i++)
  220. {
  221. Sum += pData[i];
  222. }
  223. return (ushort)(Sum/Length);
  224. }
  225. #endregion
  226. #region 标准差计算
  227. private ushort GetStDev(byte[] pData, UInt16 Length)
  228. {
  229. double Result = 0;
  230. //计算平均值
  231. UInt32 Avg = GetAvg(pData, Length);
  232. //计算各个数值与平均值的差值平方和
  233. UInt32 Sum = 0;
  234. for (ushort i = 0; i < Length; i++)
  235. {
  236. Sum += (pData[i] - Avg) * (pData[i] - Avg);
  237. }
  238. //计算标准差
  239. Result = Math.Sqrt((double)Sum / Length);
  240. return (ushort)(Result);
  241. }
  242. #endregion
  243. #region 发送指令函数
  244. private bool SendCmd(ushort ID, byte Mode, ushort Cmd, byte[] Data)
  245. {
  246. if (!serialPort1.IsOpen)
  247. {
  248. MessageBox.Show("串口未连接");
  249. return false;
  250. }
  251. ushort DataLength = (ushort)(Cmd & 0xFF);
  252. var SendCmdTmp = new byte[DataLength + 13];
  253. //帧头
  254. SendCmdTmp[0] = 0x55;
  255. SendCmdTmp[1] = 0xAA;
  256. //ID
  257. SendCmdTmp[2] = (byte)(ID >> 8);
  258. SendCmdTmp[3] = (byte)(ID & 0xFF);
  259. //MODE
  260. SendCmdTmp[4] = Mode;
  261. //长度
  262. SendCmdTmp[5] = (byte)((Cmd & 0xFF) + 2);
  263. //命令字
  264. SendCmdTmp[6] = (byte)(Cmd >> 8);
  265. SendCmdTmp[7] = (byte)(Cmd & 0xFF);
  266. //数据段
  267. for (UInt16 i = 0; i < (Cmd & 0xFF); i++)
  268. {
  269. SendCmdTmp[8 + i] = Data[i];
  270. }
  271. //校验
  272. UInt32 CRC_Result = crc32_cal(SendCmdTmp, (ushort)(DataLength + 8));
  273. SendCmdTmp[DataLength + 8] = Convert.ToByte(CRC_Result >> 24);
  274. SendCmdTmp[DataLength + 9] = Convert.ToByte((CRC_Result >> 16) % 256);
  275. SendCmdTmp[DataLength + 10] = Convert.ToByte((CRC_Result >> 8) % 256);
  276. SendCmdTmp[DataLength + 11] = Convert.ToByte(CRC_Result % 256);
  277. //帧尾
  278. SendCmdTmp[DataLength + 12] = 0xF0;
  279. //发送数据
  280. serialPort1.Write(SendCmdTmp, 0, SendCmdTmp.Length);
  281. return true;
  282. }
  283. #endregion
  284. #region 力矩传感器查询和写入相关
  285. //发送力矩传感器校正系数
  286. private void button_WriteK_Click(object sender, EventArgs e)
  287. {
  288. var K = new byte[2];
  289. ushort DataTemp;
  290. if ((checkBox_Sensor1.Checked == false) && (checkBox_Sensor2.Checked == false) && (checkBox_Sensor3.Checked == false))
  291. {
  292. timer1.Enabled = false;
  293. MessageBox.Show("请选择传感器", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  294. timer1.Enabled = true;
  295. return;
  296. }
  297. else
  298. {
  299. if (MessageBox.Show("确认写入", "确认写入?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  300. {
  301. if (checkBox_Sensor1.Checked == true)
  302. {
  303. if (textBox_Sensor1_K.Text != string.Empty)
  304. {
  305. K[0] = 1;
  306. DataTemp = (ushort)(decimal.Parse(textBox_Sensor1_K.Text) * 100);
  307. K[1] = Convert.ToByte(DataTemp);
  308. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3402, K);
  309. Thread.Sleep(20);
  310. }
  311. }
  312. if (checkBox_Sensor2.Checked == true)
  313. {
  314. if (textBox_Sensor2_K.Text != string.Empty)
  315. {
  316. K[0] = 2;
  317. DataTemp = (ushort)(decimal.Parse(textBox_Sensor2_K.Text) * 100);
  318. K[1] = Convert.ToByte(DataTemp);
  319. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3402, K);
  320. Thread.Sleep(20);
  321. }
  322. }
  323. if (checkBox_Sensor3.Checked == true)
  324. {
  325. if (textBox_Sensor3_K.Text != string.Empty)
  326. {
  327. K[0] = 3;
  328. DataTemp = (ushort)(decimal.Parse(textBox_Sensor3_K.Text) * 100);
  329. K[1] = Convert.ToByte(DataTemp);
  330. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3402, K);
  331. Thread.Sleep(20);
  332. }
  333. }
  334. }
  335. }
  336. }
  337. //查询传感器零点
  338. private void button_ReadZero_Click(object sender, EventArgs e)
  339. {
  340. var K = new byte[1];
  341. textBox_Sensor1_Zero.Text = "---";
  342. textBox_Sensor2_Zero.Text = "---";
  343. textBox_Sensor3_Zero.Text = "---";
  344. textBox_Sensor1_K.Text = "---";
  345. textBox_Sensor2_K.Text = "---";
  346. textBox_Sensor3_K.Text = "---";
  347. //轮询零点
  348. for (ushort i = 1; i <= 3; i++)
  349. {
  350. K[0] = (byte)i;
  351. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3201, K);
  352. }
  353. //轮询校正系数
  354. for (ushort i = 1; i <= 3; i++)
  355. {
  356. K[0] = (byte)i;
  357. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3301, K);
  358. }
  359. }
  360. //计算校正系数
  361. private void label119_DoubleClick(object sender, EventArgs e)
  362. {
  363. float K;
  364. string DataTemp;
  365. if ((checkBox_Sensor1.Checked == false) && (checkBox_Sensor2.Checked == false) && (checkBox_Sensor3.Checked == false))
  366. {
  367. timer1.Enabled = false;
  368. MessageBox.Show("请选择传感器", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  369. timer1.Enabled = true;
  370. return;
  371. }
  372. else
  373. {
  374. if (checkBox_Sensor1.Checked == true)
  375. {
  376. if ((textBox_Sensor1_Avg.Text != "---") && (textBox_Sensor1_Avg.Text != string.Empty))
  377. {
  378. DataTemp = textBox_Sensor1_Avg.Text;
  379. DataTemp = System.Text.RegularExpressions.Regex.Replace(DataTemp, @"[^0-9]+", "");
  380. K = float.Parse(DataTemp) / 75f;
  381. textBox_Sensor1_K.Text = K.ToString("#0.00");
  382. }
  383. else
  384. {
  385. textBox_Sensor1_K.Text = "";
  386. }
  387. }
  388. if (checkBox_Sensor2.Checked == true)
  389. {
  390. if ((textBox_Sensor2_Avg.Text != "---") && (textBox_Sensor2_Avg.Text != string.Empty))
  391. {
  392. DataTemp = textBox_Sensor2_Avg.Text;
  393. DataTemp = System.Text.RegularExpressions.Regex.Replace(DataTemp, @"[^0-9]+", "");
  394. K = float.Parse(DataTemp) / 75f;
  395. textBox_Sensor2_K.Text = K.ToString("#0.00");
  396. }
  397. else
  398. {
  399. textBox_Sensor2_K.Text = "";
  400. }
  401. }
  402. if (checkBox_Sensor3.Checked == true)
  403. {
  404. if ((textBox_Sensor3_Avg.Text != "---") && (textBox_Sensor3_Avg.Text != string.Empty))
  405. {
  406. DataTemp = textBox_Sensor3_Avg.Text;
  407. DataTemp = System.Text.RegularExpressions.Regex.Replace(DataTemp, @"[^0-9]+", "");
  408. K = float.Parse(DataTemp) / 75f;
  409. textBox_Sensor3_K.Text = K.ToString("#0.00");
  410. }
  411. else
  412. {
  413. textBox_Sensor3_K.Text = "";
  414. }
  415. }
  416. }
  417. }
  418. //发送力矩传感器启动值
  419. private void button_Write_Torque_Start_Click(object sender, EventArgs e)
  420. {
  421. ushort StarData = Convert.ToUInt16(textBox_Torque_Start.Text);
  422. var StarData_Array = new byte[2];
  423. StarData_Array[0] = (byte)(StarData & 0xFF);
  424. StarData_Array[1] = (byte)(StarData >> 8);
  425. if (MessageBox.Show("确认写入", "确认写入?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  426. {
  427. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2B02, StarData_Array);
  428. }
  429. }
  430. //查询力矩传感器启动值
  431. private void button_ReadStartData_Click(object sender, EventArgs e)
  432. {
  433. textBox_Torque_Start.Text = "---";
  434. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3500, null);
  435. }
  436. //根据采集值计算启动值
  437. private void label9_DoubleClick(object sender, EventArgs e)
  438. {
  439. ushort StartData;
  440. string DataTemp;
  441. if ((textBox_TorqueData.Text == "---") || (textBox_TorqueData.Text == string.Empty))
  442. {
  443. return;
  444. }
  445. DataTemp = textBox_TorqueData.Text;
  446. DataTemp = System.Text.RegularExpressions.Regex.Replace(DataTemp, @"[^0-9]+", "");
  447. StartData = (ushort)(Convert.ToUInt16(DataTemp) * 28);
  448. textBox_Torque_Start.Text = StartData.ToString();
  449. }
  450. #endregion
  451. #region 工作模式设置
  452. //进入运行模式
  453. private void radioButton_WorkMode_Run_Click(object sender, EventArgs e)
  454. {
  455. var RunMode = new byte[1];
  456. RunMode[0] = 0x00;
  457. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, RunMode);
  458. checkBox_ReadTorqueData.Checked = false;
  459. }
  460. //进入配置模式
  461. private void radioButton_WorkMode_Confg_Click(object sender, EventArgs e)
  462. {
  463. var RunMode = new byte[1];
  464. RunMode[0] = 0x01;
  465. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, RunMode);
  466. checkBox_AutoSend.Checked = false;
  467. }
  468. #endregion
  469. #region 助力档位设置
  470. //设置OFF档位
  471. private void radioButton_Gear_OFF_Click(object sender, EventArgs e)
  472. {
  473. var CtrlCode = new byte[2];
  474. CtrlCode[0] = 0x00;
  475. if (radioButton_Light_OFF.Checked == true)
  476. {
  477. CtrlCode[1] = 0xF0;
  478. }
  479. else if (radioButton_Light_ON.Checked == true)
  480. {
  481. CtrlCode[1] = 0xF1;
  482. }
  483. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  484. }
  485. //设置ECO档位
  486. private void radioButton_Gear_ECO_Click(object sender, EventArgs e)
  487. {
  488. var CtrlCode = new byte[2];
  489. CtrlCode[0] = 0x01;
  490. if (radioButton_Light_OFF.Checked == true)
  491. {
  492. CtrlCode[1] = 0xF0;
  493. }
  494. else if (radioButton_Light_ON.Checked == true)
  495. {
  496. CtrlCode[1] = 0xF1;
  497. }
  498. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  499. }
  500. //设置NORM档位
  501. private void radioButton_Gear_NORM_Click(object sender, EventArgs e)
  502. {
  503. var CtrlCode = new byte[2];
  504. CtrlCode[0] = 0x02;
  505. if (radioButton_Light_OFF.Checked == true)
  506. {
  507. CtrlCode[1] = 0xF0;
  508. }
  509. else if (radioButton_Light_ON.Checked == true)
  510. {
  511. CtrlCode[1] = 0xF1;
  512. }
  513. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  514. }
  515. //设置SPORT档位
  516. private void radioButton_Gear_SPORT_Click(object sender, EventArgs e)
  517. {
  518. var CtrlCode = new byte[2];
  519. CtrlCode[0] = 0x03;
  520. if (radioButton_Light_OFF.Checked == true)
  521. {
  522. CtrlCode[1] = 0xF0;
  523. }
  524. else if (radioButton_Light_ON.Checked == true)
  525. {
  526. CtrlCode[1] = 0xF1;
  527. }
  528. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  529. }
  530. //设置TURBO档位
  531. private void radioButton_Gear_TURBO_Click(object sender, EventArgs e)
  532. {
  533. var CtrlCode = new byte[2];
  534. CtrlCode[0] = 0x04;
  535. if (radioButton_Light_OFF.Checked == true)
  536. {
  537. CtrlCode[1] = 0xF0;
  538. }
  539. else if (radioButton_Light_ON.Checked == true)
  540. {
  541. CtrlCode[1] = 0xF1;
  542. }
  543. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  544. }
  545. //设置SMART档位
  546. private void radioButton_Gear_SMART_Click(object sender, EventArgs e)
  547. {
  548. var CtrlCode = new byte[2];
  549. CtrlCode[0] = 0x33;
  550. if (radioButton_Light_OFF.Checked == true)
  551. {
  552. CtrlCode[1] = 0xF0;
  553. }
  554. else if (radioButton_Light_ON.Checked == true)
  555. {
  556. CtrlCode[1] = 0xF1;
  557. }
  558. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  559. }
  560. //设置WALK档位
  561. private void radioButton_Gear_WALK_Click(object sender, EventArgs e)
  562. {
  563. var CtrlCode = new byte[2];
  564. CtrlCode[0] = 0x22;
  565. if (radioButton_Light_OFF.Checked == true)
  566. {
  567. CtrlCode[1] = 0xF0;
  568. }
  569. else if (radioButton_Light_ON.Checked == true)
  570. {
  571. CtrlCode[1] = 0xF1;
  572. }
  573. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  574. }
  575. #endregion
  576. #region 大灯开关
  577. //大灯关
  578. private void radioButton_Light_OFF_Click(object sender, EventArgs e)
  579. {
  580. var CtrlCode = new byte[2];
  581. if (radioButton_Gear_OFF.Checked == true)
  582. {
  583. CtrlCode[0] = 0x00;
  584. }
  585. else if (radioButton_Gear_ECO.Checked == true)
  586. {
  587. CtrlCode[0] = 0x01;
  588. }
  589. else if (radioButton_Gear_NORM.Checked == true)
  590. {
  591. CtrlCode[0] = 0x02;
  592. }
  593. else if (radioButton_Gear_SPORT.Checked == true)
  594. {
  595. CtrlCode[0] = 0x03;
  596. }
  597. else if (radioButton_Gear_TURBO.Checked == true)
  598. {
  599. CtrlCode[0] = 0x04;
  600. }
  601. else if (radioButton_Gear_SMART.Checked == true)
  602. {
  603. CtrlCode[0] = 0x33;
  604. }
  605. else if (radioButton_Gear_WALK.Checked == true)
  606. {
  607. CtrlCode[0] = 0x22;
  608. }
  609. CtrlCode[1] = 0xF0;
  610. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  611. }
  612. //大灯开
  613. private void radioButton_Light_ON_Click(object sender, EventArgs e)
  614. {
  615. var CtrlCode = new byte[2];
  616. if (radioButton_Gear_OFF.Checked == true)
  617. {
  618. CtrlCode[0] = 0x00;
  619. }
  620. else if (radioButton_Gear_ECO.Checked == true)
  621. {
  622. CtrlCode[0] = 0x01;
  623. }
  624. else if (radioButton_Gear_NORM.Checked == true)
  625. {
  626. CtrlCode[0] = 0x02;
  627. }
  628. else if (radioButton_Gear_SPORT.Checked == true)
  629. {
  630. CtrlCode[0] = 0x03;
  631. }
  632. else if (radioButton_Gear_TURBO.Checked == true)
  633. {
  634. CtrlCode[0] = 0x04;
  635. }
  636. else if (radioButton_Gear_SMART.Checked == true)
  637. {
  638. CtrlCode[0] = 0x33;
  639. }
  640. else if (radioButton_Gear_WALK.Checked == true)
  641. {
  642. CtrlCode[0] = 0x22;
  643. }
  644. CtrlCode[1] = 0xF1;
  645. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  646. }
  647. #endregion
  648. //数据清除指令
  649. private void button_ClearData_Click(object sender, EventArgs e)
  650. {
  651. var Code = new byte[5];
  652. Code[0] = (byte)'C';
  653. Code[1] = (byte)'L';
  654. Code[2] = (byte)'E';
  655. Code[3] = (byte)'A';
  656. Code[4] = (byte)'R';
  657. timer1.Enabled = false;
  658. if (MessageBox.Show("系统清除后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  659. {
  660. if (MessageBox.Show("请再次确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  661. {
  662. if (MessageBox.Show("请最后确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  663. {
  664. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2605, Code);
  665. }
  666. }
  667. }
  668. timer1.Enabled = true;
  669. }
  670. //查询版本信息
  671. private void button_Read_VerInfo_Click(object sender, EventArgs e)
  672. {
  673. textBox_Mode.Text = "---";
  674. textBox_SN.Text = "---";
  675. textBox_HW.Text = "---";
  676. textBox_FW.Text = "---";
  677. textBox_Firmware_Special.Text = "---";
  678. Class_Motor_Ver.Mode = "---";
  679. Class_Motor_Ver.SN = "---";
  680. Class_Motor_Ver.HW = "---";
  681. Class_Motor_Ver.FW = "---";
  682. Class_Motor_Ver.Special = "---";
  683. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
  684. textBox_TE_FW.Text = "---";
  685. SendCmd((ushort)0x651, (byte)0x11, (ushort)0x1200, null);
  686. }
  687. //串口数据接收
  688. private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  689. {
  690. try
  691. {
  692. Listening = true;
  693. int n = serialPort1.BytesToRead;
  694. byte[] buf = new byte[n];//将一次串口事件中接收到的数据暂存于buf中(注:对于超过8个字节的数据,C#的
  695. //串口接收事件将按每次处理8个字节的方式多次处理
  696. serialPort1.Read(buf, 0, n);//将接收到的数据读入buf
  697. this.Invoke((EventHandler)delegate
  698. {
  699. });
  700. buffer.AddRange(buf);//将读取的数据放入buffer中
  701. }
  702. catch (Exception ex)
  703. {
  704. MessageBox.Show(ex.Message);
  705. return;
  706. }
  707. finally
  708. {
  709. Listening = false;
  710. }
  711. }
  712. //time1定时任务
  713. private void timer1_Tick(object sender, EventArgs e)
  714. {
  715. //MC_RunInfo超时清除
  716. if (MC_RunInfo_Refresh == false)
  717. {
  718. MC_RunInfo_Refresh_Cnt++;
  719. if (MC_RunInfo_Refresh_Cnt > 50)
  720. {
  721. //运行信息清除
  722. MC_RunInfo_Clear();
  723. //传感器数值清除
  724. textBox_TorqueData.Text = "---";
  725. textBox_Torque_Stdev.Text = "---";
  726. textBox_Sensor1_Avg.Text = "---";
  727. textBox_Sensor1_Std.Text = "---";
  728. textBox_Sensor2_Avg.Text = "---";
  729. textBox_Sensor2_Std.Text = "---";
  730. textBox_Sensor3_Avg.Text = "---";
  731. textBox_Sensor3_Std.Text = "---";
  732. MC_RunInfo_Refresh_Cnt = 0;
  733. TorqueArray_Index = 0;
  734. }
  735. }
  736. else
  737. {
  738. MC_RunInfo_Refresh_Cnt = 0;
  739. }
  740. MC_RunInfo_Refresh = false;
  741. //MC故障信息超时清除
  742. if (MC_ErrorCode_Refresh == false)
  743. {
  744. MC_ErrorCode_Refresh_Cnt++;
  745. if (MC_ErrorCode_Refresh_Cnt > 50)
  746. {
  747. MC_ErrorCode_Clear();
  748. MC_ErrorCode_Refresh_Cnt = 0;
  749. }
  750. }
  751. else
  752. {
  753. MC_ErrorCode_Refresh_Cnt = 0;
  754. }
  755. MC_ErrorCode_Refresh = false;
  756. //MC骑行信息超时清除
  757. if (MC_RideInfo_Refresh == false)
  758. {
  759. MC_RideInfo_Refresh_Cnt++;
  760. if (MC_RideInfo_Refresh_Cnt > 50)
  761. {
  762. MC_RideInfo_Clear();
  763. MC_RideInfo_Refresh_Cnt = 0;
  764. }
  765. }
  766. else
  767. {
  768. MC_RideInfo_Refresh_Cnt = 0;
  769. }
  770. MC_RideInfo_Refresh = false;
  771. //MC姿态信息超时清除
  772. if (MC_AttitudeAngle_Refresh == false)
  773. {
  774. MC_AttitudeAngle_Refresh_Cnt++;
  775. if (MC_AttitudeAngle_Refresh_Cnt > 50)
  776. {
  777. MC_AttitudeAngle_Clear();
  778. MC_AttitudeAngle_Refresh_Cnt = 0;
  779. }
  780. }
  781. else
  782. {
  783. MC_AttitudeAngle_Refresh_Cnt = 0;
  784. }
  785. MC_AttitudeAngle_Refresh = false;
  786. //定时发送助力档位
  787. if (checkBox_AutoSend.Checked == true)
  788. {
  789. var CtrlCode = new byte[2];
  790. AutoSendTimeCnt++;
  791. if (AutoSendTimeCnt >= 10)
  792. {
  793. AutoSendTimeCnt = 0;
  794. //助力档位
  795. if (radioButton_Gear_OFF.Checked == true)
  796. {
  797. CtrlCode[0] = 0x00;
  798. }
  799. else if (radioButton_Gear_ECO.Checked == true)
  800. {
  801. CtrlCode[0] = 0x01;
  802. }
  803. else if (radioButton_Gear_NORM.Checked == true)
  804. {
  805. CtrlCode[0] = 0x02;
  806. }
  807. else if (radioButton_Gear_SPORT.Checked == true)
  808. {
  809. CtrlCode[0] = 0x03;
  810. }
  811. else if (radioButton_Gear_TURBO.Checked == true)
  812. {
  813. CtrlCode[0] = 0x04;
  814. }
  815. else if (radioButton_Gear_SMART.Checked == true)
  816. {
  817. CtrlCode[0] = 0x33;
  818. }
  819. else if (radioButton_Gear_WALK.Checked == true)
  820. {
  821. CtrlCode[0] = 0x22;
  822. }
  823. //大灯状态
  824. if (radioButton_Light_OFF.Checked == true)
  825. {
  826. CtrlCode[1] = 0xF0;
  827. }
  828. else if (radioButton_Light_ON.Checked == true)
  829. {
  830. CtrlCode[1] = 0xF1;
  831. }
  832. if (serialPort1.IsOpen)
  833. {
  834. if (!SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode))
  835. checkBox_AutoSend.Checked = false;
  836. }
  837. }
  838. }
  839. else
  840. {
  841. AutoSendTimeCnt = 0;
  842. }
  843. //定时查询姿态信息
  844. if (checkBox_ReadAttitudeAngleAutoSend.Checked == true)
  845. {
  846. AutoSendReadAttitudeAngleTimeCnt++;
  847. if (AutoSendReadAttitudeAngleTimeCnt >= 20)
  848. {
  849. AutoSendReadAttitudeAngleTimeCnt = 0;
  850. if (serialPort1.IsOpen)
  851. {
  852. if (!SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2F00, null))
  853. checkBox_ReadAttitudeAngleAutoSend.Checked = false;
  854. }
  855. }
  856. }
  857. else
  858. {
  859. AutoSendReadAttitudeAngleTimeCnt = 0;
  860. }
  861. //定时查询骑行信息
  862. if ( checkBox_ReadRideInfo.Checked == true)
  863. {
  864. AutoSendReadRideInfoTimeCnt++;
  865. if (AutoSendReadRideInfoTimeCnt >= 20)
  866. {
  867. AutoSendReadRideInfoTimeCnt = 0;
  868. if (serialPort1.IsOpen)
  869. {
  870. if (!SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2E00, null))
  871. checkBox_ReadRideInfo.Checked = false;
  872. }
  873. }
  874. }
  875. else
  876. {
  877. AutoSendReadRideInfoTimeCnt = 0;
  878. }
  879. //CDL在线检测超时判断
  880. if (CDL_Online_Flag == false)
  881. {
  882. CDL_OnlineCheck_Cnt++;
  883. if (CDL_OnlineCheck_Cnt > 10)
  884. {
  885. timer1.Enabled = false;
  886. MessageBox.Show("连接失败!", "提示");
  887. timer1.Enabled = true;
  888. //关闭串口
  889. serialPort1.Close();
  890. comboBox_ComIndex.Enabled = true;
  891. comboBox_Baudrate.Enabled = true;
  892. button_ComOpen.Text = "连接(&C)";
  893. g_blnIsOpen = false;
  894. timer1.Enabled = false;
  895. button_RefreshPort.Enabled = true;
  896. }
  897. }
  898. //...
  899. //串口解析
  900. byte DataNum = 0; //记录每条命令数据段的长度
  901. while (buffer.Count >= 13)//当buffer中的数据的个数大于等于4个时,即每一帧的数据长度所在位(第4位)出现时
  902. {
  903. if (buffer[0] == 0x55 && buffer[1] == 0xAA)//判断帧头
  904. {
  905. ushort PackageID;
  906. PackageID = (ushort)(buffer[2] * 256 + buffer[3]);
  907. if ((PackageID == 0x0710) || (PackageID == 0x0715) || (PackageID == 0x0610) || (PackageID == 0x0615) || (PackageID == 0x07FF))
  908. {
  909. int CmdLen = buffer[5];
  910. if (buffer.Count < CmdLen + 11) break;//如果接收到的数据没有达到一帧数据的指定长度, 则执行下次循环
  911. buffer.RemoveRange(0, 6);//数据达到要求长度后删去帧头,ID,模式,和命令长度
  912. while (CmdLen > 0) //读取命令段
  913. {
  914. DataNum = (byte)(buffer[1]);//命令字的第二个字节的表示数据位的长度
  915. if (DataNum <= (CmdLen - 2))
  916. {
  917. for (int i = 0; i < DataNum + 2; i++)
  918. {
  919. binary_data_1[i] = buffer[i];
  920. }
  921. DataCmdProcess(PackageID);//对于不同的命令段,做不同的处理
  922. buffer.RemoveRange(0, DataNum + 2);//移除处理过的命令段与数据段
  923. CmdLen -= (DataNum + 2);
  924. if (CmdLen < 2)
  925. {
  926. buffer.RemoveRange(0, CmdLen + 2);
  927. return;
  928. }
  929. }
  930. else
  931. {
  932. buffer.RemoveRange(0, CmdLen + 11);
  933. }
  934. }
  935. }
  936. else
  937. {
  938. buffer.RemoveAt(0);//ID不对
  939. }
  940. }
  941. else
  942. {
  943. buffer.RemoveAt(0);//帧头不对,删除帧头高字节
  944. }
  945. }
  946. }
  947. //故障转换和显示
  948. List<string> ErrorInfo = new List<string>{ "过流保护", "低压保护", "过压保护", "堵转保护","过热保护","速度传感器故障","力矩传感器故障","霍尔故障",
  949. "马达缺相","NTC故障","","","","MCU故障","踏频传感器故障","指拨故障","MOS短路","电压异常","电路故障","TE故障","TE电路故障","","","","","","","","","",
  950. "",""};
  951. private string ErrorInfoDisplay(UInt32 Code)
  952. {
  953. string Result = "";
  954. if (Code == 0)
  955. {
  956. Result = "无故障";
  957. }
  958. else
  959. {
  960. for (int i = 0; i < 32; i++)
  961. {
  962. if ((Code & 0x01) == 0x01)
  963. {
  964. Result += ErrorInfo[i] + " ";
  965. }
  966. Code >>= 1;
  967. }
  968. }
  969. return Result;
  970. }
  971. #region 命令段处理函数
  972. private void DataCmdProcess(ushort ID)
  973. {
  974. ushort CmdTemp = (ushort)(binary_data_1[0] * 256 + binary_data_1[1]);
  975. ushort DataTemp;
  976. short DataTemp_int16;
  977. textBox_RevCnt.Text = Convert.ToString(Convert.ToInt32(textBox_RevCnt.Text) + 1);
  978. #region 解析发送给CDL的指令
  979. if (ID == 0x7FF)
  980. {
  981. switch (CmdTemp)
  982. {
  983. case 0x1100://TE APP版本信息
  984. {
  985. unchecked
  986. {
  987. this.Invoke((EventHandler)(delegate
  988. {
  989. //CDL连接成功
  990. CDL_Online_Flag = true;
  991. timer1.Enabled = false;
  992. MessageBox.Show("连接成功!", "提示");
  993. timer1.Enabled = true;
  994. //显示其它界面
  995. foreach (Control c in this.Controls)
  996. {
  997. if (c is GroupBox)
  998. {
  999. if (c != groupBox1)
  1000. {
  1001. c.Visible = true;
  1002. }
  1003. }
  1004. }
  1005. button_ReadAll.Visible = true;
  1006. label1_Welcom.Visible = false;
  1007. }));
  1008. }
  1009. break;
  1010. }
  1011. default: break;
  1012. }
  1013. }
  1014. #endregion
  1015. #region 解析主控的命令
  1016. else if ((ID == 0x715) || (ID == 0x710))
  1017. {
  1018. switch (CmdTemp)
  1019. {
  1020. case 0xA318://力矩传感器零点值
  1021. {
  1022. unchecked
  1023. {
  1024. this.Invoke((EventHandler)(delegate
  1025. {
  1026. //三力矩传感器不用
  1027. }));
  1028. }
  1029. break;
  1030. }
  1031. case 0xAE1A://力矩传感器零点值,针对3力矩传感器使用
  1032. {
  1033. unchecked
  1034. {
  1035. this.Invoke((EventHandler)(delegate
  1036. {
  1037. if (binary_data_1[2] == 1)
  1038. {
  1039. DataTemp = (ushort)(binary_data_1[26] * 256 + binary_data_1[25]);
  1040. textBox_Sensor1_Zero.Text = Convert.ToString(DataTemp);
  1041. }
  1042. else if (binary_data_1[2] == 2)
  1043. {
  1044. DataTemp = (ushort)(binary_data_1[26] * 256 + binary_data_1[25]);
  1045. textBox_Sensor2_Zero.Text = Convert.ToString(DataTemp);
  1046. }
  1047. else if (binary_data_1[2] == 3)
  1048. {
  1049. DataTemp = (ushort)(binary_data_1[26] * 256 + binary_data_1[25]);
  1050. textBox_Sensor3_Zero.Text = Convert.ToString(DataTemp);
  1051. }
  1052. }));
  1053. }
  1054. break;
  1055. }
  1056. case 0xAA04://力矩传感器值校正参数
  1057. {
  1058. unchecked
  1059. {
  1060. this.Invoke((EventHandler)(delegate
  1061. {
  1062. //三力矩传感器不用
  1063. }));
  1064. }
  1065. break;
  1066. }
  1067. case 0xAF02://力矩传感器值校正参数,针对3力矩传感器使用
  1068. {
  1069. unchecked
  1070. {
  1071. this.Invoke((EventHandler)(delegate
  1072. {
  1073. if (binary_data_1[2] == 1)
  1074. {
  1075. DataTemp = (ushort)(binary_data_1[3]);
  1076. float K = (float)DataTemp / 100f;
  1077. textBox_Sensor1_K.Text = K.ToString("#0.00");
  1078. }
  1079. else if (binary_data_1[2] == 2)
  1080. {
  1081. DataTemp = (ushort)(binary_data_1[3]);
  1082. float K = (float)DataTemp / 100f;
  1083. textBox_Sensor2_K.Text = K.ToString("#0.00");
  1084. }
  1085. else if (binary_data_1[2] == 3)
  1086. {
  1087. DataTemp = (ushort)(binary_data_1[3]);
  1088. float K = (float)DataTemp / 100f;
  1089. textBox_Sensor3_K.Text = K.ToString("#0.00");
  1090. }
  1091. }));
  1092. }
  1093. break;
  1094. }
  1095. case 0xB002://力矩传感器值启动值
  1096. {
  1097. unchecked
  1098. {
  1099. this.Invoke((EventHandler)(delegate
  1100. {
  1101. DataTemp = (ushort)(binary_data_1[3] * 256 + binary_data_1[2]);
  1102. textBox_Torque_Start.Text = Convert.ToString(DataTemp);
  1103. }));
  1104. }
  1105. break;
  1106. }
  1107. case 0x1240://电机版本信息
  1108. {
  1109. unchecked
  1110. {
  1111. this.Invoke((EventHandler)(delegate
  1112. {
  1113. //电机型号
  1114. textBox_Mode.Text = "";
  1115. for (ushort i = 0; i < 16; i++)
  1116. {
  1117. if (binary_data_1[2 + i] == 0x2E)
  1118. {
  1119. break;
  1120. }
  1121. textBox_Mode.Text += ((char)binary_data_1[2 + i]).ToString();
  1122. }
  1123. Class_Motor_Ver.Mode = textBox_Mode.Text;
  1124. //电机SN
  1125. textBox_SN.Text = "";
  1126. for (ushort i = 0; i < 16; i++)
  1127. {
  1128. if (binary_data_1[18 + i] == 0x2E)
  1129. {
  1130. break;
  1131. }
  1132. textBox_SN.Text += ((char)binary_data_1[18 + i]).ToString();
  1133. }
  1134. Class_Motor_Ver.SN = textBox_SN.Text;
  1135. //电机HW
  1136. textBox_HW.Text = "";
  1137. for (ushort i = 0; i < 10; i++)//前10位是PCB版号,以'.'结束
  1138. {
  1139. if (binary_data_1[34 + i] == 0x2E)
  1140. {
  1141. break;
  1142. }
  1143. if (binary_data_1[34 + i] == 'r')
  1144. {
  1145. binary_data_1[34 + i] = (byte)'.';
  1146. }
  1147. textBox_HW.Text += ((char)binary_data_1[34 + i]).ToString();
  1148. }
  1149. switch (binary_data_1[34 + 10])//第10位代表MCU品牌
  1150. {
  1151. case (byte)'1':
  1152. textBox_HW.Text += " " + "ST" + " "; break;
  1153. case (byte)'2':
  1154. textBox_HW.Text += " " + "APM" + " "; break;
  1155. case (byte)'3':
  1156. textBox_HW.Text += " " + "HK" + " "; break;
  1157. case (byte)'4':
  1158. textBox_HW.Text += " " + "GD" + " "; break;
  1159. default:
  1160. textBox_HW.Text += " " + "NULL" + " "; break;
  1161. }
  1162. for (ushort i = 0; i < 4; i++)//后4位是MCU的ID校验码,用于识别板子的ID
  1163. {
  1164. textBox_HW.Text += Convert.ToString(binary_data_1[46 + i], 16).PadLeft(2, '0').ToUpper();//MCU ID 的CRC32值
  1165. }
  1166. Class_Motor_Ver.HW = textBox_HW.Text;
  1167. //电机FW
  1168. textBox_FW.Text = "";
  1169. for (ushort i = 0; i < 16; i++)
  1170. {
  1171. if (binary_data_1[50 + i] == 0x2E)
  1172. {
  1173. break;
  1174. }
  1175. if (binary_data_1[50 + i] == 'r')
  1176. {
  1177. binary_data_1[50 + i] = (byte)'.';
  1178. }
  1179. textBox_FW.Text += ((char)binary_data_1[50 + i]).ToString();
  1180. }
  1181. Class_Motor_Ver.FW = textBox_FW.Text;
  1182. }));
  1183. }
  1184. break;
  1185. }
  1186. case 0x1720://电机程序特性信息
  1187. {
  1188. unchecked
  1189. {
  1190. this.Invoke((EventHandler)(delegate
  1191. {
  1192. //程序特性
  1193. textBox_Firmware_Special.Text = "";
  1194. for (ushort i = 0; i < 32; i++)
  1195. {
  1196. if (binary_data_1[2 + i] == 0x2E)
  1197. {
  1198. break;
  1199. }
  1200. textBox_Firmware_Special.Text += ((char)binary_data_1[2 + i]).ToString();
  1201. }
  1202. Class_Motor_Ver.Mode = textBox_Firmware_Special.Text;
  1203. }));
  1204. }
  1205. break;
  1206. }
  1207. case 0x1020://电机运行信息
  1208. {
  1209. unchecked
  1210. {
  1211. this.Invoke((EventHandler)(delegate
  1212. {
  1213. MC_RunInfo_Refresh = true;
  1214. //车速
  1215. DataTemp = (ushort)(binary_data_1[3] * 256 + binary_data_1[2]);
  1216. textBox_RunInfo_BikeSpeed.Text = ((float)DataTemp / 10f).ToString("0.0") + " km/h";
  1217. //输出转速
  1218. DataTemp = (ushort)(binary_data_1[5] * 256 + binary_data_1[4]);
  1219. textBox_RunInfo_OutSpeed.Text = Convert.ToString(DataTemp) + " rpm";
  1220. Class_MotorSpeed.String = textBox_RunInfo_OutSpeed.Text;
  1221. Class_MotorSpeed.Data = DataTemp;
  1222. //电功率
  1223. DataTemp = (ushort)(binary_data_1[7] * 256 + binary_data_1[6]);
  1224. textBox_RunInfo_Power.Text = Convert.ToString(DataTemp * 2) + " W";
  1225. //母线电压
  1226. DataTemp = (ushort)(binary_data_1[9] * 256 + binary_data_1[8]);
  1227. textBox_RunInfo_Voltage.Text = Convert.ToString(DataTemp) + " mV";
  1228. //母线电流
  1229. DataTemp = (ushort)(binary_data_1[11] * 256 + binary_data_1[10]);
  1230. textBox_RunInfo_Current.Text = Convert.ToString(DataTemp) + " mA";
  1231. Class_CurrentData.String = textBox_RunInfo_Current.Text;
  1232. Class_CurrentData.Data = DataTemp;
  1233. //踏频
  1234. DataTemp = (ushort)(binary_data_1[12]);
  1235. textBox_RunInfo_Cadence.Text = Convert.ToString(DataTemp) + " rpm";
  1236. //踩踏力矩
  1237. DataTemp = (ushort)(binary_data_1[13]);
  1238. textBox_RunInfo_Torque.Text = Convert.ToString(DataTemp) + " N.m";
  1239. Class_TorqueData.String = textBox_RunInfo_Torque.Text;
  1240. Class_TorqueData.Data = DataTemp;
  1241. //踩踏方向
  1242. DataTemp = (ushort)(binary_data_1[14]);
  1243. if (DataTemp == 0)
  1244. {
  1245. textBox_RunInfo_Dir.Text = "正向";
  1246. }
  1247. else if (DataTemp == 1)
  1248. {
  1249. textBox_RunInfo_Dir.Text = "反向";
  1250. }
  1251. else if (DataTemp == 2)
  1252. {
  1253. textBox_RunInfo_Dir.Text = "停止";
  1254. }
  1255. //助力档位
  1256. DataTemp = (ushort)(binary_data_1[15]);
  1257. if (DataTemp == 0x00)
  1258. {
  1259. textBox_RunInfo_Gear.Text = "OFF";
  1260. radioButton_Gear_OFF.Checked = true;
  1261. }
  1262. else if (DataTemp == 0x01)
  1263. {
  1264. textBox_RunInfo_Gear.Text = "ECO";
  1265. radioButton_Gear_ECO.Checked = true;
  1266. }
  1267. else if (DataTemp == 0x02)
  1268. {
  1269. textBox_RunInfo_Gear.Text = "NORM";
  1270. radioButton_Gear_NORM.Checked = true;
  1271. }
  1272. else if (DataTemp == 0x03)
  1273. {
  1274. textBox_RunInfo_Gear.Text = "SPORT";
  1275. radioButton_Gear_SPORT.Checked = true;
  1276. }
  1277. else if (DataTemp == 0x04)
  1278. {
  1279. textBox_RunInfo_Gear.Text = "TURBO";
  1280. radioButton_Gear_TURBO.Checked = true;
  1281. }
  1282. else if (DataTemp == 0x33)
  1283. {
  1284. textBox_RunInfo_Gear.Text = "SMART";
  1285. radioButton_Gear_SMART.Checked = true;
  1286. }
  1287. else if (DataTemp == 0x22)
  1288. {
  1289. textBox_RunInfo_Gear.Text = "WALK";
  1290. radioButton_Gear_WALK.Checked = true;
  1291. }
  1292. //大灯状态
  1293. DataTemp = (ushort)(binary_data_1[16]);
  1294. if (DataTemp == 0xF0)
  1295. {
  1296. textBox_RunInfo_Light.Text = "OFF";
  1297. radioButton_Light_OFF.Checked = true;
  1298. }
  1299. else if (DataTemp == 0xF1)
  1300. {
  1301. textBox_RunInfo_Light.Text = "ON";
  1302. radioButton_Light_ON.Checked = true;
  1303. }
  1304. //剩余电量
  1305. DataTemp = (ushort)(binary_data_1[17]);
  1306. if (DataTemp == 0xFF)
  1307. {
  1308. textBox_RunInfo_SOC.Text = "---";
  1309. }
  1310. else
  1311. {
  1312. textBox_RunInfo_SOC.Text = Convert.ToString(DataTemp) + " %";
  1313. }
  1314. //剩余续航里程
  1315. DataTemp = (ushort)(binary_data_1[19] * 256 + binary_data_1[18]);
  1316. if ((DataTemp == 0xEEEE) || (DataTemp == 0xFFFF))
  1317. {
  1318. textBox_RunInfo_Range.Text = "---";
  1319. }
  1320. else
  1321. {
  1322. textBox_RunInfo_Range.Text = Convert.ToString(DataTemp) + " km";
  1323. }
  1324. //ODO里程
  1325. DataTemp = (ushort)(binary_data_1[21] * 256 + binary_data_1[20]);
  1326. textBox_RunInfo_ODO_Km.Text = Convert.ToString(DataTemp) + " km";
  1327. //平均功耗
  1328. DataTemp = (ushort)(binary_data_1[22]);
  1329. textBox_RunInfo_PowerAvg.Text = Convert.ToString(DataTemp * 10) + " mAh/km";
  1330. //PCB温度
  1331. DataTemp = (ushort)(binary_data_1[23]);
  1332. textBox_RunInfo_T_PCB.Text = Convert.ToString((int)(DataTemp - 40));
  1333. //绕组温度
  1334. DataTemp = (ushort)(binary_data_1[24]);
  1335. textBox_RunInfo_T_Roil.Text = Convert.ToString((int)(DataTemp - 40));
  1336. //MCU温度
  1337. DataTemp = (ushort)(binary_data_1[25]);
  1338. textBox_RunInfo_T_MCU.Text = Convert.ToString((int)(DataTemp - 40));
  1339. //单次里程
  1340. DataTemp = (ushort)(binary_data_1[27] * 256 + binary_data_1[26]);
  1341. textBox_RunInfo_Distance.Text = ((float)DataTemp / 10f).ToString("0.0") + " km";
  1342. //单次时间
  1343. DataTemp = (ushort)(binary_data_1[29] * 256 + binary_data_1[28]);
  1344. textBox_RunInfo_Time.Text = Convert.ToString(DataTemp) + " s";
  1345. //传感器1
  1346. DataTemp = (ushort)(binary_data_1[30]);
  1347. textBox_Sensor1.Text = Convert.ToString(DataTemp) + " N.m";
  1348. Class_Sensor1Data.String = textBox_Sensor1.Text;
  1349. Class_Sensor1Data.Data = DataTemp;
  1350. //传感器2
  1351. DataTemp = (ushort)(binary_data_1[31]);
  1352. textBox_Sensor2.Text = Convert.ToString(DataTemp) + " N.m";
  1353. Class_Sensor2Data.String = textBox_Sensor2.Text;
  1354. Class_Sensor2Data.Data = DataTemp;
  1355. //传感器3
  1356. DataTemp = (ushort)(binary_data_1[32]);
  1357. textBox_Sensor3.Text = Convert.ToString(DataTemp) + " N.m";
  1358. Class_Sensor3Data.String = textBox_Sensor3.Text;
  1359. Class_Sensor3Data.Data = DataTemp;
  1360. //计算传感器均值和标准差
  1361. TorqueArray[TorqueArray_Index] = binary_data_1[13];
  1362. Sensor1Array[TorqueArray_Index] = binary_data_1[30];
  1363. Sensor2Array[TorqueArray_Index] = binary_data_1[31];
  1364. Sensor3Array[TorqueArray_Index] = binary_data_1[32];
  1365. TorqueArray_Index++;
  1366. if (TorqueArray_Index >= 25)
  1367. {
  1368. textBox_TorqueData.Text = Convert.ToString(GetAvg(TorqueArray, 25)) + " N.m";
  1369. textBox_Torque_Stdev.Text = Convert.ToString(GetStDev(TorqueArray, 25));
  1370. textBox_Sensor1_Avg.Text = Convert.ToString(GetAvg(Sensor1Array, 25)) + " N.m";
  1371. textBox_Sensor1_Std.Text = Convert.ToString(GetStDev(Sensor1Array, 25));
  1372. textBox_Sensor2_Avg.Text = Convert.ToString(GetAvg(Sensor2Array, 25)) + " N.m";
  1373. textBox_Sensor2_Std.Text = Convert.ToString(GetStDev(Sensor2Array, 25));
  1374. textBox_Sensor3_Avg.Text = Convert.ToString(GetAvg(Sensor3Array, 25)) + " N.m";
  1375. textBox_Sensor3_Std.Text = Convert.ToString(GetStDev(Sensor3Array, 25));
  1376. TorqueArray_Index = 0;
  1377. }
  1378. //数据自动保存
  1379. DateAutoSave();
  1380. }));
  1381. }
  1382. break;
  1383. }
  1384. case 0xA020://用户参数
  1385. {
  1386. unchecked
  1387. {
  1388. this.Invoke((EventHandler)(delegate
  1389. {
  1390. //指拨模式
  1391. DataTemp = (ushort)(binary_data_1[2]);
  1392. textBox_Config_GasMode.Text = Convert.ToString(DataTemp, 16).PadLeft(2, '0').ToUpper();
  1393. //启动模式
  1394. DataTemp = (ushort)(binary_data_1[3]);
  1395. if (DataTemp == 0x01)
  1396. comboBox_StartMode.Text = "柔和";
  1397. else if (DataTemp == 0x02)
  1398. comboBox_StartMode.Text = "正常";
  1399. else if (DataTemp == 0x03)
  1400. comboBox_StartMode.Text = "强劲";
  1401. //停机时间
  1402. DataTemp = (ushort)(binary_data_1[5] * 256 + binary_data_1[4]);
  1403. textBox_Config_StopTime.Text = Convert.ToString(DataTemp);
  1404. //限速值
  1405. DataTemp = (ushort)(binary_data_1[6]);
  1406. textBox_Config_MaxSpeed.Text = Convert.ToString(DataTemp);
  1407. //下降速度
  1408. DataTemp = (ushort)(binary_data_1[7]);
  1409. if (DataTemp == 0x01)
  1410. comboBox_DownDec.Text = "超慢";
  1411. else if (DataTemp == 0x02)
  1412. comboBox_DownDec.Text = "慢";
  1413. else if (DataTemp == 0x03)
  1414. comboBox_DownDec.Text = "正常";
  1415. else if (DataTemp == 0x02)
  1416. comboBox_DownDec.Text = "快";
  1417. else if (DataTemp == 0x03)
  1418. comboBox_DownDec.Text = "超快";
  1419. //前飞
  1420. DataTemp = (ushort)(binary_data_1[8]);
  1421. textBox_Config_Teeth_F.Text = Convert.ToString(DataTemp);
  1422. //后飞
  1423. DataTemp = (ushort)(binary_data_1[9]);
  1424. textBox_Config_Teeth_B.Text = Convert.ToString(DataTemp);
  1425. //限流
  1426. DataTemp = (ushort)(binary_data_1[10]);
  1427. textBox_Config_MaxCurrent.Text = Convert.ToString(DataTemp);
  1428. //温度预警
  1429. DataTemp = (ushort)(binary_data_1[11]);
  1430. textBox_Config_T_Alarm.Text = Convert.ToString((Int16)(DataTemp - 40));
  1431. //温度保护
  1432. DataTemp = (ushort)(binary_data_1[12]);
  1433. textBox_Config_T_Protect.Text = Convert.ToString((Int16)(DataTemp - 40));
  1434. //无PBU标志
  1435. DataTemp = (ushort)(binary_data_1[13]);
  1436. if (DataTemp == 0x55)
  1437. {
  1438. comboBox_NoWatch.Text = "有";
  1439. }
  1440. else if (DataTemp == 0xAA)
  1441. {
  1442. comboBox_NoWatch.Text = "无";
  1443. }
  1444. //轮胎周长
  1445. DataTemp = (ushort)(binary_data_1[14]);
  1446. textBox_Config_WheelSize.Text = Convert.ToString(DataTemp);
  1447. //电机系列号
  1448. DataTemp = (ushort)(binary_data_1[15]);
  1449. comboBox_SerNum.Text = Convert.ToString(DataTemp);
  1450. //ECO助力增益
  1451. DataTemp = (ushort)(binary_data_1[16]);
  1452. textBox_Config_ECO_K.Text = Convert.ToString(DataTemp);
  1453. //ECO加速
  1454. DataTemp = (ushort)(binary_data_1[17]);
  1455. textBox_Config_ECO_Acc.Text = Convert.ToString(DataTemp);
  1456. //NORM助力增益
  1457. DataTemp = (ushort)(binary_data_1[18]);
  1458. textBox_Config_NORM_K.Text = Convert.ToString(DataTemp);
  1459. //NORM加速
  1460. DataTemp = (ushort)(binary_data_1[19]);
  1461. textBox_Config_NORM_Acc.Text = Convert.ToString(DataTemp);
  1462. //SPORT助力增益
  1463. DataTemp = (ushort)(binary_data_1[20]);
  1464. textBox_Config_SPORT_K.Text = Convert.ToString(DataTemp);
  1465. //SPORT加速
  1466. DataTemp = (ushort)(binary_data_1[21]);
  1467. textBox_Config_SPORT_Acc.Text = Convert.ToString(DataTemp);
  1468. //TURBO助力增益
  1469. DataTemp = (ushort)(binary_data_1[22]);
  1470. textBox_Config_TURBO_K.Text = Convert.ToString(DataTemp);
  1471. //TURBO加速
  1472. DataTemp = (ushort)(binary_data_1[23]);
  1473. textBox_Config_TURBO_Acc.Text = Convert.ToString(DataTemp);
  1474. //SMART助力增益
  1475. DataTemp = (ushort)(binary_data_1[24]);
  1476. textBox_Config_SMART_K.Text = Convert.ToString(DataTemp);
  1477. //SMART加速
  1478. DataTemp = (ushort)(binary_data_1[25]);
  1479. textBox_Config_SMART_Acc.Text = Convert.ToString(DataTemp);
  1480. //速度传感器信号个数
  1481. DataTemp = (ushort)(binary_data_1[26]);
  1482. textBox_Config_SpeedNum.Text = Convert.ToString(DataTemp);
  1483. //踏频启动信号个数
  1484. DataTemp = (ushort)(binary_data_1[27]);
  1485. textBox_Config_CadenceNum.Text = Convert.ToString(DataTemp);
  1486. //无速度传感器
  1487. DataTemp = (ushort)(binary_data_1[28]);
  1488. if (DataTemp == 0x55)
  1489. {
  1490. comboBox_SpeedSing.Text = "传感器";
  1491. }
  1492. else if (DataTemp == 0xAA)
  1493. {
  1494. comboBox_SpeedSing.Text = "踏频";
  1495. }
  1496. else if (DataTemp == 0xEE)
  1497. {
  1498. comboBox_SpeedSing.Text = "通信";
  1499. }
  1500. //周长微调
  1501. DataTemp_int16 = (short)binary_data_1[29];
  1502. textBox_Config_WheelAdj.Text = Convert.ToString((DataTemp_int16 >= 128) ? (DataTemp_int16 - 256) : DataTemp_int16);
  1503. //低压保护阈值
  1504. DataTemp = (ushort)(binary_data_1[31] * 256 + binary_data_1[30]);
  1505. textBox_Config_UV_Protect.Text = Convert.ToString(DataTemp);
  1506. //推行限速值
  1507. DataTemp = (ushort)(binary_data_1[32]);
  1508. textBox_Config_WalkSpeedLimit.Text = ((float)DataTemp / 10f).ToString("#0.0");
  1509. //推行马达转速
  1510. DataTemp = (ushort)(binary_data_1[33]);
  1511. textBox_Config_WalkMotorSpeed.Text = Convert.ToString(DataTemp);
  1512. }));
  1513. }
  1514. break;
  1515. }
  1516. case 0xA110://马达参数
  1517. {
  1518. unchecked
  1519. {
  1520. this.Invoke((EventHandler)(delegate
  1521. {
  1522. //额定功率
  1523. DataTemp = (ushort)(binary_data_1[3] * 256 + binary_data_1[2]);
  1524. textBox_Motor_Power.Text = Convert.ToString(DataTemp);
  1525. //额定转速
  1526. DataTemp = (ushort)(binary_data_1[5] * 256 + binary_data_1[4]);
  1527. textBox_Motor_Speed.Text = Convert.ToString(DataTemp);
  1528. //定子电阻
  1529. DataTemp = (ushort)(binary_data_1[7] * 256 + binary_data_1[6]);
  1530. textBox_Motor_R.Text = Convert.ToString(DataTemp);
  1531. //Lq
  1532. DataTemp = (ushort)(binary_data_1[9] * 256 + binary_data_1[8]);
  1533. textBox_Motor_Lq.Text = Convert.ToString(DataTemp);
  1534. //Ld
  1535. DataTemp = (ushort)(binary_data_1[11] * 256 + binary_data_1[10]);
  1536. textBox_Motor_Ld.Text = Convert.ToString(DataTemp);
  1537. //反电动势
  1538. DataTemp = (ushort)(binary_data_1[13] * 256 + binary_data_1[12]);
  1539. textBox_Motor_E.Text = Convert.ToString(DataTemp);
  1540. //额定电压
  1541. DataTemp = (ushort)(binary_data_1[14]);
  1542. textBox_Motor_Voltage.Text = Convert.ToString(DataTemp);
  1543. }));
  1544. }
  1545. break;
  1546. }
  1547. case 0xA230://历史信息1
  1548. {
  1549. unchecked
  1550. {
  1551. this.Invoke((EventHandler)(delegate
  1552. {
  1553. UInt32 DataTemp_Uint32;
  1554. ushort DataTemp_Uint16;
  1555. short DataTemp_Int16;
  1556. //开机次数
  1557. DataTemp_Uint32 = (UInt32)((binary_data_1[5] << 24) + (binary_data_1[4] << 16) + (binary_data_1[3] << 8) + binary_data_1[2]);
  1558. textBox_Runlog1_PowerCount.Text = Convert.ToString(DataTemp_Uint32);
  1559. //使用时间
  1560. DataTemp_Uint32 = (UInt32)((binary_data_1[9] << 24) + (binary_data_1[8] << 16) + (binary_data_1[7] << 8) + binary_data_1[6]);
  1561. textBox_Runlog1_RunTime.Text = Convert.ToString(DataTemp_Uint32);
  1562. //过流保护
  1563. DataTemp_Uint16 = (ushort)(binary_data_1[11] * 256 + binary_data_1[10]);
  1564. textBox_Runlog1_OC.Text = Convert.ToString(DataTemp_Uint16);
  1565. //低压保护
  1566. DataTemp_Uint16 = (ushort)(binary_data_1[13] * 256 + binary_data_1[12]);
  1567. textBox_Runlog1_UV.Text = Convert.ToString(DataTemp_Uint16);
  1568. //过压保护
  1569. DataTemp_Uint16 = (ushort)(binary_data_1[15] * 256 + binary_data_1[14]);
  1570. textBox_Runlog1_OV.Text = Convert.ToString(DataTemp_Uint16);
  1571. //堵转保护
  1572. DataTemp_Uint16 = (ushort)(binary_data_1[17] * 256 + binary_data_1[16]);
  1573. textBox_Runlog1_Lock.Text = Convert.ToString(DataTemp_Uint16);
  1574. //过热保护
  1575. DataTemp_Uint16 = (ushort)(binary_data_1[19] * 256 + binary_data_1[18]);
  1576. textBox_Runlog1_OT.Text = Convert.ToString(DataTemp_Uint16);
  1577. //速度传感器故障
  1578. DataTemp_Uint16 = (ushort)(binary_data_1[21] * 256 + binary_data_1[20]);
  1579. textBox_Runlog1_SPSFault.Text = Convert.ToString(DataTemp_Uint16);
  1580. //力矩传感器
  1581. DataTemp_Uint16 = (ushort)(binary_data_1[23] * 256 + binary_data_1[22]);
  1582. textBox_Runlog1_TQSFault.Text = Convert.ToString(DataTemp_Uint16);
  1583. //霍尔故障
  1584. DataTemp_Uint16 = (ushort)(binary_data_1[25] * 256 + binary_data_1[24]);
  1585. textBox_Runlog1_HallFault.Text = Convert.ToString(DataTemp_Uint16);
  1586. //马达缺相
  1587. DataTemp_Uint16 = (ushort)(binary_data_1[27] * 256 + binary_data_1[26]);
  1588. textBox_Runlog1_Phase.Text = Convert.ToString(DataTemp_Uint16);
  1589. //NTC故障
  1590. DataTemp_Uint16 = (ushort)(binary_data_1[29] * 256 + binary_data_1[28]);
  1591. textBox_Runlog1_NTCFault.Text = Convert.ToString(DataTemp_Uint16);
  1592. //BMS校验失败
  1593. DataTemp_Uint16 = (ushort)(binary_data_1[31] * 256 + binary_data_1[30]);
  1594. textBox_Runlog1_BMSFault.Text = Convert.ToString(DataTemp_Uint16);
  1595. //HMI校验失败
  1596. DataTemp_Uint16 = (ushort)(binary_data_1[33] * 256 + binary_data_1[32]);
  1597. textBox_Runlog1_HMIFault.Text = Convert.ToString(DataTemp_Uint16);
  1598. //PBU校验失败
  1599. DataTemp_Uint16 = (ushort)(binary_data_1[35] * 256 + binary_data_1[34]);
  1600. textBox_Runlog1_PBUFault.Text = Convert.ToString(DataTemp_Uint16);
  1601. //PCB最高温
  1602. DataTemp_Int16 = (short)(ushort)binary_data_1[36];
  1603. textBox_Runlog1_PCB_MAX.Text = Convert.ToString(DataTemp_Int16 - 40);
  1604. //PCB最低温
  1605. DataTemp_Int16 = (short)(ushort)binary_data_1[37];
  1606. textBox_Runlog1_PCB_MIN.Text = Convert.ToString(DataTemp_Int16 - 40);
  1607. //绕组最高温
  1608. DataTemp_Int16 = (short)(ushort)binary_data_1[42];
  1609. textBox_Runlog1_Coil_MAX.Text = Convert.ToString(DataTemp_Int16 - 40);
  1610. //绕组最低温
  1611. DataTemp_Int16 = (short)(ushort)binary_data_1[43];
  1612. textBox_Runlog1_Coil_MIN.Text = Convert.ToString(DataTemp_Int16 - 40);
  1613. //MCU最高温
  1614. DataTemp_Int16 = (short)(ushort)binary_data_1[44];
  1615. textBox_Runlog1_MCU_MAX.Text = Convert.ToString(DataTemp_Int16 - 40);
  1616. //MCU最低温
  1617. DataTemp_Int16 = (short)(ushort)binary_data_1[45];
  1618. textBox_Runlog1_MCU_MIN.Text = Convert.ToString(DataTemp_Int16 - 40);
  1619. }));
  1620. }
  1621. break;
  1622. }
  1623. case 0xAB09://存储器数据
  1624. case 0xAB0A:
  1625. case 0xAB0B:
  1626. case 0xAB0C:
  1627. {
  1628. unchecked
  1629. {
  1630. this.Invoke((EventHandler)(delegate
  1631. {
  1632. long ByteNum = 0;
  1633. 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;
  1634. if (ByteNum > 4) //数据长度超过4Bytes,按照16进制显示字节流
  1635. {
  1636. for (int i = 0; i < ByteNum; i++)
  1637. {
  1638. richTextBox_DebugInfo.AppendText(Convert.ToString(binary_data_1[10 + i], 16).PadLeft(2, '0').ToUpper() + " ");
  1639. }
  1640. }
  1641. else //根据设置自动转换10进制显示
  1642. {
  1643. if (radioButton_DebugDec.Checked == true) //10进制显示
  1644. {
  1645. switch (ByteNum)
  1646. {
  1647. case 1:
  1648. {
  1649. richTextBox_DebugInfo.AppendText(Convert.ToString(binary_data_1[10]) + ", ");
  1650. break;
  1651. }
  1652. case 2:
  1653. {
  1654. richTextBox_DebugInfo.AppendText(Convert.ToString(binary_data_1[10] + (binary_data_1[11] << 8)) + ", ");
  1655. break;
  1656. }
  1657. case 3:
  1658. {
  1659. richTextBox_DebugInfo.AppendText(Convert.ToString(binary_data_1[10] + (binary_data_1[11] << 8) + (binary_data_1[12] << 16)) + ", ");
  1660. break;
  1661. }
  1662. case 4:
  1663. {
  1664. richTextBox_DebugInfo.AppendText(Convert.ToString(binary_data_1[10] + (binary_data_1[11] << 8) + (binary_data_1[12] << 16) + (binary_data_1[13] << 24)) + ", ");
  1665. break;
  1666. }
  1667. default:
  1668. {
  1669. richTextBox_DebugInfo.AppendText("无效数据" + ", ");
  1670. break;
  1671. }
  1672. }
  1673. }
  1674. else if (radioButton_DebugHex.Checked == true)//16进制显示
  1675. {
  1676. switch (ByteNum)
  1677. {
  1678. case 1:
  1679. {
  1680. richTextBox_DebugInfo.AppendText("0x" + Convert.ToString(binary_data_1[10], 16).PadLeft(2, '0').ToUpper() + ", ");
  1681. break;
  1682. }
  1683. case 2:
  1684. {
  1685. richTextBox_DebugInfo.AppendText("0x" + Convert.ToString(binary_data_1[10] + (binary_data_1[11] << 8), 16).PadLeft(4, '0').ToUpper() + ", ");
  1686. break;
  1687. }
  1688. case 3:
  1689. {
  1690. 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() + ", ");
  1691. break;
  1692. }
  1693. case 4:
  1694. {
  1695. 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() + ", ");
  1696. break;
  1697. }
  1698. default:
  1699. {
  1700. richTextBox_DebugInfo.AppendText("无效数据" + ", ");
  1701. break;
  1702. }
  1703. }
  1704. }
  1705. }
  1706. }));
  1707. }
  1708. break;
  1709. }
  1710. case 0xAB88://故障日志
  1711. {
  1712. unchecked
  1713. {
  1714. this.Invoke((EventHandler)(delegate
  1715. {
  1716. //电机存储的故障日志,一包128字节包含2条故障日志
  1717. var ErrorLogSaveInfo1 = new MC_ErrorLogSaveInfo_Struct_t();
  1718. var ErrorLogSaveInfo2 = new MC_ErrorLogSaveInfo_Struct_t();
  1719. ErrorLogSaveInfo1 = MyConverter.ByteToStructure<MC_ErrorLogSaveInfo_Struct_t>(binary_data_1, 10);
  1720. ErrorLogSaveInfo2 = MyConverter.ByteToStructure<MC_ErrorLogSaveInfo_Struct_t>(binary_data_1, 74);
  1721. //数据存储
  1722. ErrorLogDateSave(ErrorLogSaveInfo1, ErrorLogSaveInfo2);
  1723. }));
  1724. }
  1725. break;
  1726. }
  1727. case 0xAC10://运行历史2
  1728. {
  1729. unchecked
  1730. {
  1731. this.Invoke((EventHandler)(delegate
  1732. {
  1733. ushort DataTemp_Uint16;
  1734. //MCU故障
  1735. DataTemp_Uint16 = (ushort)(binary_data_1[3] * 256 + binary_data_1[2]);
  1736. textBox_Runlog2_MCUFault.Text = Convert.ToString(DataTemp_Uint16);
  1737. //踏频传感器故障
  1738. DataTemp_Uint16 = (ushort)(binary_data_1[5] * 256 + binary_data_1[4]);
  1739. textBox_Runlog2_CDSFault.Text = Convert.ToString(DataTemp_Uint16);
  1740. //指拨故障
  1741. DataTemp_Uint16 = (ushort)(binary_data_1[7] * 256 + binary_data_1[6]);
  1742. textBox_Runlog2_GasFault.Text = Convert.ToString(DataTemp_Uint16);
  1743. //MOS短路
  1744. DataTemp_Uint16 = (ushort)(binary_data_1[9] * 256 + binary_data_1[8]);
  1745. textBox_Runlog2_MOS_SC.Text = Convert.ToString(DataTemp_Uint16);
  1746. //电压异常
  1747. DataTemp_Uint16 = (ushort)(binary_data_1[11] * 256 + binary_data_1[10]);
  1748. textBox_Runlog2_VolFault.Text = Convert.ToString(DataTemp_Uint16);
  1749. //电路异常
  1750. DataTemp_Uint16 = (ushort)(binary_data_1[13] * 256 + binary_data_1[12]);
  1751. textBox_Runlog2_CircuitFault.Text = Convert.ToString(DataTemp_Uint16);
  1752. //TE MCU异常
  1753. DataTemp_Uint16 = (ushort)(binary_data_1[15] * 256 + binary_data_1[14]);
  1754. textBox_Runlog2_TE_MCUFault.Text = Convert.ToString(DataTemp_Uint16);
  1755. //TE电路故障
  1756. DataTemp_Uint16 = (ushort)(binary_data_1[17] * 256 + binary_data_1[16]);
  1757. textBox_Runlog2_TE_CircuitFault.Text = Convert.ToString(DataTemp_Uint16);
  1758. }));
  1759. }
  1760. break;
  1761. }
  1762. case 0x1104://故障码
  1763. {
  1764. unchecked
  1765. {
  1766. this.Invoke((EventHandler)(delegate
  1767. {
  1768. MC_ErrorCode_Refresh = true;
  1769. textBox_RunInfo_ErrorCode.Text =
  1770. Convert.ToString(binary_data_1[5], 16).PadLeft(2, '0').ToUpper() +
  1771. Convert.ToString(binary_data_1[4], 16).PadLeft(2, '0').ToUpper() +
  1772. Convert.ToString(binary_data_1[3], 16).PadLeft(2, '0').ToUpper() +
  1773. Convert.ToString(binary_data_1[2], 16).PadLeft(2, '0').ToUpper() +
  1774. "H";
  1775. }));
  1776. }
  1777. break;
  1778. }
  1779. case 0xA903://反馈指令
  1780. {
  1781. unchecked
  1782. {
  1783. this.Invoke((EventHandler)(delegate
  1784. {
  1785. timer1.Enabled = false;
  1786. MessageBox.Show("OK", "反馈指令", MessageBoxButtons.OK, MessageBoxIcon.Information);
  1787. timer1.Enabled = true;
  1788. }));
  1789. }
  1790. break;
  1791. }
  1792. case 0xAD20://用户参数2
  1793. {
  1794. unchecked
  1795. {
  1796. this.Invoke((EventHandler)(delegate
  1797. {
  1798. short DataTemp_Int16;
  1799. sbyte DataTemp_byte;
  1800. //俯仰角零偏
  1801. DataTemp_Int16 = (short)(binary_data_1[3] * 256 + binary_data_1[2]);
  1802. textBox_Config_Angle_Pitch.Text = ((float)DataTemp_Int16 / 10f).ToString("0.0");
  1803. //横滚角零偏
  1804. DataTemp_Int16 = (short)(binary_data_1[5] * 256 + binary_data_1[4]);
  1805. textBox_Config_Angle_Roll.Text = ((float)DataTemp_Int16 / 10f).ToString("0.0");
  1806. //支持姿态传感器
  1807. DataTemp = (ushort)(binary_data_1[6]);
  1808. if (DataTemp == 0x55)
  1809. {
  1810. comboBox_AttiSensor.Text = "不支持";
  1811. }
  1812. else if (DataTemp == 0xAA)
  1813. {
  1814. comboBox_AttiSensor.Text = "支持";
  1815. }
  1816. //尾灯模式
  1817. DataTemp = (ushort)(binary_data_1[7]);
  1818. if (DataTemp == 0x01)
  1819. {
  1820. comboBox_TailLightMode.Text = "模式1";
  1821. }
  1822. else if (DataTemp == 0x02)
  1823. {
  1824. comboBox_TailLightMode.Text = "模式2";
  1825. }
  1826. else if (DataTemp == 0x03)
  1827. {
  1828. comboBox_TailLightMode.Text = "模式3";
  1829. }
  1830. else if (DataTemp == 0x04)
  1831. {
  1832. comboBox_TailLightMode.Text = "模式4";
  1833. }
  1834. else if (DataTemp == 0x05)
  1835. {
  1836. comboBox_TailLightMode.Text = "模式5";
  1837. }
  1838. else if (DataTemp == 0x06)
  1839. {
  1840. comboBox_TailLightMode.Text = "模式6";
  1841. }
  1842. else
  1843. {
  1844. comboBox_TailLightMode.Text = "0";
  1845. }
  1846. //前灯电压
  1847. DataTemp = (ushort)(binary_data_1[8]);
  1848. if (DataTemp == 0x06)
  1849. {
  1850. comboBox_FrontLigthVol.Text = "6V";
  1851. }
  1852. else if (DataTemp == 0x0C)
  1853. {
  1854. comboBox_FrontLigthVol.Text = "12V";
  1855. }
  1856. else if (DataTemp == 0xFF)
  1857. {
  1858. comboBox_FrontLigthVol.Text = "随电池";
  1859. }
  1860. else if (DataTemp == 0x00)
  1861. {
  1862. comboBox_FrontLigthVol.Text = "硬件";
  1863. }
  1864. else
  1865. {
  1866. comboBox_FrontLigthVol.Text = "无效";
  1867. }
  1868. //尾灯电压
  1869. DataTemp = (ushort)(binary_data_1[9]);
  1870. if (DataTemp == 0x06)
  1871. {
  1872. comboBox_TailLightVol.Text = "6V";
  1873. }
  1874. else if (DataTemp == 0x0C)
  1875. {
  1876. comboBox_TailLightVol.Text = "12V";
  1877. }
  1878. else if (DataTemp == 0xFF)
  1879. {
  1880. comboBox_TailLightVol.Text = "随电池";
  1881. }
  1882. else if (DataTemp == 0x00)
  1883. {
  1884. comboBox_TailLightVol.Text = "硬件";
  1885. }
  1886. else
  1887. {
  1888. comboBox_TailLightVol.Text = "无效";
  1889. }
  1890. //超速报警阈值
  1891. DataTemp = (ushort)(binary_data_1[11] * 256 + binary_data_1[10]);
  1892. textBox_Config_SpeedAlarm.Text = ((float)DataTemp / 10f).ToString("#0.0");
  1893. //尾灯脉宽
  1894. DataTemp = (ushort)(binary_data_1[13] * 256 + binary_data_1[12]);
  1895. textBox_Config_Pulse.Text = Convert.ToString(DataTemp);
  1896. //尾灯周期
  1897. DataTemp = (ushort)(binary_data_1[15] * 256 + binary_data_1[14]);
  1898. textBox_Config_Period.Text = Convert.ToString(DataTemp);
  1899. //起始限速
  1900. DataTemp_byte = (sbyte)(binary_data_1[16]);
  1901. textBox_Config_SpeedlimitStart.Text = ((float)(DataTemp_byte) / 10f).ToString("#0.0");
  1902. //结束限速
  1903. DataTemp_byte = (sbyte)(binary_data_1[17]);
  1904. textBox_Config_SpeedlimitStop.Text = ((float)DataTemp_byte / 10f).ToString("#0.0");
  1905. }));
  1906. }
  1907. break;
  1908. }
  1909. case 0x1510:
  1910. {
  1911. unchecked
  1912. {
  1913. this.Invoke((EventHandler)(delegate
  1914. {
  1915. long DataTemp32;
  1916. MC_RideInfo_Refresh = true;
  1917. //ODO里程
  1918. DataTemp32 = (long)((binary_data_1[5] << 24) + (binary_data_1[4] << 16) + (binary_data_1[3] << 8) + binary_data_1[2]);
  1919. textBox_RideInfo_ODO_Km.Text = ((float)DataTemp32 / 10f).ToString("0.0") + " km";
  1920. //ODO时间
  1921. DataTemp32 = (long)((binary_data_1[9] << 24) + (binary_data_1[8] << 16) + (binary_data_1[7] << 8) + binary_data_1[6]);
  1922. if (DataTemp32 >= 60 * 1000)
  1923. textBox_RideInfo_ODO_Time.Text = Convert.ToString(DataTemp32 / 60) + " h";
  1924. else
  1925. textBox_RideInfo_ODO_Time.Text = Convert.ToString(DataTemp32 / 60) + " h " + Convert.ToString(DataTemp32 % 60) + " m";
  1926. //TRIP里程
  1927. DataTemp32 = (long)((binary_data_1[13] << 24) + (binary_data_1[12] << 16) + (binary_data_1[11] << 8) + binary_data_1[10]);
  1928. textBox_RideInfo_Trip_Km.Text = ((float)DataTemp32 / 10f).ToString("0.0") + " km";
  1929. //TRIP时间
  1930. DataTemp32 = (long)((binary_data_1[17] << 24) + (binary_data_1[16] << 16) + (binary_data_1[15] << 8) + binary_data_1[14]);
  1931. if (DataTemp32 >= 60 * 1000)
  1932. textBox_RideInfo_Trip_Time.Text = Convert.ToString(DataTemp32 / 60) + " h";
  1933. else
  1934. textBox_RideInfo_Trip_Time.Text = Convert.ToString(DataTemp32 / 60) + " h " + Convert.ToString(DataTemp32 % 60) + " m";
  1935. }));
  1936. }
  1937. break;
  1938. }
  1939. case 0x1608://姿态信息
  1940. {
  1941. unchecked
  1942. {
  1943. this.Invoke((EventHandler)(delegate
  1944. {
  1945. short DataTemp_Int16;
  1946. MC_AttitudeAngle_Refresh = true;
  1947. //俯仰角度绝对
  1948. DataTemp_Int16 = (short)(binary_data_1[3] * 256 + binary_data_1[2]);
  1949. textBox_Attitude_PitchAngle_Abs.Text = ((float)DataTemp_Int16 / 10f).ToString("0.0");
  1950. //横滚角度绝对
  1951. DataTemp_Int16 = (short)(binary_data_1[5] * 256 + binary_data_1[4]);
  1952. textBox_Attitude_RollAngle_Abs.Text = ((float)DataTemp_Int16 / 10f).ToString("0.0");
  1953. //俯仰角度相对
  1954. DataTemp_Int16 = (short)(binary_data_1[7] * 256 + binary_data_1[6]);
  1955. textBox_Attitude_PitchAngle_Rel.Text = ((float)DataTemp_Int16 / 10f).ToString("0.0");
  1956. //横滚角度相对
  1957. DataTemp_Int16 = (short)(binary_data_1[9] * 256 + binary_data_1[8]);
  1958. textBox_Attitude_RollAngle_Rel.Text = ((float)DataTemp_Int16 / 10f).ToString("0.0");
  1959. }));
  1960. }
  1961. break;
  1962. }
  1963. default: break;
  1964. }
  1965. //存储器导出数据显示
  1966. if ((CmdTemp & 0xFF00) == 0xAB00)
  1967. {
  1968. unchecked
  1969. {
  1970. this.Invoke((EventHandler)(delegate
  1971. {
  1972. long ByteNum = 0;
  1973. 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;
  1974. if (ByteNum > 4) //数据长度超过4Bytes,按照16进制显示字节流
  1975. {
  1976. for (int i = 0; i < ByteNum; i++)
  1977. {
  1978. richTextBox_DebugInfo.AppendText(Convert.ToString(binary_data_1[10 + i], 16).PadLeft(2, '0').ToUpper() + " ");
  1979. }
  1980. }
  1981. }));
  1982. }
  1983. }
  1984. }
  1985. #endregion
  1986. #region 解析MC TE的命令
  1987. else if ((ID == 0x615) || (ID == 0x610))
  1988. {
  1989. switch (CmdTemp)
  1990. {
  1991. case 0x1240://TE APP版本信息
  1992. {
  1993. unchecked
  1994. {
  1995. this.Invoke((EventHandler)(delegate
  1996. {
  1997. //电机FW
  1998. textBox_TE_FW.Text = "";
  1999. for (ushort i = 0; i < 16; i++)
  2000. {
  2001. if (binary_data_1[50 + i] == 0x2E)
  2002. {
  2003. break;
  2004. }
  2005. if (binary_data_1[50 + i] == 'r')
  2006. {
  2007. binary_data_1[50 + i] = (byte)'.';
  2008. }
  2009. textBox_TE_FW.Text += ((char)binary_data_1[50 + i]).ToString();
  2010. }
  2011. }));
  2012. }
  2013. break;
  2014. }
  2015. default: break;
  2016. }
  2017. #endregion
  2018. }
  2019. }
  2020. #endregion
  2021. //用户参数显示清除
  2022. private void MC_Config_Clear()
  2023. {
  2024. foreach (Control c in groupBox10.Controls)
  2025. {
  2026. if ((c is TextBox) || (c is ComboBox))
  2027. {
  2028. c.Text = "---";
  2029. }
  2030. }
  2031. }
  2032. //用户参数2清除
  2033. private void MC_Config_Ext_Clear()
  2034. {
  2035. foreach (Control c in groupBox4.Controls)
  2036. {
  2037. if ((c is TextBox) || (c is ComboBox))
  2038. {
  2039. c.Text = "---";
  2040. }
  2041. }
  2042. }
  2043. //马达参数显示清除
  2044. private void Motor_Config_Clear()
  2045. {
  2046. foreach (Control c in groupBox11.Controls)
  2047. {
  2048. if (c is TextBox)
  2049. {
  2050. c.Text = "---";
  2051. }
  2052. }
  2053. }
  2054. //电机运行信息清除
  2055. private void MC_RunInfo_Clear()
  2056. {
  2057. foreach (Control c in groupBox3.Controls)
  2058. {
  2059. if (c is TextBox)
  2060. {
  2061. c.Text = "---";
  2062. }
  2063. }
  2064. }
  2065. //姿态传感器信息清除
  2066. private void MC_AttitudeAngle_Clear()
  2067. {
  2068. foreach (Control c in groupBox13.Controls)
  2069. {
  2070. if (c is TextBox)
  2071. {
  2072. c.Text = "---";
  2073. }
  2074. }
  2075. }
  2076. //骑行信息清除
  2077. private void MC_RideInfo_Clear()
  2078. {
  2079. foreach (Control c in groupBox12.Controls)
  2080. {
  2081. if (c is TextBox)
  2082. {
  2083. c.Text = "---";
  2084. }
  2085. }
  2086. }
  2087. //历史信息清除
  2088. private void MC_RunLog_Clear()
  2089. {
  2090. foreach (Control c in groupBox14.Controls)
  2091. {
  2092. if (c is TextBox)
  2093. {
  2094. c.Text = "---";
  2095. }
  2096. }
  2097. }
  2098. //电机故障码显示清除
  2099. private void MC_ErrorCode_Clear()
  2100. {
  2101. //故障码
  2102. textBox_RunInfo_ErrorCode.Text = "---";
  2103. }
  2104. //接收计数清零
  2105. private void label58_Click(object sender, EventArgs e)
  2106. {
  2107. textBox_RevCnt.Text = "0";
  2108. }
  2109. //读取用户参数
  2110. private void button_Read_Config_Click(object sender, EventArgs e)
  2111. {
  2112. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1A00, null);
  2113. MC_Config_Clear();
  2114. }
  2115. //写入用户参数
  2116. private void button_Write_Config_Click(object sender, EventArgs e)
  2117. {
  2118. var ConfigParam = new byte[32];
  2119. for (int i = 0; i < 32; i++)
  2120. ConfigParam[i] = 0;
  2121. ushort DataTemp;
  2122. //指拨参数
  2123. ConfigParam[0] = byte.Parse(textBox_Config_GasMode.Text, System.Globalization.NumberStyles.HexNumber);
  2124. //启动模式
  2125. if (comboBox_StartMode.Text == "柔和")
  2126. ConfigParam[1] = 0x01;
  2127. else if (comboBox_StartMode.Text == "强劲")
  2128. ConfigParam[1] = 0x03;
  2129. else
  2130. ConfigParam[1] = 0x02;
  2131. //停机时间
  2132. DataTemp = Convert.ToUInt16(textBox_Config_StopTime.Text);
  2133. ConfigParam[2] = (byte)(DataTemp & 0xFF);
  2134. ConfigParam[3] = (byte)(DataTemp >> 8);
  2135. //限速
  2136. ConfigParam[4] = Convert.ToByte(textBox_Config_MaxSpeed.Text);
  2137. //下降速度
  2138. if (comboBox_DownDec.Text == "超慢")
  2139. ConfigParam[5] = 0x01;
  2140. else if (comboBox_DownDec.Text == "慢")
  2141. ConfigParam[5] = 0x02;
  2142. else if (comboBox_DownDec.Text == "快")
  2143. ConfigParam[5] = 0x04;
  2144. else if (comboBox_DownDec.Text == "超快")
  2145. ConfigParam[5] = 0x05;
  2146. else
  2147. ConfigParam[5] = 0x03;
  2148. //前飞
  2149. ConfigParam[6] = Convert.ToByte(textBox_Config_Teeth_F.Text);
  2150. //后飞
  2151. ConfigParam[7] = Convert.ToByte(textBox_Config_Teeth_B.Text);
  2152. //限流
  2153. ConfigParam[8] = Convert.ToByte(textBox_Config_MaxCurrent.Text);
  2154. //温度预警
  2155. ConfigParam[9] = (byte)(Convert.ToByte(textBox_Config_T_Alarm.Text) + 40);
  2156. //温度保护
  2157. ConfigParam[10] = (byte)(Convert.ToByte(textBox_Config_T_Protect.Text) + 40);
  2158. //无PBU
  2159. if (comboBox_NoWatch.Text == "无")
  2160. ConfigParam[11] = 0xAA;
  2161. else
  2162. ConfigParam[11] = 0x55;
  2163. //轮胎周长
  2164. ConfigParam[12] = Convert.ToByte(textBox_Config_WheelSize.Text);
  2165. //电机系列号
  2166. ConfigParam[13] = Convert.ToByte(comboBox_SerNum.Text);
  2167. //ECO助力增益
  2168. ConfigParam[14] = Convert.ToByte(textBox_Config_ECO_K.Text);
  2169. //ECO加速
  2170. ConfigParam[15] = Convert.ToByte(textBox_Config_ECO_Acc.Text);
  2171. //NORM助力增益
  2172. ConfigParam[16] = Convert.ToByte(textBox_Config_NORM_K.Text);
  2173. //NORM加速
  2174. ConfigParam[17] = Convert.ToByte(textBox_Config_NORM_Acc.Text);
  2175. //SPORT助力增益
  2176. ConfigParam[18] = Convert.ToByte(textBox_Config_SPORT_K.Text);
  2177. //SPORT加速
  2178. ConfigParam[19] = Convert.ToByte(textBox_Config_SPORT_Acc.Text);
  2179. //TURBO助力增益
  2180. ConfigParam[20] = Convert.ToByte(textBox_Config_TURBO_K.Text);
  2181. //TURBO加速
  2182. ConfigParam[21] = Convert.ToByte(textBox_Config_TURBO_Acc.Text);
  2183. //SMART助力增益
  2184. ConfigParam[22] = Convert.ToByte(textBox_Config_SMART_K.Text);
  2185. //SMART加速
  2186. ConfigParam[23] = Convert.ToByte(textBox_Config_SMART_Acc.Text);
  2187. //速度传感器信号个数
  2188. ConfigParam[24] = Convert.ToByte(textBox_Config_SpeedNum.Text);
  2189. //踏频启动信号个数
  2190. ConfigParam[25] = Convert.ToByte(textBox_Config_CadenceNum.Text);
  2191. //速度传感器
  2192. if (comboBox_SpeedSing.Text == "踏频")
  2193. ConfigParam[26] = 0xAA;
  2194. else if (comboBox_SpeedSing.Text == "通信")
  2195. ConfigParam[26] = 0xEE;
  2196. else
  2197. ConfigParam[26] = 0x55;
  2198. //周长微调
  2199. ConfigParam[27] = (byte)Convert.ToInt16(textBox_Config_WheelAdj.Text);
  2200. //低压保护阈值
  2201. DataTemp = Convert.ToUInt16(textBox_Config_UV_Protect.Text);
  2202. ConfigParam[28] = (byte)(DataTemp & 0xFF);
  2203. ConfigParam[29] = (byte)(DataTemp >> 8);
  2204. //推行限速
  2205. DataTemp = (ushort)(decimal.Parse(textBox_Config_WalkSpeedLimit.Text) * 10);
  2206. ConfigParam[30] = (byte)Convert.ToByte(DataTemp);
  2207. //推行马达转速
  2208. ConfigParam[31] = (byte)Convert.ToByte(textBox_Config_WalkMotorSpeed.Text);
  2209. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1B20, ConfigParam);
  2210. }
  2211. //读取马达参数
  2212. private void button_Read_MortoConfig_Click(object sender, EventArgs e)
  2213. {
  2214. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1C00, null);
  2215. Motor_Config_Clear();
  2216. }
  2217. //写入马达参数
  2218. private void button_Write_MotorConfig_Click(object sender, EventArgs e)
  2219. {
  2220. var ConfigParam = new byte[16];
  2221. for (int i = 0; i < 16; i++)
  2222. ConfigParam[i] = 0;
  2223. ushort DataTemp;
  2224. //额定功率
  2225. DataTemp = Convert.ToUInt16(textBox_Motor_Power.Text);
  2226. ConfigParam[0] = (byte)(DataTemp & 0xFF);
  2227. ConfigParam[1] = (byte)(DataTemp >> 8);
  2228. //额定转速
  2229. DataTemp = Convert.ToUInt16(textBox_Motor_Speed.Text);
  2230. ConfigParam[2] = (byte)(DataTemp & 0xFF);
  2231. ConfigParam[3] = (byte)(DataTemp >> 8);
  2232. //定子电阻
  2233. DataTemp = Convert.ToUInt16(textBox_Motor_R.Text);
  2234. ConfigParam[4] = (byte)(DataTemp & 0xFF);
  2235. ConfigParam[5] = (byte)(DataTemp >> 8);
  2236. //Lq
  2237. DataTemp = Convert.ToUInt16(textBox_Motor_Lq.Text);
  2238. ConfigParam[6] = (byte)(DataTemp & 0xFF);
  2239. ConfigParam[7] = (byte)(DataTemp >> 8);
  2240. //Ld
  2241. DataTemp = Convert.ToUInt16(textBox_Motor_Ld.Text);
  2242. ConfigParam[8] = (byte)(DataTemp & 0xFF);
  2243. ConfigParam[9] = (byte)(DataTemp >> 8);
  2244. //反电动势
  2245. DataTemp = Convert.ToUInt16(textBox_Motor_E.Text);
  2246. ConfigParam[10] = (byte)(DataTemp & 0xFF);
  2247. ConfigParam[11] = (byte)(DataTemp >> 8);
  2248. //额定电压
  2249. ConfigParam[12] = Convert.ToByte(textBox_Motor_Voltage.Text);
  2250. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1D10, ConfigParam);
  2251. }
  2252. //写入电机型号
  2253. private void button_Write_Mode_Click(object sender, EventArgs e)
  2254. {
  2255. string Mode = textBox_Mode.Text;
  2256. var ModeArray = new byte[16];
  2257. for (ushort i = 0; i < Mode.Length; i++)
  2258. {
  2259. ModeArray[i] = (byte)Mode[i];
  2260. }
  2261. if (Mode.Length < 16)
  2262. {
  2263. ModeArray[Mode.Length] = (byte)'.';
  2264. for (ushort i = 0; i < 16 - Mode.Length - 1; i++)
  2265. {
  2266. ModeArray[Mode.Length + 1 + i] = 0x20;
  2267. }
  2268. }
  2269. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2210, ModeArray);
  2270. }
  2271. //写入电机SN
  2272. private void button_Write_SN_Click(object sender, EventArgs e)
  2273. {
  2274. string SN = textBox_SN.Text;
  2275. var SNArray = new byte[16];
  2276. for (ushort i = 0; i < SN.Length; i++)
  2277. {
  2278. SNArray[i] = (byte)SN[i];
  2279. }
  2280. if (SN.Length < 16)
  2281. {
  2282. SNArray[SN.Length] = (byte)'.';
  2283. for (ushort i = 0; i < 16 - SN.Length - 1; i++)
  2284. {
  2285. SNArray[SN.Length + 1 + i] = 0x20;
  2286. }
  2287. }
  2288. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2310, SNArray);
  2289. }
  2290. //系统复位
  2291. private void button_Reset_Click(object sender, EventArgs e)
  2292. {
  2293. var Code = new byte[5];
  2294. Code[0] = (byte)'R';
  2295. Code[1] = (byte)'E';
  2296. Code[2] = (byte)'S';
  2297. Code[3] = (byte)'E';
  2298. Code[4] = (byte)'T';
  2299. timer1.Enabled = false;
  2300. if (MessageBox.Show("系统将重启", "确认重启?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2301. {
  2302. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2505, Code);
  2303. }
  2304. timer1.Enabled = true;
  2305. }
  2306. private void button_RefreshPort_Click(object sender, EventArgs e)
  2307. {
  2308. string[] ports = SerialPort.GetPortNames();
  2309. Array.Sort(ports);
  2310. comboBox_ComIndex.Items.Clear();
  2311. comboBox_ComIndex.Items.AddRange(ports);
  2312. comboBox_ComIndex.SelectedIndex = comboBox_ComIndex.Items.Count > 0 ? 0 : -1;
  2313. }
  2314. private void checkBox_AutoSend_CheckedChanged(object sender, EventArgs e)
  2315. {
  2316. if (checkBox_AutoSend.Checked == false)
  2317. {
  2318. radioButton_Gear_ECO.Enabled = true;
  2319. radioButton_Gear_NORM.Enabled = true;
  2320. radioButton_Gear_SPORT.Enabled = true;
  2321. radioButton_Gear_TURBO.Enabled = true;
  2322. radioButton_Gear_WALK.Enabled = true;
  2323. radioButton_Gear_SMART.Enabled = true;
  2324. radioButton_Gear_OFF.Enabled = true;
  2325. }
  2326. else
  2327. {
  2328. radioButton_Gear_ECO.Enabled = false;
  2329. radioButton_Gear_NORM.Enabled = false;
  2330. radioButton_Gear_SPORT.Enabled = false;
  2331. radioButton_Gear_TURBO.Enabled = false;
  2332. radioButton_Gear_WALK.Enabled = false;
  2333. radioButton_Gear_SMART.Enabled = false;
  2334. radioButton_Gear_OFF.Enabled = false;
  2335. }
  2336. }
  2337. private void button_Read_Config_Ext_Click(object sender, EventArgs e)
  2338. {
  2339. MC_Config_Ext_Clear();
  2340. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3000, null);
  2341. }
  2342. private void button_Write_Config_Ext_Click(object sender, EventArgs e)
  2343. {
  2344. var ConfigParam = new byte[32];
  2345. for (int i = 0; i < 32; i++)
  2346. ConfigParam[i] = 0;
  2347. short DataTemp_Int16;
  2348. ushort DataTemp;
  2349. //俯仰角零偏
  2350. DataTemp_Int16 = (short)(decimal.Parse(textBox_Config_Angle_Pitch.Text) * 10);
  2351. ConfigParam[0] = (byte)(DataTemp_Int16 & 0xFF);
  2352. ConfigParam[1] = (byte)(DataTemp_Int16 >> 8);
  2353. //横滚角零偏
  2354. DataTemp_Int16 = (short)(decimal.Parse(textBox_Config_Angle_Roll.Text) * 10);
  2355. ConfigParam[2] = (byte)(DataTemp_Int16 & 0xFF);
  2356. ConfigParam[3] = (byte)(DataTemp_Int16 >> 8);
  2357. //支持姿态传感器
  2358. if (comboBox_AttiSensor.Text == "支持")
  2359. ConfigParam[4] = 0xAA;
  2360. else
  2361. ConfigParam[4] = 0x55;
  2362. //尾灯模式
  2363. if (comboBox_TailLightMode.Text == "模式1")
  2364. {
  2365. ConfigParam[5] = 0x01;
  2366. }
  2367. else if (comboBox_TailLightMode.Text == "模式3")
  2368. {
  2369. ConfigParam[5] = 0x03;
  2370. }
  2371. else if (comboBox_TailLightMode.Text == "模式4")
  2372. {
  2373. ConfigParam[5] = 0x04;
  2374. }
  2375. else if (comboBox_TailLightMode.Text == "模式5")
  2376. {
  2377. ConfigParam[5] = 0x05;
  2378. }
  2379. else if (comboBox_TailLightMode.Text == "模式6")
  2380. {
  2381. ConfigParam[5] = 0x06;
  2382. }
  2383. else
  2384. ConfigParam[5] = 0x02;
  2385. //前灯电压
  2386. if (comboBox_FrontLigthVol.Text == "6V")
  2387. ConfigParam[6] = 0x06;
  2388. else if (comboBox_FrontLigthVol.Text == "12V")
  2389. ConfigParam[6] = 0x0C;
  2390. else if (comboBox_FrontLigthVol.Text == "随电池")
  2391. ConfigParam[6] = 0xFF;
  2392. else
  2393. ConfigParam[6] = 0x00;
  2394. //尾灯电压
  2395. if (comboBox_TailLightVol.Text == "6V")
  2396. ConfigParam[7] = 0x06;
  2397. else if (comboBox_TailLightVol.Text == "12V")
  2398. ConfigParam[7] = 0x0C;
  2399. else if (comboBox_TailLightVol.Text == "随电池")
  2400. ConfigParam[7] = 0xFF;
  2401. else
  2402. ConfigParam[7] = 0x00;
  2403. //超速报警阈值
  2404. DataTemp = (ushort)(decimal.Parse(textBox_Config_SpeedAlarm.Text) * 10);
  2405. ConfigParam[8] = (byte)(DataTemp & 0xFF);
  2406. ConfigParam[9] = (byte)(DataTemp >> 8);
  2407. //尾灯脉宽
  2408. DataTemp = Convert.ToUInt16(textBox_Config_Pulse.Text);
  2409. ConfigParam[10] = (byte)(DataTemp & 0xFF);
  2410. ConfigParam[11] = (byte)(DataTemp >> 8);
  2411. //尾灯周期
  2412. DataTemp = Convert.ToUInt16(textBox_Config_Period.Text);
  2413. ConfigParam[12] = (byte)(DataTemp & 0xFF);
  2414. ConfigParam[13] = (byte)(DataTemp >> 8);
  2415. //起始限速
  2416. DataTemp_Int16 = (short)(decimal.Parse(textBox_Config_SpeedlimitStart.Text) * 10);
  2417. ConfigParam[14] = (byte)(DataTemp_Int16 & 0xFF);
  2418. //结束限速
  2419. DataTemp_Int16 = (short)(decimal.Parse(textBox_Config_SpeedlimitStop.Text) * 10);
  2420. ConfigParam[15] = (byte)(DataTemp_Int16 & 0xFF);
  2421. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3120, ConfigParam);
  2422. }
  2423. private void button_Read_AttitudeAngle_Click(object sender, EventArgs e)
  2424. {
  2425. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2F00, null);
  2426. MC_AttitudeAngle_Clear();
  2427. }
  2428. private void button_Read_RidedInfo_Click(object sender, EventArgs e)
  2429. {
  2430. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2E00, null);
  2431. MC_RideInfo_Clear();
  2432. }
  2433. private void button_ReadRunLog_Click(object sender, EventArgs e)
  2434. {
  2435. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1E00, null);
  2436. MC_RunLog_Clear();
  2437. }
  2438. private void timer2_Tick(object sender, EventArgs e)
  2439. {
  2440. //定时显示系统时钟
  2441. label_DateTime.Text = DateTime.Now.ToString();
  2442. //定时读取存储器
  2443. if (checkBox_DebugAuto.Checked == true)
  2444. {
  2445. var Address = new byte[4];
  2446. UInt32 Address_Begin;
  2447. UInt32 Addres_End;
  2448. if (!serialPort1.IsOpen)
  2449. {
  2450. checkBox_DebugAuto.Checked = false;
  2451. return;
  2452. }
  2453. if (checkBox_DebugReset.Checked == true)
  2454. richTextBox_DebugInfo.Clear();
  2455. do
  2456. {
  2457. if ((textBox_DebugAddrBegin.Text == string.Empty) || (textBox_DebugAddrEnd.Text == string.Empty))
  2458. {
  2459. checkBox_DebugAuto.Checked = false;
  2460. MessageBox.Show("请输入起始和结束地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2461. continue;
  2462. }
  2463. else if ((textBox_DebugAddrBegin.Text.Length != 8) || (textBox_DebugAddrEnd.Text.Length != 8))
  2464. {
  2465. checkBox_DebugAuto.Checked = false;
  2466. MessageBox.Show("请检查地址长度!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2467. continue;
  2468. }
  2469. else
  2470. {
  2471. Address = HexStringToBytes(textBox_DebugAddrBegin.Text, false);
  2472. Address_Begin = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
  2473. Address = HexStringToBytes(textBox_DebugAddrEnd.Text, false);
  2474. Addres_End = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
  2475. if (Addres_End < Address_Begin)
  2476. {
  2477. checkBox_DebugAuto.Checked = false;
  2478. MessageBox.Show("结束地址必须大于或等于起始地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2479. continue;
  2480. }
  2481. else
  2482. {
  2483. var SendByte = new byte[8];
  2484. SendByte[0] = (byte)(Address_Begin >> 24);
  2485. SendByte[1] = (byte)(Address_Begin >> 16);
  2486. SendByte[2] = (byte)(Address_Begin >> 8);
  2487. SendByte[3] = (byte)(Address_Begin);
  2488. SendByte[4] = (byte)(Addres_End >> 24);
  2489. SendByte[5] = (byte)(Addres_End >> 16);
  2490. SendByte[6] = (byte)(Addres_End >> 8);
  2491. SendByte[7] = (byte)(Addres_End);
  2492. if (!SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2D08, SendByte))
  2493. checkBox_DebugAuto.Checked = false;
  2494. }
  2495. }
  2496. } while (false);
  2497. }
  2498. }
  2499. private void checkBox_ReadTorqueData_CheckedChanged(object sender, EventArgs e)
  2500. {
  2501. if (checkBox_ReadTorqueData.Checked == true)
  2502. {
  2503. #if true
  2504. foreach (Control c in groupBox3.Controls)
  2505. {
  2506. if (c is TextBox)
  2507. {
  2508. if ((c != textBox_RunInfo_Range) && (c != textBox_RunInfo_ErrorCode))
  2509. {
  2510. if ((c.Text == string.Empty) || (c.Text == "---"))
  2511. {
  2512. timer1.Enabled = false;
  2513. MessageBox.Show("请确认进入配置模式!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2514. timer1.Enabled = true;
  2515. checkBox_Savedata.Checked = false;
  2516. checkBox_ReadTorqueData.Checked = false;
  2517. return;
  2518. }
  2519. else
  2520. {
  2521. LoginForm.Show();
  2522. }
  2523. }
  2524. }
  2525. }
  2526. #else
  2527. if (radioButton_WorkMode_Confg.Checked == false)
  2528. {
  2529. timer1.Enabled = false;
  2530. MessageBox.Show("请确认进入配置模式", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2531. timer1.Enabled = true;
  2532. checkBox_ReadTorqueData.Checked = false;
  2533. }
  2534. else
  2535. {
  2536. LoginForm.Show();
  2537. }
  2538. #endif
  2539. }
  2540. else
  2541. {
  2542. LoginForm.Hide();
  2543. }
  2544. }
  2545. private void checkBox_Savedata_CheckedChanged(object sender, EventArgs e)
  2546. {
  2547. if (checkBox_Savedata.Checked == true)
  2548. {
  2549. //判断是否显示运行信息
  2550. foreach (Control c in groupBox3.Controls)
  2551. {
  2552. if (c is TextBox)
  2553. {
  2554. if ((c != textBox_RunInfo_Range) && (c != textBox_RunInfo_ErrorCode))
  2555. {
  2556. if ((c.Text == string.Empty) || (c.Text == "---"))
  2557. {
  2558. timer1.Enabled = false;
  2559. MessageBox.Show("请设置电机进入配置模式!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2560. timer1.Enabled = true;
  2561. checkBox_Savedata.Checked = false;
  2562. return;
  2563. }
  2564. }
  2565. }
  2566. }
  2567. //判断是否查询版本信息
  2568. foreach (Control c in groupBox9.Controls)
  2569. {
  2570. if (c is TextBox)
  2571. {
  2572. if ((c.Text == string.Empty) || (c.Text == "---"))
  2573. {
  2574. if ((c == textBox_Mode) || (c == textBox_SN))
  2575. {
  2576. timer1.Enabled = false;
  2577. MessageBox.Show("请查询版本信息!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2578. timer1.Enabled = true;
  2579. checkBox_Savedata.Checked = false;
  2580. return;
  2581. }
  2582. }
  2583. }
  2584. }
  2585. AutoSaveFileName = textBox_Mode.Text + "_" + textBox_SN.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_实时数据" + ".txt"; ;
  2586. timer1.Enabled = false;
  2587. MessageBox.Show("开始记录数据!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2588. timer1.Enabled = true;
  2589. }
  2590. else
  2591. {
  2592. AutoSaveFileName = "";
  2593. timer1.Enabled = false;
  2594. MessageBox.Show("停止记录数据!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2595. timer1.Enabled = true;
  2596. }
  2597. }
  2598. private void DateAutoSave()
  2599. {
  2600. if (checkBox_Savedata.Checked == true)
  2601. {
  2602. if (AutoSaveFileName == string.Empty)
  2603. {
  2604. return;
  2605. }
  2606. string SaveData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace(" ", "_")+" ";
  2607. foreach (Control c in groupBox3.Controls)
  2608. {
  2609. if (c is TextBox)
  2610. {
  2611. SaveData += c.Text + " , ";
  2612. }
  2613. }
  2614. SaveData += "\r\n";
  2615. System.IO.File.AppendAllText(AutoSaveFileName, SaveData);//sb.ToString());
  2616. }
  2617. }
  2618. private void button_DataExport_Click(object sender, EventArgs e)
  2619. {
  2620. //增加控件查空处理,存在没有数据时提示
  2621. //判断是否查询力矩传感器
  2622. foreach (Control c in groupBox2.Controls)
  2623. {
  2624. if (c is TextBox)
  2625. {
  2626. if ((c != textBox_TorqueData) && (c != textBox_Torque_Stdev))
  2627. {
  2628. if ((c.Text == string.Empty) || (c.Text == "---"))
  2629. {
  2630. timer1.Enabled = false;
  2631. MessageBox.Show("请查询力矩传感器!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2632. timer1.Enabled = true;
  2633. return;
  2634. }
  2635. }
  2636. }
  2637. }
  2638. //判断是否显示运行信息
  2639. foreach (Control c in groupBox3.Controls)
  2640. {
  2641. if (c is TextBox)
  2642. {
  2643. if ((c != textBox_RunInfo_Range) && (c != textBox_RunInfo_ErrorCode))
  2644. {
  2645. if ((c.Text == string.Empty) || (c.Text == "---"))
  2646. {
  2647. timer1.Enabled = false;
  2648. MessageBox.Show("请设置电机进入配置模式!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2649. timer1.Enabled = true;
  2650. return;
  2651. }
  2652. }
  2653. }
  2654. }
  2655. //判断是否查询用户参数1
  2656. foreach (Control c in groupBox10.Controls)
  2657. {
  2658. if (c is TextBox)
  2659. {
  2660. if (c.Text == string.Empty)
  2661. {
  2662. timer1.Enabled = false;
  2663. MessageBox.Show("请查询用户参数1!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2664. timer1.Enabled = true;
  2665. return;
  2666. }
  2667. }
  2668. }
  2669. //判断是否查询用户参数2
  2670. foreach (Control c in groupBox4.Controls)
  2671. {
  2672. if (c is TextBox)
  2673. {
  2674. if (c.Text == string.Empty)
  2675. {
  2676. timer1.Enabled = false;
  2677. MessageBox.Show("请查询用户参数2!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2678. timer1.Enabled = true;
  2679. return;
  2680. }
  2681. }
  2682. }
  2683. #if false
  2684. //判断是否显示骑行信息
  2685. foreach (Control c in groupBox12.Controls)
  2686. {
  2687. if (c is TextBox)
  2688. {
  2689. if ((c.Text == string.Empty) || (c.Text == "---"))
  2690. {
  2691. timer1.Enabled = false;
  2692. MessageBox.Show("请自动查询骑行信息!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2693. timer1.Enabled = true;
  2694. return;
  2695. }
  2696. }
  2697. }
  2698. #endif
  2699. #if false
  2700. //判断是否查询姿态信息
  2701. foreach (Control c in groupBox13.Controls)
  2702. {
  2703. if (c is TextBox)
  2704. {
  2705. if ((c.Text == string.Empty) || (c.Text == "---"))
  2706. {
  2707. timer1.Enabled = false;
  2708. MessageBox.Show("请自动查询姿态信息!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2709. timer1.Enabled = true;
  2710. return;
  2711. }
  2712. }
  2713. }
  2714. #endif
  2715. //判断是否查询马达参数信息
  2716. foreach (Control c in groupBox11.Controls)
  2717. {
  2718. if (c is TextBox)
  2719. {
  2720. if ((c.Text == string.Empty) || (c.Text == "---"))
  2721. {
  2722. timer1.Enabled = false;
  2723. MessageBox.Show("请查询马达参数!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2724. timer1.Enabled = true ;
  2725. return;
  2726. }
  2727. }
  2728. }
  2729. //判断是否查询版本信息
  2730. foreach (Control c in groupBox9.Controls)
  2731. {
  2732. if (c is TextBox)
  2733. {
  2734. if ((c.Text == string.Empty) || (c.Text == "---"))
  2735. {
  2736. if (c == textBox_TE_FW)//忽略TE版本和软件标识
  2737. {
  2738. continue;
  2739. }
  2740. else
  2741. {
  2742. timer1.Enabled = false;
  2743. MessageBox.Show("请查询版本信息!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2744. timer1.Enabled = true;
  2745. return;
  2746. }
  2747. }
  2748. }
  2749. }
  2750. //判断是否查询历史信息
  2751. foreach (Control c in groupBox14.Controls)
  2752. {
  2753. if (c is TextBox)
  2754. {
  2755. if ((c.Text == string.Empty) || (c.Text == "---"))
  2756. {
  2757. timer1.Enabled = false;
  2758. MessageBox.Show("请查询历史信息!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  2759. timer1.Enabled = true;
  2760. return;
  2761. }
  2762. }
  2763. }
  2764. //...
  2765. Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap
  2766. Graphics g = Graphics.FromImage(bit);
  2767. g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片
  2768. SaveFileDialog sf = new SaveFileDialog();
  2769. sf.Title = "数据保存";
  2770. sf.FileName = textBox_Mode.Text + "_" + textBox_SN.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_页面存储" + ".png";
  2771. sf.Filter = "图片|*.png";
  2772. if (sf.ShowDialog() == DialogResult.OK)
  2773. {
  2774. bit.Save(sf.FileName);//默认保存格式为PNG,保存成jpg格式质量不是很好
  2775. }
  2776. }
  2777. private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  2778. {
  2779. }
  2780. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  2781. {
  2782. timer1.Enabled = false;
  2783. DialogResult result = MessageBox.Show("确定关闭吗!", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
  2784. if (result == DialogResult.OK)
  2785. {
  2786. if (serialPort1.IsOpen == true)
  2787. {
  2788. //发送指令退出配置模式
  2789. var RunMode = new byte[1];
  2790. RunMode[0] = 0x00;
  2791. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, RunMode);
  2792. serialPort1.Close();
  2793. }
  2794. e.Cancel = false; //点击OK
  2795. }
  2796. else
  2797. {
  2798. e.Cancel = true;
  2799. timer1.Enabled = true;
  2800. }
  2801. }
  2802. private void trackBar_MotorSpeedSet_Scroll(object sender, EventArgs e)
  2803. {
  2804. var Data = new byte[1];
  2805. label_MotorSpeedSet.Text = Convert.ToString(trackBar_MotorSpeedSet.Value) + "%";
  2806. Data[0] = (byte)trackBar_MotorSpeedSet.Value;
  2807. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2C01, Data);
  2808. }
  2809. private void button_Recovery_Click(object sender, EventArgs e)
  2810. {
  2811. var Code = new byte[8];
  2812. Code[0] = (byte)'R';
  2813. Code[1] = (byte)'E';
  2814. Code[2] = (byte)'C';
  2815. Code[3] = (byte)'O';
  2816. Code[4] = (byte)'V';
  2817. Code[5] = (byte)'E';
  2818. Code[6] = (byte)'R';
  2819. Code[7] = (byte)'Y';
  2820. timer1.Enabled = false;
  2821. if (MessageBox.Show("参数还原后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2822. {
  2823. if (MessageBox.Show("请再次确认", "确认还原?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2824. {
  2825. if (MessageBox.Show("请最后确认", "确认还原?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2826. {
  2827. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2708, Code);
  2828. }
  2829. }
  2830. }
  2831. timer1.Enabled = true;
  2832. }
  2833. #region 字符串转换为字节流
  2834. public static byte[] HexStringToBytes(string hexStr, bool Flag_Space)
  2835. {
  2836. if (string.IsNullOrEmpty(hexStr))
  2837. {
  2838. return new byte[0];
  2839. }
  2840. if (hexStr.StartsWith("0x"))
  2841. {
  2842. hexStr = hexStr.Remove(0, 2);
  2843. }
  2844. var count = hexStr.Length;
  2845. var byteCount = 0;
  2846. if (Flag_Space == true)//相邻字节含空格
  2847. {
  2848. if ((count % 3) == 0)//最后字节含空格
  2849. {
  2850. byteCount = count / 3;
  2851. }
  2852. else if ((count % 3) == 2)//最后字节不含空格
  2853. {
  2854. byteCount = (count + 1) / 3;
  2855. }
  2856. else//数据无效
  2857. {
  2858. MessageBox.Show("相邻字节请插入空格!", "提示");
  2859. }
  2860. var result = new byte[byteCount];
  2861. for (int ii = 0; ii < byteCount; ++ii)
  2862. {
  2863. var tempBytes = Byte.Parse(hexStr.Substring(3 * ii, 2), System.Globalization.NumberStyles.HexNumber);
  2864. result[ii] = tempBytes;
  2865. }
  2866. return result;
  2867. }
  2868. else if (Flag_Space == false)//相邻字节不含空格
  2869. {
  2870. if (count % 2 == 1)
  2871. {
  2872. MessageBox.Show("请删除相邻字节之间空格!", "提示");
  2873. }
  2874. byteCount = count / 2;
  2875. var result = new byte[byteCount];
  2876. for (int ii = 0; ii < byteCount; ++ii)
  2877. {
  2878. var tempBytes = Byte.Parse(hexStr.Substring(2 * ii, 2), System.Globalization.NumberStyles.HexNumber);
  2879. result[ii] = tempBytes;
  2880. }
  2881. return result;
  2882. }
  2883. else
  2884. {
  2885. return new byte[0];
  2886. }
  2887. }
  2888. #endregion
  2889. #region 故障日志结构体定义
  2890. //故障日志记录信息 60 bytes,1K空间可以保存最近16条故障记录
  2891. [StructLayout(LayoutKind.Sequential)]
  2892. public struct MC_ErrorLogSaveInfo_Struct_t
  2893. {
  2894. public UInt16 Error_Index; //故障列表索引 2 bytes, 地址偏移 0
  2895. public UInt16 NotesInfo1; //备注信息1,地址偏移 2
  2896. public UInt16 NotesInfo2; //备注信息2,地址偏移 4
  2897. public UInt16 NotesInfo3; //备注信息3,地址偏移 6
  2898. public UInt32 ErrorCode; //故障码 4 bytes, 地址偏移 8
  2899. public UInt32 RunTime; //运行时间 4bytes, 地址偏移 12
  2900. //电机运行信息,占用空间32bytes
  2901. [StructLayout(LayoutKind.Sequential)]
  2902. public struct MC_RunInfo_Struct_t
  2903. {
  2904. public ushort BikeSpeed; //车速 0.1km/h,地址偏移0
  2905. public ushort MotorSpeed; //输出转速 1rpm,地址偏移2
  2906. public ushort Power; //电功率 1W,地址偏移4
  2907. public ushort BusVoltage; //母线电压 1mV,地址偏移6
  2908. public ushort BusCurrent; //母线电流 1mA,地址偏移8
  2909. public byte Cadence; //踏频 1rpm,地址偏移10
  2910. public byte Torque; //踩踏力矩 1Nm,地址偏移11
  2911. public byte CadenceDir; //踩踏方向 0-正,1-反,2-停止,地址偏移12
  2912. public byte GearSt; //助力档位,地址偏移13
  2913. public byte LightSwitch; //灯开关 0xF0-关,0xF1-开,地址偏移14
  2914. public byte SOC; //剩余电量 1%,地址偏移15
  2915. public ushort RemainDistance; //续航里程 1km,地址偏移16
  2916. public ushort ODO_Km; //总里程 1km,地址偏移18
  2917. public byte PowerPerKm; //平均功耗 0.01Ah/km ,地址偏移20
  2918. public byte T_PCB; //PCB温度 +40℃,地址偏移21
  2919. public byte T_Coil; //绕组温度 +40℃,地址偏移22
  2920. public byte T_MCU; //MCU温度 +40℃,地址偏移23
  2921. public ushort Ride_Km; //开机后骑行里程 0.1km, 地址偏移24
  2922. public ushort Ride_Time; //开机后骑行时间 1s,地址偏移26
  2923. public UInt32 RS; //预留 4bytes
  2924. };
  2925. [MarshalAs(UnmanagedType.Struct)]
  2926. public MC_RunInfo_Struct_t RunInfo; //运行信息 32bytes, 地址偏移 16
  2927. //姿态传感器数值
  2928. [StructLayout(LayoutKind.Sequential)]
  2929. public struct MC_AttitudeAngle_Struct_t
  2930. {
  2931. public Int16 Angle_Pitch_Absolute; //俯仰角绝对角度
  2932. public Int16 Angle_Roll_Absolute; //横滚角绝对角度
  2933. public Int16 Angle_Pitch_Relative; //俯仰角相对角度
  2934. public Int16 Angle_Roll_Relative; //横滚角相对角度
  2935. };
  2936. [MarshalAs(UnmanagedType.Struct)]
  2937. public MC_AttitudeAngle_Struct_t AttitudeAngle; //车辆姿态角度 8bytes, 地址偏移 48
  2938. //电机控制参数
  2939. [StructLayout(LayoutKind.Sequential)]
  2940. public struct MC_CalParam_Struct_t
  2941. {
  2942. public byte AssistRunMode; //助力模式
  2943. public Int16 Ref_Torque; //力矩模式输入FOC值
  2944. public Int16 Ref_Speed; //速度模式输入FOC值
  2945. public byte Foc_Flag; //FCO运算标志
  2946. };
  2947. [MarshalAs(UnmanagedType.Struct)]
  2948. public MC_CalParam_Struct_t MC_CalParam; //马达控制参数 8bytes , 地址偏移 56
  2949. };
  2950. #endregion
  2951. public static class MyConverter
  2952. {
  2953. /// <summary>
  2954. /// 由结构体转换为byte数组
  2955. /// </summary>
  2956. public static byte[] StructureToByte<T>(T structure)
  2957. {
  2958. int size = Marshal.SizeOf(typeof(T));
  2959. byte[] buffer = new byte[size];
  2960. IntPtr bufferIntPtr = Marshal.AllocHGlobal(size);
  2961. try
  2962. {
  2963. Marshal.StructureToPtr(structure, bufferIntPtr, true);
  2964. Marshal.Copy(bufferIntPtr, buffer, 0, size);
  2965. }
  2966. finally
  2967. {
  2968. Marshal.FreeHGlobal(bufferIntPtr);
  2969. }
  2970. return buffer;
  2971. }
  2972. /// <summary>
  2973. /// 由byte数组转换为结构体
  2974. /// </summary>
  2975. public static T ByteToStructure<T>(byte[] dataBuffer, ushort StarIndex)
  2976. {
  2977. object structure = null;
  2978. int size = Marshal.SizeOf(typeof(T));
  2979. IntPtr allocIntPtr = Marshal.AllocHGlobal(size);
  2980. try
  2981. {
  2982. Marshal.Copy(dataBuffer, StarIndex, allocIntPtr, size);
  2983. structure = Marshal.PtrToStructure(allocIntPtr, typeof(T));
  2984. }
  2985. finally
  2986. {
  2987. Marshal.FreeHGlobal(allocIntPtr);
  2988. }
  2989. return (T)structure;
  2990. }
  2991. }
  2992. private void button_ReadErrorLog_Click(object sender, EventArgs e)
  2993. {
  2994. var Address = new byte[4];
  2995. UInt32 Address_Begin;
  2996. UInt32 Addres_End;
  2997. string textBox_Address_Begin = "0801F800";
  2998. string textBox_Address_End = "0801FBFF";
  2999. if (!serialPort1.IsOpen)
  3000. {
  3001. timer1.Enabled = false;
  3002. MessageBox.Show("串口未连接");
  3003. timer1.Enabled = true;
  3004. return;
  3005. }
  3006. if ((textBox_Address_Begin == string.Empty) || (textBox_Address_End == string.Empty))
  3007. {
  3008. timer1.Enabled = false;
  3009. MessageBox.Show("请输入地址!", "提示");
  3010. timer1.Enabled = true;
  3011. return;
  3012. }
  3013. else
  3014. {
  3015. Address = HexStringToBytes(textBox_Address_Begin, false);
  3016. Address_Begin = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
  3017. Address = HexStringToBytes(textBox_Address_End, false);
  3018. Addres_End = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
  3019. if (Addres_End <= Address_Begin)
  3020. {
  3021. timer1.Enabled = false;
  3022. MessageBox.Show("结束地址应大于起始地址!", "提示");
  3023. timer1.Enabled = true;
  3024. return;
  3025. }
  3026. else if (((Addres_End - Address_Begin + 1) % 128) != 0) //
  3027. {
  3028. timer1.Enabled = false;
  3029. MessageBox.Show("(结束地址 - 起始地址 + 1)应为128整数倍!", "提示");
  3030. timer1.Enabled = true;
  3031. return;
  3032. }
  3033. else
  3034. {
  3035. button_ReadErrorLog.Text = "读取中";
  3036. button_ReadErrorLog.Enabled = false;
  3037. //设置保存路经
  3038. SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  3039. saveFileDialog1.Title = "设置保存路经";
  3040. saveFileDialog1.RestoreDirectory = true;
  3041. saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
  3042. if (((textBox_Mode.Text == string.Empty) || (textBox_Mode.Text == "---")) && ((textBox_SN.Text == string.Empty) || (textBox_SN.Text == "---")))
  3043. {
  3044. timer1.Enabled = false;
  3045. MessageBox.Show("未查询电机版本信息!", "提示");
  3046. timer1.Enabled = true;
  3047. saveFileDialog1.FileName = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_故障日志"; ;
  3048. }
  3049. else
  3050. {
  3051. saveFileDialog1.FileName = textBox_Mode.Text + "_" + textBox_SN.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_故障日志";
  3052. }
  3053. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  3054. {
  3055. ErrorLogSaveFileName = saveFileDialog1.FileName.ToString();
  3056. }
  3057. else
  3058. {
  3059. button_ReadErrorLog.Enabled = true;
  3060. button_ReadErrorLog.Text = "读取";
  3061. return;
  3062. }
  3063. string SaveInfo = "读取时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n\r\n";
  3064. SaveInfo += "电机型号:" + textBox_Mode.Text + "\r\n";
  3065. SaveInfo += "序列号:" + textBox_SN.Text + "\r\n";
  3066. SaveInfo += "硬件版本:" + textBox_HW.Text + "\r\n";
  3067. SaveInfo += "软件版本:" + textBox_FW.Text + "\r\n";
  3068. SaveInfo += "TE软件版本:" + textBox_TE_FW.Text + "\r\n\r\n";
  3069. System.IO.File.WriteAllText(ErrorLogSaveFileName, SaveInfo + "无日志记录");
  3070. ErrorLogSaveFlag = false;
  3071. UInt32 ReadCount = 0;
  3072. ReadCount = (Addres_End - Address_Begin + 1) / 128;
  3073. for (ushort i = 0; i < ReadCount; i++)
  3074. {
  3075. var SendByte = new byte[8];
  3076. UInt32 Address_Temp_Begin;
  3077. UInt32 Address_Temp_End;
  3078. Address_Temp_Begin = Address_Begin + 128 * (UInt32)i;
  3079. SendByte[0] = (byte)(Address_Temp_Begin >> 24);
  3080. SendByte[1] = (byte)(Address_Temp_Begin >> 16);
  3081. SendByte[2] = (byte)(Address_Temp_Begin >> 8);
  3082. SendByte[3] = (byte)(Address_Temp_Begin);
  3083. Address_Temp_End = Address_Temp_Begin + 127;
  3084. SendByte[4] = (byte)(Address_Temp_End >> 24);
  3085. SendByte[5] = (byte)(Address_Temp_End >> 16);
  3086. SendByte[6] = (byte)(Address_Temp_End >> 8);
  3087. SendByte[7] = (byte)(Address_Temp_End);
  3088. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2D08, SendByte);
  3089. Thread.Sleep(200);
  3090. }
  3091. timer1.Enabled = false;
  3092. MessageBox.Show("读取完成,请查看日志!", "提示");
  3093. timer1.Enabled = true;
  3094. button_ReadErrorLog.Enabled = true;
  3095. button_ReadErrorLog.Text = "读取";
  3096. }
  3097. }
  3098. }
  3099. private void ErrorLogDateSave(MC_ErrorLogSaveInfo_Struct_t Log1, MC_ErrorLogSaveInfo_Struct_t Log2)
  3100. {
  3101. if (ErrorLogSaveFileName.Substring(ErrorLogSaveFileName.Length - 4, 4) != ".txt")
  3102. {
  3103. timer1.Enabled = false;
  3104. MessageBox.Show("日志存储失败!", "提示");
  3105. timer1.Enabled = true;
  3106. return;
  3107. }
  3108. string SaveData = "";
  3109. //Log1
  3110. if (Log1.Error_Index != 0xFFFF)
  3111. {
  3112. if (ErrorLogSaveFlag == false)
  3113. {
  3114. ErrorLogSaveFlag = true;
  3115. string SaveInfo = "读取时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n\r\n";
  3116. SaveInfo += "电机型号:" + textBox_Mode.Text + "\r\n";
  3117. SaveInfo += "序列号:" + textBox_SN.Text + "\r\n";
  3118. SaveInfo += "硬件版本:" + textBox_HW.Text + "\r\n";
  3119. SaveInfo += "软件版本:" + textBox_FW.Text + "\r\n";
  3120. SaveInfo += "TE软件版本:" + textBox_TE_FW.Text + "\r\n\r\n";
  3121. System.IO.File.WriteAllText(ErrorLogSaveFileName, SaveInfo);
  3122. }
  3123. SaveData += "故障序号:" + Convert.ToString(Log1.Error_Index) + ", ";
  3124. SaveData += "备注1:" + Convert.ToString(Log1.NotesInfo1) + ", ";
  3125. SaveData += "备注2:" + Convert.ToString(Log1.NotesInfo2) + ", ";
  3126. SaveData += "备注3:" + Convert.ToString(Log1.NotesInfo3) + ", ";
  3127. SaveData += "故障码:" + "0x" + Convert.ToString(Log1.ErrorCode, 16).PadLeft(8, '0').ToUpper() + ", ";
  3128. SaveData += "运行时间:" + Convert.ToString(Log1.RunTime) + "min" + ", ";
  3129. SaveData += "车速:" + ((float)Log1.RunInfo.BikeSpeed / 10f).ToString("0.0") + "km/h" + ", ";
  3130. SaveData += "电机转速:" + Convert.ToString(Log1.RunInfo.MotorSpeed) + "rpm" + ", ";
  3131. SaveData += "功率:" + Convert.ToString(Log1.RunInfo.Power * 2) + "w" + ", ";
  3132. SaveData += "电压:" + Convert.ToString(Log1.RunInfo.BusVoltage) + "mV" + ", ";
  3133. SaveData += "电流:" + Convert.ToString(Log1.RunInfo.BusCurrent) + "mA" + ", ";
  3134. SaveData += "踏频:" + Convert.ToString(Log1.RunInfo.Cadence) + "rpm" + ", ";
  3135. SaveData += "力矩:" + Convert.ToString(Log1.RunInfo.Torque) + "N.m" + ", ";
  3136. SaveData += "方向:" + ((Log1.RunInfo.CadenceDir == 0) ? "正" : ((Log1.RunInfo.CadenceDir == 1) ? "反" : "停止")) + ", ";
  3137. SaveData += "档位:" + "0x" + Convert.ToString(Log1.RunInfo.GearSt, 16).PadLeft(2, '0').ToUpper() + ", ";
  3138. SaveData += "灯开关:" + ((Log1.RunInfo.LightSwitch == 0xF1) ? "开" : "关") + ", ";
  3139. SaveData += "电量:" + Convert.ToString(Log1.RunInfo.SOC) + "%" + ", ";
  3140. if ((Log1.RunInfo.RemainDistance == 0xEEEE) || (Log1.RunInfo.RemainDistance == 0xFFFF))
  3141. {
  3142. SaveData += "续航:" + "---km" + ", ";
  3143. }
  3144. else
  3145. {
  3146. SaveData += "续航:" + Convert.ToString(Log1.RunInfo.RemainDistance) + "km" + ", ";
  3147. }
  3148. SaveData += "总里程:" + Convert.ToString(Log1.RunInfo.ODO_Km) + "km" + ", ";
  3149. SaveData += "功耗:" + Convert.ToString(Log1.RunInfo.PowerPerKm * 10) + "mAh/km" + ", ";
  3150. SaveData += "PCB温度:" + Convert.ToString((int)(Log1.RunInfo.T_PCB - 40)) + "DegreeC" + ", ";
  3151. SaveData += "绕组温度:" + Convert.ToString((int)(Log1.RunInfo.T_Coil - 40)) + "DegreeC" + ", ";
  3152. SaveData += "MCU温度:" + Convert.ToString((int)(Log1.RunInfo.T_MCU - 40)) + "DegreeC" + ", ";
  3153. SaveData += "单次里程:" + Convert.ToString((float)Log1.RunInfo.Ride_Km / 10f) + "km" + ", ";
  3154. SaveData += "单次时间:" + Convert.ToString(Log1.RunInfo.Ride_Time) + "s" + ", ";
  3155. SaveData += "俯仰绝对角度:" + ((float)Log1.AttitudeAngle.Angle_Pitch_Absolute / 10f).ToString("0.0") + "Degree" + ", ";
  3156. SaveData += "横滚绝对角度:" + ((float)Log1.AttitudeAngle.Angle_Roll_Absolute / 10f).ToString("0.0") + "Degree" + ", ";
  3157. SaveData += "俯仰相对角度:" + ((float)Log1.AttitudeAngle.Angle_Pitch_Relative / 10f).ToString("0.0") + "Degree" + ", ";
  3158. SaveData += "横滚相对角度:" + ((float)Log1.AttitudeAngle.Angle_Roll_Relative / 10f).ToString("0.0") + "Degree" + ", ";
  3159. SaveData += "运行模式:" + Convert.ToString(Log1.MC_CalParam.AssistRunMode) + ", ";
  3160. SaveData += "力矩环给定:" + Convert.ToString(Log1.MC_CalParam.Ref_Torque) + ", ";
  3161. SaveData += "速度环给定:" + Convert.ToString(Log1.MC_CalParam.Ref_Speed) + ", ";
  3162. SaveData += "FOC_Flag:" + Convert.ToString(Log1.MC_CalParam.Foc_Flag) + ", ";
  3163. SaveData += "\r\n\r\n";
  3164. System.IO.File.AppendAllText(ErrorLogSaveFileName, SaveData);
  3165. SaveData = "";
  3166. }
  3167. //Log2
  3168. if (Log2.Error_Index != 0xFFFF)
  3169. {
  3170. if (ErrorLogSaveFlag == false)
  3171. {
  3172. ErrorLogSaveFlag = true;
  3173. string SaveInfo = "读取时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n\r\n";
  3174. SaveInfo += "电机型号:" + textBox_Mode.Text + "\r\n";
  3175. SaveInfo += "序列号:" + textBox_SN.Text + "\r\n";
  3176. SaveInfo += "硬件版本:" + textBox_HW.Text + "\r\n";
  3177. SaveInfo += "软件版本:" + textBox_FW.Text + "\r\n";
  3178. SaveInfo += "TE软件版本:" + textBox_TE_FW.Text + "\r\n\r\n";
  3179. System.IO.File.WriteAllText(ErrorLogSaveFileName, SaveInfo);
  3180. }
  3181. SaveData += "故障序号:" + Convert.ToString(Log2.Error_Index) + ", ";
  3182. SaveData += "备注1:" + Convert.ToString(Log2.NotesInfo1) + ", ";
  3183. SaveData += "备注2:" + Convert.ToString(Log2.NotesInfo2) + ", ";
  3184. SaveData += "备注3:" + Convert.ToString(Log2.NotesInfo3) + ", ";
  3185. SaveData += "故障码:" + "0x" + Convert.ToString(Log2.ErrorCode, 16).PadLeft(8, '0').ToUpper() + ", ";
  3186. SaveData += "运行时间:" + Convert.ToString(Log2.RunTime) + "min" + ", ";
  3187. SaveData += "车速:" + ((float)Log2.RunInfo.BikeSpeed / 10f).ToString("0.0") + "km/h" + ", ";
  3188. SaveData += "电机转速:" + Convert.ToString(Log2.RunInfo.MotorSpeed) + "rpm" + ", ";
  3189. SaveData += "功率:" + Convert.ToString(Log2.RunInfo.Power * 2) + "w" + ", ";
  3190. SaveData += "电压:" + Convert.ToString(Log2.RunInfo.BusVoltage) + "mV" + ", ";
  3191. SaveData += "电流:" + Convert.ToString(Log2.RunInfo.BusCurrent) + "mA" + ", ";
  3192. SaveData += "踏频:" + Convert.ToString(Log2.RunInfo.Cadence) + "rpm" + ", ";
  3193. SaveData += "力矩:" + Convert.ToString(Log2.RunInfo.Torque) + "N.m" + ", ";
  3194. SaveData += "方向:" + ((Log2.RunInfo.CadenceDir == 0) ? "正" : ((Log2.RunInfo.CadenceDir == 1) ? "反" : "停止")) + ", ";
  3195. SaveData += "档位:" + "0x" + Convert.ToString(Log2.RunInfo.GearSt, 16).PadLeft(2, '0').ToUpper() + ", ";
  3196. SaveData += "灯开关:" + ((Log2.RunInfo.LightSwitch == 0xF1) ? "开" : "关") + ", ";
  3197. SaveData += "电量:" + Convert.ToString(Log2.RunInfo.SOC) + "%" + ", ";
  3198. if ((Log1.RunInfo.RemainDistance == 0xEEEE) || (Log1.RunInfo.RemainDistance == 0xFFFF))
  3199. {
  3200. SaveData += "续航:" + "---km" + ", ";
  3201. }
  3202. else
  3203. {
  3204. SaveData += "续航:" + Convert.ToString(Log1.RunInfo.RemainDistance) + "km" + ", ";
  3205. }
  3206. SaveData += "总里程:" + Convert.ToString(Log2.RunInfo.ODO_Km) + "km" + ", ";
  3207. SaveData += "功耗:" + Convert.ToString(Log2.RunInfo.PowerPerKm * 10) + "mAh/km" + ", ";
  3208. SaveData += "PCB温度:" + Convert.ToString((int)(Log2.RunInfo.T_PCB - 40)) + "DegreeC" + ", ";
  3209. SaveData += "绕组温度:" + Convert.ToString((int)(Log2.RunInfo.T_Coil - 40)) + "DegreeC" + ", ";
  3210. SaveData += "MCU温度:" + Convert.ToString((int)(Log2.RunInfo.T_MCU - 40)) + "DegreeC" + ", ";
  3211. SaveData += "单次里程:" + Convert.ToString((float)Log2.RunInfo.Ride_Km / 10f) + "km" + ", ";
  3212. SaveData += "单次时间:" + Convert.ToString(Log2.RunInfo.Ride_Time) + "s" + ", ";
  3213. SaveData += "俯仰绝对角度:" + ((float)Log2.AttitudeAngle.Angle_Pitch_Absolute / 10f).ToString("0.0") + "Degree" + ", ";
  3214. SaveData += "横滚绝对角度:" + ((float)Log2.AttitudeAngle.Angle_Roll_Absolute / 10f).ToString("0.0") + "Degree" + ", ";
  3215. SaveData += "俯仰相对角度:" + ((float)Log2.AttitudeAngle.Angle_Pitch_Relative / 10f).ToString("0.0") + "Degree" + ", ";
  3216. SaveData += "横滚相对角度:" + ((float)Log2.AttitudeAngle.Angle_Roll_Relative / 10f).ToString("0.0") + "Degree" + ", ";
  3217. SaveData += "运行模式:" + Convert.ToString(Log2.MC_CalParam.AssistRunMode) + ", ";
  3218. SaveData += "力矩环给定:" + Convert.ToString(Log2.MC_CalParam.Ref_Torque) + ", ";
  3219. SaveData += "速度环给定:" + Convert.ToString(Log2.MC_CalParam.Ref_Speed) + ", ";
  3220. SaveData += "FOC_Flag:" + Convert.ToString(Log2.MC_CalParam.Foc_Flag) + ", ";
  3221. SaveData += "\r\n\r\n";
  3222. System.IO.File.AppendAllText(ErrorLogSaveFileName, SaveData);
  3223. SaveData = "";
  3224. }
  3225. }
  3226. private void button_ClearData_MouseHover(object sender, EventArgs e)
  3227. {
  3228. // 创建the ToolTip
  3229. ToolTip toolTip1 = new ToolTip();
  3230. // 设置显示样式
  3231. toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
  3232. toolTip1.InitialDelay = 50;//事件触发多久后出现提示
  3233. toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  3234. toolTip1.ShowAlways = true;//是否显示提示框
  3235. // 设置伴随的对象.
  3236. toolTip1.SetToolTip(this.button_ClearData, "清除电机内部所有数据,包括配置参数、历史记录等。");//设置提示按钮和提示内容
  3237. }
  3238. private void button_Recovery_MouseHover(object sender, EventArgs e)
  3239. {
  3240. // 创建the ToolTip
  3241. ToolTip toolTip1 = new ToolTip();
  3242. // 设置显示样式
  3243. toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
  3244. toolTip1.InitialDelay = 50;//事件触发多久后出现提示
  3245. toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  3246. toolTip1.ShowAlways = true;//是否显示提示框
  3247. // 设置伴随的对象.
  3248. toolTip1.SetToolTip(this.button_Recovery, "还原配置参数为初始值。");//设置提示按钮和提示内容
  3249. }
  3250. private void button_DataExport_MouseHover(object sender, EventArgs e)
  3251. {
  3252. // 创建the ToolTip
  3253. ToolTip toolTip1 = new ToolTip();
  3254. // 设置显示样式
  3255. toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
  3256. toolTip1.InitialDelay = 50;//事件触发多久后出现提示
  3257. toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  3258. toolTip1.ShowAlways = true;//是否显示提示框
  3259. // 设置伴随的对象.
  3260. toolTip1.SetToolTip(this.button_DataExport, "存储窗体页面,请先查询相关信息后操作。");//设置提示按钮和提示内容
  3261. }
  3262. private void label9_MouseHover(object sender, EventArgs e)
  3263. {
  3264. // 创建the ToolTip
  3265. ToolTip toolTip1 = new ToolTip();
  3266. // 设置显示样式
  3267. toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
  3268. toolTip1.InitialDelay = 50;//事件触发多久后出现提示
  3269. toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  3270. toolTip1.ShowAlways = true;//是否显示提示框
  3271. // 设置伴随的对象.
  3272. toolTip1.SetToolTip(this.label9, "双击自动计算启动值。");//设置提示按钮和提示内容
  3273. }
  3274. private void button_ReadErrorLog_MouseHover(object sender, EventArgs e)
  3275. {
  3276. // 创建the ToolTip
  3277. ToolTip toolTip1 = new ToolTip();
  3278. // 设置显示样式
  3279. toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
  3280. toolTip1.InitialDelay = 50;//事件触发多久后出现提示
  3281. toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  3282. toolTip1.ShowAlways = true;//是否显示提示框
  3283. // 设置伴随的对象.
  3284. toolTip1.SetToolTip(this.button_ReadErrorLog, "导出电机内部存储的故障日志,读取完后自动保存。");//设置提示按钮和提示内容
  3285. }
  3286. private void label119_MouseHover(object sender, EventArgs e)
  3287. {
  3288. // 创建the ToolTip
  3289. ToolTip toolTip1 = new ToolTip();
  3290. // 设置显示样式
  3291. toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
  3292. toolTip1.InitialDelay = 50;//事件触发多久后出现提示
  3293. toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  3294. toolTip1.ShowAlways = true;//是否显示提示框
  3295. // 设置伴随的对象.
  3296. toolTip1.SetToolTip(this.label119, "双击自动计算校正系数。");//设置提示按钮和提示内容
  3297. }
  3298. private void button_LogClear_Click(object sender, EventArgs e)
  3299. {
  3300. var Code = new byte[9];
  3301. Code[0] = (byte)'L';
  3302. Code[1] = (byte)'O';
  3303. Code[2] = (byte)'G';
  3304. Code[3] = (byte)' ';
  3305. Code[4] = (byte)'C';
  3306. Code[5] = (byte)'L';
  3307. Code[6] = (byte)'E';
  3308. Code[7] = (byte)'A';
  3309. Code[8] = (byte)'R';
  3310. timer1.Enabled = false;
  3311. if (MessageBox.Show("历史记录信息清除后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  3312. {
  3313. if (MessageBox.Show("请再次确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  3314. {
  3315. if (MessageBox.Show("请最后确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  3316. {
  3317. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3909, Code);
  3318. }
  3319. }
  3320. }
  3321. timer1.Enabled = true;
  3322. }
  3323. private void textBox_RunInfo_ErrorCode_MouseHover(object sender, EventArgs e)
  3324. {
  3325. // 创建the ToolTip
  3326. ToolTip toolTip1 = new ToolTip();
  3327. // 设置显示样式
  3328. toolTip1.AutoPopDelay = 30000;//提示信息的可见时间
  3329. toolTip1.InitialDelay = 50;//事件触发多久后出现提示
  3330. toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  3331. toolTip1.ShowAlways = true;//是否显示提示框
  3332. // 设置伴随的对象.
  3333. string ErrorCode = this.textBox_RunInfo_ErrorCode.Text;
  3334. UInt32 Code = ErrorCode.Contains("H") ? Convert.ToUInt32(ErrorCode.Split('H')[0], 16) : 0;
  3335. toolTip1.SetToolTip(this.textBox_RunInfo_ErrorCode, ErrorInfoDisplay(Code));//设置故障内容显示
  3336. }
  3337. private void button_LogClear_MouseHover(object sender, EventArgs e)
  3338. {
  3339. // 创建the ToolTip
  3340. ToolTip toolTip1 = new ToolTip();
  3341. // 设置显示样式
  3342. toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
  3343. toolTip1.InitialDelay = 50;//事件触发多久后出现提示
  3344. toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  3345. toolTip1.ShowAlways = true;//是否显示提示框
  3346. // 设置伴随的对象.
  3347. toolTip1.SetToolTip(this.button_LogClear, "清除电机历史记录信息");//设置提示按钮和提示内容
  3348. }
  3349. private void button_ReadAll_Click(object sender, EventArgs e)
  3350. {
  3351. if (!serialPort1.IsOpen)
  3352. {
  3353. MessageBox.Show("串口未连接");
  3354. return;
  3355. }
  3356. //查询力矩传感器
  3357. var K = new byte[1];
  3358. textBox_Sensor1_Zero.Text = "---";
  3359. textBox_Sensor2_Zero.Text = "---";
  3360. textBox_Sensor3_Zero.Text = "---";
  3361. textBox_Sensor1_K.Text = "---";
  3362. textBox_Sensor2_K.Text = "---";
  3363. textBox_Sensor3_K.Text = "---";
  3364. //轮询零点
  3365. for (ushort i = 1; i <= 3; i++)
  3366. {
  3367. K[0] = (byte)i;
  3368. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3201, K);
  3369. }
  3370. //轮询校正系数
  3371. for (ushort i = 1; i <= 3; i++)
  3372. {
  3373. K[0] = (byte)i;
  3374. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3301, K);
  3375. }
  3376. textBox_Torque_Start.Text = "---";
  3377. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3500, null);
  3378. //查询用户参数1
  3379. MC_Config_Clear();
  3380. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1A00, null);
  3381. //查询用户参数2
  3382. MC_Config_Ext_Clear();
  3383. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3000, null);
  3384. //查询姿态传感器参数
  3385. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2F00, null);
  3386. MC_AttitudeAngle_Clear();
  3387. //查询马达参数
  3388. Motor_Config_Clear();
  3389. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1C00, null);
  3390. //查询版本信息
  3391. textBox_Mode.Text = "---";
  3392. textBox_SN.Text = "---";
  3393. textBox_HW.Text = "---";
  3394. textBox_FW.Text = "---";
  3395. textBox_Firmware_Special.Text = "---";
  3396. Class_Motor_Ver.Mode = "---";
  3397. Class_Motor_Ver.SN = "---";
  3398. Class_Motor_Ver.HW = "---";
  3399. Class_Motor_Ver.FW = "---";
  3400. Class_Motor_Ver.Special = "---";
  3401. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
  3402. textBox_TE_FW.Text = "---";
  3403. SendCmd((ushort)0x651, (byte)0x11, (ushort)0x1200, null);
  3404. //查询历史信息
  3405. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1E00, null);
  3406. MC_RunLog_Clear();
  3407. //查询骑行历史
  3408. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2E00, null);
  3409. MC_RideInfo_Clear();
  3410. }
  3411. //双击时允许解锁写保护
  3412. private void label127_DoubleClick(object sender, EventArgs e)
  3413. {
  3414. WriteProtectUnlock = true;
  3415. }
  3416. //双击解锁写保护
  3417. private void label1_Welcom_DoubleClick(object sender, EventArgs e)
  3418. {
  3419. if (WriteProtectUnlock == false)
  3420. return;
  3421. button_ClearData.Enabled = true;
  3422. button_Recovery.Enabled = true;
  3423. button_Write_Config.Enabled = true;
  3424. button_Write_Config_Ext.Enabled = true;
  3425. button_Write_MotorConfig.Enabled = true;
  3426. button_Write_Mode.Enabled = true;
  3427. button_Write_SN.Enabled = true;
  3428. }
  3429. //读取指定地址的数据
  3430. private void button_DebugRead_Click(object sender, EventArgs e)
  3431. {
  3432. var Address = new byte[4];
  3433. UInt32 Address_Begin;
  3434. UInt32 Addres_End;
  3435. if (checkBox_DebugReset.Checked == true)
  3436. richTextBox_DebugInfo.Clear();
  3437. if ((textBox_DebugAddrBegin.Text == string.Empty) || (textBox_DebugAddrEnd.Text == string.Empty))
  3438. {
  3439. MessageBox.Show("请输入起始和结束地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  3440. return;
  3441. }
  3442. else if ((textBox_DebugAddrBegin.Text.Length != 8) || (textBox_DebugAddrEnd.Text.Length != 8))
  3443. {
  3444. MessageBox.Show("请检查地址长度!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  3445. return;
  3446. }
  3447. else
  3448. {
  3449. Address = HexStringToBytes(textBox_DebugAddrBegin.Text, false);
  3450. Address_Begin = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
  3451. Address = HexStringToBytes(textBox_DebugAddrEnd.Text, false);
  3452. Addres_End = ((UInt32)Address[0] << 24) + ((UInt32)Address[1] << 16) + ((UInt32)Address[2] << 8) + ((UInt32)Address[3]);
  3453. if (Addres_End < Address_Begin)
  3454. {
  3455. MessageBox.Show("结束地址必须大于或等于起始地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  3456. return;
  3457. }
  3458. else
  3459. {
  3460. var SendByte = new byte[8];
  3461. SendByte[0] = (byte)(Address_Begin >> 24);
  3462. SendByte[1] = (byte)(Address_Begin >> 16);
  3463. SendByte[2] = (byte)(Address_Begin >> 8);
  3464. SendByte[3] = (byte)(Address_Begin);
  3465. SendByte[4] = (byte)(Addres_End >> 24);
  3466. SendByte[5] = (byte)(Addres_End >> 16);
  3467. SendByte[6] = (byte)(Addres_End >> 8);
  3468. SendByte[7] = (byte)(Addres_End);
  3469. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2D08, SendByte);
  3470. }
  3471. }
  3472. }
  3473. }
  3474. }