Form1.cs 204 KB

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