CheckForm.cs 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading;
  11. using System.Windows.Forms;
  12. namespace MOTINOVA_Motor_Factory_Set
  13. {
  14. public partial class CheckForm : Form
  15. {
  16. #region 文件路径
  17. public string LocalCfgFilePath = "";
  18. public string LogSavePath = "";
  19. public string LogSaveFileName = "";
  20. #endregion
  21. #region Boot版本正确标志
  22. bool IsBootVerOK = false;
  23. #endregion
  24. #region 端口设置界面
  25. public static PortSettings portsettings_form = new PortSettings();
  26. #endregion
  27. #region 力矩传感器判断标准
  28. TorqueCheckParam TorqueCheck = new TorqueCheckParam();
  29. #endregion
  30. #region 三个传感器校正系数
  31. List<float> K_array = new List<float>();
  32. #endregion
  33. #region 硬件版本检测判断值
  34. string HW_Version_Check = "";
  35. #endregion
  36. #region 串口缓存
  37. public static List<byte> buffer = new List<byte>(4096);
  38. private byte[] binary_data_1 = new byte[1024];
  39. #endregion
  40. #region MC故障码超时计数
  41. bool MC_ErrorCode_Refresh = false;
  42. ushort MC_ErrorCode_Refresh_Cnt = 0;
  43. #endregion
  44. #region 配置文件信息
  45. ArrayList array_CfgInfo = new ArrayList();
  46. #endregion
  47. #region CRC32校验
  48. static UInt32[] crc32_table = new UInt32[256]
  49. {
  50. 0x00000000, 0x04C11DB7, 0x09823B6E, 0x0D4326D9, 0x130476DC, 0x17C56B6B,
  51. 0x1A864DB2, 0x1E475005, 0x2608EDB8, 0x22C9F00F, 0x2F8AD6D6, 0x2B4BCB61,
  52. 0x350C9B64, 0x31CD86D3, 0x3C8EA00A, 0x384FBDBD, 0x4C11DB70, 0x48D0C6C7,
  53. 0x4593E01E, 0x4152FDA9, 0x5F15ADAC, 0x5BD4B01B, 0x569796C2, 0x52568B75,
  54. 0x6A1936C8, 0x6ED82B7F, 0x639B0DA6, 0x675A1011, 0x791D4014, 0x7DDC5DA3,
  55. 0x709F7B7A, 0x745E66CD, 0x9823B6E0, 0x9CE2AB57, 0x91A18D8E, 0x95609039,
  56. 0x8B27C03C, 0x8FE6DD8B, 0x82A5FB52, 0x8664E6E5, 0xBE2B5B58, 0xBAEA46EF,
  57. 0xB7A96036, 0xB3687D81, 0xAD2F2D84, 0xA9EE3033, 0xA4AD16EA, 0xA06C0B5D,
  58. 0xD4326D90, 0xD0F37027, 0xDDB056FE, 0xD9714B49, 0xC7361B4C, 0xC3F706FB,
  59. 0xCEB42022, 0xCA753D95, 0xF23A8028, 0xF6FB9D9F, 0xFBB8BB46, 0xFF79A6F1,
  60. 0xE13EF6F4, 0xE5FFEB43, 0xE8BCCD9A, 0xEC7DD02D, 0x34867077, 0x30476DC0,
  61. 0x3D044B19, 0x39C556AE, 0x278206AB, 0x23431B1C, 0x2E003DC5, 0x2AC12072,
  62. 0x128E9DCF, 0x164F8078, 0x1B0CA6A1, 0x1FCDBB16, 0x018AEB13, 0x054BF6A4,
  63. 0x0808D07D, 0x0CC9CDCA, 0x7897AB07, 0x7C56B6B0, 0x71159069, 0x75D48DDE,
  64. 0x6B93DDDB, 0x6F52C06C, 0x6211E6B5, 0x66D0FB02, 0x5E9F46BF, 0x5A5E5B08,
  65. 0x571D7DD1, 0x53DC6066, 0x4D9B3063, 0x495A2DD4, 0x44190B0D, 0x40D816BA,
  66. 0xACA5C697, 0xA864DB20, 0xA527FDF9, 0xA1E6E04E, 0xBFA1B04B, 0xBB60ADFC,
  67. 0xB6238B25, 0xB2E29692, 0x8AAD2B2F, 0x8E6C3698, 0x832F1041, 0x87EE0DF6,
  68. 0x99A95DF3, 0x9D684044, 0x902B669D, 0x94EA7B2A, 0xE0B41DE7, 0xE4750050,
  69. 0xE9362689, 0xEDF73B3E, 0xF3B06B3B, 0xF771768C, 0xFA325055, 0xFEF34DE2,
  70. 0xC6BCF05F, 0xC27DEDE8, 0xCF3ECB31, 0xCBFFD686, 0xD5B88683, 0xD1799B34,
  71. 0xDC3ABDED, 0xD8FBA05A, 0x690CE0EE, 0x6DCDFD59, 0x608EDB80, 0x644FC637,
  72. 0x7A089632, 0x7EC98B85, 0x738AAD5C, 0x774BB0EB, 0x4F040D56, 0x4BC510E1,
  73. 0x46863638, 0x42472B8F, 0x5C007B8A, 0x58C1663D, 0x558240E4, 0x51435D53,
  74. 0x251D3B9E, 0x21DC2629, 0x2C9F00F0, 0x285E1D47, 0x36194D42, 0x32D850F5,
  75. 0x3F9B762C, 0x3B5A6B9B, 0x0315D626, 0x07D4CB91, 0x0A97ED48, 0x0E56F0FF,
  76. 0x1011A0FA, 0x14D0BD4D, 0x19939B94, 0x1D528623, 0xF12F560E, 0xF5EE4BB9,
  77. 0xF8AD6D60, 0xFC6C70D7, 0xE22B20D2, 0xE6EA3D65, 0xEBA91BBC, 0xEF68060B,
  78. 0xD727BBB6, 0xD3E6A601, 0xDEA580D8, 0xDA649D6F, 0xC423CD6A, 0xC0E2D0DD,
  79. 0xCDA1F604, 0xC960EBB3, 0xBD3E8D7E, 0xB9FF90C9, 0xB4BCB610, 0xB07DABA7,
  80. 0xAE3AFBA2, 0xAAFBE615, 0xA7B8C0CC, 0xA379DD7B, 0x9B3660C6, 0x9FF77D71,
  81. 0x92B45BA8, 0x9675461F, 0x8832161A, 0x8CF30BAD, 0x81B02D74, 0x857130C3,
  82. 0x5D8A9099, 0x594B8D2E, 0x5408ABF7, 0x50C9B640, 0x4E8EE645, 0x4A4FFBF2,
  83. 0x470CDD2B, 0x43CDC09C, 0x7B827D21, 0x7F436096, 0x7200464F, 0x76C15BF8,
  84. 0x68860BFD, 0x6C47164A, 0x61043093, 0x65C52D24, 0x119B4BE9, 0x155A565E,
  85. 0x18197087, 0x1CD86D30, 0x029F3D35, 0x065E2082, 0x0B1D065B, 0x0FDC1BEC,
  86. 0x3793A651, 0x3352BBE6, 0x3E119D3F, 0x3AD08088, 0x2497D08D, 0x2056CD3A,
  87. 0x2D15EBE3, 0x29D4F654, 0xC5A92679, 0xC1683BCE, 0xCC2B1D17, 0xC8EA00A0,
  88. 0xD6AD50A5, 0xD26C4D12, 0xDF2F6BCB, 0xDBEE767C, 0xE3A1CBC1, 0xE760D676,
  89. 0xEA23F0AF, 0xEEE2ED18, 0xF0A5BD1D, 0xF464A0AA, 0xF9278673, 0xFDE69BC4,
  90. 0x89B8FD09, 0x8D79E0BE, 0x803AC667, 0x84FBDBD0, 0x9ABC8BD5, 0x9E7D9662,
  91. 0x933EB0BB, 0x97FFAD0C, 0xAFB010B1, 0xAB710D06, 0xA6322BDF, 0xA2F33668,
  92. 0xBCB4666D, 0xB8757BDA, 0xB5365D03, 0xB1F740B4
  93. };
  94. static UInt32 crc32_cal(byte[] pData, UInt16 Length)
  95. {
  96. UInt32 nReg;
  97. UInt32 nTemp = 0;
  98. UInt16 i, n;
  99. nReg = 0xFFFFFFFF;
  100. for (n = 0; n < Length; n++)
  101. {
  102. nReg ^= (UInt32)pData[n];
  103. for (i = 0; i < 4; i++)
  104. {
  105. nTemp = crc32_table[((nReg >> 24) & 0xFF)];
  106. nReg <<= 8;
  107. nReg ^= nTemp;
  108. }
  109. }
  110. return nReg;
  111. }
  112. #endregion
  113. #region 命令发送
  114. private void SendCmd(ushort ID, byte Mode, ushort Cmd, byte[] Data)
  115. {
  116. if (!portsettings_form.serialPort1.IsOpen)
  117. {
  118. MessageBox.Show("串口未连接");
  119. return;
  120. }
  121. ushort DataLength = (ushort)(Cmd & 0xFF);
  122. var SendCmdTmp = new byte[DataLength + 13];
  123. //帧头
  124. SendCmdTmp[0] = 0x55;
  125. SendCmdTmp[1] = 0xAA;
  126. //ID
  127. SendCmdTmp[2] = (byte)(ID >> 8);
  128. SendCmdTmp[3] = (byte)(ID & 0xFF);
  129. //MODE
  130. SendCmdTmp[4] = Mode;
  131. //长度
  132. SendCmdTmp[5] = (byte)((Cmd & 0xFF) + 2);
  133. //命令字
  134. SendCmdTmp[6] = (byte)(Cmd >> 8);
  135. SendCmdTmp[7] = (byte)(Cmd & 0xFF);
  136. //数据段
  137. for (UInt16 i = 0; i < (Cmd & 0xFF); i++)
  138. {
  139. SendCmdTmp[8 + i] = Data[i];
  140. }
  141. //校验
  142. UInt32 CRC_Result = crc32_cal(SendCmdTmp, (ushort)(DataLength + 8));
  143. SendCmdTmp[DataLength + 8] = Convert.ToByte(CRC_Result >> 24);
  144. SendCmdTmp[DataLength + 9] = Convert.ToByte((CRC_Result >> 16) % 256);
  145. SendCmdTmp[DataLength + 10] = Convert.ToByte((CRC_Result >> 8) % 256);
  146. SendCmdTmp[DataLength + 11] = Convert.ToByte(CRC_Result % 256);
  147. //帧尾
  148. SendCmdTmp[DataLength + 12] = 0xF0;
  149. //发送数据
  150. portsettings_form.serialPort1.Write(SendCmdTmp, 0, SendCmdTmp.Length);
  151. }
  152. #endregion
  153. #region 串口数据解析
  154. private void Serial_DataReceived_Process()
  155. {
  156. //串口解析
  157. byte DataNum = 0; //记录每条命令数据段的长度
  158. while (buffer.Count >= 13)//当buffer中的数据的个数大于等于4个时,即每一帧的数据长度所在位(第4位)出现时
  159. {
  160. if (buffer[0] == 0x55 && buffer[1] == 0xAA)//判断帧头
  161. {
  162. ushort PackageID;
  163. PackageID = (ushort)(buffer[2] * 256 + buffer[3]);
  164. if ((PackageID == 0x0710) || (PackageID == 0x0715) || (PackageID == 0x0610) || (PackageID == 0x0615) || (PackageID == 0x07FF))
  165. {
  166. int CmdLen = buffer[5];
  167. if (buffer.Count < CmdLen + 11) break;//如果接收到的数据没有达到一帧数据的指定长度, 则执行下次循环
  168. buffer.RemoveRange(0, 6);//数据达到要求长度后删去帧头,ID,模式,和命令长度
  169. DataNum = (byte)(buffer[1]);//命令字的第二个字节的表示数据位的长度
  170. if (DataNum == (CmdLen - 2))
  171. {
  172. for (int i = 0; i < DataNum + 2; i++)
  173. {
  174. binary_data_1[i] = buffer[i];
  175. }
  176. DataCmdProcess(PackageID);//对于不同的命令段,做不同的处理
  177. }
  178. buffer.RemoveRange(0, CmdLen + 5);//解析完成
  179. }
  180. else
  181. {
  182. buffer.RemoveAt(0);//ID不对
  183. }
  184. }
  185. else
  186. {
  187. buffer.RemoveAt(0);//帧头不对,删除帧头高字节
  188. }
  189. }
  190. }
  191. #endregion
  192. #region 故障转换和显示
  193. List<string> ErrorInfo = new List<string>{ "过流保护", "低压保护", "过压保护", "堵转保护","过热保护","速度传感器故障","力矩传感器故障","霍尔故障",
  194. "马达缺相","NTC故障","","","","MCU故障","踏频传感器故障","指拨故障","MOS短路","电压异常","电路故障","TE故障","TE电路故障","","","","","","","","","",
  195. "",""};
  196. private string ErrorInfoDisplay(UInt32 Code)
  197. {
  198. string Result = "";
  199. if (Code == 0)
  200. {
  201. Result = "无故障";
  202. }
  203. else
  204. {
  205. for (int i = 0; i < 32; i++)
  206. {
  207. if ((Code & 0x01) == 0x01)
  208. {
  209. Result += ErrorInfo[i] + " ";
  210. }
  211. Code >>= 1;
  212. }
  213. }
  214. return Result;
  215. }
  216. #endregion
  217. #region 数据解析处理
  218. private void DataCmdProcess(ushort ID)
  219. {
  220. ushort CmdTemp = (ushort)(binary_data_1[0] * 256 + binary_data_1[1]);
  221. ushort DataTemp;
  222. #region 主控信息
  223. if ((ID == 0x715) || (ID == 0x710))
  224. {
  225. switch (CmdTemp)
  226. {
  227. case 0xC109://Boot 版本
  228. {
  229. unchecked
  230. {
  231. this.Invoke((EventHandler)(delegate
  232. {
  233. if (IsBootVerOK == false)//开机时一般会发送4条,只显示1条
  234. {
  235. richTextBox_TestRecord.AppendText("Bootloader版本:");
  236. string boot_Version = "";
  237. for (ushort i = 0; i < 9; i++)
  238. {
  239. boot_Version += ((char)binary_data_1[2 + i]).ToString();
  240. }
  241. richTextBox_TestRecord.AppendText(boot_Version+" ");
  242. richTextBox_TestRecord.AppendText(boot_Version.Contains("V1") ? "125K" : "250K");
  243. richTextBox_TestRecord.AppendText("\r\n");
  244. }
  245. IsBootVerOK = true;
  246. }));
  247. }
  248. break;
  249. }
  250. case 0x1104://故障码
  251. {
  252. unchecked
  253. {
  254. this.Invoke((EventHandler)(delegate
  255. {
  256. MC_ErrorCode_Refresh = true;
  257. label_error_code.Text =
  258. Convert.ToString(binary_data_1[5], 16).PadLeft(2, '0').ToUpper() +
  259. Convert.ToString(binary_data_1[4], 16).PadLeft(2, '0').ToUpper() +
  260. Convert.ToString(binary_data_1[3], 16).PadLeft(2, '0').ToUpper() +
  261. Convert.ToString(binary_data_1[2], 16).PadLeft(2, '0').ToUpper() +
  262. "H";
  263. label_error_code.BackColor = Color.Red;
  264. }));
  265. }
  266. break;
  267. }
  268. case 0x1240://电机版本信息
  269. {
  270. unchecked
  271. {
  272. this.Invoke((EventHandler)(delegate
  273. {
  274. richTextBox_VerInfo.Text = "";
  275. //电机型号
  276. richTextBox_VerInfo.Text += "型号:";
  277. for (ushort i = 0; i < 16; i++)
  278. {
  279. if (binary_data_1[2 + i] == 0x2E)
  280. {
  281. break;
  282. }
  283. richTextBox_VerInfo.Text += ((char)binary_data_1[2 + i]).ToString();
  284. }
  285. richTextBox_VerInfo.Text += "\r\n";
  286. //电机SN
  287. richTextBox_VerInfo.Text += "序列号:";
  288. for (ushort i = 0; i < 16; i++)
  289. {
  290. if (binary_data_1[18 + i] == 0x2E)
  291. {
  292. break;
  293. }
  294. richTextBox_VerInfo.Text += ((char)binary_data_1[18 + i]).ToString();
  295. }
  296. richTextBox_VerInfo.Text += "\r\n";
  297. //电机HW
  298. richTextBox_VerInfo.Text += "硬件版本:";
  299. for (ushort i = 0; i < 10; i++)//前10位是PCB版号,以'.'结束
  300. {
  301. if (binary_data_1[34 + i] == 0x2E)
  302. {
  303. break;
  304. }
  305. if (binary_data_1[34 + i] == 'r')
  306. {
  307. binary_data_1[34 + i] = (byte)'.';
  308. }
  309. richTextBox_VerInfo.Text += ((char)binary_data_1[34 + i]).ToString();
  310. }
  311. switch (binary_data_1[34 + 10])//第10位代表MCU品牌
  312. {
  313. case (byte)'1':
  314. richTextBox_VerInfo.Text += " " + "ST" + " "; break;
  315. case (byte)'2':
  316. richTextBox_VerInfo.Text += " " + "APM" + " "; break;
  317. case (byte)'3':
  318. richTextBox_VerInfo.Text += " " + "HK" + " "; break;
  319. case (byte)'4':
  320. richTextBox_VerInfo.Text += " " + "GD" + " "; break;
  321. default:
  322. richTextBox_VerInfo.Text += " " + "NULL" + " "; break;
  323. }
  324. for (ushort i = 0; i < 4; i++)//后4位是MCU的ID校验码,用于识别板子的ID
  325. {
  326. richTextBox_VerInfo.Text += Convert.ToString(binary_data_1[46 + i], 16).PadLeft(2, '0').ToUpper();//MCU ID 的CRC32值
  327. }
  328. richTextBox_VerInfo.Text += "\r\n";
  329. //电机FW
  330. richTextBox_VerInfo.Text += "软件版本:";
  331. for (ushort i = 0; i < 16; i++)
  332. {
  333. if (binary_data_1[50 + i] == 0x2E)
  334. {
  335. break;
  336. }
  337. if (binary_data_1[50 + i] == 'r')
  338. {
  339. binary_data_1[50 + i] = (byte)'.';
  340. }
  341. richTextBox_VerInfo.Text += ((char)binary_data_1[50 + i]).ToString();
  342. }
  343. richTextBox_VerInfo.Text += "\r\n";
  344. }));
  345. }
  346. break;
  347. }
  348. case 0x1720://电机程序特性信息
  349. {
  350. unchecked
  351. {
  352. this.Invoke((EventHandler)(delegate
  353. {
  354. //程序特性
  355. richTextBox_VerInfo.Text += "软件属性:";
  356. for (ushort i = 0; i < 32; i++)
  357. {
  358. if (binary_data_1[2 + i] == 0x2E)
  359. {
  360. break;
  361. }
  362. if (binary_data_1[2 + i] == 'r')
  363. {
  364. binary_data_1[2 + i] = (byte)'.';
  365. }
  366. richTextBox_VerInfo.Text += ((char)binary_data_1[2 + i]).ToString();
  367. }
  368. richTextBox_VerInfo.Text += "\r\n";
  369. }));
  370. }
  371. break;
  372. }
  373. case 0xA020://用户参数1
  374. {
  375. unchecked
  376. {
  377. this.Invoke((EventHandler)(delegate
  378. {
  379. richTextBox_ReadCfg.Text += "[用户参数1]" + "\r\n";
  380. richTextBox_ReadCfg.Text += "启动模式," + binary_data_1[3].ToString() + "\r\n";
  381. richTextBox_ReadCfg.Text += "停机时间," + ((ushort)(binary_data_1[5] * 256 + binary_data_1[4])).ToString() + "\r\n";
  382. richTextBox_ReadCfg.Text += "限速值," + binary_data_1[6].ToString() + "\r\n";
  383. richTextBox_ReadCfg.Text += "下降速度," + binary_data_1[7].ToString() + "\r\n";
  384. richTextBox_ReadCfg.Text += "前飞," + binary_data_1[8].ToString() + "\r\n";
  385. richTextBox_ReadCfg.Text += "后飞," + binary_data_1[9].ToString() + "\r\n";
  386. richTextBox_ReadCfg.Text += "限流," + binary_data_1[10].ToString() + "\r\n";
  387. richTextBox_ReadCfg.Text += "温度预警," + (binary_data_1[11] - 40).ToString() + "\r\n";
  388. richTextBox_ReadCfg.Text += "温度保护," + (binary_data_1[12] - 40).ToString() + "\r\n";
  389. richTextBox_ReadCfg.Text += "码表," + binary_data_1[13].ToString()+",";
  390. richTextBox_ReadCfg.Text += ((binary_data_1[13] == 0x55) ? "有" : "无") + "\r\n";
  391. richTextBox_ReadCfg.Text += "轮胎周长," + binary_data_1[14].ToString() + "\r\n";
  392. richTextBox_ReadCfg.Text += "系列号," + binary_data_1[15].ToString() + "\r\n";
  393. richTextBox_ReadCfg.Text += "ECO增益," + binary_data_1[16].ToString() + "\r\n";
  394. richTextBox_ReadCfg.Text += "ECO加速," + binary_data_1[17].ToString() + "\r\n";
  395. richTextBox_ReadCfg.Text += "NORM增益," + binary_data_1[18].ToString() + "\r\n";
  396. richTextBox_ReadCfg.Text += "NORM加速," + binary_data_1[19].ToString() + "\r\n";
  397. richTextBox_ReadCfg.Text += "SPORT增益," + binary_data_1[20].ToString() + "\r\n";
  398. richTextBox_ReadCfg.Text += "SPORT加速," + binary_data_1[21].ToString() + "\r\n";
  399. richTextBox_ReadCfg.Text += "TURBO增益," + binary_data_1[22].ToString() + "\r\n";
  400. richTextBox_ReadCfg.Text += "TURBO加速," + binary_data_1[23].ToString() + "\r\n";
  401. richTextBox_ReadCfg.Text += "SMART增益," + binary_data_1[24].ToString() + "\r\n";
  402. richTextBox_ReadCfg.Text += "SMART加速," + binary_data_1[25].ToString() + "\r\n";
  403. richTextBox_ReadCfg.Text += "车速级数," + binary_data_1[26].ToString() + "\r\n";
  404. richTextBox_ReadCfg.Text += "踏频启动," + binary_data_1[27].ToString() + "\r\n";
  405. richTextBox_ReadCfg.Text += "指拨模式," + binary_data_1[2].ToString() + ",";
  406. richTextBox_ReadCfg.Text += ((binary_data_1[2] == 0x55) ? "不支持" : "支持") + "\r\n";
  407. richTextBox_ReadCfg.Text += "速度信号来源," + binary_data_1[28].ToString() + ",";
  408. richTextBox_ReadCfg.Text += ((binary_data_1[28] == 0x55) ? "传感器" :
  409. (binary_data_1[28] == 0xAA) ? "踏频" :
  410. (binary_data_1[28] == 0xEE) ? "通信" : "") + "\r\n";
  411. richTextBox_ReadCfg.Text += "周长微调," + ((int)binary_data_1[29]).ToString() + "\r\n";
  412. richTextBox_ReadCfg.Text += "低压保护," + ((ushort)(binary_data_1[31] * 256 + binary_data_1[30])).ToString() + "\r\n";
  413. richTextBox_ReadCfg.Text += "推行限速," + binary_data_1[32].ToString() + "\r\n";
  414. richTextBox_ReadCfg.Text += "推行转速," + binary_data_1[33].ToString() + "\r\n";
  415. }));
  416. }
  417. break;
  418. }
  419. case 0xA110://马达参数
  420. {
  421. unchecked
  422. {
  423. this.Invoke((EventHandler)(delegate
  424. {
  425. richTextBox_ReadCfg.Text += "\r\n";
  426. richTextBox_ReadCfg.Text += "[马达参数]" + "\r\n";
  427. richTextBox_ReadCfg.Text += "额定功率," + ((ushort)(binary_data_1[3] * 256 + binary_data_1[2])).ToString() + "\r\n";
  428. richTextBox_ReadCfg.Text += "额定转速," + ((ushort)(binary_data_1[5] * 256 + binary_data_1[4])).ToString() + "\r\n";
  429. richTextBox_ReadCfg.Text += "定子电阻," + ((ushort)(binary_data_1[7] * 256 + binary_data_1[6])).ToString() + "\r\n";
  430. richTextBox_ReadCfg.Text += "定子Lq," + ((ushort)(binary_data_1[9] * 256 + binary_data_1[8])).ToString() + "\r\n";
  431. richTextBox_ReadCfg.Text += "定子Ld," + ((ushort)(binary_data_1[11] * 256 + binary_data_1[10])).ToString() + "\r\n";
  432. richTextBox_ReadCfg.Text += "反电动势," + ((ushort)(binary_data_1[13] * 256 + binary_data_1[12])).ToString() + "\r\n";
  433. richTextBox_ReadCfg.Text += "额定电压," + binary_data_1[14].ToString() + "\r\n";
  434. }));
  435. }
  436. break;
  437. }
  438. case 0xA318://单传感器零点值
  439. {
  440. unchecked
  441. {
  442. this.Invoke((EventHandler)(delegate
  443. {
  444. richTextBox_TQ.Text += "零点:";
  445. for (int i = 0; i < 12; i++)
  446. {
  447. richTextBox_TQ.Text += ((ushort)((binary_data_1[3 + 2 * i] << 8) + binary_data_1[2 + 2 * i])).ToString() + ", ";
  448. }
  449. richTextBox_TQ.Text = richTextBox_TQ.Text.Substring(0, richTextBox_TQ.Text.Length - 2) + "\r\n";
  450. }));
  451. }
  452. break;
  453. }
  454. case 0xA408://校验秘钥
  455. {
  456. unchecked
  457. {
  458. this.Invoke((EventHandler)(delegate
  459. {
  460. richTextBox_ReadCfg.Text += "校验密钥,";
  461. for (int i = 0; i < 8; i++)
  462. {
  463. richTextBox_ReadCfg.Text += ((char)binary_data_1[2+i]).ToString();
  464. }
  465. richTextBox_ReadCfg.Text += "\r\n";
  466. }));
  467. }
  468. break;
  469. }
  470. case 0xA520://生产信息
  471. {
  472. unchecked
  473. {
  474. this.Invoke((EventHandler)(delegate
  475. {
  476. richTextBox_ReadCfg.Text += "\r\n";
  477. richTextBox_ReadCfg.Text += "[其它信息]" + "\r\n";
  478. richTextBox_ReadCfg.Text += "生产商,";
  479. for (int i = 0; i < 8; i++)
  480. {
  481. if (binary_data_1[2 + i] == '.') break;
  482. richTextBox_ReadCfg.Text += ((char)binary_data_1[2 + i]).ToString();
  483. }
  484. richTextBox_ReadCfg.Text += "\r\n";
  485. richTextBox_ReadCfg.Text += "生产地,";
  486. for (int i = 0; i < 8; i++)
  487. {
  488. if (binary_data_1[10 + i] == '.') break;
  489. richTextBox_ReadCfg.Text += ((char)binary_data_1[10 + i]).ToString();
  490. }
  491. richTextBox_ReadCfg.Text += "\r\n";
  492. richTextBox_ReadCfg.Text += "生产日期,";
  493. for (int i = 0; i < 8; i++)
  494. {
  495. if (binary_data_1[18 + i] == '.') break;
  496. richTextBox_ReadCfg.Text += ((char)binary_data_1[18 + i]).ToString();
  497. }
  498. richTextBox_ReadCfg.Text += "\r\n";
  499. }));
  500. }
  501. break;
  502. }
  503. case 0xA610://自定义1
  504. {
  505. unchecked
  506. {
  507. this.Invoke((EventHandler)(delegate
  508. {
  509. richTextBox_ReadCfg.Text += "信息1,";
  510. for (int i = 0; i < 16; i++)
  511. {
  512. if (binary_data_1[2 + i] == '.') break;
  513. richTextBox_ReadCfg.Text += ((char)binary_data_1[2 + i]).ToString();
  514. }
  515. richTextBox_ReadCfg.Text += "\r\n";
  516. }));
  517. }
  518. break;
  519. }
  520. case 0xA710://自定义2
  521. {
  522. unchecked
  523. {
  524. this.Invoke((EventHandler)(delegate
  525. {
  526. richTextBox_ReadCfg.Text += "信息2,";
  527. for (int i = 0; i < 16; i++)
  528. {
  529. if (binary_data_1[2 + i] == '.') break;
  530. richTextBox_ReadCfg.Text += ((char)binary_data_1[2 + i]).ToString();
  531. }
  532. richTextBox_ReadCfg.Text += "\r\n";
  533. }));
  534. }
  535. break;
  536. }
  537. case 0xA810://自定义3
  538. {
  539. unchecked
  540. {
  541. this.Invoke((EventHandler)(delegate
  542. {
  543. richTextBox_ReadCfg.Text += "信息3,";
  544. for (int i = 0; i < 16; i++)
  545. {
  546. if (binary_data_1[2 + i] == '.') break;
  547. richTextBox_ReadCfg.Text += ((char)binary_data_1[2 + i]).ToString();
  548. }
  549. richTextBox_ReadCfg.Text += "\r\n";
  550. }));
  551. }
  552. break;
  553. }
  554. case 0xA903://写入反馈
  555. {
  556. break;
  557. }
  558. case 0xAA04://单传感器校正值
  559. {
  560. unchecked
  561. {
  562. this.Invoke((EventHandler)(delegate
  563. {
  564. richTextBox_TQ.Text += "校正系数:";
  565. richTextBox_TQ.Text += ((float)binary_data_1[4] / 100f).ToString("#0.00") + "\r\n";
  566. richTextBox_TQ.Text += "启动值:";
  567. richTextBox_TQ.Text += ((ushort)((binary_data_1[3] << 8) + binary_data_1[2])).ToString() + "\r\n";
  568. }));
  569. }
  570. break;
  571. }
  572. case 0xAD20://用户参数2
  573. {
  574. unchecked
  575. {
  576. this.Invoke((EventHandler)(delegate
  577. {
  578. richTextBox_ReadCfg.Text += "\r\n";
  579. richTextBox_ReadCfg.Text += "[用户参数2]" + "\r\n";
  580. richTextBox_ReadCfg.Text+="姿态传感器," + binary_data_1[6].ToString() + ",";
  581. richTextBox_ReadCfg.Text += ((binary_data_1[6] == 0x55) ? "不支持" : "支持") + "\r\n";
  582. richTextBox_ReadCfg.Text += "俯仰角零偏," + ((ushort)(binary_data_1[3] * 256 + binary_data_1[2])).ToString() + "\r\n";
  583. richTextBox_ReadCfg.Text += "横滚角零偏," + ((ushort)(binary_data_1[5] * 256 + binary_data_1[4])).ToString() + "\r\n";
  584. richTextBox_ReadCfg.Text += "尾灯模式," + binary_data_1[7].ToString() + "\r\n";
  585. richTextBox_ReadCfg.Text += "前灯电压," + binary_data_1[8].ToString() + ",";
  586. richTextBox_ReadCfg.Text += ((binary_data_1[8] == 0xFF) ? "随电池" : (binary_data_1[8] == 0) ? "硬件配置" : (binary_data_1[8].ToString() + "V")) + "\r\n";
  587. richTextBox_ReadCfg.Text += "尾灯电压," + binary_data_1[9].ToString() + ",";
  588. richTextBox_ReadCfg.Text += ((binary_data_1[9] == 0xFF) ? "随电池" : (binary_data_1[9] == 0) ? "硬件配置" : (binary_data_1[9].ToString() + "V")) + "\r\n";
  589. }));
  590. }
  591. break;
  592. }
  593. case 0xAE1A://三传感器零点
  594. {
  595. unchecked
  596. {
  597. this.Invoke((EventHandler)(delegate
  598. {
  599. richTextBox_TQ.Text += "传感器" + binary_data_1[2].ToString() + "零点:";
  600. for (int i = 0; i < 12; i++)
  601. {
  602. richTextBox_TQ.Text += ((ushort)((binary_data_1[4 + 2 * i] << 8) + binary_data_1[3 + 2 * i])).ToString() + ", ";
  603. }
  604. richTextBox_TQ.Text = richTextBox_TQ.Text.Substring(0, richTextBox_TQ.Text.Length - 2) + "\r\n";
  605. }));
  606. }
  607. break;
  608. }
  609. case 0xAF02://三传感器校正系数
  610. {
  611. unchecked
  612. {
  613. this.Invoke((EventHandler)(delegate
  614. {
  615. richTextBox_TQ.Text += "传感器" + binary_data_1[2].ToString() + "校正系数:";
  616. richTextBox_TQ.Text += ((float)binary_data_1[3] / 100f).ToString("#0.00") + "\r\n";
  617. K_array.Add((float)binary_data_1[3] / 100f);
  618. if (K_array.Count >= 3)
  619. {
  620. double avg = (K_array[0] + K_array[1] + K_array[2]) / 3.0f;
  621. double sum = Math.Pow(K_array[0] - avg, 2) + Math.Pow(K_array[1] - avg, 2) + Math.Pow(K_array[2] - avg, 2);
  622. double std = Math.Sqrt(sum / 3.0f);
  623. richTextBox_TQ.Text += "校正系数标准差:" + std.ToString("#0.0000") + "\r\n";
  624. K_array.Clear();
  625. }
  626. }));
  627. }
  628. break;
  629. }
  630. case 0xB002://三传感器启动值
  631. {
  632. unchecked
  633. {
  634. this.Invoke((EventHandler)(delegate
  635. {
  636. richTextBox_TQ.Text += "启动值:";
  637. richTextBox_TQ.Text += ((ushort)((binary_data_1[3] << 8) + binary_data_1[2])).ToString() + "\r\n";
  638. }));
  639. }
  640. break;
  641. }
  642. default: break;
  643. }
  644. }
  645. #endregion
  646. #region TE信息
  647. else if ((ID == 0x615) || (ID == 0x610))
  648. {
  649. switch (CmdTemp)
  650. {
  651. case 0x1240://TE APP版本信息
  652. {
  653. unchecked
  654. {
  655. this.Invoke((EventHandler)(delegate
  656. {
  657. //TE 软件版本
  658. richTextBox_VerInfo.Text += "TE软件版本:";
  659. for (ushort i = 0; i < 16; i++)
  660. {
  661. if (binary_data_1[50 + i] == 0x2E)
  662. {
  663. break;
  664. }
  665. if (binary_data_1[50 + i] == 'r')
  666. {
  667. binary_data_1[50 + i] = (byte)'.';
  668. }
  669. richTextBox_VerInfo.Text += ((char)binary_data_1[50 + i]).ToString();
  670. }
  671. }));
  672. }
  673. break;
  674. }
  675. default: break;
  676. }
  677. #endregion
  678. }
  679. }
  680. #endregion
  681. #region 20ms定时器
  682. private void timer1_Tick(object sender, EventArgs e)
  683. {
  684. //串口数据解析
  685. Serial_DataReceived_Process();
  686. }
  687. #endregion
  688. #region 非独占性延时函数
  689. public static void Delay_ms(int milliSecond)
  690. {
  691. int start = Environment.TickCount;
  692. while (Math.Abs(Environment.TickCount - start) < milliSecond)//毫秒
  693. {
  694. Application.DoEvents();
  695. }
  696. }
  697. #endregion
  698. #region 获取配置文件日期
  699. private void GetDateList(bool DisAll)
  700. {
  701. string[] DateList = { "" };
  702. if (checkBox_LocalFile.Checked == true)//从本地更新日期列表
  703. {
  704. DateList = System.IO.Directory.GetDirectories(LocalCfgFilePath);
  705. }
  706. else//从服务器更新日期列表
  707. {
  708. DateList = StartForm.myFtp.GetDirectoryList(StartForm.myServerCfg.RootPath + "/cfg");
  709. }
  710. comboBox_cfgFileDate.Items.Clear();
  711. //日期列表加载
  712. if (DisAll == true)
  713. {
  714. comboBox_cfgFileDate.Items.Add("显示近期");
  715. comboBox_cfgFileDate.Items.Add("---");
  716. foreach (var file in DateList)
  717. comboBox_cfgFileDate.Items.Add(file.Substring(file.LastIndexOf("\\") + 1));
  718. }
  719. else
  720. {
  721. if (DateList.Length > 10)
  722. {
  723. comboBox_cfgFileDate.Items.Add("显示全部");
  724. comboBox_cfgFileDate.Items.Add("---");
  725. }
  726. for (int i = (DateList.Length > 10) ? (DateList.Length - 10) : 0; i < DateList.Length; i++)//显示最近10个日期
  727. comboBox_cfgFileDate.Items.Add(DateList[i].Substring(DateList[i].LastIndexOf("\\") + 1));
  728. }
  729. }
  730. #endregion
  731. #region 获取配置文件列表
  732. private void GetFileListFrom(string date)
  733. {
  734. string[] FileList = { "" };
  735. if (checkBox_LocalFile.Checked == true)//从本地更新文件列表
  736. {
  737. FileList = System.IO.Directory.GetFiles(LocalCfgFilePath + date);
  738. }
  739. else//从服务器更新文件列表
  740. {
  741. FileList = StartForm.myFtp.GetFileNameList(StartForm.myServerCfg.RootPath + "/cfg/" + date);
  742. }
  743. comboBox_File.Items.Clear();
  744. foreach (var file in FileList)
  745. {
  746. if (file.Contains(".ttcfg"))
  747. {
  748. comboBox_File.Items.Add(file.Substring(file.LastIndexOf("\\") + 1));
  749. }
  750. }
  751. }
  752. #endregion
  753. #region 选择文件日期后更新文件列表
  754. private void comboBox_cfgFileDate_SelectionChangeCommitted(object sender, EventArgs e)
  755. {
  756. if (comboBox_cfgFileDate.SelectedItem.ToString() == "显示全部")
  757. {
  758. GetDateList(true);
  759. }
  760. else if (comboBox_cfgFileDate.SelectedItem.ToString() == "显示近期")
  761. {
  762. GetDateList(false);
  763. }
  764. else
  765. {
  766. comboBox_File.Text = "";
  767. GetFileListFrom(comboBox_cfgFileDate.SelectedItem.ToString());
  768. }
  769. }
  770. #endregion
  771. #region 选择文件后加载文件
  772. private void comboBox_File_SelectionChangeCommitted(object sender, EventArgs e)
  773. {
  774. //清空原来数据
  775. array_CfgInfo.Clear();
  776. string SelectFile = "";
  777. if (checkBox_LocalFile.Checked == false)//从服务器下载文件
  778. {
  779. SelectFile = comboBox_cfgFileDate.SelectedItem.ToString() + "\\" + comboBox_File.SelectedItem.ToString();
  780. if (File.Exists(LocalCfgFilePath + SelectFile))//本地存在选定文件,先删除后再下载
  781. {
  782. File.Delete(LocalCfgFilePath + SelectFile);
  783. }
  784. SelectFile = comboBox_cfgFileDate.SelectedItem.ToString() + "/" + comboBox_File.SelectedItem.ToString();
  785. StartForm.myFtp.DownloadFile(StartForm.myServerCfg.RootPath + "/cfg/" + SelectFile, LocalCfgFilePath + comboBox_cfgFileDate.SelectedItem.ToString());
  786. }
  787. SelectFile = comboBox_cfgFileDate.SelectedItem.ToString() + "\\" + comboBox_File.SelectedItem.ToString();
  788. //打开选中文件
  789. StreamReader objReader = new StreamReader(LocalCfgFilePath + SelectFile);
  790. string sLine = "";
  791. while (sLine != null)
  792. {
  793. sLine = objReader.ReadLine();
  794. array_CfgInfo.Add(sLine);
  795. }
  796. objReader.Close();
  797. //解析显示配置文件
  798. richTextBox_CfgFile.Text = "";
  799. try
  800. {
  801. foreach (string sOutput in array_CfgInfo)
  802. {
  803. if (sOutput.Contains("[测试阈值]"))
  804. {
  805. //删除最后一行,不显示后面内容
  806. richTextBox_CfgFile.Text = richTextBox_CfgFile.Text.Remove(richTextBox_CfgFile.Text.Length - 1);
  807. break;
  808. }
  809. richTextBox_CfgFile.AppendText(sOutput + "\r\n");
  810. }
  811. }
  812. catch (System.Exception)
  813. {
  814. return;
  815. }
  816. //加载力矩传感器判断标准
  817. TorqueCheck.CheckParam_Load(LocalCfgFilePath + SelectFile);
  818. textBox_TQ_Zero.Text = TorqueCheck.Zero_Min.ToString() + " - " + TorqueCheck.Zero_Max.ToString();
  819. textBox_TQ_K.Text = TorqueCheck.Cal_K_Min.ToString("#0.00") + " - " + TorqueCheck.Cal_K_Max.ToString("#0.00");
  820. textBox_TQ_Start.Text = TorqueCheck.Start_Min.ToString() + " - " + TorqueCheck.Start_Max.ToString();
  821. //加载PCBA检测值
  822. try
  823. {
  824. for (int i = 0; i < array_CfgInfo.Count; i++)
  825. {
  826. if (array_CfgInfo[i].ToString().Contains("[其它测试备注信息]"))
  827. {
  828. HW_Version_Check = array_CfgInfo[i + 1].ToString().Split(',')[1];
  829. break;
  830. }
  831. }
  832. }
  833. catch (System.Exception)
  834. {
  835. return;
  836. }
  837. }
  838. #endregion
  839. #region 修改获取文件方式,本地或服务器
  840. private void checkBox_LocalFile_CheckedChanged(object sender, EventArgs e)
  841. {
  842. comboBox_cfgFileDate.Text = "";
  843. comboBox_File.Text = "";
  844. GetDateList(false);
  845. }
  846. #endregion
  847. public CheckForm()
  848. {
  849. InitializeComponent();
  850. System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
  851. }
  852. #region 端口设置
  853. private void toolStripMenuIte_PortSet_Click(object sender, EventArgs e)
  854. {
  855. portsettings_form.Show();
  856. }
  857. #endregion
  858. #region 打开电源
  859. private void 开ToolStripMenuItem_Click(object sender, EventArgs e)
  860. {
  861. byte[] Data = new byte[1];
  862. if (portsettings_form.serialPort1.IsOpen)
  863. {
  864. //开机
  865. Data[0] = 0xF1;
  866. SendCmd(0x7FF, 0x16, 0x2201, Data);
  867. 开ToolStripMenuItem.Checked = true;
  868. 关ToolStripMenuItem.Checked = false;
  869. }
  870. else
  871. {
  872. MessageBox.Show("串口未连接");
  873. return;
  874. }
  875. }
  876. #endregion
  877. #region 关闭电源
  878. private void 关ToolStripMenuItem_Click(object sender, EventArgs e)
  879. {
  880. byte[] Data = new byte[1];
  881. if (portsettings_form.serialPort1.IsOpen)
  882. {
  883. //关机
  884. Data[0] = 0xF0;
  885. SendCmd(0x7FF, 0x16, 0x2201, Data);
  886. 开ToolStripMenuItem.Checked = false;
  887. 关ToolStripMenuItem.Checked = true;
  888. }
  889. else
  890. {
  891. MessageBox.Show("串口未连接");
  892. return;
  893. }
  894. }
  895. #endregion
  896. #region 1s定时器更新时间
  897. private void timer2_Tick(object sender, EventArgs e)
  898. {
  899. label_SystemTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  900. //MC故障信息超时清除
  901. if (MC_ErrorCode_Refresh == false)
  902. {
  903. MC_ErrorCode_Refresh_Cnt++;
  904. if (MC_ErrorCode_Refresh_Cnt > 5)
  905. {
  906. label_error_code.Text = "OK";
  907. label_error_code.BackColor = Color.Green;
  908. MC_ErrorCode_Refresh_Cnt = 0;
  909. }
  910. }
  911. else
  912. {
  913. MC_ErrorCode_Refresh_Cnt = 0;
  914. }
  915. MC_ErrorCode_Refresh = false;
  916. }
  917. #endregion
  918. #region 故障码标签鼠标滑过显示故障内容
  919. private void label_error_code_MouseHover(object sender, EventArgs e)
  920. {
  921. // 创建the ToolTip
  922. ToolTip toolTip1 = new ToolTip();
  923. // 设置显示样式
  924. toolTip1.AutoPopDelay = 30000;//提示信息的可见时间
  925. toolTip1.InitialDelay = 50;//事件触发多久后出现提示
  926. toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  927. toolTip1.ShowAlways = true;//是否显示提示框
  928. // 设置伴随的对象.
  929. string ErrorCode = this.label_error_code.Text;
  930. UInt32 Code = ErrorCode.Contains("H") ? Convert.ToUInt32(ErrorCode.Split('H')[0], 16) : 0;
  931. toolTip1.SetToolTip(this.label_error_code, ErrorInfoDisplay(Code));//设置故障内容显示
  932. }
  933. #endregion
  934. //创建线程执行一次测试任务
  935. /*
  936. 执行流程如下:
  937. 1、读取BOOT版本信息;
  938. 2、读取电机版本信息 -> 确认SN是否存在为0、确认版本和cfg文件是否一致;
  939. 3、读取力矩传感器标定值 -> 依次判断零点、校正系数、启动值是否在允许范围内,判断零点值差异是否过大;
  940. 4、依次读取配置参数,严格按照cfg文件的顺序读取 -> 对比两个richbox内容是否一致;
  941. 5、显示测试结果;
  942. 6、存储测试结果,上传ftp服务器。
  943. */
  944. private void Thread_Task()
  945. {
  946. //开始测试
  947. richTextBox_TestRecord.Text += "电机信息:" + textBox_PDinfo.Text + "\r\n";
  948. richTextBox_TestRecord.Text += "配置文件:" + comboBox_File.Text + "\r\n";
  949. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "开始测试" + "\r\n";
  950. label_Result.Text = "不合格";
  951. label_Result.BackColor = Color.Red;
  952. //公有变量
  953. var Code = new byte[32];//发送的指令数据
  954. //检查是否有故障
  955. if (label_error_code.Text != "OK")
  956. {
  957. label_Step.Text = "电机存在故障!";
  958. button_Start.Enabled = true;
  959. textBox_PDinfo.Enabled = true;
  960. //关机
  961. Code[0] = 0xF0;
  962. SendCmd(0x7FF, 0x16, 0x2201, Code);
  963. 开ToolStripMenuItem.Checked = false;
  964. 关ToolStripMenuItem.Checked = true;
  965. Delay_ms(500);
  966. //关闭串口
  967. portsettings_form.closePort();
  968. //存储测试数据
  969. ErrorSaveLog();
  970. return;
  971. }
  972. //开机
  973. Code[0] = 0xF1;
  974. SendCmd(0x7FF, 0x16, 0x2201, Code);
  975. 开ToolStripMenuItem.Checked = true;
  976. 关ToolStripMenuItem.Checked = false;
  977. Delay_ms(2000);
  978. #region 判断BootLoader版本
  979. if (IsBootVerOK == false)
  980. {
  981. //发送系统复位指令
  982. Code[0] = (byte)'R';
  983. Code[1] = (byte)'E';
  984. Code[2] = (byte)'S';
  985. Code[3] = (byte)'E';
  986. Code[4] = (byte)'T';
  987. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2505, Code);
  988. Delay_ms(2000);
  989. if (IsBootVerOK == false)
  990. {
  991. label_Step.Text = "Bootloader波特率错误!";
  992. button_Start.Enabled = true;
  993. textBox_PDinfo.Enabled = true;
  994. //关机
  995. Code[0] = 0xF0;
  996. SendCmd(0x7FF, 0x16, 0x2201, Code);
  997. 开ToolStripMenuItem.Checked = false;
  998. 关ToolStripMenuItem.Checked = true;
  999. Delay_ms(500);
  1000. //关闭串口
  1001. portsettings_form.closePort();
  1002. //存储测试数据
  1003. ErrorSaveLog();
  1004. return;
  1005. }
  1006. }
  1007. #endregion
  1008. #region 版本查询和确认
  1009. label_Step.Text = "检查版本信息";
  1010. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "发送指令查询版本信息" + "\r\n";
  1011. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
  1012. Delay_ms(500);
  1013. SendCmd((ushort)0x651, (byte)0x11, (ushort)0x1200, null);
  1014. Delay_ms(500);
  1015. string FW_Version_Check = comboBox_File.Text.Split('_')[2] + "_" + comboBox_File.Text.Split('_')[3];
  1016. string FW_Version_Result = "";
  1017. string TE_FW_Ver_Check = comboBox_File.Text.Split('_')[4] + "_" + comboBox_File.Text.Split('_')[5];
  1018. string TE_FW_Ver_Result = "";
  1019. string HW_Version_Result = "";
  1020. try
  1021. {
  1022. HW_Version_Result = richTextBox_VerInfo.Lines[2].Split(':')[1];
  1023. FW_Version_Result = richTextBox_VerInfo.Lines[3].Split(':')[1];
  1024. FW_Version_Result = FW_Version_Result.Split('_')[0] + richTextBox_VerInfo.Lines[4].Split('-')[2].Replace('V', '.') + "_" + FW_Version_Result.Split('_')[1];
  1025. TE_FW_Ver_Result = richTextBox_VerInfo.Lines[5].Split(':')[1];
  1026. }
  1027. catch (System.Exception)
  1028. {
  1029. //label_Step.Text = "软件版本信息不完整!";
  1030. }
  1031. //PCBA版本确认
  1032. if (HW_Version_Result.Contains(HW_Version_Check) == false)//PCBA版号错误,直接结束
  1033. {
  1034. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "PCBA版本错误" + "\r\n";
  1035. label_Step.Text = "PCBA版本错误,测试结束!";
  1036. label_Step.ForeColor = Color.Red;
  1037. button_Start.Enabled = true;
  1038. textBox_PDinfo.Enabled = true;
  1039. ErrorSaveLog();
  1040. //关机
  1041. Code[0] = 0xF0;
  1042. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1043. 开ToolStripMenuItem.Checked = false;
  1044. 关ToolStripMenuItem.Checked = true;
  1045. Delay_ms(500);
  1046. //关闭串口
  1047. portsettings_form.closePort();
  1048. return;
  1049. }
  1050. //主控软件版本检查
  1051. if (string.Equals(FW_Version_Check, FW_Version_Result) == false) //主控版本错误,直接结束
  1052. {
  1053. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "主控版本错误" + "\r\n";
  1054. label_Step.Text = "主控软件版本错误,测试结束!";
  1055. label_Step.ForeColor = Color.Red;
  1056. button_Start.Enabled = true;
  1057. textBox_PDinfo.Enabled = true;
  1058. ErrorSaveLog();
  1059. //关机
  1060. Code[0] = 0xF0;
  1061. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1062. 开ToolStripMenuItem.Checked = false;
  1063. 关ToolStripMenuItem.Checked = true;
  1064. Delay_ms(500);
  1065. //关闭串口
  1066. portsettings_form.closePort();
  1067. return;
  1068. }
  1069. else
  1070. {
  1071. //TE软件版本检查
  1072. if (TE_FW_Ver_Check != "N_A")//单MCU主控板不检测TE版本
  1073. {
  1074. if (string.Equals(TE_FW_Ver_Check, TE_FW_Ver_Result) == false) //TE版本错误,给出提示
  1075. {
  1076. if (MessageBox.Show("TE版本错误,请确认主控板是否为HK?", "TE版本错误", MessageBoxButtons.YesNo) == DialogResult.No)
  1077. {
  1078. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "TE版本错误" + "\r\n";
  1079. label_Step.Text = "TE软件版本错误,测试结束!";
  1080. label_Step.ForeColor = Color.Red;
  1081. button_Start.Enabled = true;
  1082. textBox_PDinfo.Enabled = true;
  1083. ErrorSaveLog();
  1084. //关机
  1085. Code[0] = 0xF0;
  1086. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1087. 开ToolStripMenuItem.Checked = false;
  1088. 关ToolStripMenuItem.Checked = true;
  1089. Delay_ms(500);
  1090. //关闭串口
  1091. portsettings_form.closePort();
  1092. return;
  1093. }
  1094. }
  1095. }
  1096. //型号和SN确认
  1097. try
  1098. {
  1099. string mode1 = "", mode2 = "";
  1100. mode1 = richTextBox_VerInfo.Lines[0].Split(':')[1].Trim();
  1101. mode2 = textBox_PDinfo.Text.Contains("%#") ? textBox_PDinfo.Text.Substring(0, textBox_PDinfo.Text.LastIndexOf('%'))
  1102. : textBox_PDinfo.Text.Substring(0, textBox_PDinfo.Text.LastIndexOf(' ')).Trim();
  1103. if (string.Equals(mode1, mode2) == false)//型号不一致
  1104. {
  1105. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "型号错误" + "\r\n";
  1106. label_Step.Text = "型号不一致,测试结束!";
  1107. label_Step.ForeColor = Color.Red;
  1108. button_Start.Enabled = true;
  1109. textBox_PDinfo.Enabled = true;
  1110. ErrorSaveLog();
  1111. //关机
  1112. Code[0] = 0xF0;
  1113. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1114. 开ToolStripMenuItem.Checked = false;
  1115. 关ToolStripMenuItem.Checked = true;
  1116. Delay_ms(500);
  1117. //关闭串口
  1118. portsettings_form.closePort();
  1119. return;
  1120. }
  1121. string SN1 = "", SN2 = "";
  1122. SN1 = richTextBox_VerInfo.Lines[1].Split(':')[1].Trim();
  1123. SN2 = textBox_PDinfo.Text.Contains("%#") ? textBox_PDinfo.Text.Remove(0, textBox_PDinfo.Text.LastIndexOf('#') + 1)
  1124. : textBox_PDinfo.Text.Remove(0, textBox_PDinfo.Text.LastIndexOf(' ') + 1);
  1125. if (string.Equals(SN1, SN2) == false)//SN不一致
  1126. {
  1127. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "SN错误" + "\r\n";
  1128. label_Step.Text = "SN不一致,测试结束!";
  1129. label_Step.ForeColor = Color.Red;
  1130. button_Start.Enabled = true;
  1131. textBox_PDinfo.Enabled = true;
  1132. ErrorSaveLog();
  1133. //关机
  1134. Code[0] = 0xF0;
  1135. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1136. 开ToolStripMenuItem.Checked = false;
  1137. 关ToolStripMenuItem.Checked = true;
  1138. Delay_ms(500);
  1139. //关闭串口
  1140. portsettings_form.closePort();
  1141. return;
  1142. }
  1143. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "版本信息正确" + "\r\n";
  1144. }
  1145. catch (Exception)
  1146. {
  1147. label_Step.Text = "电机信息错误,测试结束!";
  1148. label_Step.ForeColor = Color.Red;
  1149. button_Start.Enabled = true;
  1150. textBox_PDinfo.Enabled = true;
  1151. ErrorSaveLog();
  1152. //关机
  1153. Code[0] = 0xF0;
  1154. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1155. 开ToolStripMenuItem.Checked = false;
  1156. 关ToolStripMenuItem.Checked = true;
  1157. Delay_ms(500);
  1158. //关闭串口
  1159. portsettings_form.closePort();
  1160. return;
  1161. }
  1162. }
  1163. #endregion
  1164. #region 判断力矩传感器的标定值是否在允许范围内
  1165. label_Step.Text = "判断力矩传感器";
  1166. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "发送指令查询力矩传感器" + "\r\n";
  1167. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2000, null);//单传感器零点
  1168. Delay_ms(200);
  1169. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2900, null);//单传感器校正信息
  1170. Delay_ms(200);
  1171. Code[0] = 1;
  1172. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, Code);//传感器1零点
  1173. Delay_ms(200);
  1174. Code[0] = 2;
  1175. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, Code);//传感器2零点
  1176. Delay_ms(200);
  1177. Code[0] = 3;
  1178. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, Code);//传感器3零点
  1179. Delay_ms(200);
  1180. Code[0] = 1;
  1181. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, Code);//传感器1校正系数
  1182. Delay_ms(200);
  1183. Code[0] = 2;
  1184. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, Code);//传感器2校正系数
  1185. Delay_ms(200);
  1186. Code[0] = 3;
  1187. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, Code);//传感器3校正系数
  1188. Delay_ms(200);
  1189. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3500, null);//三传感器启动值
  1190. Delay_ms(200);
  1191. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "判断力矩传感器" + "\r\n";
  1192. for (int i = 0; i < richTextBox_TQ.Lines.Length; i++)
  1193. {
  1194. switch (richTextBox_TQ.Lines[i].Split(':')[0])
  1195. {
  1196. case "零点": case "传感器1零点": case "传感器2零点": case "传感器3零点":
  1197. {
  1198. string[] zero = richTextBox_TQ.Lines[i].Split(':')[1].Split(new string[] { ", " }, StringSplitOptions.None);
  1199. for (int j = 0; j < zero.Length; j++)
  1200. {
  1201. if ((Convert.ToUInt16(zero[j]) > TorqueCheck.Zero_Max) || (Convert.ToUInt16(zero[j]) < TorqueCheck.Zero_Min))//零点超差
  1202. {
  1203. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "传感器零点超范围" + "\r\n";
  1204. label_Step.Text = "传感器零点超范围,测试结束!";
  1205. label_Step.ForeColor = Color.Red;
  1206. button_Start.Enabled = true;
  1207. textBox_PDinfo.Enabled = true;
  1208. ErrorSaveLog();
  1209. //关机
  1210. Code[0] = 0xF0;
  1211. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1212. 开ToolStripMenuItem.Checked = false;
  1213. 关ToolStripMenuItem.Checked = true;
  1214. Delay_ms(500);
  1215. //关闭串口
  1216. portsettings_form.closePort();
  1217. return;
  1218. }
  1219. }
  1220. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + richTextBox_TQ.Lines[i].Split(':')[0] + "正常" + "\r\n";
  1221. break;
  1222. }
  1223. case "校正系数": case "传感器1校正系数": case "传感器2校正系数": case "传感器3校正系数":
  1224. {
  1225. ushort k = (ushort)(Convert.ToSingle(richTextBox_TQ.Lines[i].Split(':')[1]) * 100);
  1226. if ((k > (ushort)(TorqueCheck.Cal_K_Max * 100f)) || (k < (ushort)(TorqueCheck.Cal_K_Min * 100f)))//校正系数超差
  1227. {
  1228. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "校正系数超范围" + "\r\n";
  1229. label_Step.Text = "校正系数超范围,测试结束!";
  1230. label_Step.ForeColor = Color.Red;
  1231. button_Start.Enabled = true;
  1232. textBox_PDinfo.Enabled = true;
  1233. ErrorSaveLog();
  1234. //关机
  1235. Code[0] = 0xF0;
  1236. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1237. 开ToolStripMenuItem.Checked = false;
  1238. 关ToolStripMenuItem.Checked = true;
  1239. Delay_ms(500);
  1240. //关闭串口
  1241. portsettings_form.closePort();
  1242. return;
  1243. }
  1244. if (k == 100)
  1245. {
  1246. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "传感器未标定" + "\r\n";
  1247. label_Step.Text = "传感器未标定,测试结束!";
  1248. label_Step.ForeColor = Color.Red;
  1249. button_Start.Enabled = true;
  1250. textBox_PDinfo.Enabled = true;
  1251. ErrorSaveLog();
  1252. //关机
  1253. Code[0] = 0xF0;
  1254. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1255. 开ToolStripMenuItem.Checked = false;
  1256. 关ToolStripMenuItem.Checked = true;
  1257. Delay_ms(500);
  1258. //关闭串口
  1259. portsettings_form.closePort();
  1260. return;
  1261. }
  1262. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + richTextBox_TQ.Lines[i].Split(':')[0] + "正常" + "\r\n";
  1263. break;
  1264. }
  1265. case "校正系数标准差":
  1266. {
  1267. double std = Convert.ToDouble(richTextBox_TQ.Lines[i].Split(':')[1]);
  1268. if (std > 0.3f)
  1269. {
  1270. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "校正系数差异大" + "\r\n";
  1271. label_Step.Text = "校正系数标准差超范围,测试结束!";
  1272. label_Step.ForeColor = Color.Red;
  1273. button_Start.Enabled = true;
  1274. textBox_PDinfo.Enabled = true;
  1275. ErrorSaveLog();
  1276. //关机
  1277. Code[0] = 0xF0;
  1278. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1279. 开ToolStripMenuItem.Checked = false;
  1280. 关ToolStripMenuItem.Checked = true;
  1281. Delay_ms(500);
  1282. //关闭串口
  1283. portsettings_form.closePort();
  1284. return;
  1285. }
  1286. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + richTextBox_TQ.Lines[i].Split(':')[0] + "正常" + "\r\n";
  1287. break;
  1288. }
  1289. case "启动值":
  1290. {
  1291. string start = richTextBox_TQ.Lines[i].Split(':')[1];
  1292. if ((Convert.ToUInt16(start) > TorqueCheck.Start_Max) || (Convert.ToUInt16(start) < TorqueCheck.Start_Min))//启动值超差
  1293. {
  1294. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "启动值超范围" + "\r\n";
  1295. label_Step.Text = "启动值超范围,测试结束!";
  1296. label_Step.ForeColor = Color.Red;
  1297. button_Start.Enabled = true;
  1298. textBox_PDinfo.Enabled = true;
  1299. ErrorSaveLog();
  1300. //关机
  1301. Code[0] = 0xF0;
  1302. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1303. 开ToolStripMenuItem.Checked = false;
  1304. 关ToolStripMenuItem.Checked = true;
  1305. Delay_ms(500);
  1306. //关闭串口
  1307. portsettings_form.closePort();
  1308. return;
  1309. }
  1310. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + richTextBox_TQ.Lines[i].Split(':')[0] + "正常" + "\r\n";
  1311. break;
  1312. }
  1313. default:break;
  1314. }
  1315. }
  1316. #endregion
  1317. #region 依次读取配置参数
  1318. label_Step.Text = "判断配置参数";
  1319. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "发送指令查询电机配置参数" + "\r\n";
  1320. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1A00, null);//用户参数1
  1321. Delay_ms(200);
  1322. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3000, null);//用户参数2
  1323. Delay_ms(200);
  1324. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1C00, null);//马达参数
  1325. Delay_ms(200);
  1326. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1F00, null);//生产信息
  1327. Delay_ms(200);
  1328. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1000, null);//校验秘钥
  1329. Delay_ms(200);
  1330. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1300, null);//自定义1
  1331. Delay_ms(200);
  1332. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1500, null);//自定义2
  1333. Delay_ms(200);
  1334. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1700, null);//自定义3
  1335. Delay_ms(200);
  1336. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "判断配置参数" + "\r\n";
  1337. for (int line = 0; line < richTextBox_CfgFile.Lines.Length; line++)
  1338. {
  1339. try
  1340. {
  1341. if (richTextBox_ReadCfg.Lines[line].Equals(richTextBox_CfgFile.Lines[line]) == false)
  1342. {
  1343. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + richTextBox_CfgFile.Lines[line].Split(',')[0] + "错误" + "\r\n";
  1344. label_Step.Text = "检查配置参数失败,测试结束!";
  1345. label_Step.ForeColor = Color.Red;
  1346. button_Start.Enabled = true;
  1347. textBox_PDinfo.Enabled = true;
  1348. ErrorSaveLog();
  1349. //关机
  1350. Code[0] = 0xF0;
  1351. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1352. 开ToolStripMenuItem.Checked = false;
  1353. 关ToolStripMenuItem.Checked = true;
  1354. Delay_ms(500);
  1355. //关闭串口
  1356. portsettings_form.closePort();
  1357. return;
  1358. }
  1359. }
  1360. catch (System.Exception)
  1361. {
  1362. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "读取电机配置参数失败" + "\r\n";
  1363. label_Step.Text = "检查配置参数失败,测试结束!";
  1364. label_Step.ForeColor = Color.Red;
  1365. button_Start.Enabled = true;
  1366. textBox_PDinfo.Enabled = true;
  1367. ErrorSaveLog();
  1368. //关机
  1369. Code[0] = 0xF0;
  1370. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1371. 开ToolStripMenuItem.Checked = false;
  1372. 关ToolStripMenuItem.Checked = true;
  1373. Delay_ms(500);
  1374. //关闭串口
  1375. portsettings_form.closePort();
  1376. return;
  1377. }
  1378. }
  1379. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机配置参数正确" + "\r\n";
  1380. #endregion
  1381. //检查是否有故障
  1382. if (label_error_code.Text != "OK")
  1383. {
  1384. label_Step.Text = "电机存在故障!";
  1385. button_Start.Enabled = true;
  1386. textBox_PDinfo.Enabled = true;
  1387. //关机
  1388. Code[0] = 0xF0;
  1389. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1390. 开ToolStripMenuItem.Checked = false;
  1391. 关ToolStripMenuItem.Checked = true;
  1392. Delay_ms(500);
  1393. //关闭串口
  1394. portsettings_form.closePort();
  1395. //存储测试数据
  1396. ErrorSaveLog();
  1397. return;
  1398. }
  1399. #region 测试结束
  1400. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "测试结束" + "\r\n";
  1401. label_Step.Text = "测试结束!";
  1402. label_Step.ForeColor = Color.Green;
  1403. label_Result.Text = "合格";
  1404. label_Result.BackColor = Color.Green;
  1405. button_Start.Enabled = true;
  1406. textBox_PDinfo.Enabled = true;
  1407. //关机
  1408. Code[0] = 0xF0;
  1409. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1410. 开ToolStripMenuItem.Checked = false;
  1411. 关ToolStripMenuItem.Checked = true;
  1412. Delay_ms(500);
  1413. //关闭串口
  1414. portsettings_form.closePort();
  1415. //合格计数累加和存储
  1416. label_OK_Count.Text = (Convert.ToInt32(label_OK_Count.Text) + 1).ToString();
  1417. string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\CheckSerialNum";
  1418. StreamReader objReader = new StreamReader(cfg_file);
  1419. string sLine = "";
  1420. ArrayList arrText = new ArrayList();//创建一个动态数组
  1421. while (sLine != null)
  1422. {
  1423. sLine = objReader.ReadLine();
  1424. arrText.Add(sLine);
  1425. }
  1426. arrText.RemoveAt(arrText.Count - 1);
  1427. objReader.Close();
  1428. arrText[3] = label_OK_Count.Text;
  1429. using (StreamWriter file = new StreamWriter(cfg_file))
  1430. {
  1431. foreach (var entry in arrText)
  1432. {
  1433. file.WriteLine(entry);
  1434. }
  1435. }
  1436. #region 按照产品信息设置测试文件路径
  1437. string ResultPathName = textBox_PDinfo.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_");
  1438. if (System.IO.Directory.Exists(LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName) == false)
  1439. System.IO.Directory.CreateDirectory(LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName);
  1440. #endregion
  1441. //页面存储
  1442. Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap
  1443. Graphics g = Graphics.FromImage(bit);
  1444. g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片
  1445. LogSaveFileName = LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName + "\\测试记录" + ".png";
  1446. bit.Save(LogSaveFileName);
  1447. //测试页面上传服务器
  1448. if (StartForm.myFtp.IsNetConnected == true)
  1449. {
  1450. string DateNow = DateTime.Now.ToString("yyyy-MM-dd");
  1451. if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/qcTest/log/", DateNow) == false)
  1452. {
  1453. StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow);
  1454. }
  1455. if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/qcTest/log/", DateNow + "/" + ResultPathName) == false)
  1456. {
  1457. StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow + "/" + ResultPathName);
  1458. }
  1459. bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow + "/" + ResultPathName);
  1460. if (result == true)
  1461. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "测试页面上传服务器成功" + "\r\n";
  1462. }
  1463. //电机数据存储
  1464. LogSaveFileName = LogSavePath + "\\log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName + "\\电机参数" + ".txt";
  1465. string fileInfo = "";
  1466. fileInfo += "检验记录:\r\n" + richTextBox_TestRecord.Text.Replace("\n", "\r\n") + "\r\n\r\n";
  1467. fileInfo += "电机生产参数:\r\n";
  1468. fileInfo += richTextBox_CfgFile.Text.Replace("\n", "\r\n") + "\r\n\r\n";
  1469. fileInfo += "电机版本信息查询:\r\n";
  1470. fileInfo += richTextBox_VerInfo.Text.Replace("\n", "\r\n") + "\r\n\r\n";
  1471. fileInfo += "力矩传感器标定结果:\r\n";
  1472. fileInfo += richTextBox_TQ.Text.Replace("\n", "\r\n") + "\r\n\r\n";
  1473. fileInfo += "电机参数查询:\r\n";
  1474. fileInfo += richTextBox_ReadCfg.Text.Replace("\n", "\r\n") + "\r\n\r\n";
  1475. fileInfo += "电机故障码:\r\n";
  1476. fileInfo += label_error_code.Text.Replace("\n", "\r\n") + "\r\n\r\n";
  1477. fileInfo += "测试结果:\r\n";
  1478. fileInfo += label_Result.Text.Replace("\n", "\r\n") + "\r\n\r\n";
  1479. System.IO.File.WriteAllText(LogSaveFileName, fileInfo);
  1480. //电机数据上传服务器
  1481. if (StartForm.myFtp.IsNetConnected == true)
  1482. {
  1483. string DateNow = DateTime.Now.ToString("yyyy-MM-dd");
  1484. if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/qcTest/log/", DateNow) == false)
  1485. {
  1486. StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow);
  1487. }
  1488. if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow, ResultPathName) == false)
  1489. {
  1490. StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow + "/" + ResultPathName);
  1491. }
  1492. bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow + "/" + ResultPathName);
  1493. if (result == true)
  1494. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机数据上传服务器成功" + "\r\n";
  1495. }
  1496. #endregion
  1497. //测试正常,清空扫码信息
  1498. Delay_ms(1000);
  1499. textBox_PDinfo.Text = "";
  1500. }
  1501. private void CheckForm_Load(object sender, EventArgs e)
  1502. {
  1503. timer1.Enabled = true;
  1504. timer2.Enabled = true;
  1505. //显示测试员信息
  1506. label_TestUser.Text = "测试员:" + StartForm.EnterForm1.comboBox_User.Text;
  1507. //窗口加载时,从FTP服务器或本地获取文件日期列表
  1508. GetDateList(false);
  1509. //装载序列号文件
  1510. string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\CheckSerialNum";
  1511. StreamReader objReader = new StreamReader(cfg_file);
  1512. string sLine = "";
  1513. ArrayList arrText = new ArrayList();//创建一个动态数组
  1514. while (sLine != null)
  1515. {
  1516. sLine = objReader.ReadLine();
  1517. arrText.Add(sLine);
  1518. }
  1519. objReader.Close();
  1520. label_SerialNum.Text = arrText[1].ToString();//流水号
  1521. label_OK_Count.Text = arrText[3].ToString();//合格计数
  1522. }
  1523. //测试失败存储测试界面
  1524. private void ErrorSaveLog()
  1525. {
  1526. //页面存储
  1527. Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap
  1528. Graphics g = Graphics.FromImage(bit);
  1529. g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片
  1530. string LogSaveFileName = LogSavePath + "errorlog\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\";
  1531. if (System.IO.Directory.Exists(LogSaveFileName) == false)
  1532. System.IO.Directory.CreateDirectory(LogSaveFileName);
  1533. LogSaveFileName += textBox_PDinfo.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_测试记录" + ".png";
  1534. bit.Save(LogSaveFileName);
  1535. //测试页面上传服务器
  1536. if (StartForm.myFtp.IsNetConnected == true)
  1537. {
  1538. string DateNow = DateTime.Now.ToString("yyyy-MM-dd");
  1539. if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/qcTest/errorlog/", DateNow) == false)
  1540. {
  1541. StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/qcTest/errorlog/" + DateNow);
  1542. }
  1543. bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/qcTest/errorlog/" + DateNow);
  1544. if (result == true)
  1545. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "测试异常页面上传服务器成功" + "\r\n";
  1546. }
  1547. }
  1548. //开始测试按钮点击
  1549. private void button_Start_Click(object sender, EventArgs e)
  1550. {
  1551. TestStart();
  1552. }
  1553. //开始一次测试
  1554. private void TestStart()
  1555. {
  1556. //显示数据清空
  1557. richTextBox_ReadCfg.Text = "";
  1558. richTextBox_TestRecord.Text = "";
  1559. richTextBox_TQ.Text = "";
  1560. richTextBox_VerInfo.Text = "";
  1561. //Boot版本标志复位
  1562. IsBootVerOK = false;
  1563. #region 检查配置文件
  1564. if (comboBox_File.Items.Count == 0) //配置文件目录为空
  1565. {
  1566. label_Step.Text = "无可用配置文件!";
  1567. label_Step.ForeColor = Color.Red;
  1568. return;
  1569. }
  1570. if (comboBox_File.Text.Trim().Length == 0)//未选择配置文件
  1571. {
  1572. label_Step.Text = "请选择配置文件!";
  1573. label_Step.ForeColor = Color.Red;
  1574. return;
  1575. }
  1576. #endregion
  1577. #region 检查产品信息是否符合格式要求
  1578. try
  1579. {
  1580. 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]$");
  1581. if (textBox_PDinfo.Text.Contains(" "))
  1582. {
  1583. string Mode = textBox_PDinfo.Text.Split(new string[] { " " }, StringSplitOptions.None)[0];
  1584. string Sn = textBox_PDinfo.Text.Split(new string[] { " " }, StringSplitOptions.None)[1];
  1585. if (regex.IsMatch(Sn) == false)
  1586. {
  1587. label_Step.Text = "SN格式错误!";
  1588. label_Step.ForeColor = Color.Red;
  1589. return;
  1590. }
  1591. }
  1592. else if (textBox_PDinfo.Text.Contains("%#"))
  1593. {
  1594. string Mode = textBox_PDinfo.Text.Split(new string[] { "%#" }, StringSplitOptions.None)[0];
  1595. string Sn = textBox_PDinfo.Text.Split(new string[] { "%#" }, StringSplitOptions.None)[1];
  1596. if (regex.IsMatch(Sn) == false)
  1597. {
  1598. label_Step.Text = "SN格式错误!";
  1599. label_Step.ForeColor = Color.Red;
  1600. return;
  1601. }
  1602. }
  1603. else
  1604. {
  1605. label_Step.Text = "标签格式错误!";
  1606. label_Step.ForeColor = Color.Red;
  1607. return;
  1608. }
  1609. }
  1610. catch (System.Exception)
  1611. {
  1612. label_Step.Text = "标签格式错误!";
  1613. label_Step.ForeColor = Color.Red;
  1614. return;
  1615. }
  1616. #endregion
  1617. #region 检查产品型号和配置文件是否一致
  1618. do
  1619. {
  1620. string cfg_mode = "", pd_mode = "";
  1621. cfg_mode = comboBox_File.Text.Split('_')[0];
  1622. if (textBox_PDinfo.Text.Contains(" "))
  1623. {
  1624. pd_mode = textBox_PDinfo.Text.Split(new string[] { " " }, StringSplitOptions.None)[0];
  1625. }
  1626. else if (textBox_PDinfo.Text.Contains("%#"))
  1627. {
  1628. pd_mode = textBox_PDinfo.Text.Split(new string[] { "%#" }, StringSplitOptions.None)[0];
  1629. }
  1630. if (pd_mode != cfg_mode)
  1631. {
  1632. label_Step.Text = "打标信息与配置文件不一致!";
  1633. label_Step.ForeColor = Color.Red;
  1634. return;
  1635. }
  1636. } while (false);
  1637. #endregion
  1638. //连接串口
  1639. if (portsettings_form.openPort() == false)
  1640. {
  1641. label_Step.Text = "串口连接失败!";
  1642. label_Step.ForeColor = Color.Red;
  1643. return;
  1644. }
  1645. //测试状态更新
  1646. label_Step.Text = "准备开始测试";
  1647. label_Step.ForeColor = Color.Red;
  1648. //流水号累加和存储
  1649. label_SerialNum.Text = (Convert.ToInt32(label_SerialNum.Text) + 1).ToString();
  1650. string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\CheckSerialNum";
  1651. StreamReader objReader = new StreamReader(cfg_file);
  1652. string sLine = "";
  1653. ArrayList arrText = new ArrayList();//创建一个动态数组
  1654. while (sLine != null)
  1655. {
  1656. sLine = objReader.ReadLine();
  1657. arrText.Add(sLine);
  1658. }
  1659. arrText.RemoveAt(arrText.Count - 1);
  1660. objReader.Close();
  1661. arrText[1] = label_SerialNum.Text;
  1662. using (StreamWriter file = new StreamWriter(cfg_file))
  1663. {
  1664. foreach (var entry in arrText)
  1665. {
  1666. file.WriteLine(entry);
  1667. }
  1668. }
  1669. button_Start.Enabled = false;
  1670. textBox_PDinfo.Enabled = false;
  1671. //创建线程,执行测试任务
  1672. Thread th = new Thread(Thread_Task);
  1673. th.Start();
  1674. }
  1675. //双击清零流水号
  1676. private void label32_DoubleClick(object sender, EventArgs e)
  1677. {
  1678. StartForm.EnterForm1.ShowDialog();
  1679. try
  1680. {
  1681. if (StartForm.EnterForm1.textBox_Passwd.Text == StartForm.EnterForm1.UserAccount[StartForm.EnterForm1.comboBox_User.Text])
  1682. {
  1683. if (StartForm.EnterForm1.comboBox_User.Text == "工程Engineer")
  1684. {
  1685. //流水号清零
  1686. if (MessageBox.Show("流水号清零", "确认清零?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1687. {
  1688. label_SerialNum.Text = "0";
  1689. label_OK_Count.Text = "0";
  1690. //存储
  1691. string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\CheckSerialNum";
  1692. StreamReader objReader = new StreamReader(cfg_file);
  1693. string sLine = "";
  1694. ArrayList arrText = new ArrayList();//创建一个动态数组
  1695. while (sLine != null)
  1696. {
  1697. sLine = objReader.ReadLine();
  1698. arrText.Add(sLine);
  1699. }
  1700. arrText.RemoveAt(arrText.Count - 1);
  1701. objReader.Close();
  1702. arrText[1] = label_SerialNum.Text;
  1703. arrText[3] = label_OK_Count.Text;
  1704. using (StreamWriter file = new StreamWriter(cfg_file))
  1705. {
  1706. foreach (var entry in arrText)
  1707. {
  1708. file.WriteLine(entry);
  1709. }
  1710. }
  1711. }
  1712. }
  1713. else
  1714. {
  1715. MessageBox.Show("权限不支持", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1716. }
  1717. }
  1718. else
  1719. {
  1720. MessageBox.Show("密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1721. }
  1722. }
  1723. catch (Exception)
  1724. {
  1725. MessageBox.Show("用户不存在或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1726. }
  1727. }
  1728. private void textBox_PDinfo_KeyUp(object sender, KeyEventArgs e)
  1729. {
  1730. if (e.KeyCode == Keys.Enter)
  1731. TestStart();
  1732. }
  1733. private void textBox_PDinfo_Click(object sender, EventArgs e)
  1734. {
  1735. textBox_PDinfo.SelectAll();
  1736. }
  1737. private void 屏幕键盘ToolStripMenuItem_Click_1(object sender, EventArgs e)
  1738. {
  1739. System.Diagnostics.Process.Start("osk.exe");
  1740. }
  1741. private void 端口设置ToolStripMenuItem_Click(object sender, EventArgs e)
  1742. {
  1743. portsettings_form.Show();
  1744. }
  1745. private void 合格ToolStripMenuItem_Click(object sender, EventArgs e)
  1746. {
  1747. System.Diagnostics.Process.Start("explorer.exe", LogSavePath + "log\\");
  1748. }
  1749. private void 不合格ToolStripMenuItem_Click(object sender, EventArgs e)
  1750. {
  1751. System.Diagnostics.Process.Start("explorer.exe", LogSavePath + "errorlog\\");
  1752. }
  1753. private void CheckForm_FormClosing(object sender, FormClosingEventArgs e)
  1754. {
  1755. portsettings_form.serialPort1.Close();
  1756. portsettings_form.comboBox_ComIndex.Enabled = true;
  1757. portsettings_form.button_Enter.Text = "确认";
  1758. portsettings_form.g_blnIsOpen = false;
  1759. #if false
  1760. //针对工程和越南账户,直接退出,不执行删除本地配置
  1761. if ((StartForm.EnterForm1.comboBox_User.Text == "越南Vietnam") || (StartForm.EnterForm1.comboBox_User.Text == "工程Engineer"))
  1762. {
  1763. return;
  1764. }
  1765. //关闭前,删除本地所有配置文件
  1766. string path = StartForm.UserPath + "\\cfg\\";
  1767. try
  1768. {
  1769. DirectoryInfo dir = new DirectoryInfo(path);
  1770. FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录
  1771. foreach (FileSystemInfo i in fileinfo)
  1772. {
  1773. if (i is DirectoryInfo) //判断是否文件夹
  1774. {
  1775. DirectoryInfo subdir = new DirectoryInfo(i.FullName);
  1776. subdir.Delete(true); //删除子目录和文件
  1777. }
  1778. else
  1779. {
  1780. File.Delete(i.FullName); //删除指定文件
  1781. }
  1782. }
  1783. //MessageBox.Show("文件清理成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1784. }
  1785. catch (Exception)
  1786. {
  1787. MessageBox.Show("文件清理失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1788. }
  1789. #endif
  1790. }
  1791. }
  1792. }