Form1.cs 161 KB

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