ImportForm.cs 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO.Ports;
  10. using System.Collections;
  11. using System.Threading;
  12. using System.Text.RegularExpressions;
  13. namespace MOTINOVA_Motor_Factory_Set
  14. {
  15. public partial class ImportForm : Form
  16. {
  17. #region 文件路径
  18. string LogSaveFileName = "";
  19. public string LogSavePath = "";
  20. public string LocalCfgFilePath = "";
  21. #endregion
  22. #region 测试参数配置
  23. //以下值在根据电机测试的配置文件中更改
  24. ushort MotorRadeSpeed = 1200; //空载转速
  25. ushort RateCadence = 100; //最高踏频
  26. ushort RateVoltage = 36000; //额定电压
  27. bool GasModeTest = true; //转把模式检测开关
  28. bool SpeedSensorTest = true; //速度传感器检测开关
  29. //以下值在软件测试文件params.cfg文件中修改
  30. ushort No_load_Current = 1000; //空载电流
  31. ushort SpeedSensorThData = 60; //车速传感器判断阈值
  32. ushort GasModeSpeed = 1200; //指拨模式测试转速
  33. #endregion
  34. #region 配置文件信息
  35. ArrayList array_CfgInfo = new ArrayList();
  36. #endregion
  37. #region 端口设置界面
  38. public static PortSettings portsettings_form = new PortSettings();
  39. #endregion
  40. #region 串口缓存
  41. public static List<byte> buffer = new List<byte>(4096);
  42. private byte[] binary_data_1 = new byte[1024];
  43. #endregion
  44. #region MC运行信息超时计数
  45. bool MC_RunInfo_Refresh = false;
  46. ushort MC_RunInfo_Refresh_Cnt = 0;
  47. #endregion
  48. #region MC故障码超时计数
  49. bool MC_ErrorCode_Refresh = false;
  50. ushort MC_ErrorCode_Refresh_Cnt = 0;
  51. #endregion
  52. #region 执行步骤
  53. public enum TestStep_Enum
  54. {
  55. Step_Init = 0,
  56. Step_SysClear = 1, //系统清除
  57. Step_Write = 2, //参数写入
  58. Step_Light = 3, //灯电压测试
  59. Step_Walk = 4, //推行测试
  60. Step_GasMode = 5, //转把测试
  61. Step_CheckVer = 6, //版本查询和确认
  62. Step_Save = 7 //数据读取和存储
  63. }
  64. TestStep_Enum TestStep;
  65. #endregion
  66. #region 反馈指令判断计时
  67. bool Ack_Flag = true;
  68. ushort Ack_Cnt = 0;
  69. #endregion
  70. #region 单步测试标志
  71. public enum StepTestResult_Enum
  72. {
  73. StepTestResult_Ready = 0,
  74. StepTestResult_Testing = 1,
  75. StepTestResult_OK = 2,
  76. StepTestResult_Fail = 3
  77. }
  78. StepTestResult_Enum StepTestFlag = StepTestResult_Enum.StepTestResult_Ready;
  79. #endregion
  80. #region 推行/转把测试计时
  81. ushort WalkProcess_Cnt = 0;
  82. ushort OK_cnt = 0;
  83. #endregion
  84. public ImportForm()
  85. {
  86. InitializeComponent();
  87. System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
  88. //加载用户路径
  89. LogSavePath = StartForm.UserPath;
  90. LocalCfgFilePath = StartForm.UserPath;
  91. }
  92. #region 获取配置文件日期
  93. private void GetDateList()
  94. {
  95. string[] DateList = { "" };
  96. if (checkBox_LocalFile.Checked == true)//从本地更新日期列表
  97. {
  98. DateList = System.IO.Directory.GetDirectories(LocalCfgFilePath);
  99. }
  100. else//从服务器更新日期列表
  101. {
  102. DateList = StartForm.myFtp.GetDirectoryList(StartForm.myServerCfg.RootPath + "/cfg");
  103. }
  104. comboBox_cfgFileDate.Items.Clear();
  105. foreach (var file in DateList)
  106. {
  107. comboBox_cfgFileDate.Items.Add(file.Substring(file.LastIndexOf("\\") + 1));
  108. }
  109. }
  110. #endregion
  111. #region 获取配置文件列表
  112. private void GetFileListFrom(string date)
  113. {
  114. string[] FileList = { "" };
  115. if (checkBox_LocalFile.Checked == true)//从本地更新文件列表
  116. {
  117. FileList = System.IO.Directory.GetFiles(LocalCfgFilePath + date);
  118. }
  119. else//从服务器更新文件列表
  120. {
  121. FileList = StartForm.myFtp.GetFileNameList(StartForm.myServerCfg.RootPath + "/cfg/" + date);
  122. }
  123. comboBox_File.Items.Clear();
  124. foreach (var file in FileList)
  125. {
  126. comboBox_File.Items.Add(file.Substring(file.LastIndexOf("\\") + 1));
  127. }
  128. }
  129. #endregion
  130. #region 选择文件日期后更新文件列表
  131. private void comboBox_cfgFileDate_SelectionChangeCommitted(object sender, EventArgs e)
  132. {
  133. comboBox_File.Text = "";
  134. GetFileListFrom(comboBox_cfgFileDate.SelectedItem.ToString());
  135. }
  136. #endregion
  137. #region 选择文件后加载文件
  138. private void comboBox_File_SelectionChangeCommitted(object sender, EventArgs e)
  139. {
  140. //清空原来数据
  141. array_CfgInfo.Clear();
  142. string SelectFile = "";
  143. if (checkBox_LocalFile.Checked == false)//非本地模式从服务器下载文件
  144. {
  145. SelectFile = comboBox_cfgFileDate.SelectedItem.ToString() + "\\" + comboBox_File.SelectedItem.ToString();
  146. if (!File.Exists(LocalCfgFilePath + SelectFile))//检查本地是否存在选定文件
  147. {
  148. SelectFile = comboBox_cfgFileDate.SelectedItem.ToString() + "/" + comboBox_File.SelectedItem.ToString();
  149. StartForm.myFtp.DownloadFile(StartForm.myServerCfg.RootPath + "/cfg/" + SelectFile, LocalCfgFilePath + comboBox_cfgFileDate.SelectedItem.ToString());
  150. }
  151. }
  152. SelectFile = comboBox_cfgFileDate.SelectedItem.ToString() + "\\" + comboBox_File.SelectedItem.ToString();
  153. //打开选中文件
  154. StreamReader objReader = new StreamReader(LocalCfgFilePath + SelectFile);
  155. string sLine = "";
  156. while (sLine != null)
  157. {
  158. sLine = objReader.ReadLine();
  159. array_CfgInfo.Add(sLine);
  160. }
  161. objReader.Close();
  162. //解析显示配置文件
  163. richTextBox_CfgFile.Text = "";
  164. try
  165. {
  166. foreach (string sOutput in array_CfgInfo)
  167. {
  168. richTextBox_CfgFile.AppendText(sOutput + "\r\n");
  169. }
  170. }
  171. catch (System.Exception)
  172. {
  173. return;
  174. }
  175. //更新推行模式测试判断标准
  176. MotorRadeSpeed = Convert.ToUInt16(array_CfgInfo[42].ToString().Split(',')[1]);
  177. RateCadence = (ushort)(MotorRadeSpeed / 11U);
  178. RateVoltage = (ushort)(Convert.ToByte(array_CfgInfo[47].ToString().Split(',')[1]) * 1000);
  179. //更新指拨和速度传感器测试标志
  180. byte temp = Convert.ToByte(array_CfgInfo[25].ToString().Split(',')[1]);
  181. GasModeTest = temp == 0x55 ? false : temp == 0xAA ? true : false;
  182. temp = Convert.ToByte(array_CfgInfo[26].ToString().Split(',')[1]);
  183. SpeedSensorTest = temp == 0x55 || (temp == 0xAA ? false : temp == 0xEE ? false : false);
  184. //更新空载电流和车速传感器判断阈值
  185. if (array_CfgInfo.Count > 56)//旧版本无改配置项,采用默认值
  186. {
  187. No_load_Current = Convert.ToUInt16(array_CfgInfo[59].ToString().Split(',')[1]);
  188. SpeedSensorThData = Convert.ToUInt16(array_CfgInfo[60].ToString().Split(',')[1]);
  189. GasModeSpeed = Convert.ToUInt16(array_CfgInfo[61].ToString().Split(',')[1]);
  190. }
  191. }
  192. #endregion
  193. #region 修改获取文件方式,本地或服务器
  194. private void checkBox_LocalFile_CheckedChanged(object sender, EventArgs e)
  195. {
  196. comboBox_cfgFileDate.Text = "";
  197. comboBox_File.Text = "";
  198. richTextBox_CfgFile.Text = "";
  199. GetDateList();
  200. }
  201. #endregion
  202. private void ImportForm_Load(object sender, EventArgs e)
  203. {
  204. timer1.Enabled = true;
  205. timer2.Enabled = true;
  206. //装载配置文件
  207. GetDateList();
  208. //装载序列号文件
  209. string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\SerialNum";
  210. StreamReader objReader = new StreamReader(cfg_file);
  211. string sLine = "";
  212. ArrayList arrText = new ArrayList();//创建一个动态数组
  213. while (sLine != null)
  214. {
  215. sLine = objReader.ReadLine();
  216. arrText.Add(sLine);
  217. }
  218. objReader.Close();
  219. label_SerialNum.Text = arrText[1].ToString();//流水号
  220. }
  221. private void toolStripMenuIte_PortSet_Click(object sender, EventArgs e)
  222. {
  223. portsettings_form.Show();
  224. }
  225. #region CRC32校验
  226. static UInt32[] crc32_table = new UInt32[256]
  227. {
  228. 0x00000000, 0x04C11DB7, 0x09823B6E, 0x0D4326D9, 0x130476DC, 0x17C56B6B,
  229. 0x1A864DB2, 0x1E475005, 0x2608EDB8, 0x22C9F00F, 0x2F8AD6D6, 0x2B4BCB61,
  230. 0x350C9B64, 0x31CD86D3, 0x3C8EA00A, 0x384FBDBD, 0x4C11DB70, 0x48D0C6C7,
  231. 0x4593E01E, 0x4152FDA9, 0x5F15ADAC, 0x5BD4B01B, 0x569796C2, 0x52568B75,
  232. 0x6A1936C8, 0x6ED82B7F, 0x639B0DA6, 0x675A1011, 0x791D4014, 0x7DDC5DA3,
  233. 0x709F7B7A, 0x745E66CD, 0x9823B6E0, 0x9CE2AB57, 0x91A18D8E, 0x95609039,
  234. 0x8B27C03C, 0x8FE6DD8B, 0x82A5FB52, 0x8664E6E5, 0xBE2B5B58, 0xBAEA46EF,
  235. 0xB7A96036, 0xB3687D81, 0xAD2F2D84, 0xA9EE3033, 0xA4AD16EA, 0xA06C0B5D,
  236. 0xD4326D90, 0xD0F37027, 0xDDB056FE, 0xD9714B49, 0xC7361B4C, 0xC3F706FB,
  237. 0xCEB42022, 0xCA753D95, 0xF23A8028, 0xF6FB9D9F, 0xFBB8BB46, 0xFF79A6F1,
  238. 0xE13EF6F4, 0xE5FFEB43, 0xE8BCCD9A, 0xEC7DD02D, 0x34867077, 0x30476DC0,
  239. 0x3D044B19, 0x39C556AE, 0x278206AB, 0x23431B1C, 0x2E003DC5, 0x2AC12072,
  240. 0x128E9DCF, 0x164F8078, 0x1B0CA6A1, 0x1FCDBB16, 0x018AEB13, 0x054BF6A4,
  241. 0x0808D07D, 0x0CC9CDCA, 0x7897AB07, 0x7C56B6B0, 0x71159069, 0x75D48DDE,
  242. 0x6B93DDDB, 0x6F52C06C, 0x6211E6B5, 0x66D0FB02, 0x5E9F46BF, 0x5A5E5B08,
  243. 0x571D7DD1, 0x53DC6066, 0x4D9B3063, 0x495A2DD4, 0x44190B0D, 0x40D816BA,
  244. 0xACA5C697, 0xA864DB20, 0xA527FDF9, 0xA1E6E04E, 0xBFA1B04B, 0xBB60ADFC,
  245. 0xB6238B25, 0xB2E29692, 0x8AAD2B2F, 0x8E6C3698, 0x832F1041, 0x87EE0DF6,
  246. 0x99A95DF3, 0x9D684044, 0x902B669D, 0x94EA7B2A, 0xE0B41DE7, 0xE4750050,
  247. 0xE9362689, 0xEDF73B3E, 0xF3B06B3B, 0xF771768C, 0xFA325055, 0xFEF34DE2,
  248. 0xC6BCF05F, 0xC27DEDE8, 0xCF3ECB31, 0xCBFFD686, 0xD5B88683, 0xD1799B34,
  249. 0xDC3ABDED, 0xD8FBA05A, 0x690CE0EE, 0x6DCDFD59, 0x608EDB80, 0x644FC637,
  250. 0x7A089632, 0x7EC98B85, 0x738AAD5C, 0x774BB0EB, 0x4F040D56, 0x4BC510E1,
  251. 0x46863638, 0x42472B8F, 0x5C007B8A, 0x58C1663D, 0x558240E4, 0x51435D53,
  252. 0x251D3B9E, 0x21DC2629, 0x2C9F00F0, 0x285E1D47, 0x36194D42, 0x32D850F5,
  253. 0x3F9B762C, 0x3B5A6B9B, 0x0315D626, 0x07D4CB91, 0x0A97ED48, 0x0E56F0FF,
  254. 0x1011A0FA, 0x14D0BD4D, 0x19939B94, 0x1D528623, 0xF12F560E, 0xF5EE4BB9,
  255. 0xF8AD6D60, 0xFC6C70D7, 0xE22B20D2, 0xE6EA3D65, 0xEBA91BBC, 0xEF68060B,
  256. 0xD727BBB6, 0xD3E6A601, 0xDEA580D8, 0xDA649D6F, 0xC423CD6A, 0xC0E2D0DD,
  257. 0xCDA1F604, 0xC960EBB3, 0xBD3E8D7E, 0xB9FF90C9, 0xB4BCB610, 0xB07DABA7,
  258. 0xAE3AFBA2, 0xAAFBE615, 0xA7B8C0CC, 0xA379DD7B, 0x9B3660C6, 0x9FF77D71,
  259. 0x92B45BA8, 0x9675461F, 0x8832161A, 0x8CF30BAD, 0x81B02D74, 0x857130C3,
  260. 0x5D8A9099, 0x594B8D2E, 0x5408ABF7, 0x50C9B640, 0x4E8EE645, 0x4A4FFBF2,
  261. 0x470CDD2B, 0x43CDC09C, 0x7B827D21, 0x7F436096, 0x7200464F, 0x76C15BF8,
  262. 0x68860BFD, 0x6C47164A, 0x61043093, 0x65C52D24, 0x119B4BE9, 0x155A565E,
  263. 0x18197087, 0x1CD86D30, 0x029F3D35, 0x065E2082, 0x0B1D065B, 0x0FDC1BEC,
  264. 0x3793A651, 0x3352BBE6, 0x3E119D3F, 0x3AD08088, 0x2497D08D, 0x2056CD3A,
  265. 0x2D15EBE3, 0x29D4F654, 0xC5A92679, 0xC1683BCE, 0xCC2B1D17, 0xC8EA00A0,
  266. 0xD6AD50A5, 0xD26C4D12, 0xDF2F6BCB, 0xDBEE767C, 0xE3A1CBC1, 0xE760D676,
  267. 0xEA23F0AF, 0xEEE2ED18, 0xF0A5BD1D, 0xF464A0AA, 0xF9278673, 0xFDE69BC4,
  268. 0x89B8FD09, 0x8D79E0BE, 0x803AC667, 0x84FBDBD0, 0x9ABC8BD5, 0x9E7D9662,
  269. 0x933EB0BB, 0x97FFAD0C, 0xAFB010B1, 0xAB710D06, 0xA6322BDF, 0xA2F33668,
  270. 0xBCB4666D, 0xB8757BDA, 0xB5365D03, 0xB1F740B4
  271. };
  272. static UInt32 crc32_cal(byte[] pData, UInt16 Length)
  273. {
  274. UInt32 nReg;
  275. UInt32 nTemp = 0;
  276. UInt16 i, n;
  277. nReg = 0xFFFFFFFF;
  278. for (n = 0; n < Length; n++)
  279. {
  280. nReg ^= (UInt32)pData[n];
  281. for (i = 0; i < 4; i++)
  282. {
  283. nTemp = crc32_table[((nReg >> 24) & 0xFF)];
  284. nReg <<= 8;
  285. nReg ^= nTemp;
  286. }
  287. }
  288. return nReg;
  289. }
  290. #endregion
  291. #region 命令发送
  292. private void SendCmd(ushort ID, byte Mode, ushort Cmd, byte[] Data)
  293. {
  294. if (!portsettings_form.serialPort1.IsOpen)
  295. {
  296. MessageBox.Show("串口未连接");
  297. return;
  298. }
  299. ushort DataLength = (ushort)(Cmd & 0xFF);
  300. var SendCmdTmp = new byte[DataLength + 13];
  301. //帧头
  302. SendCmdTmp[0] = 0x55;
  303. SendCmdTmp[1] = 0xAA;
  304. //ID
  305. SendCmdTmp[2] = (byte)(ID >> 8);
  306. SendCmdTmp[3] = (byte)(ID & 0xFF);
  307. //MODE
  308. SendCmdTmp[4] = Mode;
  309. //长度
  310. SendCmdTmp[5] = (byte)((Cmd & 0xFF) + 2);
  311. //命令字
  312. SendCmdTmp[6] = (byte)(Cmd >> 8);
  313. SendCmdTmp[7] = (byte)(Cmd & 0xFF);
  314. //数据段
  315. for (UInt16 i = 0; i < (Cmd & 0xFF); i++)
  316. {
  317. SendCmdTmp[8 + i] = Data[i];
  318. }
  319. //校验
  320. UInt32 CRC_Result = crc32_cal(SendCmdTmp, (ushort)(DataLength + 8));
  321. SendCmdTmp[DataLength + 8] = Convert.ToByte(CRC_Result >> 24);
  322. SendCmdTmp[DataLength + 9] = Convert.ToByte((CRC_Result >> 16) % 256);
  323. SendCmdTmp[DataLength + 10] = Convert.ToByte((CRC_Result >> 8) % 256);
  324. SendCmdTmp[DataLength + 11] = Convert.ToByte(CRC_Result % 256);
  325. //帧尾
  326. SendCmdTmp[DataLength + 12] = 0xF0;
  327. //发送数据
  328. portsettings_form.serialPort1.Write(SendCmdTmp, 0, SendCmdTmp.Length);
  329. }
  330. #endregion
  331. #region 串口数据解析
  332. private void Serial_DataReceived_Process()
  333. {
  334. //串口解析
  335. byte DataNum = 0; //记录每条命令数据段的长度
  336. while (buffer.Count >= 13)//当buffer中的数据的个数大于等于4个时,即每一帧的数据长度所在位(第4位)出现时
  337. {
  338. if (buffer[0] == 0x55 && buffer[1] == 0xAA)//判断帧头
  339. {
  340. ushort PackageID;
  341. PackageID = (ushort)(buffer[2] * 256 + buffer[3]);
  342. if ((PackageID == 0x0710) || (PackageID == 0x0715) ||
  343. (PackageID == 0x0610) || (PackageID == 0x0615) ||
  344. (PackageID == 0x07FF) ||
  345. (PackageID == 0x0725) || (PackageID == 0x0720))
  346. {
  347. int CmdLen = buffer[5];
  348. if (buffer.Count < CmdLen + 11) break;//如果接收到的数据没有达到一帧数据的指定长度, 则执行下次循环
  349. buffer.RemoveRange(0, 6);//数据达到要求长度后删去帧头,ID,模式,和命令长度
  350. DataNum = (byte)(buffer[1]);//命令字的第二个字节的表示数据位的长度
  351. if (DataNum == (CmdLen - 2))
  352. {
  353. for (int i = 0; i < DataNum + 2; i++)
  354. {
  355. binary_data_1[i] = buffer[i];
  356. }
  357. if (TestStep == TestStep_Enum.Step_Save)
  358. {
  359. //数据存储
  360. var RecData = new byte[CmdLen];
  361. Array.Copy(binary_data_1, RecData, CmdLen);
  362. if ((PackageID == 0x0710) || (PackageID == 0x0715) ||
  363. (PackageID == 0x0610) || (PackageID == 0x0615) ||
  364. (PackageID == 0x07FF))
  365. {
  366. DataSave(RecData);
  367. }
  368. }
  369. DataCmdProcess(PackageID);//对于不同的命令段,做不同的处理
  370. }
  371. buffer.RemoveRange(0, CmdLen + 5);//解析完成
  372. }
  373. else
  374. {
  375. buffer.RemoveAt(0);//ID不对
  376. }
  377. }
  378. else
  379. {
  380. buffer.RemoveAt(0);//帧头不对,删除帧头高字节
  381. }
  382. }
  383. }
  384. #endregion
  385. #region 数据存储
  386. private void DataSave(byte[] Data)
  387. {
  388. string SaveData = "";
  389. //数据存储
  390. for (int i = 0; i < Data.Length; i++)
  391. {
  392. SaveData += Convert.ToString(Data[i], 16).PadLeft(2, '0').ToUpper() + " ";
  393. }
  394. SaveData += "\r\n";
  395. System.IO.File.AppendAllText(LogSaveFileName, SaveData);
  396. }
  397. #endregion
  398. //故障转换和显示
  399. List<string> ErrorInfo = new List<string>{ "过流保护", "低压保护", "过压保护", "堵转保护","过热保护","速度传感器故障","力矩传感器故障","霍尔故障",
  400. "马达缺相","NTC故障","","","","MCU故障","踏频传感器故障","指拨故障","MOS短路","电压异常","电路故障","TE故障","TE电路故障","","","","","","","","","",
  401. "",""};
  402. private void ErrorInfoDisplay(UInt32 Code)
  403. {
  404. label_ErrorInfo.Text = "";
  405. if (Code == 0)
  406. {
  407. label_ErrorInfo.Text = "无故障";
  408. label_ErrorInfo.ForeColor = Color.Green;
  409. }
  410. else
  411. {
  412. label_ErrorInfo.ForeColor = Color.Red;
  413. for (int i = 0; i < 32; i++)
  414. {
  415. if ((Code & 0x01) == 0x01)
  416. {
  417. label_ErrorInfo.Text += ErrorInfo[i] + " ";
  418. }
  419. Code >>= 1;
  420. }
  421. }
  422. }
  423. #region 数据解析处理
  424. private void DataCmdProcess(ushort ID)
  425. {
  426. ushort CmdTemp = (ushort)(binary_data_1[0] * 256 + binary_data_1[1]);
  427. ushort DataTemp;
  428. #region 主控信息
  429. if ((ID == 0x715) || (ID == 0x710))
  430. {
  431. switch (CmdTemp)
  432. {
  433. case 0x1020://电机运行信息
  434. {
  435. unchecked
  436. {
  437. this.Invoke((EventHandler)(delegate
  438. {
  439. MC_RunInfo_Refresh = true;
  440. //车速
  441. DataTemp = (ushort)(binary_data_1[3] * 256 + binary_data_1[2]);
  442. textBox_BikeSpeed.Text = ((float)DataTemp / 10f).ToString("0.0");
  443. //输出转速
  444. DataTemp = (ushort)(binary_data_1[5] * 256 + binary_data_1[4]);
  445. textBox_MotorSpeed.Text = Convert.ToString(DataTemp);
  446. //母线电压
  447. DataTemp = (ushort)(binary_data_1[9] * 256 + binary_data_1[8]);
  448. textBox_Vol.Text = Convert.ToString(DataTemp);
  449. //母线电流
  450. DataTemp = (ushort)(binary_data_1[11] * 256 + binary_data_1[10]);
  451. textBox_Cur.Text = Convert.ToString(DataTemp);
  452. //踏频
  453. DataTemp = (ushort)(binary_data_1[12]);
  454. textBox_Cadence.Text = Convert.ToString(DataTemp);
  455. //踩踏力矩
  456. DataTemp = (ushort)(binary_data_1[13]);
  457. textBox_Torque.Text = Convert.ToString(DataTemp);
  458. //踩踏方向
  459. DataTemp = (ushort)(binary_data_1[14]);
  460. if (DataTemp == 0)
  461. {
  462. textBox_Dir.Text = "正向";
  463. }
  464. else if (DataTemp == 1)
  465. {
  466. textBox_Dir.Text = "反向";
  467. }
  468. else if (DataTemp == 2)
  469. {
  470. textBox_Dir.Text = "停止";
  471. }
  472. //PCB温度
  473. DataTemp = (ushort)(binary_data_1[23]);
  474. textBox_PCB.Text = Convert.ToString((int)(DataTemp - 40));
  475. //绕组温度
  476. DataTemp = (ushort)(binary_data_1[24]);
  477. textBox_Coil.Text = Convert.ToString((int)(DataTemp - 40));
  478. //MCU温度
  479. DataTemp = (ushort)(binary_data_1[25]);
  480. textBox_MCU.Text = Convert.ToString((int)(DataTemp - 40));
  481. }));
  482. }
  483. break;
  484. }
  485. case 0x1104://故障码
  486. {
  487. unchecked
  488. {
  489. this.Invoke((EventHandler)(delegate
  490. {
  491. MC_ErrorCode_Refresh = true;
  492. label_error_code.Text =
  493. Convert.ToString(binary_data_1[5], 16).PadLeft(2, '0').ToUpper() +
  494. Convert.ToString(binary_data_1[4], 16).PadLeft(2, '0').ToUpper() +
  495. Convert.ToString(binary_data_1[3], 16).PadLeft(2, '0').ToUpper() +
  496. Convert.ToString(binary_data_1[2], 16).PadLeft(2, '0').ToUpper() +
  497. "H";
  498. label_error_code.BackColor = Color.Red;
  499. UInt32 ErrorCode = (UInt32)((binary_data_1[5] << 24) + (binary_data_1[4] << 16) + (binary_data_1[3] << 8) + binary_data_1[2]);
  500. ErrorInfoDisplay(ErrorCode);
  501. }));
  502. }
  503. break;
  504. }
  505. case 0x1240://电机版本信息
  506. {
  507. unchecked
  508. {
  509. this.Invoke((EventHandler)(delegate
  510. {
  511. //电机型号
  512. richTextBox_Ver.Text += "型号:";
  513. for (ushort i = 0; i < 16; i++)
  514. {
  515. if (binary_data_1[2 + i] == 0x2E)
  516. {
  517. break;
  518. }
  519. richTextBox_Ver.Text += ((char)binary_data_1[2 + i]).ToString();
  520. }
  521. richTextBox_Ver.Text += "\r\n";
  522. //电机SN
  523. richTextBox_Ver.Text += "序列号:";
  524. for (ushort i = 0; i < 16; i++)
  525. {
  526. if (binary_data_1[18 + i] == 0x2E)
  527. {
  528. break;
  529. }
  530. richTextBox_Ver.Text += ((char)binary_data_1[18 + i]).ToString();
  531. }
  532. richTextBox_Ver.Text += "\r\n";
  533. //电机HW
  534. richTextBox_Ver.Text += "硬件版本:";
  535. for (ushort i = 0; i < 10; i++)//前10位是PCB版号,以'.'结束
  536. {
  537. if (binary_data_1[34 + i] == 0x2E)
  538. {
  539. break;
  540. }
  541. if (binary_data_1[34 + i] == 'r')
  542. {
  543. binary_data_1[34 + i] = (byte)'.';
  544. }
  545. richTextBox_Ver.Text += ((char)binary_data_1[34 + i]).ToString();
  546. }
  547. switch (binary_data_1[34 + 10])//第10位代表MCU品牌
  548. {
  549. case (byte)'1':
  550. richTextBox_Ver.Text += " " + "ST" + " "; break;
  551. case (byte)'2':
  552. richTextBox_Ver.Text += " " + "APM" + " "; break;
  553. case (byte)'3':
  554. richTextBox_Ver.Text += " " + "HK" + " "; break;
  555. case (byte)'4':
  556. richTextBox_Ver.Text += " " + "GD" + " "; break;
  557. default:
  558. richTextBox_Ver.Text += " " + "NULL" + " "; break;
  559. }
  560. for (ushort i = 0; i < 4; i++)//后4位是MCU的ID校验码,用于识别板子的ID
  561. {
  562. richTextBox_Ver.Text += Convert.ToString(binary_data_1[46 + i], 16).PadLeft(2, '0').ToUpper();//MCU ID 的CRC32值
  563. }
  564. richTextBox_Ver.Text += "\r\n";
  565. //电机FW
  566. richTextBox_Ver.Text += "软件版本:";
  567. for (ushort i = 0; i < 16; i++)
  568. {
  569. if (binary_data_1[50 + i] == 0x2E)
  570. {
  571. break;
  572. }
  573. if (binary_data_1[50 + i] == 'r')
  574. {
  575. binary_data_1[50 + i] = (byte)'.';
  576. }
  577. richTextBox_Ver.Text += ((char)binary_data_1[50 + i]).ToString();
  578. }
  579. richTextBox_Ver.Text += "\r\n";
  580. }));
  581. }
  582. break;
  583. }
  584. case 0x1720://电机程序特性信息
  585. {
  586. unchecked
  587. {
  588. this.Invoke((EventHandler)(delegate
  589. {
  590. //程序特性
  591. for (ushort i = 0; i < 32; i++)
  592. {
  593. if (binary_data_1[2 + i] == 0x2E)
  594. {
  595. break;
  596. }
  597. if (binary_data_1[2 + i] == 'r')
  598. {
  599. binary_data_1[2 + i] = (byte)'.';
  600. }
  601. richTextBox_Ver.Text += ((char)binary_data_1[2 + i]).ToString();
  602. }
  603. richTextBox_Ver.Text += "\r\n";
  604. }));
  605. }
  606. break;
  607. }
  608. case 0xA903://反馈指令
  609. {
  610. unchecked
  611. {
  612. this.Invoke((EventHandler)(delegate
  613. {
  614. Ack_Flag = true;
  615. StepTestFlag = StepTestResult_Enum.StepTestResult_OK;
  616. }));
  617. }
  618. break;
  619. }
  620. default: break;
  621. }
  622. }
  623. #endregion
  624. #region TE
  625. else if ((ID == 0x615) || (ID == 0x610))
  626. {
  627. switch (CmdTemp)
  628. {
  629. case 0x1240://TE APP版本信息
  630. {
  631. unchecked
  632. {
  633. this.Invoke((EventHandler)(delegate
  634. {
  635. //TE 软件版本
  636. richTextBox_Ver.Text += "TE软件版本:";
  637. for (ushort i = 0; i < 16; i++)
  638. {
  639. if (binary_data_1[50 + i] == 0x2E)
  640. {
  641. break;
  642. }
  643. if (binary_data_1[50 + i] == 'r')
  644. {
  645. binary_data_1[50 + i] = (byte)'.';
  646. }
  647. richTextBox_Ver.Text += ((char)binary_data_1[50 + i]).ToString();
  648. }
  649. }));
  650. }
  651. break;
  652. }
  653. default: break;
  654. }
  655. }
  656. #endregion
  657. #region //电池信息
  658. else if ((ID == 0x725) || (ID == 0x720))
  659. {
  660. label_BMS_COM.BackColor = Color.Green;
  661. label_BMS_COM.Text = "OK";
  662. }
  663. #endregion
  664. }
  665. #endregion
  666. //20ms定时器
  667. private void timer1_Tick(object sender, EventArgs e)
  668. {
  669. //串口数据解析
  670. Serial_DataReceived_Process();
  671. }
  672. #region 非独占性延时函数
  673. public static void Delay_ms(int milliSecond)
  674. {
  675. int start = Environment.TickCount;
  676. while (Math.Abs(Environment.TickCount - start) < milliSecond)//毫秒
  677. {
  678. Application.DoEvents();
  679. }
  680. }
  681. #endregion
  682. //电机运行信息清除
  683. private void MC_RunInfo_Clear()
  684. {
  685. foreach (Control c in groupBox9.Controls)
  686. {
  687. if (c is TextBox)
  688. {
  689. c.Text = "---";
  690. }
  691. }
  692. }
  693. private void ImportForm_FormClosing(object sender, FormClosingEventArgs e)
  694. {
  695. portsettings_form.serialPort1.Close();
  696. portsettings_form.comboBox_ComIndex.Enabled = true;
  697. portsettings_form.button_Enter.Text = "连接";
  698. portsettings_form.g_blnIsOpen = false;
  699. }
  700. //开关灯
  701. private void LightProcess()
  702. {
  703. var CtrlCode = new byte[2];
  704. CtrlCode[0] = 0x00;
  705. if (button_LightCtl.Text == "开灯")
  706. {
  707. button_LightCtl.Text = "关灯";
  708. CtrlCode[1] = 0xF1;
  709. button_LightCtl.BackColor = Color.Yellow;
  710. button_LightCtl.ForeColor = Color.Black;
  711. }
  712. else
  713. {
  714. button_LightCtl.Text = "开灯";
  715. CtrlCode[1] = 0xF0;
  716. button_LightCtl.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
  717. button_LightCtl.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(48)))), ((int)(((byte)(113)))), ((int)(((byte)(185)))));
  718. }
  719. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  720. }
  721. //开关灯按钮
  722. private void button_LightCtl_Click(object sender, EventArgs e)
  723. {
  724. LightProcess();
  725. }
  726. //电机启动/停止控制
  727. private void Walk_Process(byte GearSt)
  728. {
  729. var Code = new byte[2];
  730. if (button_Walk.Text == "启动")
  731. {
  732. button_Walk.Text = "停止";
  733. //发送指令使电机进入配置模式
  734. Code[0] = 0x01;
  735. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, Code);
  736. Thread.Sleep(200);
  737. //发送指令进入设定档位
  738. Code[0] = GearSt;
  739. if (button_LightCtl.Text == "关灯")
  740. Code[1] = 0xF1;
  741. else
  742. Code[1] = 0xF0;
  743. button_Walk.BackColor = Color.Yellow;
  744. button_Walk.ForeColor = Color.Black;
  745. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, Code);
  746. }
  747. else
  748. {
  749. button_Walk.Text = "启动";
  750. //发送指令关闭档位模式
  751. Code[0] = 0x00;
  752. if (button_LightCtl.Text == "关灯")
  753. Code[1] = 0xF1;
  754. else
  755. Code[1] = 0xF0;
  756. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, Code);
  757. Thread.Sleep(200);
  758. //发送指令使电机退出配置模式
  759. Code[0] = 0x00;
  760. button_Walk.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
  761. button_Walk.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(48)))), ((int)(((byte)(113)))), ((int)(((byte)(185)))));
  762. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, Code);
  763. }
  764. }
  765. //电机启动/停止按钮
  766. private void button_Walk_Click(object sender, EventArgs e)
  767. {
  768. Walk_Process(0x22);
  769. }
  770. //查询版本信息
  771. private void button_Check_Ver_Click(object sender, EventArgs e)
  772. {
  773. richTextBox_Ver.Text = "";
  774. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
  775. Delay_ms(200);
  776. SendCmd((ushort)0x651, (byte)0x11, (ushort)0x1200, null);
  777. }
  778. //按钮初始化
  779. private void label_Init()
  780. {
  781. radioButton_Light_OK.Checked = false;//灯电压确认按钮
  782. radioButton_Light_OK.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
  783. radioButton_Light_OK.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(48)))), ((int)(((byte)(113)))), ((int)(((byte)(185)))));
  784. radioButton_Light_NG.Checked = false;
  785. radioButton_Light_NG.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
  786. radioButton_Light_NG.ForeColor= System.Drawing.Color.FromArgb(((int)(((byte)(48)))), ((int)(((byte)(113)))), ((int)(((byte)(185)))));
  787. label_error_code.BackColor = Color.Green;//故障码
  788. label_error_code.Text = "OK";
  789. label_Sta_Clear.BackColor = Color.Red;//系统清除
  790. label_Sta_Clear.Text = "NG";
  791. label_Sta_Write.BackColor = Color.Red;//参数写入
  792. label_Sta_Write.Text = "NG";
  793. label_sta_light.BackColor = Color.Red;//灯电压
  794. label_sta_light.Text = "NG";
  795. label_sensor_vol.BackColor = Color.Red;//电压检测
  796. label_sensor_vol.Text = "NG";
  797. label_sensor_cur.BackColor = Color.Red;//电流检测
  798. label_sensor_cur.Text = "NG";
  799. label_sensor_speed.BackColor = Color.Red;//车速检测
  800. label_sensor_speed.Text = "NG";
  801. label_sensor_cadence.BackColor = Color.Red;//踏频检测
  802. label_sensor_cadence.Text = "NG";
  803. label_sensor_motor.BackColor = Color.Red;//电机转速检测
  804. label_sensor_motor.Text = "NG";
  805. label_sensor_PCB_temp.BackColor = Color.Red;//PCB温度检测
  806. label_sensor_PCB_temp.Text = "NG";
  807. label_sensor_coil_temp.BackColor = Color.Red;//绕组温度检测
  808. label_sensor_coil_temp.Text = "NG";
  809. label_sensor_MCU_temp.BackColor = Color.Red;//MCU温度检测
  810. label_sensor_MCU_temp.Text = "NG";
  811. label_Sensor_Break1.BackColor = Color.Red;//断电刹把
  812. label_Sensor_Break1.Text = "NG";
  813. label_Sensor_Break2.BackColor = Color.Red;//断电刹把
  814. label_Sensor_Break2.Text = "NG";
  815. label_Sensor_Gas.BackColor = Color.Red;//转把
  816. label_Sensor_Gas.Text = "NG";
  817. label_FW_Version.BackColor = Color.Red;//版本信息
  818. label_FW_Version.Text = "NG";
  819. label_BMS_COM.BackColor = Color.Red;//BMS通信
  820. label_BMS_COM.Text = "NG";
  821. label_Result.BackColor = Color.Red;//测试结果
  822. label_Result.Text = "不合格";
  823. richTextBox_Ver.Text = "";//版本信息
  824. richTextBox_TestRecord.Text = "";//测试过程记录
  825. }
  826. //测试失败存储测试界面
  827. private void ErrorSaveLog()
  828. {
  829. //页面存储
  830. Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap
  831. Graphics g = Graphics.FromImage(bit);
  832. g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片
  833. LogSaveFileName = LogSavePath + "\\errorlog\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\";
  834. if (System.IO.Directory.Exists(LogSaveFileName) == false)
  835. System.IO.Directory.CreateDirectory(LogSaveFileName);
  836. LogSaveFileName += textBox_PDinfo.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_测试记录" + ".png";
  837. bit.Save(LogSaveFileName);
  838. //测试页面上传服务器
  839. //if (StartForm.myFtp.IsNetConnected == true)
  840. //{
  841. // string DateNow = DateTime.Now.ToString("yyyy-MM-dd");
  842. // if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/walkTest/errorlog/", DateNow) == false)
  843. // {
  844. // StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/walkTest/errorlog/" + DateNow);
  845. // }
  846. // bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/walkTest/errorlog/" + DateNow);
  847. // if (result == true)
  848. // richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "测试异常页面上传服务器成功" + "\r\n";
  849. //}
  850. }
  851. //执行一次测试任务
  852. private void Thread_Task()
  853. {
  854. //开始测试
  855. richTextBox_TestRecord.Text += "电机信息:" + textBox_PDinfo.Text + "\r\n";
  856. richTextBox_TestRecord.Text += "配置文件:" + comboBox_File.Text + "\r\n";
  857. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "开始测试" + "\r\n";
  858. //公有变量
  859. var Code = new byte[32];//发送的指令数据
  860. ushort DataTemp;
  861. short DataTemp_Int16;
  862. string ResultPathName = "";
  863. //开机
  864. Code[0] = 0xF1;
  865. SendCmd(0x7FF, 0x16, 0x2201, Code);
  866. 开ToolStripMenuItem.Checked = true;
  867. 关ToolStripMenuItem.Checked = false;
  868. Delay_ms(500);
  869. //检查是否有故障
  870. if (label_error_code.Text != "OK")
  871. {
  872. label_Step.Text = "电机存在故障!";
  873. button_Start.Enabled = true;
  874. //关机
  875. Code[0] = 0xF0;
  876. SendCmd(0x7FF, 0x16, 0x2201, Code);
  877. 开ToolStripMenuItem.Checked = false;
  878. 关ToolStripMenuItem.Checked = true;
  879. //关闭串口
  880. portsettings_form.closePort();
  881. //存储测试数据
  882. ErrorSaveLog();
  883. return;
  884. }
  885. #region Step1:发送指令,系统清除
  886. TestStep = TestStep_Enum.Step_SysClear;
  887. label_Step.Text = "系统清除,请等待!";
  888. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "发送系统清除指令" + "\r\n";
  889. Array.Clear(Code, 0, 32);
  890. Code[0] = (byte)'C';
  891. Code[1] = (byte)'L';
  892. Code[2] = (byte)'E';
  893. Code[3] = (byte)'A';
  894. Code[4] = (byte)'R';
  895. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2605, Code);
  896. Ack_Flag = false;
  897. Ack_Cnt = 0;
  898. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  899. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing);//等待测试结束
  900. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  901. {
  902. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "系统清除成功" + "\r\n";
  903. label_Sta_Clear.BackColor = Color.Green;
  904. label_Sta_Clear.Text = "OK";
  905. }
  906. else//失败
  907. {
  908. label_Step.Text = "系统清除失败,测试结束!";
  909. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "系统清除失败,测试结束" + "\r\n";
  910. button_Start.Enabled = true;
  911. //关机
  912. Code[0] = 0xF0;
  913. SendCmd(0x7FF, 0x16, 0x2201, Code);
  914. 开ToolStripMenuItem.Checked = false;
  915. 关ToolStripMenuItem.Checked = true;
  916. //关闭串口
  917. portsettings_form.closePort();
  918. //存储测试数据
  919. ErrorSaveLog();
  920. return;
  921. }
  922. #endregion
  923. //系统清除完成后会重启,等待3s
  924. Delay_ms(2000);
  925. #region Step2:发送指令,依次写入参数
  926. TestStep = TestStep_Enum.Step_Write;
  927. label_Step.Text = "参数写入中,请等待!";
  928. #region Step2.1:写入电机型号和序列号
  929. string Mode = textBox_PDinfo.Text.Substring(0, textBox_PDinfo.Text.LastIndexOf(' '));
  930. string SN = textBox_PDinfo.Text.Remove(0, textBox_PDinfo.Text.LastIndexOf(' ') + 1);
  931. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入电机型号" + "\r\n";
  932. Array.Clear(Code, 0, 32);
  933. for (ushort i = 0; i < ((Mode.Length > 16) ? 16 : Mode.Length); i++)
  934. {
  935. Code[i] = (byte)Mode[i];
  936. }
  937. if (Mode.Length < 16)
  938. {
  939. Code[Mode.Length] = (byte)'.';
  940. for (ushort i = 0; i < 16 - Mode.Length - 1; i++)
  941. {
  942. Code[Mode.Length + 1 + i] = 0x20;
  943. }
  944. }
  945. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2210, Code);
  946. Ack_Flag = false;
  947. Ack_Cnt = 0;
  948. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  949. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  950. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  951. {
  952. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机型号写入成功" + "\r\n";
  953. }
  954. else//失败
  955. {
  956. label_Step.Text = "参数写入失败,测试结束!";
  957. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机型号写入失败,测试结束" + "\r\n";
  958. //关机
  959. Code[0] = 0xF0;
  960. SendCmd(0x7FF, 0x16, 0x2201, Code);
  961. 开ToolStripMenuItem.Checked = false;
  962. 关ToolStripMenuItem.Checked = true;
  963. button_Start.Enabled = true;
  964. //关闭串口
  965. portsettings_form.closePort();
  966. //存储测试数据
  967. ErrorSaveLog();
  968. return;
  969. }
  970. Delay_ms(200);
  971. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入电机序列号" + "\r\n";
  972. Array.Clear(Code, 0, 32);
  973. for (ushort i = 0; i < ((SN.Length > 16) ? 16 : SN.Length); i++)
  974. {
  975. Code[i] = (byte)SN[i];
  976. }
  977. if (SN.Length < 16)
  978. {
  979. Code[SN.Length] = (byte)'.';
  980. for (ushort i = 0; i < 16 - SN.Length - 1; i++)
  981. {
  982. Code[SN.Length + 1 + i] = 0x20;
  983. }
  984. }
  985. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2310, Code);
  986. Ack_Flag = false;
  987. Ack_Cnt = 0;
  988. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  989. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  990. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  991. {
  992. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机序列号写入成功" + "\r\n";
  993. }
  994. else//失败
  995. {
  996. label_Step.Text = "参数写入失败,测试结束!";
  997. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机序列号写入失败,测试结束" + "\r\n";
  998. //关机
  999. Code[0] = 0xF0;
  1000. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1001. 开ToolStripMenuItem.Checked = false;
  1002. 关ToolStripMenuItem.Checked = true;
  1003. button_Start.Enabled = true;
  1004. //关闭串口
  1005. portsettings_form.closePort();
  1006. //存储测试数据
  1007. ErrorSaveLog();
  1008. return;
  1009. }
  1010. #endregion
  1011. #region Step2.2:写入用户参数1
  1012. Delay_ms(200);
  1013. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入用户参数1" + "\r\n";
  1014. Array.Clear(Code, 0, 32);
  1015. //指拨模式
  1016. Code[0] = Convert.ToByte(array_CfgInfo[25].ToString().Split(',')[1]);
  1017. //启动模式
  1018. Code[1] = Convert.ToByte(array_CfgInfo[1].ToString().Split(',')[1]);
  1019. //停机时间
  1020. DataTemp = Convert.ToUInt16(array_CfgInfo[2].ToString().Split(',')[1]);
  1021. Code[2] = (byte)(DataTemp & 0xFF);
  1022. Code[3] = (byte)(DataTemp >> 8);
  1023. //限速
  1024. Code[4] = Convert.ToByte(array_CfgInfo[3].ToString().Split(',')[1]);
  1025. //下降速度
  1026. Code[5] = Convert.ToByte(array_CfgInfo[4].ToString().Split(',')[1]);
  1027. //前飞
  1028. Code[6] = Convert.ToByte(array_CfgInfo[5].ToString().Split(',')[1]);
  1029. //后飞
  1030. Code[7] = Convert.ToByte(array_CfgInfo[6].ToString().Split(',')[1]);
  1031. //限流
  1032. Code[8] = Convert.ToByte(array_CfgInfo[7].ToString().Split(',')[1]);
  1033. //温度预警
  1034. Code[9] = (byte)(Convert.ToByte(array_CfgInfo[8].ToString().Split(',')[1]) + 40);
  1035. //温度保护
  1036. Code[10] = (byte)(Convert.ToByte(array_CfgInfo[9].ToString().Split(',')[1]) + 40);
  1037. //无PBU
  1038. Code[11] = Convert.ToByte(array_CfgInfo[10].ToString().Split(',')[1]);
  1039. //轮胎周长
  1040. Code[12] = Convert.ToByte(array_CfgInfo[11].ToString().Split(',')[1]);
  1041. //电机系列号
  1042. Code[13] = Convert.ToByte(array_CfgInfo[12].ToString().Split(',')[1]);
  1043. //ECO助力增益
  1044. Code[14] = Convert.ToByte(array_CfgInfo[13].ToString().Split(',')[1]);
  1045. //ECO加速
  1046. Code[15] = Convert.ToByte(array_CfgInfo[14].ToString().Split(',')[1]);
  1047. //NORM助力增益
  1048. Code[16] = Convert.ToByte(array_CfgInfo[15].ToString().Split(',')[1]);
  1049. //NORM加速
  1050. Code[17] = Convert.ToByte(array_CfgInfo[16].ToString().Split(',')[1]);
  1051. //SPORT助力增益
  1052. Code[18] = Convert.ToByte(array_CfgInfo[17].ToString().Split(',')[1]);
  1053. //SPORT加速
  1054. Code[19] = Convert.ToByte(array_CfgInfo[18].ToString().Split(',')[1]);
  1055. //TURBO助力增益
  1056. Code[20] = Convert.ToByte(array_CfgInfo[19].ToString().Split(',')[1]);
  1057. //TURBO加速
  1058. Code[21] = Convert.ToByte(array_CfgInfo[20].ToString().Split(',')[1]);
  1059. //SMART助力增益
  1060. Code[22] = Convert.ToByte(array_CfgInfo[21].ToString().Split(',')[1]);
  1061. //SMART加速
  1062. Code[23] = Convert.ToByte(array_CfgInfo[22].ToString().Split(',')[1]);
  1063. //速度传感器信号个数
  1064. Code[24] = Convert.ToByte(array_CfgInfo[23].ToString().Split(',')[1]);
  1065. //踏频启动信号个数
  1066. Code[25] = Convert.ToByte(array_CfgInfo[24].ToString().Split(',')[1]);
  1067. //速度传感器
  1068. Code[26] = Convert.ToByte(array_CfgInfo[26].ToString().Split(',')[1]);
  1069. //周长微调
  1070. Code[27] = (byte)Convert.ToInt16(array_CfgInfo[27].ToString().Split(',')[1]);
  1071. //低压保护阈值
  1072. DataTemp = Convert.ToUInt16(array_CfgInfo[28].ToString().Split(',')[1]);
  1073. Code[28] = (byte)(DataTemp & 0xFF);
  1074. Code[29] = (byte)(DataTemp >> 8);
  1075. //推行限速
  1076. DataTemp = Convert.ToByte(array_CfgInfo[29].ToString().Split(',')[1]);
  1077. Code[30] = (byte)Convert.ToByte(DataTemp);
  1078. //推行马达转速
  1079. Code[31] = (byte)Convert.ToByte(array_CfgInfo[30].ToString().Split(',')[1]);
  1080. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1B20, Code);
  1081. Ack_Flag = false;
  1082. Ack_Cnt = 0;
  1083. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1084. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1085. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1086. {
  1087. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "用户参数1写入成功" + "\r\n";
  1088. }
  1089. else//失败
  1090. {
  1091. label_Step.Text = "参数写入失败,测试结束!";
  1092. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "用户参数1写入失败,测试结束" + "\r\n";
  1093. //关机
  1094. Code[0] = 0xF0;
  1095. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1096. 开ToolStripMenuItem.Checked = false;
  1097. 关ToolStripMenuItem.Checked = true;
  1098. button_Start.Enabled = true;
  1099. //关闭串口
  1100. portsettings_form.closePort();
  1101. //存储测试数据
  1102. ErrorSaveLog();
  1103. return;
  1104. }
  1105. #endregion
  1106. #region Step2.3:写入用户参数2
  1107. Delay_ms(200);
  1108. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入用户参数2" + "\r\n";
  1109. Array.Clear(Code, 0, 32);
  1110. //俯仰角零偏
  1111. DataTemp_Int16 = (short)(decimal.Parse(array_CfgInfo[34].ToString().Split(',')[1]) * 10);
  1112. Code[0] = (byte)(DataTemp_Int16 & 0xFF);
  1113. Code[1] = (byte)(DataTemp_Int16 >> 8);
  1114. //横滚角零偏
  1115. DataTemp_Int16 = (short)(decimal.Parse(array_CfgInfo[35].ToString().Split(',')[1]) * 10);
  1116. Code[2] = (byte)(DataTemp_Int16 & 0xFF);
  1117. Code[3] = (byte)(DataTemp_Int16 >> 8);
  1118. //支持姿态传感器
  1119. Code[4] = Convert.ToByte(array_CfgInfo[33].ToString().Split(',')[1]);
  1120. //尾灯模式
  1121. Code[5] = Convert.ToByte(array_CfgInfo[36].ToString().Split(',')[1]);
  1122. //前灯电压
  1123. Code[6] = Convert.ToByte(array_CfgInfo[37].ToString().Split(',')[1]);
  1124. //后灯电压
  1125. Code[7] = Convert.ToByte(array_CfgInfo[38].ToString().Split(',')[1]);
  1126. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3120, Code);
  1127. Ack_Flag = false;
  1128. Ack_Cnt = 0;
  1129. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1130. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1131. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1132. {
  1133. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "用户参数2写入成功" + "\r\n";
  1134. }
  1135. else//失败
  1136. {
  1137. label_Step.Text = "参数写入失败,测试结束!";
  1138. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "用户参数2写入失败,测试结束" + "\r\n";
  1139. //关机
  1140. Code[0] = 0xF0;
  1141. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1142. 开ToolStripMenuItem.Checked = false;
  1143. 关ToolStripMenuItem.Checked = true;
  1144. button_Start.Enabled = true;
  1145. //关闭串口
  1146. portsettings_form.closePort();
  1147. //存储测试数据
  1148. ErrorSaveLog();
  1149. return;
  1150. }
  1151. #endregion
  1152. #region Step2.4:写入马达参数
  1153. Delay_ms(200);
  1154. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入马达参数" + "\r\n";
  1155. Array.Clear(Code, 0, 32);
  1156. //额定功率
  1157. DataTemp = Convert.ToUInt16(array_CfgInfo[41].ToString().Split(',')[1]);
  1158. Code[0] = (byte)(DataTemp & 0xFF);
  1159. Code[1] = (byte)(DataTemp >> 8);
  1160. //额定转速
  1161. DataTemp = Convert.ToUInt16(array_CfgInfo[42].ToString().Split(',')[1]);
  1162. Code[2] = (byte)(DataTemp & 0xFF);
  1163. Code[3] = (byte)(DataTemp >> 8);
  1164. //定子电阻
  1165. DataTemp = Convert.ToUInt16(array_CfgInfo[43].ToString().Split(',')[1]);
  1166. Code[4] = (byte)(DataTemp & 0xFF);
  1167. Code[5] = (byte)(DataTemp >> 8);
  1168. //Lq
  1169. DataTemp = Convert.ToUInt16(array_CfgInfo[44].ToString().Split(',')[1]);
  1170. Code[6] = (byte)(DataTemp & 0xFF);
  1171. Code[7] = (byte)(DataTemp >> 8);
  1172. //Ld
  1173. DataTemp = Convert.ToUInt16(array_CfgInfo[45].ToString().Split(',')[1]);
  1174. Code[8] = (byte)(DataTemp & 0xFF);
  1175. Code[9] = (byte)(DataTemp >> 8);
  1176. //反电动势
  1177. DataTemp = Convert.ToUInt16(array_CfgInfo[46].ToString().Split(',')[1]);
  1178. Code[10] = (byte)(DataTemp & 0xFF);
  1179. Code[11] = (byte)(DataTemp >> 8);
  1180. //额定电压
  1181. Code[12] = Convert.ToByte(array_CfgInfo[47].ToString().Split(',')[1]);
  1182. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1D10, Code);
  1183. Ack_Flag = false;
  1184. Ack_Cnt = 0;
  1185. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1186. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1187. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1188. {
  1189. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "马达参数写入成功" + "\r\n";
  1190. }
  1191. else//失败
  1192. {
  1193. label_Step.Text = "参数写入失败,测试结束!";
  1194. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "马达参数写入失败,测试结束" + "\r\n";
  1195. //关机
  1196. Code[0] = 0xF0;
  1197. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1198. 开ToolStripMenuItem.Checked = false;
  1199. 关ToolStripMenuItem.Checked = true;
  1200. button_Start.Enabled = true;
  1201. //关闭串口
  1202. portsettings_form.closePort();
  1203. //存储测试数据
  1204. ErrorSaveLog();
  1205. return;
  1206. }
  1207. #endregion
  1208. #region Step2.5:写入生产信息
  1209. Delay_ms(200);
  1210. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入生产信息" + "\r\n";
  1211. Array.Clear(Code, 0, 32);
  1212. string MAC_Info = array_CfgInfo[50].ToString().Split(',')[1];
  1213. for (ushort i = 0; i < ((MAC_Info.Length > 8) ? 8 : MAC_Info.Length); i++)
  1214. {
  1215. Code[i] = (byte)MAC_Info[i];
  1216. }
  1217. MAC_Info = array_CfgInfo[51].ToString().Split(',')[1];
  1218. for (ushort i = 0; i < ((MAC_Info.Length > 8) ? 8 : MAC_Info.Length); i++)
  1219. {
  1220. Code[8 + i] = (byte)MAC_Info[i];
  1221. }
  1222. MAC_Info = array_CfgInfo[52].ToString().Split(',')[1];
  1223. for (ushort i = 0; i < ((MAC_Info.Length > 8) ? 8 : MAC_Info.Length); i++)
  1224. {
  1225. Code[16 + i] = (byte)MAC_Info[i];
  1226. }
  1227. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2420, Code);
  1228. Ack_Flag = false;
  1229. Ack_Cnt = 0;
  1230. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1231. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1232. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1233. {
  1234. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "生产信息写入成功" + "\r\n";
  1235. }
  1236. else//失败
  1237. {
  1238. label_Step.Text = "参数写入失败,测试结束!";
  1239. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "生产信息写入失败,测试结束" + "\r\n";
  1240. //关机
  1241. Code[0] = 0xF0;
  1242. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1243. 开ToolStripMenuItem.Checked = false;
  1244. 关ToolStripMenuItem.Checked = true;
  1245. button_Start.Enabled = true;
  1246. //关闭串口
  1247. portsettings_form.closePort();
  1248. //存储测试数据
  1249. ErrorSaveLog();
  1250. return;
  1251. }
  1252. #endregion
  1253. #region Step2.6:写入校验密钥
  1254. Delay_ms(200);
  1255. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入校验密钥" + "\r\n";
  1256. Array.Clear(Code, 0, 32);
  1257. if (array_CfgInfo[53].ToString().EndsWith(",") == false)
  1258. {
  1259. string KeyString = array_CfgInfo[53].ToString().Split(',')[1];
  1260. for (ushort i = 0; i < ((KeyString.Length > 8) ? 8 : KeyString.Length); i++)
  1261. {
  1262. Code[i] = (byte)KeyString[i];
  1263. }
  1264. }
  1265. else
  1266. {
  1267. Code.Initialize();
  1268. }
  1269. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1108, Code);
  1270. Ack_Flag = false;
  1271. Ack_Cnt = 0;
  1272. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1273. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1274. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1275. {
  1276. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "校验密钥写入成功" + "\r\n";
  1277. }
  1278. else//失败
  1279. {
  1280. label_Step.Text = "参数写入失败,测试结束!";
  1281. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "校验密钥写入失败,测试结束" + "\r\n";
  1282. //关机
  1283. Code[0] = 0xF0;
  1284. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1285. 开ToolStripMenuItem.Checked = false;
  1286. 关ToolStripMenuItem.Checked = true;
  1287. button_Start.Enabled = true;
  1288. //关闭串口
  1289. portsettings_form.closePort();
  1290. //存储测试数据
  1291. ErrorSaveLog();
  1292. return;
  1293. }
  1294. #endregion
  1295. #region Step2.7:写入自定义信息1、2、3
  1296. //自定义信息1
  1297. Delay_ms(200);
  1298. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入自定义信息1" + "\r\n";
  1299. Array.Clear(Code, 0, 32);
  1300. if (array_CfgInfo[54].ToString().EndsWith(",") == false)
  1301. {
  1302. string UserInfo1String = array_CfgInfo[54].ToString().Split(',')[1];
  1303. for (ushort i = 0; i < (UserInfo1String.Length > 16 ? 16 : UserInfo1String.Length); i++)
  1304. {
  1305. Code[i] = (byte)UserInfo1String[i];
  1306. }
  1307. }
  1308. else
  1309. {
  1310. Array.Clear(Code, 0, 16);
  1311. }
  1312. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1410, Code);
  1313. Ack_Flag = false;
  1314. Ack_Cnt = 0;
  1315. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1316. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1317. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1318. {
  1319. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "自定义信息1写入成功" + "\r\n";
  1320. }
  1321. else//失败
  1322. {
  1323. label_Step.Text = "参数写入失败,测试结束!";
  1324. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "自定义信息1写入失败,测试结束" + "\r\n";
  1325. //关机
  1326. Code[0] = 0xF0;
  1327. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1328. 开ToolStripMenuItem.Checked = false;
  1329. 关ToolStripMenuItem.Checked = true;
  1330. button_Start.Enabled = true;
  1331. //关闭串口
  1332. portsettings_form.closePort();
  1333. //存储测试数据
  1334. ErrorSaveLog();
  1335. return;
  1336. }
  1337. //自定义信息2
  1338. Delay_ms(200);
  1339. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入自定义信息2" + "\r\n";
  1340. Array.Clear(Code, 0, 32);
  1341. if (array_CfgInfo[55].ToString().EndsWith(",") == false)
  1342. {
  1343. string UserInfo2String = array_CfgInfo[55].ToString().Split(',')[1];
  1344. for (ushort i = 0; i < (UserInfo2String.Length > 16 ? 16 : UserInfo2String.Length); i++)
  1345. {
  1346. Code[i] = (byte)UserInfo2String[i];
  1347. }
  1348. }
  1349. else
  1350. {
  1351. Array.Clear(Code, 0, 16);
  1352. }
  1353. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1610, Code);
  1354. Ack_Flag = false;
  1355. Ack_Cnt = 0;
  1356. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1357. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1358. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1359. {
  1360. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "自定义信息2写入成功" + "\r\n";
  1361. }
  1362. else//失败
  1363. {
  1364. label_Step.Text = "参数写入失败,测试结束!";
  1365. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "自定义信息2写入失败,测试结束" + "\r\n";
  1366. //关机
  1367. Code[0] = 0xF0;
  1368. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1369. 开ToolStripMenuItem.Checked = false;
  1370. 关ToolStripMenuItem.Checked = true;
  1371. button_Start.Enabled = true;
  1372. //关闭串口
  1373. portsettings_form.closePort();
  1374. //存储测试数据
  1375. ErrorSaveLog();
  1376. return;
  1377. }
  1378. //自定义信息3
  1379. Delay_ms(200);
  1380. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入自定义信息3" + "\r\n";
  1381. Array.Clear(Code, 0, 32);
  1382. if (array_CfgInfo[56].ToString().EndsWith(",") == false)
  1383. {
  1384. string UserInfo3String = array_CfgInfo[56].ToString().Split(',')[1];
  1385. for (ushort i = 0; i < (UserInfo3String.Length > 16 ? 16 : UserInfo3String.Length); i++)
  1386. {
  1387. Code[i] = (byte)UserInfo3String[i];
  1388. }
  1389. }
  1390. else
  1391. {
  1392. Array.Clear(Code, 0, 16);
  1393. }
  1394. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1810, Code);
  1395. Ack_Flag = false;
  1396. Ack_Cnt = 0;
  1397. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1398. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1399. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1400. {
  1401. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "自定义信息3写入成功" + "\r\n";
  1402. }
  1403. else//失败
  1404. {
  1405. label_Step.Text = "参数写入失败,测试结束!";
  1406. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "自定义信息3写入失败,测试结束" + "\r\n";
  1407. //关机
  1408. Code[0] = 0xF0;
  1409. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1410. 开ToolStripMenuItem.Checked = false;
  1411. 关ToolStripMenuItem.Checked = true;
  1412. button_Start.Enabled = true;
  1413. //关闭串口
  1414. portsettings_form.closePort();
  1415. //存储测试数据
  1416. ErrorSaveLog();
  1417. return;
  1418. }
  1419. #endregion
  1420. //参数写入完成
  1421. label_Sta_Write.BackColor = Color.Green;
  1422. label_Sta_Write.Text = "OK";
  1423. #endregion
  1424. #region Step3:灯电压测试
  1425. TestStep = TestStep_Enum.Step_Light;
  1426. label_Step.Text = "灯电压测试,请确认电压!";
  1427. button_LightCtl.Text = "开灯";
  1428. LightProcess();
  1429. radioButton_Light_OK.Checked = false;
  1430. radioButton_Light_OK.BackColor = Color.Green;
  1431. radioButton_Light_OK.ForeColor = Color.White;
  1432. radioButton_Light_OK.Enabled = true;
  1433. radioButton_Light_NG.Checked = false;
  1434. radioButton_Light_NG.Enabled = true;
  1435. radioButton_Light_NG.BackColor = Color.Red;
  1436. radioButton_Light_NG.ForeColor = Color.White;
  1437. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "发送开灯指令,请确认灯电压是否正确……" + "\r\n";
  1438. while ((radioButton_Light_OK.Checked == false) && (radioButton_Light_NG.Checked == false)) ;
  1439. if (radioButton_Light_OK.Checked == true)
  1440. {
  1441. label_sta_light.BackColor = Color.Green;
  1442. radioButton_Light_NG.BackColor = Color.Gray;
  1443. label_sta_light.Text = "OK";
  1444. radioButton_Light_OK.Enabled = false;
  1445. radioButton_Light_NG.Enabled = false;
  1446. }
  1447. else if (radioButton_Light_NG.Checked == true)
  1448. {
  1449. label_sta_light.BackColor = Color.Red;
  1450. radioButton_Light_OK.BackColor = Color.Gray;
  1451. label_sta_light.Text = "NG";
  1452. label_Step.Text = "灯电压错误,测试结束!";
  1453. button_LightCtl.Text = "关灯";
  1454. LightProcess();
  1455. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "灯电压错误,测试结束" + "\r\n";
  1456. //关机
  1457. Code[0] = 0xF0;
  1458. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1459. 开ToolStripMenuItem.Checked = false;
  1460. 关ToolStripMenuItem.Checked = true;
  1461. button_Start.Enabled = true;
  1462. radioButton_Light_OK.Enabled = false;
  1463. radioButton_Light_NG.Enabled = false;
  1464. //关闭串口
  1465. portsettings_form.closePort();
  1466. //存储测试数据
  1467. ErrorSaveLog();
  1468. return;
  1469. }
  1470. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "灯电压确认完成" + "\r\n";
  1471. button_LightCtl.Text = "关灯";
  1472. LightProcess();
  1473. #endregion
  1474. #region Step4:推行测试
  1475. TestStep = TestStep_Enum.Step_Walk;
  1476. label_Step.Text = "推行测试,电机开始自动运转!";
  1477. button_Walk.Text = "启动";
  1478. Walk_Process(0x22);
  1479. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机进入推行模式,请注意安全!" + "\r\n";
  1480. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "传感器自动检测开始" + "\r\n";
  1481. OK_cnt = 0;
  1482. WalkProcess_Cnt = 0;
  1483. while (WalkProcess_Cnt < 20)//开始判断传感器,超时20s
  1484. {
  1485. //车速传感器
  1486. if (SpeedSensorTest == false)
  1487. {
  1488. if (label_sensor_speed.BackColor == Color.Red)
  1489. {
  1490. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "跳过车速传感器测试" + "\r\n";
  1491. label_sensor_speed.BackColor = Color.Green;
  1492. label_sensor_speed.Text = "OK";
  1493. OK_cnt++;
  1494. }
  1495. }
  1496. else
  1497. {
  1498. try
  1499. {
  1500. DataTemp = (ushort)(Convert.ToDecimal(textBox_BikeSpeed.Text) * 10);
  1501. }
  1502. catch (System.Exception ex)
  1503. {
  1504. MessageBox.Show(ex.Message);
  1505. label_Step.Text = "推行测试异常,测试结束!";
  1506. //关机
  1507. Code[0] = 0xF0;
  1508. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1509. 开ToolStripMenuItem.Checked = false;
  1510. 关ToolStripMenuItem.Checked = true;
  1511. button_Start.Enabled = true;
  1512. //关闭串口
  1513. portsettings_form.closePort();
  1514. //存储测试数据
  1515. ErrorSaveLog();
  1516. return;
  1517. }
  1518. if ((DataTemp > (SpeedSensorThData - 50)) && (DataTemp < (SpeedSensorThData + 50)))
  1519. {
  1520. if (label_sensor_speed.BackColor == Color.Red)
  1521. {
  1522. label_sensor_speed.BackColor = Color.Green;
  1523. label_sensor_speed.Text = "OK";
  1524. OK_cnt++;
  1525. }
  1526. }
  1527. }
  1528. //马达转速
  1529. try
  1530. {
  1531. DataTemp = Convert.ToUInt16(textBox_MotorSpeed.Text);
  1532. }
  1533. catch (System.Exception ex)
  1534. {
  1535. MessageBox.Show(ex.Message);
  1536. label_Step.Text = "推行测试异常,测试结束!";
  1537. //关机
  1538. Code[0] = 0xF0;
  1539. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1540. 开ToolStripMenuItem.Checked = false;
  1541. 关ToolStripMenuItem.Checked = true;
  1542. button_Start.Enabled = true;
  1543. //关闭串口
  1544. portsettings_form.closePort();
  1545. //存储测试数据
  1546. ErrorSaveLog();
  1547. return;
  1548. }
  1549. if ((DataTemp > (MotorRadeSpeed - 200)) && (DataTemp < (MotorRadeSpeed + 200)))
  1550. {
  1551. if (label_sensor_motor.BackColor == Color.Red)
  1552. {
  1553. label_sensor_motor.BackColor = Color.Green;
  1554. label_sensor_motor.Text = "OK";
  1555. OK_cnt++;
  1556. }
  1557. }
  1558. //母线电压
  1559. try
  1560. {
  1561. DataTemp = Convert.ToUInt16(textBox_Vol.Text);
  1562. }
  1563. catch (System.Exception ex)
  1564. {
  1565. MessageBox.Show(ex.Message);
  1566. label_Step.Text = "推行测试异常,测试结束!";
  1567. //关机
  1568. Code[0] = 0xF0;
  1569. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1570. 开ToolStripMenuItem.Checked = false;
  1571. 关ToolStripMenuItem.Checked = true;
  1572. button_Start.Enabled = true;
  1573. //关闭串口
  1574. portsettings_form.closePort();
  1575. //存储测试数据
  1576. ErrorSaveLog();
  1577. return;
  1578. }
  1579. if ((DataTemp > (RateVoltage - 1000)) && (DataTemp < (RateVoltage + 1000)))
  1580. {
  1581. if (label_sensor_vol.BackColor == Color.Red)
  1582. {
  1583. label_sensor_vol.BackColor = Color.Green;
  1584. label_sensor_vol.Text = "OK";
  1585. OK_cnt++;
  1586. }
  1587. }
  1588. //母线电流
  1589. try
  1590. {
  1591. DataTemp = Convert.ToUInt16(textBox_Cur.Text);
  1592. }
  1593. catch (System.Exception ex)
  1594. {
  1595. MessageBox.Show(ex.Message);
  1596. label_Step.Text = "推行测试异常,测试结束!";
  1597. //关机
  1598. Code[0] = 0xF0;
  1599. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1600. 开ToolStripMenuItem.Checked = false;
  1601. 关ToolStripMenuItem.Checked = true;
  1602. button_Start.Enabled = true;
  1603. //关闭串口
  1604. portsettings_form.closePort();
  1605. //存储测试数据
  1606. ErrorSaveLog();
  1607. return;
  1608. }
  1609. if ((DataTemp > No_load_Current - 600) && (DataTemp < No_load_Current + 600))
  1610. {
  1611. if (label_sensor_cur.BackColor == Color.Red)
  1612. {
  1613. label_sensor_cur.BackColor = Color.Green;
  1614. label_sensor_cur.Text = "OK";
  1615. OK_cnt++;
  1616. }
  1617. }
  1618. //踏频
  1619. try
  1620. {
  1621. DataTemp = Convert.ToUInt16(textBox_Cadence.Text);
  1622. }
  1623. catch (System.Exception ex)
  1624. {
  1625. MessageBox.Show(ex.Message);
  1626. label_Step.Text = "推行测试异常,测试结束!";
  1627. //关机
  1628. Code[0] = 0xF0;
  1629. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1630. 开ToolStripMenuItem.Checked = false;
  1631. 关ToolStripMenuItem.Checked = true;
  1632. button_Start.Enabled = true;
  1633. //关闭串口
  1634. portsettings_form.closePort();
  1635. //存储测试数据
  1636. ErrorSaveLog();
  1637. return;
  1638. }
  1639. if ((DataTemp > (RateCadence - 20)) && (DataTemp < (RateCadence + 20)))
  1640. {
  1641. if (label_sensor_cadence.BackColor == Color.Red)
  1642. {
  1643. label_sensor_cadence.BackColor = Color.Green;
  1644. label_sensor_cadence.Text = "OK";
  1645. OK_cnt++;
  1646. }
  1647. }
  1648. //PCB温度
  1649. try
  1650. {
  1651. DataTemp_Int16 = Convert.ToInt16(textBox_PCB.Text);
  1652. }
  1653. catch (System.Exception ex)
  1654. {
  1655. MessageBox.Show(ex.Message);
  1656. label_Step.Text = "推行测试异常,测试结束!";
  1657. //关机
  1658. Code[0] = 0xF0;
  1659. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1660. 开ToolStripMenuItem.Checked = false;
  1661. 关ToolStripMenuItem.Checked = true;
  1662. button_Start.Enabled = true;
  1663. //关闭串口
  1664. portsettings_form.closePort();
  1665. //存储测试数据
  1666. ErrorSaveLog();
  1667. return;
  1668. }
  1669. if ((DataTemp_Int16 > -20) && (DataTemp_Int16 < 60))
  1670. {
  1671. if (label_sensor_PCB_temp.BackColor == Color.Red)
  1672. {
  1673. label_sensor_PCB_temp.BackColor = Color.Green;
  1674. label_sensor_PCB_temp.Text = "OK";
  1675. OK_cnt++;
  1676. }
  1677. }
  1678. //绕组温度
  1679. try
  1680. {
  1681. DataTemp_Int16 = Convert.ToInt16(textBox_Coil.Text);
  1682. }
  1683. catch (System.Exception ex)
  1684. {
  1685. MessageBox.Show(ex.Message);
  1686. label_Step.Text = "推行测试异常,测试结束!";
  1687. //关机
  1688. Code[0] = 0xF0;
  1689. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1690. 开ToolStripMenuItem.Checked = false;
  1691. 关ToolStripMenuItem.Checked = true;
  1692. button_Start.Enabled = true;
  1693. //关闭串口
  1694. portsettings_form.closePort();
  1695. //存储测试数据
  1696. ErrorSaveLog();
  1697. return;
  1698. }
  1699. if ((DataTemp_Int16 > -20) && (DataTemp_Int16 < 60))
  1700. {
  1701. if (label_sensor_coil_temp.BackColor == Color.Red)
  1702. {
  1703. label_sensor_coil_temp.BackColor = Color.Green;
  1704. label_sensor_coil_temp.Text = "OK";
  1705. OK_cnt++;
  1706. }
  1707. }
  1708. //MCU温度
  1709. try
  1710. {
  1711. DataTemp_Int16 = Convert.ToInt16(textBox_MCU.Text);
  1712. }
  1713. catch (System.Exception ex)
  1714. {
  1715. MessageBox.Show(ex.Message);
  1716. label_Step.Text = "推行测试异常,测试结束!";
  1717. //关机
  1718. Code[0] = 0xF0;
  1719. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1720. 开ToolStripMenuItem.Checked = false;
  1721. 关ToolStripMenuItem.Checked = true;
  1722. button_Start.Enabled = true;
  1723. //关闭串口
  1724. portsettings_form.closePort();
  1725. //存储测试数据
  1726. ErrorSaveLog();
  1727. return;
  1728. }
  1729. if ((DataTemp_Int16 > -20) && (DataTemp_Int16 < 60))
  1730. {
  1731. if (label_sensor_MCU_temp.BackColor == Color.Red)
  1732. {
  1733. label_sensor_MCU_temp.BackColor = Color.Green;
  1734. label_sensor_MCU_temp.Text = "OK";
  1735. OK_cnt++;
  1736. }
  1737. }
  1738. //测试完成,退出
  1739. if (OK_cnt == 8)
  1740. {
  1741. break;
  1742. }
  1743. }
  1744. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "传感器自动检测结束" + "\r\n";
  1745. //断电开关1
  1746. label_Step.Text = "请在10s内捏下断电刹把 1!";
  1747. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "断电刹把 1 检测开始,请捏下断电刹把 1" + "\r\n";
  1748. WalkProcess_Cnt = 0;
  1749. while (WalkProcess_Cnt < 15)//10s内判断电机是否停止
  1750. {
  1751. try
  1752. {
  1753. DataTemp = Convert.ToUInt16(textBox_MotorSpeed.Text);
  1754. }
  1755. catch (System.Exception ex)
  1756. {
  1757. MessageBox.Show(ex.Message);
  1758. label_Step.Text = "推行测试异常,测试结束!";
  1759. //关机
  1760. Code[0] = 0xF0;
  1761. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1762. 开ToolStripMenuItem.Checked = false;
  1763. 关ToolStripMenuItem.Checked = true;
  1764. button_Start.Enabled = true;
  1765. //关闭串口
  1766. portsettings_form.closePort();
  1767. //存储测试数据
  1768. ErrorSaveLog();
  1769. return;
  1770. }
  1771. if (DataTemp < 100)
  1772. {
  1773. if (label_Sensor_Break1.BackColor == Color.Red)
  1774. {
  1775. label_Sensor_Break1.BackColor = Color.Green;
  1776. label_Sensor_Break1.Text = "OK";
  1777. OK_cnt++;
  1778. }
  1779. break;
  1780. }
  1781. }
  1782. label_Step.Text = "请松开断电刹把 1";
  1783. WalkProcess_Cnt = 0;
  1784. while (WalkProcess_Cnt < 10)//等待电机再次启动,如果超时提示错误
  1785. {
  1786. try
  1787. {
  1788. DataTemp = Convert.ToUInt16(textBox_MotorSpeed.Text);
  1789. }
  1790. catch (System.Exception ex)
  1791. {
  1792. MessageBox.Show(ex.Message);
  1793. label_Step.Text = "推行测试异常,测试结束!";
  1794. //关机
  1795. Code[0] = 0xF0;
  1796. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1797. 开ToolStripMenuItem.Checked = false;
  1798. 关ToolStripMenuItem.Checked = true;
  1799. button_Start.Enabled = true;
  1800. //关闭串口
  1801. portsettings_form.closePort();
  1802. //存储测试数据
  1803. ErrorSaveLog();
  1804. return;
  1805. }
  1806. if (DataTemp > 300)//电机转速超过300rpm,再次启动测试
  1807. break;
  1808. }
  1809. if (WalkProcess_Cnt >= 10)//表示超时退出,电机未启动
  1810. {
  1811. label_Step.Text = "推行测试异常,测试结束!";
  1812. //关机
  1813. Code[0] = 0xF0;
  1814. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1815. 开ToolStripMenuItem.Checked = false;
  1816. 关ToolStripMenuItem.Checked = true;
  1817. button_Start.Enabled = true;
  1818. //关闭串口
  1819. portsettings_form.closePort();
  1820. //存储测试数据
  1821. ErrorSaveLog();
  1822. return;
  1823. }
  1824. //断电开关2
  1825. label_Step.Text = "请在10s内捏下断电刹把 2!";
  1826. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "断电刹把 2 检测开始,请捏下断电刹把 2" + "\r\n";
  1827. WalkProcess_Cnt = 0;
  1828. while (WalkProcess_Cnt < 15)//10s内判断电机是否停止
  1829. {
  1830. try
  1831. {
  1832. DataTemp = Convert.ToUInt16(textBox_MotorSpeed.Text);
  1833. }
  1834. catch (System.Exception ex)
  1835. {
  1836. MessageBox.Show(ex.Message);
  1837. label_Step.Text = "推行测试异常,测试结束!";
  1838. //关机
  1839. Code[0] = 0xF0;
  1840. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1841. 开ToolStripMenuItem.Checked = false;
  1842. 关ToolStripMenuItem.Checked = true;
  1843. button_Start.Enabled = true;
  1844. //关闭串口
  1845. portsettings_form.closePort();
  1846. //存储测试数据
  1847. ErrorSaveLog();
  1848. return;
  1849. }
  1850. if (DataTemp < 100)
  1851. {
  1852. if (label_Sensor_Break2.BackColor == Color.Red)
  1853. {
  1854. label_Sensor_Break2.BackColor = Color.Green;
  1855. label_Sensor_Break2.Text = "OK";
  1856. OK_cnt++;
  1857. }
  1858. break;
  1859. }
  1860. }
  1861. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "断电刹把检测结束" + "\r\n";
  1862. label_Step.Text = "请松开断电刹把 2!";
  1863. button_Walk.Text = "停止";
  1864. Walk_Process(0x00);
  1865. Delay_ms(1000);
  1866. //判断测试结果
  1867. if (OK_cnt < 10)
  1868. {
  1869. label_Step.Text = "推行测试异常,测试结束!";
  1870. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "推行测试失败,测试结束" + "\r\n";
  1871. //关机
  1872. Code[0] = 0xF0;
  1873. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1874. 开ToolStripMenuItem.Checked = false;
  1875. 关ToolStripMenuItem.Checked = true;
  1876. button_Start.Enabled = true;
  1877. //关闭串口
  1878. portsettings_form.closePort();
  1879. //存储测试数据
  1880. ErrorSaveLog();
  1881. return;
  1882. }
  1883. //判断BMS通信是否正常
  1884. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "检测BMS通信状态" + "\r\n";
  1885. if (label_BMS_COM.Text != "OK")
  1886. {
  1887. label_Step.Text = "BMS通信异常,测试结束!";
  1888. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "推行测试失败,测试结束" + "\r\n";
  1889. //关机
  1890. Code[0] = 0xF0;
  1891. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1892. 开ToolStripMenuItem.Checked = false;
  1893. 关ToolStripMenuItem.Checked = true;
  1894. button_Start.Enabled = true;
  1895. //关闭串口
  1896. portsettings_form.closePort();
  1897. //存储测试数据
  1898. ErrorSaveLog();
  1899. return;
  1900. }
  1901. #endregion
  1902. #region Step5:转把模式测试(非必需)
  1903. if (GasModeTest == false)
  1904. {
  1905. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "跳过指拨模式测试" + "\r\n";
  1906. label_Sensor_Gas.BackColor = Color.Green;
  1907. label_Sensor_Gas.Text = "OK";
  1908. }
  1909. else
  1910. {
  1911. TestStep = TestStep_Enum.Step_GasMode;
  1912. label_Step.Text = "指拨模式测试,请在10s内转动指拨!";
  1913. button_Walk.Text = "启动";
  1914. Walk_Process(0x04);
  1915. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "指拨模式测试开始,请转动指拨" + "\r\n";
  1916. OK_cnt = 0;
  1917. WalkProcess_Cnt = 0;
  1918. while (WalkProcess_Cnt < 10)//10s内判断电机是否启动
  1919. {
  1920. try
  1921. {
  1922. DataTemp = Convert.ToUInt16(textBox_MotorSpeed.Text);
  1923. }
  1924. catch (System.Exception ex)
  1925. {
  1926. MessageBox.Show(ex.Message);
  1927. label_Step.Text = "推行测试异常,测试结束!";
  1928. //关机
  1929. Code[0] = 0xF0;
  1930. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1931. 开ToolStripMenuItem.Checked = false;
  1932. 关ToolStripMenuItem.Checked = true;
  1933. button_Start.Enabled = true;
  1934. //关闭串口
  1935. portsettings_form.closePort();
  1936. //存储测试数据
  1937. ErrorSaveLog();
  1938. return;
  1939. }
  1940. if ((DataTemp > (GasModeSpeed - 100)) && (DataTemp < (GasModeSpeed + 100)))
  1941. {
  1942. label_Sensor_Gas.BackColor = Color.Green;
  1943. label_Sensor_Gas.Text = "OK";
  1944. OK_cnt++;
  1945. break;
  1946. }
  1947. }
  1948. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "指拨模式测试结束" + "\r\n";
  1949. Delay_ms(1000);
  1950. button_Walk.Text = "停止";
  1951. Walk_Process(0x00);
  1952. //判断测试结果
  1953. if (OK_cnt < 1)
  1954. {
  1955. label_Step.Text = "指拨模式测试异常,测试结束!";
  1956. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "指拨模式测试失败,测试结束" + "\r\n";
  1957. //关机
  1958. Code[0] = 0xF0;
  1959. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1960. 开ToolStripMenuItem.Checked = false;
  1961. 关ToolStripMenuItem.Checked = true;
  1962. button_Start.Enabled = true;
  1963. //关闭串口
  1964. portsettings_form.closePort();
  1965. //存储测试数据
  1966. ErrorSaveLog();
  1967. return;
  1968. }
  1969. }
  1970. #endregion
  1971. #region Step6:版本查询和确认
  1972. TestStep = TestStep_Enum.Step_CheckVer;
  1973. label_Step.Text = "版本查询和确认";
  1974. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "发送指令查询版本信息" + "\r\n";
  1975. richTextBox_Ver.Text = "";
  1976. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
  1977. Delay_ms(200);
  1978. SendCmd((ushort)0x651, (byte)0x11, (ushort)0x1200, null);
  1979. Delay_ms(200);
  1980. string FW_Version_Check = comboBox_File.Text.Split('_')[2] + "_" + comboBox_File.Text.Split('_')[3];
  1981. string FW_Version_Result = "";
  1982. string TE_FW_Ver_Check = comboBox_File.Text.Split('_')[4] + "_" + comboBox_File.Text.Split('_')[5];
  1983. string TE_FW_Ver_Result = "";
  1984. try
  1985. {
  1986. FW_Version_Result = richTextBox_Ver.Lines[3].Split(':')[1];
  1987. FW_Version_Result = FW_Version_Result.Split('_')[0] + richTextBox_Ver.Lines[4].Split('-')[2].Replace('V', '.') + "_" + FW_Version_Result.Split('_')[1];
  1988. TE_FW_Ver_Result = richTextBox_Ver.Lines[5].Split(':')[1];
  1989. }
  1990. catch (System.Exception)
  1991. {
  1992. label_Step.Text = "软件版本信息不完整!";
  1993. }
  1994. if (string.Equals(FW_Version_Check, FW_Version_Result) == false) //主控版本错误,直接结束
  1995. {
  1996. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "版本信息错误" + "\r\n";
  1997. label_Step.Text = "软件版本错误,测试结束!";
  1998. //关机
  1999. Code[0] = 0xF0;
  2000. SendCmd(0x7FF, 0x16, 0x2201, Code);
  2001. 开ToolStripMenuItem.Checked = false;
  2002. 关ToolStripMenuItem.Checked = true;
  2003. button_Start.Enabled = true;
  2004. //关闭串口
  2005. portsettings_form.closePort();
  2006. //存储测试数据
  2007. ErrorSaveLog();
  2008. return;
  2009. }
  2010. else
  2011. {
  2012. if (string.Equals(TE_FW_Ver_Check, TE_FW_Ver_Result) == false) //TE版本错误,给出提示
  2013. {
  2014. if (MessageBox.Show("TE版本错误,请确认主控板是否为HK?", "TE版本错误", MessageBoxButtons.YesNo) == DialogResult.No)
  2015. {
  2016. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "TE软件版本错误" + "\r\n";
  2017. label_Step.Text = "TE软件版本错误,测试结束!";
  2018. //关机
  2019. Code[0] = 0xF0;
  2020. SendCmd(0x7FF, 0x16, 0x2201, Code);
  2021. 开ToolStripMenuItem.Checked = false;
  2022. 关ToolStripMenuItem.Checked = true;
  2023. button_Start.Enabled = true;
  2024. //关闭串口
  2025. portsettings_form.closePort();
  2026. //存储测试数据
  2027. ErrorSaveLog();
  2028. return;
  2029. }
  2030. }
  2031. label_FW_Version.BackColor = Color.Green;
  2032. label_FW_Version.Text = "OK";
  2033. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "版本信息正确" + "\r\n";
  2034. }
  2035. Delay_ms(1000);
  2036. #endregion
  2037. #region 按照产品信息设置测试文件路径
  2038. ResultPathName = textBox_PDinfo.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_");
  2039. if (System.IO.Directory.Exists(LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName) == false)
  2040. System.IO.Directory.CreateDirectory(LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName);
  2041. #endregion
  2042. #region Step7:参数查询和保存
  2043. TestStep = TestStep_Enum.Step_Save;
  2044. label_Step.Text = "参数导出";
  2045. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "开始导出电机内部参数" + "\r\n";
  2046. LogSaveFileName = LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName + "\\ParamsLog" + ".ttlog";
  2047. //开始发送指令
  2048. byte[] SendData = new byte[32];
  2049. //查询校验密钥
  2050. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1000, null);
  2051. Delay_ms(100);
  2052. //查询版本信息
  2053. //SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
  2054. //Delay_ms(100);
  2055. //SendCmd((ushort)0x651, (byte)0x11, (ushort)0x1200, null);
  2056. //Delay_ms(100);
  2057. //查询用户字符串1
  2058. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1300, null);
  2059. Delay_ms(100);
  2060. //查询用户字符串2
  2061. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1500, null);
  2062. Delay_ms(100);
  2063. //查询用户字符串3
  2064. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1700, null);
  2065. Delay_ms(100);
  2066. //查询用户参数1
  2067. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1A00, null);
  2068. Delay_ms(100);
  2069. //查询马达参数
  2070. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1C00, null);
  2071. Delay_ms(100);
  2072. //查询历史信息
  2073. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1E00, null);
  2074. Delay_ms(100);
  2075. //查询生产信息
  2076. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1F00, null);
  2077. Delay_ms(100);
  2078. //查询力矩传感器零点 Volans
  2079. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2000, null);
  2080. Delay_ms(100);
  2081. //查询运行信息
  2082. SendData[0] = 0x00;
  2083. SendData[1] = 0xF0;
  2084. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
  2085. Delay_ms(100);
  2086. SendData[0] = 0x01;
  2087. SendData[1] = 0xF0;
  2088. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
  2089. Delay_ms(100);
  2090. SendData[0] = 0x02;
  2091. SendData[1] = 0xF0;
  2092. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
  2093. Delay_ms(100);
  2094. SendData[0] = 0x03;
  2095. SendData[1] = 0xF0;
  2096. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
  2097. Delay_ms(100);
  2098. SendData[0] = 0x04;
  2099. SendData[1] = 0xF0;
  2100. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
  2101. Delay_ms(100);
  2102. SendData[0] = 0x33;
  2103. SendData[1] = 0xF0;
  2104. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
  2105. Delay_ms(100);
  2106. //查询力矩传感器校正信息 Volans
  2107. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2900, null);
  2108. Delay_ms(100);
  2109. //导出故障日志
  2110. UInt32 Address_Begin;
  2111. UInt32 Addres_End;
  2112. Address_Begin = 0x0801F800;
  2113. Addres_End = 0x0801FBFF;
  2114. UInt32 ReadCount = 0;
  2115. ReadCount = (Addres_End - Address_Begin + 1) / 128;
  2116. for (ushort i = 0; i < ReadCount; i++)
  2117. {
  2118. var SendByte = new byte[8];
  2119. UInt32 Address_Temp_Begin;
  2120. UInt32 Address_Temp_End;
  2121. Address_Temp_Begin = Address_Begin + 128 * (UInt32)i;
  2122. SendByte[0] = (byte)(Address_Temp_Begin >> 24);
  2123. SendByte[1] = (byte)(Address_Temp_Begin >> 16);
  2124. SendByte[2] = (byte)(Address_Temp_Begin >> 8);
  2125. SendByte[3] = (byte)(Address_Temp_Begin);
  2126. Address_Temp_End = Address_Temp_Begin + 127;
  2127. SendByte[4] = (byte)(Address_Temp_End >> 24);
  2128. SendByte[5] = (byte)(Address_Temp_End >> 16);
  2129. SendByte[6] = (byte)(Address_Temp_End >> 8);
  2130. SendByte[7] = (byte)(Address_Temp_End);
  2131. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2D08, SendByte);
  2132. Delay_ms(100);
  2133. }
  2134. //查询骑行信息
  2135. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2E00, null);
  2136. Delay_ms(100);
  2137. //查询用户参数2
  2138. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3000, null);
  2139. Delay_ms(100);
  2140. //查询力矩传感器零点值 Pegasi
  2141. SendData[0] = 1;
  2142. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, SendData);
  2143. Delay_ms(100);
  2144. SendData[0] = 2;
  2145. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, SendData);
  2146. Delay_ms(100);
  2147. SendData[0] = 3;
  2148. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, SendData);
  2149. Delay_ms(100);
  2150. //查询校正系数 Pegasi
  2151. SendData[0] = 1;
  2152. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, SendData);
  2153. Delay_ms(100);
  2154. SendData[0] = 2;
  2155. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, SendData);
  2156. Delay_ms(100);
  2157. SendData[0] = 3;
  2158. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, SendData);
  2159. Delay_ms(100);
  2160. //查询启动值 Pegasi
  2161. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3500, null);
  2162. Delay_ms(100);
  2163. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机参数导出完成" + "\r\n";
  2164. #endregion
  2165. //检查是否有故障
  2166. if (label_error_code.Text != "OK")
  2167. {
  2168. label_Step.Text = "电机存在故障!";
  2169. ErrorSaveLog();
  2170. return;
  2171. }
  2172. //显示测试结果
  2173. label_Result.BackColor = Color.Green;
  2174. label_Result.Text = "合格";
  2175. label_Step.Text = "合格,测试结束!";
  2176. label_Step.ForeColor = Color.Green;
  2177. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "合格,测试结束" + "\r\n";
  2178. //关机
  2179. Code[0] = 0xF0;
  2180. SendCmd(0x7FF, 0x16, 0x2201, Code);
  2181. 开ToolStripMenuItem.Checked = false;
  2182. 关ToolStripMenuItem.Checked = true;
  2183. button_Start.Enabled = true;
  2184. //关闭串口
  2185. portsettings_form.closePort();
  2186. //导出的参数上传服务器
  2187. //if (StartForm.myFtp.IsNetConnected == true)
  2188. //{
  2189. // string DateNow = DateTime.Now.ToString("yyyy-MM-dd");
  2190. // if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/walkTest/log/", DateNow) == false)
  2191. // {
  2192. // StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow);
  2193. // }
  2194. // if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow, ResultPathName) == false)
  2195. // {
  2196. // StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow + "/" + ResultPathName);
  2197. // }
  2198. // bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow + "/" + ResultPathName);
  2199. // if (result == true)
  2200. // richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机配置参数上传服务器成功" + "\r\n";
  2201. //}
  2202. //保存测试信息
  2203. //页面存储
  2204. Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap
  2205. Graphics g = Graphics.FromImage(bit);
  2206. g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片
  2207. LogSaveFileName = LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName + "\\测试页面" + ".png";
  2208. bit.Save(LogSaveFileName);
  2209. //测试页面上传服务器
  2210. //if(StartForm.myFtp.IsNetConnected==true)
  2211. //{
  2212. // string DateNow = DateTime.Now.ToString("yyyy-MM-dd");
  2213. // if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/walkTest/log/", DateNow) == false)
  2214. // {
  2215. // StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow);
  2216. // }
  2217. // if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow, ResultPathName) == false)
  2218. // {
  2219. // StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow + "/" + ResultPathName);
  2220. // }
  2221. // bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow + "/" + ResultPathName);
  2222. // if (result == true)
  2223. // richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "测试页面上传服务器成功" + "\r\n";
  2224. //}
  2225. //保存测试记录
  2226. LogSaveFileName = LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName + "\\测试记录" + ".txt";
  2227. richTextBox_TestRecord.SaveFile(LogSaveFileName, RichTextBoxStreamType.PlainText);
  2228. //测试记录上传服务器
  2229. //if (StartForm.myFtp.IsNetConnected == true)
  2230. //{
  2231. // string DateNow = DateTime.Now.ToString("yyyy-MM-dd");
  2232. // if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/walkTest/log/", DateNow) == false)
  2233. // {
  2234. // StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow);
  2235. // }
  2236. // if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow, ResultPathName) == false)
  2237. // {
  2238. // StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow + "/" + ResultPathName);
  2239. // }
  2240. // bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow + "/" + ResultPathName);
  2241. // if (result == true)
  2242. // richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "测试记录上传服务器成功" + "\r\n";
  2243. //}
  2244. }
  2245. //开始测试
  2246. private void button_Start_Click(object sender, EventArgs e)
  2247. {
  2248. TestStart();
  2249. }
  2250. //测试子函数
  2251. private void TestStart()
  2252. {
  2253. //标签初始化
  2254. label_Init();
  2255. #region 检查配置文件
  2256. if (comboBox_File.Items.Count == 0) //配置文件目录为空
  2257. {
  2258. label_Step.Text = "无可用配置文件!";
  2259. return;
  2260. }
  2261. if (comboBox_File.Text.Trim().Length == 0)//未选择配置文件
  2262. {
  2263. label_Step.Text = "请选择配置文件!";
  2264. return;
  2265. }
  2266. #endregion
  2267. #region 检查产品信息是否符合格式要求
  2268. try
  2269. {
  2270. string Mode = textBox_PDinfo.Text.Split(new string[] { " " }, StringSplitOptions.None)[0];
  2271. string Sn = textBox_PDinfo.Text.Split(new string[] { " " }, StringSplitOptions.None)[1];
  2272. Regex regex = new Regex(@"^M[12][0-9](0?[1-9]|1[0-2])((0?[1-9])|((1|2)[0-9])|30|31)[A-Z]\d{4}[A-Z]$");
  2273. if (regex.IsMatch(Sn) == false)
  2274. {
  2275. label_Step.Text = "SN格式错误!";
  2276. label_Step.ForeColor = Color.Red;
  2277. return;
  2278. }
  2279. }
  2280. catch (System.Exception ex)
  2281. {
  2282. label_Step.Text = "标签格式错误!";
  2283. label_Step.ForeColor = Color.Red;
  2284. return;
  2285. }
  2286. #endregion
  2287. //连接串口
  2288. if (portsettings_form.openPort() == false)
  2289. {
  2290. label_Step.Text = "串口连接失败!";
  2291. return;
  2292. }
  2293. //测试状态更新
  2294. TestStep = TestStep_Enum.Step_Init;
  2295. label_Step.Text = "准备开始测试";
  2296. label_Step.ForeColor = Color.Red;
  2297. Delay_ms(500);
  2298. //流水号累加和存储
  2299. label_SerialNum.Text = (Convert.ToInt32(label_SerialNum.Text) + 1).ToString();
  2300. string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\SerialNum";
  2301. StreamReader objReader = new StreamReader(cfg_file);
  2302. string sLine = "";
  2303. ArrayList arrText = new ArrayList();//创建一个动态数组
  2304. while (sLine != null)
  2305. {
  2306. sLine = objReader.ReadLine();
  2307. arrText.Add(sLine);
  2308. }
  2309. arrText.RemoveAt(arrText.Count - 1);
  2310. objReader.Close();
  2311. arrText[1] = label_SerialNum.Text;
  2312. using (StreamWriter file = new StreamWriter(cfg_file))
  2313. {
  2314. foreach (var entry in arrText)
  2315. {
  2316. file.WriteLine(entry);
  2317. }
  2318. }
  2319. button_Start.Enabled = false;
  2320. //创建线程,执行测试任务
  2321. Thread th = new Thread(Thread_Task);
  2322. th.Start();
  2323. }
  2324. //1s计时
  2325. private void timer2_Tick(object sender, EventArgs e)
  2326. {
  2327. label_SystemTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  2328. //MC故障信息超时清除
  2329. if (MC_ErrorCode_Refresh == false)
  2330. {
  2331. MC_ErrorCode_Refresh_Cnt++;
  2332. if (MC_ErrorCode_Refresh_Cnt > 5)
  2333. {
  2334. label_error_code.Text = "OK";
  2335. label_error_code.BackColor = Color.Green;
  2336. label_ErrorInfo.Text = "无故障";
  2337. label_ErrorInfo.ForeColor = Color.Green;
  2338. MC_ErrorCode_Refresh_Cnt = 0;
  2339. }
  2340. }
  2341. else
  2342. {
  2343. MC_ErrorCode_Refresh_Cnt = 0;
  2344. }
  2345. MC_ErrorCode_Refresh = false;
  2346. //MC_RunInfo超时清除
  2347. if (MC_RunInfo_Refresh == false)
  2348. {
  2349. MC_RunInfo_Refresh_Cnt++;
  2350. if (MC_RunInfo_Refresh_Cnt > 5)
  2351. {
  2352. MC_RunInfo_Clear();
  2353. MC_RunInfo_Refresh_Cnt = 0;
  2354. }
  2355. }
  2356. else
  2357. {
  2358. MC_RunInfo_Refresh_Cnt = 0;
  2359. }
  2360. MC_RunInfo_Refresh = false;
  2361. //等待反馈指令
  2362. if (Ack_Flag == false)
  2363. {
  2364. Ack_Cnt++;
  2365. if (Ack_Cnt > 2)
  2366. {
  2367. Ack_Flag = true;
  2368. StepTestFlag = StepTestResult_Enum.StepTestResult_Fail;
  2369. }
  2370. }
  2371. //推行测试计时
  2372. if ((TestStep == TestStep_Enum.Step_Walk) || (TestStep == TestStep_Enum.Step_GasMode))
  2373. {
  2374. WalkProcess_Cnt++;
  2375. }
  2376. }
  2377. //打开系统开关
  2378. private void 开ToolStripMenuItem_Click(object sender, EventArgs e)
  2379. {
  2380. byte[] Data = new byte[1];
  2381. if (portsettings_form.serialPort1.IsOpen)
  2382. {
  2383. //开机
  2384. Data[0] = 0xF1;
  2385. SendCmd(0x7FF, 0x16, 0x2201, Data);
  2386. 开ToolStripMenuItem.Checked = true;
  2387. 关ToolStripMenuItem.Checked = false;
  2388. }
  2389. else
  2390. {
  2391. MessageBox.Show("串口未连接");
  2392. return;
  2393. }
  2394. }
  2395. //关闭系统开关
  2396. private void 关ToolStripMenuItem_Click(object sender, EventArgs e)
  2397. {
  2398. byte[] Data = new byte[1];
  2399. if (portsettings_form.serialPort1.IsOpen)
  2400. {
  2401. //关机
  2402. Data[0] = 0xF0;
  2403. SendCmd(0x7FF, 0x16, 0x2201, Data);
  2404. 开ToolStripMenuItem.Checked = false;
  2405. 关ToolStripMenuItem.Checked = true;
  2406. }
  2407. else
  2408. {
  2409. MessageBox.Show("串口未连接");
  2410. return;
  2411. }
  2412. }
  2413. //清除流水号
  2414. private void label8_DoubleClick(object sender, EventArgs e)
  2415. {
  2416. //流水号清零
  2417. if (MessageBox.Show("流水号清零", "确认清零?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2418. {
  2419. label_SerialNum.Text = "0";
  2420. //存储
  2421. string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\SerialNum";
  2422. StreamReader objReader = new StreamReader(cfg_file);
  2423. string sLine = "";
  2424. ArrayList arrText = new ArrayList();//创建一个动态数组
  2425. while (sLine != null)
  2426. {
  2427. sLine = objReader.ReadLine();
  2428. arrText.Add(sLine);
  2429. }
  2430. arrText.RemoveAt(arrText.Count - 1);
  2431. objReader.Close();
  2432. arrText[1] = label_SerialNum.Text;
  2433. using (StreamWriter file = new StreamWriter(cfg_file))
  2434. {
  2435. foreach (var entry in arrText)
  2436. {
  2437. file.WriteLine(entry);
  2438. }
  2439. }
  2440. }
  2441. }
  2442. private void richTextBox_TestRecord_TextChanged(object sender, EventArgs e)
  2443. {
  2444. richTextBox_TestRecord.SelectionStart = richTextBox_TestRecord.Text.Length;
  2445. richTextBox_TestRecord.ScrollToCaret();
  2446. }
  2447. private void textBox_PDinfo_Click(object sender, EventArgs e)
  2448. {
  2449. textBox_PDinfo.SelectAll();
  2450. }
  2451. private void textBox_PDinfo_KeyUp(object sender, KeyEventArgs e)
  2452. {
  2453. if (e.KeyCode == Keys.Enter)
  2454. {
  2455. TestStart();
  2456. }
  2457. }
  2458. }
  2459. }