CheckForm.cs 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  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 端口设置界面
  22. public static PortSettings portsettings_form = new PortSettings();
  23. #endregion
  24. #region 力矩传感器判断标准
  25. TorqueCheckParam TorqueCheck = new TorqueCheckParam();
  26. #endregion
  27. #region 串口缓存
  28. public static List<byte> buffer = new List<byte>(4096);
  29. private byte[] binary_data_1 = new byte[1024];
  30. #endregion
  31. #region MC故障码超时计数
  32. bool MC_ErrorCode_Refresh = false;
  33. ushort MC_ErrorCode_Refresh_Cnt = 0;
  34. #endregion
  35. #region 配置文件信息
  36. ArrayList array_CfgInfo = new ArrayList();
  37. #endregion
  38. #region CRC32校验
  39. static UInt32[] crc32_table = new UInt32[256]
  40. {
  41. 0x00000000, 0x04C11DB7, 0x09823B6E, 0x0D4326D9, 0x130476DC, 0x17C56B6B,
  42. 0x1A864DB2, 0x1E475005, 0x2608EDB8, 0x22C9F00F, 0x2F8AD6D6, 0x2B4BCB61,
  43. 0x350C9B64, 0x31CD86D3, 0x3C8EA00A, 0x384FBDBD, 0x4C11DB70, 0x48D0C6C7,
  44. 0x4593E01E, 0x4152FDA9, 0x5F15ADAC, 0x5BD4B01B, 0x569796C2, 0x52568B75,
  45. 0x6A1936C8, 0x6ED82B7F, 0x639B0DA6, 0x675A1011, 0x791D4014, 0x7DDC5DA3,
  46. 0x709F7B7A, 0x745E66CD, 0x9823B6E0, 0x9CE2AB57, 0x91A18D8E, 0x95609039,
  47. 0x8B27C03C, 0x8FE6DD8B, 0x82A5FB52, 0x8664E6E5, 0xBE2B5B58, 0xBAEA46EF,
  48. 0xB7A96036, 0xB3687D81, 0xAD2F2D84, 0xA9EE3033, 0xA4AD16EA, 0xA06C0B5D,
  49. 0xD4326D90, 0xD0F37027, 0xDDB056FE, 0xD9714B49, 0xC7361B4C, 0xC3F706FB,
  50. 0xCEB42022, 0xCA753D95, 0xF23A8028, 0xF6FB9D9F, 0xFBB8BB46, 0xFF79A6F1,
  51. 0xE13EF6F4, 0xE5FFEB43, 0xE8BCCD9A, 0xEC7DD02D, 0x34867077, 0x30476DC0,
  52. 0x3D044B19, 0x39C556AE, 0x278206AB, 0x23431B1C, 0x2E003DC5, 0x2AC12072,
  53. 0x128E9DCF, 0x164F8078, 0x1B0CA6A1, 0x1FCDBB16, 0x018AEB13, 0x054BF6A4,
  54. 0x0808D07D, 0x0CC9CDCA, 0x7897AB07, 0x7C56B6B0, 0x71159069, 0x75D48DDE,
  55. 0x6B93DDDB, 0x6F52C06C, 0x6211E6B5, 0x66D0FB02, 0x5E9F46BF, 0x5A5E5B08,
  56. 0x571D7DD1, 0x53DC6066, 0x4D9B3063, 0x495A2DD4, 0x44190B0D, 0x40D816BA,
  57. 0xACA5C697, 0xA864DB20, 0xA527FDF9, 0xA1E6E04E, 0xBFA1B04B, 0xBB60ADFC,
  58. 0xB6238B25, 0xB2E29692, 0x8AAD2B2F, 0x8E6C3698, 0x832F1041, 0x87EE0DF6,
  59. 0x99A95DF3, 0x9D684044, 0x902B669D, 0x94EA7B2A, 0xE0B41DE7, 0xE4750050,
  60. 0xE9362689, 0xEDF73B3E, 0xF3B06B3B, 0xF771768C, 0xFA325055, 0xFEF34DE2,
  61. 0xC6BCF05F, 0xC27DEDE8, 0xCF3ECB31, 0xCBFFD686, 0xD5B88683, 0xD1799B34,
  62. 0xDC3ABDED, 0xD8FBA05A, 0x690CE0EE, 0x6DCDFD59, 0x608EDB80, 0x644FC637,
  63. 0x7A089632, 0x7EC98B85, 0x738AAD5C, 0x774BB0EB, 0x4F040D56, 0x4BC510E1,
  64. 0x46863638, 0x42472B8F, 0x5C007B8A, 0x58C1663D, 0x558240E4, 0x51435D53,
  65. 0x251D3B9E, 0x21DC2629, 0x2C9F00F0, 0x285E1D47, 0x36194D42, 0x32D850F5,
  66. 0x3F9B762C, 0x3B5A6B9B, 0x0315D626, 0x07D4CB91, 0x0A97ED48, 0x0E56F0FF,
  67. 0x1011A0FA, 0x14D0BD4D, 0x19939B94, 0x1D528623, 0xF12F560E, 0xF5EE4BB9,
  68. 0xF8AD6D60, 0xFC6C70D7, 0xE22B20D2, 0xE6EA3D65, 0xEBA91BBC, 0xEF68060B,
  69. 0xD727BBB6, 0xD3E6A601, 0xDEA580D8, 0xDA649D6F, 0xC423CD6A, 0xC0E2D0DD,
  70. 0xCDA1F604, 0xC960EBB3, 0xBD3E8D7E, 0xB9FF90C9, 0xB4BCB610, 0xB07DABA7,
  71. 0xAE3AFBA2, 0xAAFBE615, 0xA7B8C0CC, 0xA379DD7B, 0x9B3660C6, 0x9FF77D71,
  72. 0x92B45BA8, 0x9675461F, 0x8832161A, 0x8CF30BAD, 0x81B02D74, 0x857130C3,
  73. 0x5D8A9099, 0x594B8D2E, 0x5408ABF7, 0x50C9B640, 0x4E8EE645, 0x4A4FFBF2,
  74. 0x470CDD2B, 0x43CDC09C, 0x7B827D21, 0x7F436096, 0x7200464F, 0x76C15BF8,
  75. 0x68860BFD, 0x6C47164A, 0x61043093, 0x65C52D24, 0x119B4BE9, 0x155A565E,
  76. 0x18197087, 0x1CD86D30, 0x029F3D35, 0x065E2082, 0x0B1D065B, 0x0FDC1BEC,
  77. 0x3793A651, 0x3352BBE6, 0x3E119D3F, 0x3AD08088, 0x2497D08D, 0x2056CD3A,
  78. 0x2D15EBE3, 0x29D4F654, 0xC5A92679, 0xC1683BCE, 0xCC2B1D17, 0xC8EA00A0,
  79. 0xD6AD50A5, 0xD26C4D12, 0xDF2F6BCB, 0xDBEE767C, 0xE3A1CBC1, 0xE760D676,
  80. 0xEA23F0AF, 0xEEE2ED18, 0xF0A5BD1D, 0xF464A0AA, 0xF9278673, 0xFDE69BC4,
  81. 0x89B8FD09, 0x8D79E0BE, 0x803AC667, 0x84FBDBD0, 0x9ABC8BD5, 0x9E7D9662,
  82. 0x933EB0BB, 0x97FFAD0C, 0xAFB010B1, 0xAB710D06, 0xA6322BDF, 0xA2F33668,
  83. 0xBCB4666D, 0xB8757BDA, 0xB5365D03, 0xB1F740B4
  84. };
  85. static UInt32 crc32_cal(byte[] pData, UInt16 Length)
  86. {
  87. UInt32 nReg;
  88. UInt32 nTemp = 0;
  89. UInt16 i, n;
  90. nReg = 0xFFFFFFFF;
  91. for (n = 0; n < Length; n++)
  92. {
  93. nReg ^= (UInt32)pData[n];
  94. for (i = 0; i < 4; i++)
  95. {
  96. nTemp = crc32_table[((nReg >> 24) & 0xFF)];
  97. nReg <<= 8;
  98. nReg ^= nTemp;
  99. }
  100. }
  101. return nReg;
  102. }
  103. #endregion
  104. #region 命令发送
  105. private void SendCmd(ushort ID, byte Mode, ushort Cmd, byte[] Data)
  106. {
  107. if (!portsettings_form.serialPort1.IsOpen)
  108. {
  109. MessageBox.Show("串口未连接");
  110. return;
  111. }
  112. ushort DataLength = (ushort)(Cmd & 0xFF);
  113. var SendCmdTmp = new byte[DataLength + 13];
  114. //帧头
  115. SendCmdTmp[0] = 0x55;
  116. SendCmdTmp[1] = 0xAA;
  117. //ID
  118. SendCmdTmp[2] = (byte)(ID >> 8);
  119. SendCmdTmp[3] = (byte)(ID & 0xFF);
  120. //MODE
  121. SendCmdTmp[4] = Mode;
  122. //长度
  123. SendCmdTmp[5] = (byte)((Cmd & 0xFF) + 2);
  124. //命令字
  125. SendCmdTmp[6] = (byte)(Cmd >> 8);
  126. SendCmdTmp[7] = (byte)(Cmd & 0xFF);
  127. //数据段
  128. for (UInt16 i = 0; i < (Cmd & 0xFF); i++)
  129. {
  130. SendCmdTmp[8 + i] = Data[i];
  131. }
  132. //校验
  133. UInt32 CRC_Result = crc32_cal(SendCmdTmp, (ushort)(DataLength + 8));
  134. SendCmdTmp[DataLength + 8] = Convert.ToByte(CRC_Result >> 24);
  135. SendCmdTmp[DataLength + 9] = Convert.ToByte((CRC_Result >> 16) % 256);
  136. SendCmdTmp[DataLength + 10] = Convert.ToByte((CRC_Result >> 8) % 256);
  137. SendCmdTmp[DataLength + 11] = Convert.ToByte(CRC_Result % 256);
  138. //帧尾
  139. SendCmdTmp[DataLength + 12] = 0xF0;
  140. //发送数据
  141. portsettings_form.serialPort1.Write(SendCmdTmp, 0, SendCmdTmp.Length);
  142. }
  143. #endregion
  144. #region 串口数据解析
  145. private void Serial_DataReceived_Process()
  146. {
  147. //串口解析
  148. byte DataNum = 0; //记录每条命令数据段的长度
  149. while (buffer.Count >= 13)//当buffer中的数据的个数大于等于4个时,即每一帧的数据长度所在位(第4位)出现时
  150. {
  151. if (buffer[0] == 0x55 && buffer[1] == 0xAA)//判断帧头
  152. {
  153. ushort PackageID;
  154. PackageID = (ushort)(buffer[2] * 256 + buffer[3]);
  155. if ((PackageID == 0x0710) || (PackageID == 0x0715) || (PackageID == 0x0610) || (PackageID == 0x0615) || (PackageID == 0x07FF))
  156. {
  157. int CmdLen = buffer[5];
  158. if (buffer.Count < CmdLen + 11) break;//如果接收到的数据没有达到一帧数据的指定长度, 则执行下次循环
  159. buffer.RemoveRange(0, 6);//数据达到要求长度后删去帧头,ID,模式,和命令长度
  160. DataNum = (byte)(buffer[1]);//命令字的第二个字节的表示数据位的长度
  161. if (DataNum == (CmdLen - 2))
  162. {
  163. for (int i = 0; i < DataNum + 2; i++)
  164. {
  165. binary_data_1[i] = buffer[i];
  166. }
  167. DataCmdProcess(PackageID);//对于不同的命令段,做不同的处理
  168. }
  169. buffer.RemoveRange(0, CmdLen + 5);//解析完成
  170. }
  171. else
  172. {
  173. buffer.RemoveAt(0);//ID不对
  174. }
  175. }
  176. else
  177. {
  178. buffer.RemoveAt(0);//帧头不对,删除帧头高字节
  179. }
  180. }
  181. }
  182. #endregion
  183. #region 故障转换和显示
  184. List<string> ErrorInfo = new List<string>{ "过流保护", "低压保护", "过压保护", "堵转保护","过热保护","速度传感器故障","力矩传感器故障","霍尔故障",
  185. "马达缺相","NTC故障","","","","MCU故障","踏频传感器故障","指拨故障","MOS短路","电压异常","电路故障","TE故障","TE电路故障","","","","","","","","","",
  186. "",""};
  187. private string ErrorInfoDisplay(UInt32 Code)
  188. {
  189. string Result = "";
  190. if (Code == 0)
  191. {
  192. Result = "无故障";
  193. }
  194. else
  195. {
  196. for (int i = 0; i < 32; i++)
  197. {
  198. if ((Code & 0x01) == 0x01)
  199. {
  200. Result += ErrorInfo[i] + " ";
  201. }
  202. Code >>= 1;
  203. }
  204. }
  205. return Result;
  206. }
  207. #endregion
  208. #region 数据解析处理
  209. private void DataCmdProcess(ushort ID)
  210. {
  211. ushort CmdTemp = (ushort)(binary_data_1[0] * 256 + binary_data_1[1]);
  212. ushort DataTemp;
  213. #region 主控信息
  214. if ((ID == 0x715) || (ID == 0x710))
  215. {
  216. switch (CmdTemp)
  217. {
  218. case 0x1104://故障码
  219. {
  220. unchecked
  221. {
  222. this.Invoke((EventHandler)(delegate
  223. {
  224. MC_ErrorCode_Refresh = true;
  225. label_error_code.Text =
  226. Convert.ToString(binary_data_1[5], 16).PadLeft(2, '0').ToUpper() +
  227. Convert.ToString(binary_data_1[4], 16).PadLeft(2, '0').ToUpper() +
  228. Convert.ToString(binary_data_1[3], 16).PadLeft(2, '0').ToUpper() +
  229. Convert.ToString(binary_data_1[2], 16).PadLeft(2, '0').ToUpper() +
  230. "H";
  231. label_error_code.BackColor = Color.Red;
  232. }));
  233. }
  234. break;
  235. }
  236. case 0x1240://电机版本信息
  237. {
  238. unchecked
  239. {
  240. this.Invoke((EventHandler)(delegate
  241. {
  242. //电机型号
  243. richTextBox_VerInfo.Text += "型号:";
  244. for (ushort i = 0; i < 16; i++)
  245. {
  246. if (binary_data_1[2 + i] == 0x2E)
  247. {
  248. break;
  249. }
  250. richTextBox_VerInfo.Text += ((char)binary_data_1[2 + i]).ToString();
  251. }
  252. richTextBox_VerInfo.Text += "\r\n";
  253. //电机SN
  254. richTextBox_VerInfo.Text += "序列号:";
  255. for (ushort i = 0; i < 16; i++)
  256. {
  257. if (binary_data_1[18 + i] == 0x2E)
  258. {
  259. break;
  260. }
  261. richTextBox_VerInfo.Text += ((char)binary_data_1[18 + i]).ToString();
  262. }
  263. richTextBox_VerInfo.Text += "\r\n";
  264. //电机HW
  265. richTextBox_VerInfo.Text += "硬件版本:";
  266. for (ushort i = 0; i < 10; i++)//前10位是PCB版号,以'.'结束
  267. {
  268. if (binary_data_1[34 + i] == 0x2E)
  269. {
  270. break;
  271. }
  272. if (binary_data_1[34 + i] == 'r')
  273. {
  274. binary_data_1[34 + i] = (byte)'.';
  275. }
  276. richTextBox_VerInfo.Text += ((char)binary_data_1[34 + i]).ToString();
  277. }
  278. switch (binary_data_1[34 + 10])//第10位代表MCU品牌
  279. {
  280. case (byte)'1':
  281. richTextBox_VerInfo.Text += " " + "ST" + " "; break;
  282. case (byte)'2':
  283. richTextBox_VerInfo.Text += " " + "APM" + " "; break;
  284. case (byte)'3':
  285. richTextBox_VerInfo.Text += " " + "HK" + " "; break;
  286. case (byte)'4':
  287. richTextBox_VerInfo.Text += " " + "GD" + " "; break;
  288. default:
  289. richTextBox_VerInfo.Text += " " + "NULL" + " "; break;
  290. }
  291. for (ushort i = 0; i < 4; i++)//后4位是MCU的ID校验码,用于识别板子的ID
  292. {
  293. richTextBox_VerInfo.Text += Convert.ToString(binary_data_1[46 + i], 16).PadLeft(2, '0').ToUpper();//MCU ID 的CRC32值
  294. }
  295. richTextBox_VerInfo.Text += "\r\n";
  296. //电机FW
  297. richTextBox_VerInfo.Text += "软件版本:";
  298. for (ushort i = 0; i < 16; i++)
  299. {
  300. if (binary_data_1[50 + i] == 0x2E)
  301. {
  302. break;
  303. }
  304. if (binary_data_1[50 + i] == 'r')
  305. {
  306. binary_data_1[50 + i] = (byte)'.';
  307. }
  308. richTextBox_VerInfo.Text += ((char)binary_data_1[50 + i]).ToString();
  309. }
  310. richTextBox_VerInfo.Text += "\r\n";
  311. }));
  312. }
  313. break;
  314. }
  315. case 0x1720://电机程序特性信息
  316. {
  317. unchecked
  318. {
  319. this.Invoke((EventHandler)(delegate
  320. {
  321. //程序特性
  322. richTextBox_VerInfo.Text += "软件属性:";
  323. for (ushort i = 0; i < 32; i++)
  324. {
  325. if (binary_data_1[2 + i] == 0x2E)
  326. {
  327. break;
  328. }
  329. if (binary_data_1[2 + i] == 'r')
  330. {
  331. binary_data_1[2 + i] = (byte)'.';
  332. }
  333. richTextBox_VerInfo.Text += ((char)binary_data_1[2 + i]).ToString();
  334. }
  335. richTextBox_VerInfo.Text += "\r\n";
  336. }));
  337. }
  338. break;
  339. }
  340. case 0xA020://用户参数1
  341. {
  342. unchecked
  343. {
  344. this.Invoke((EventHandler)(delegate
  345. {
  346. richTextBox_ReadCfg.Text += "[用户参数1]" + "\r\n";
  347. richTextBox_ReadCfg.Text += "启动模式," + binary_data_1[3].ToString() + "\r\n";
  348. richTextBox_ReadCfg.Text += "停机时间," + ((ushort)(binary_data_1[5] * 256 + binary_data_1[4])).ToString() + "\r\n";
  349. richTextBox_ReadCfg.Text += "限速值," + binary_data_1[6].ToString() + "\r\n";
  350. richTextBox_ReadCfg.Text += "下降速度," + binary_data_1[7].ToString() + "\r\n";
  351. richTextBox_ReadCfg.Text += "前飞," + binary_data_1[8].ToString() + "\r\n";
  352. richTextBox_ReadCfg.Text += "后飞," + binary_data_1[9].ToString() + "\r\n";
  353. richTextBox_ReadCfg.Text += "限流," + binary_data_1[10].ToString() + "\r\n";
  354. richTextBox_ReadCfg.Text += "温度预警," + (binary_data_1[11] - 40).ToString() + "\r\n";
  355. richTextBox_ReadCfg.Text += "温度保护," + (binary_data_1[12] - 40).ToString() + "\r\n";
  356. richTextBox_ReadCfg.Text += "码表," + binary_data_1[13].ToString()+",";
  357. richTextBox_ReadCfg.Text += ((binary_data_1[13] == 0x55) ? "有" : "无") + "\r\n";
  358. richTextBox_ReadCfg.Text += "轮胎周长," + binary_data_1[14].ToString() + "\r\n";
  359. richTextBox_ReadCfg.Text += "系列号," + binary_data_1[15].ToString() + "\r\n";
  360. richTextBox_ReadCfg.Text += "ECO增益," + binary_data_1[16].ToString() + "\r\n";
  361. richTextBox_ReadCfg.Text += "ECO加速," + binary_data_1[17].ToString() + "\r\n";
  362. richTextBox_ReadCfg.Text += "NORM增益," + binary_data_1[18].ToString() + "\r\n";
  363. richTextBox_ReadCfg.Text += "NORM加速," + binary_data_1[19].ToString() + "\r\n";
  364. richTextBox_ReadCfg.Text += "SPORT增益," + binary_data_1[20].ToString() + "\r\n";
  365. richTextBox_ReadCfg.Text += "SPORT加速," + binary_data_1[21].ToString() + "\r\n";
  366. richTextBox_ReadCfg.Text += "TURBO增益," + binary_data_1[22].ToString() + "\r\n";
  367. richTextBox_ReadCfg.Text += "TURBO加速," + binary_data_1[23].ToString() + "\r\n";
  368. richTextBox_ReadCfg.Text += "SMART增益," + binary_data_1[24].ToString() + "\r\n";
  369. richTextBox_ReadCfg.Text += "SMART加速," + binary_data_1[25].ToString() + "\r\n";
  370. richTextBox_ReadCfg.Text += "车速级数," + binary_data_1[26].ToString() + "\r\n";
  371. richTextBox_ReadCfg.Text += "踏频启动," + binary_data_1[27].ToString() + "\r\n";
  372. richTextBox_ReadCfg.Text += "指拨模式," + binary_data_1[2].ToString() + ",";
  373. richTextBox_ReadCfg.Text += ((binary_data_1[2] == 0x55) ? "不支持" : "支持") + "\r\n";
  374. richTextBox_ReadCfg.Text += "速度信号来源," + binary_data_1[28].ToString() + ",";
  375. richTextBox_ReadCfg.Text += ((binary_data_1[28] == 0x55) ? "传感器" :
  376. (binary_data_1[28] == 0xAA) ? "踏频" :
  377. (binary_data_1[28] == 0xEE) ? "通信" : "") + "\r\n";
  378. richTextBox_ReadCfg.Text += "周长微调," + ((int)binary_data_1[29]).ToString() + "\r\n";
  379. richTextBox_ReadCfg.Text += "低压保护," + ((ushort)(binary_data_1[31] * 256 + binary_data_1[30])).ToString() + "\r\n";
  380. richTextBox_ReadCfg.Text += "推行限速," + binary_data_1[32].ToString() + "\r\n";
  381. richTextBox_ReadCfg.Text += "推行转速," + binary_data_1[33].ToString() + "\r\n";
  382. }));
  383. }
  384. break;
  385. }
  386. case 0xA110://马达参数
  387. {
  388. unchecked
  389. {
  390. this.Invoke((EventHandler)(delegate
  391. {
  392. richTextBox_ReadCfg.Text += "\r\n";
  393. richTextBox_ReadCfg.Text += "[马达参数]" + "\r\n";
  394. richTextBox_ReadCfg.Text += "额定功率," + ((ushort)(binary_data_1[3] * 256 + binary_data_1[2])).ToString() + "\r\n";
  395. richTextBox_ReadCfg.Text += "额定转速," + ((ushort)(binary_data_1[5] * 256 + binary_data_1[4])).ToString() + "\r\n";
  396. richTextBox_ReadCfg.Text += "定子电阻," + ((ushort)(binary_data_1[7] * 256 + binary_data_1[6])).ToString() + "\r\n";
  397. richTextBox_ReadCfg.Text += "定子Lq," + ((ushort)(binary_data_1[9] * 256 + binary_data_1[8])).ToString() + "\r\n";
  398. richTextBox_ReadCfg.Text += "定子Ld," + ((ushort)(binary_data_1[11] * 256 + binary_data_1[10])).ToString() + "\r\n";
  399. richTextBox_ReadCfg.Text += "反电动势," + ((ushort)(binary_data_1[13] * 256 + binary_data_1[12])).ToString() + "\r\n";
  400. richTextBox_ReadCfg.Text += "额定电压," + binary_data_1[14].ToString() + "\r\n";
  401. }));
  402. }
  403. break;
  404. }
  405. case 0xA318://单传感器零点值
  406. {
  407. unchecked
  408. {
  409. this.Invoke((EventHandler)(delegate
  410. {
  411. richTextBox_TQ.Text += "零点:";
  412. for (int i = 0; i < 12; i++)
  413. {
  414. richTextBox_TQ.Text += ((ushort)((binary_data_1[3 + 2 * i] << 8) + binary_data_1[2 + 2 * i])).ToString() + ", ";
  415. }
  416. richTextBox_TQ.Text = richTextBox_TQ.Text.Substring(0, richTextBox_TQ.Text.Length - 2) + "\r\n";
  417. }));
  418. }
  419. break;
  420. }
  421. case 0xA408://校验秘钥
  422. {
  423. unchecked
  424. {
  425. this.Invoke((EventHandler)(delegate
  426. {
  427. richTextBox_ReadCfg.Text += "校验密钥,";
  428. for (int i = 0; i < 8; i++)
  429. {
  430. richTextBox_ReadCfg.Text += ((char)binary_data_1[2+i]).ToString();
  431. }
  432. richTextBox_ReadCfg.Text += "\r\n";
  433. }));
  434. }
  435. break;
  436. }
  437. case 0xA520://生产信息
  438. {
  439. unchecked
  440. {
  441. this.Invoke((EventHandler)(delegate
  442. {
  443. richTextBox_ReadCfg.Text += "\r\n";
  444. richTextBox_ReadCfg.Text += "[其它信息]" + "\r\n";
  445. richTextBox_ReadCfg.Text += "生产商,";
  446. for (int i = 0; i < 8; i++)
  447. {
  448. if (binary_data_1[2 + i] == '.') break;
  449. richTextBox_ReadCfg.Text += ((char)binary_data_1[2 + i]).ToString();
  450. }
  451. richTextBox_ReadCfg.Text += "\r\n";
  452. richTextBox_ReadCfg.Text += "生产地,";
  453. for (int i = 0; i < 8; i++)
  454. {
  455. if (binary_data_1[10 + i] == '.') break;
  456. richTextBox_ReadCfg.Text += ((char)binary_data_1[10 + i]).ToString();
  457. }
  458. richTextBox_ReadCfg.Text += "\r\n";
  459. richTextBox_ReadCfg.Text += "生产日期,";
  460. for (int i = 0; i < 8; i++)
  461. {
  462. if (binary_data_1[18 + i] == '.') break;
  463. richTextBox_ReadCfg.Text += ((char)binary_data_1[18 + i]).ToString();
  464. }
  465. richTextBox_ReadCfg.Text += "\r\n";
  466. }));
  467. }
  468. break;
  469. }
  470. case 0xA610://自定义1
  471. {
  472. unchecked
  473. {
  474. this.Invoke((EventHandler)(delegate
  475. {
  476. richTextBox_ReadCfg.Text += "信息1,";
  477. for (int i = 0; i < 16; i++)
  478. {
  479. if (binary_data_1[2 + i] == '.') break;
  480. richTextBox_ReadCfg.Text += ((char)binary_data_1[2 + i]).ToString();
  481. }
  482. richTextBox_ReadCfg.Text += "\r\n";
  483. }));
  484. }
  485. break;
  486. }
  487. case 0xA710://自定义2
  488. {
  489. unchecked
  490. {
  491. this.Invoke((EventHandler)(delegate
  492. {
  493. richTextBox_ReadCfg.Text += "信息2,";
  494. for (int i = 0; i < 16; i++)
  495. {
  496. if (binary_data_1[2 + i] == '.') break;
  497. richTextBox_ReadCfg.Text += ((char)binary_data_1[2 + i]).ToString();
  498. }
  499. richTextBox_ReadCfg.Text += "\r\n";
  500. }));
  501. }
  502. break;
  503. }
  504. case 0xA810://自定义3
  505. {
  506. unchecked
  507. {
  508. this.Invoke((EventHandler)(delegate
  509. {
  510. richTextBox_ReadCfg.Text += "信息3,";
  511. for (int i = 0; i < 16; i++)
  512. {
  513. if (binary_data_1[2 + i] == '.') break;
  514. richTextBox_ReadCfg.Text += ((char)binary_data_1[2 + i]).ToString();
  515. }
  516. richTextBox_ReadCfg.Text += "\r\n";
  517. }));
  518. }
  519. break;
  520. }
  521. case 0xA903://写入反馈
  522. {
  523. break;
  524. }
  525. case 0xAA04://单传感器校正值
  526. {
  527. unchecked
  528. {
  529. this.Invoke((EventHandler)(delegate
  530. {
  531. richTextBox_TQ.Text += "校正系数:";
  532. richTextBox_TQ.Text += ((float)binary_data_1[4] / 100f).ToString("#0.00") + "\r\n";
  533. richTextBox_TQ.Text += "启动值:";
  534. richTextBox_TQ.Text += ((ushort)((binary_data_1[3] << 8) + binary_data_1[2])).ToString() + "\r\n";
  535. }));
  536. }
  537. break;
  538. }
  539. case 0xAD20://用户参数2
  540. {
  541. unchecked
  542. {
  543. this.Invoke((EventHandler)(delegate
  544. {
  545. richTextBox_ReadCfg.Text += "\r\n";
  546. richTextBox_ReadCfg.Text += "[用户参数2]" + "\r\n";
  547. richTextBox_ReadCfg.Text+="姿态传感器," + binary_data_1[6].ToString() + ",";
  548. richTextBox_ReadCfg.Text += ((binary_data_1[6] == 0x55) ? "不支持" : "支持") + "\r\n";
  549. richTextBox_ReadCfg.Text += "俯仰角零偏," + ((ushort)(binary_data_1[3] * 256 + binary_data_1[2])).ToString() + "\r\n";
  550. richTextBox_ReadCfg.Text += "横滚角零偏," + ((ushort)(binary_data_1[5] * 256 + binary_data_1[4])).ToString() + "\r\n";
  551. richTextBox_ReadCfg.Text += "尾灯模式," + binary_data_1[7].ToString() + "\r\n";
  552. richTextBox_ReadCfg.Text += "前灯电压," + binary_data_1[8].ToString() + ",";
  553. richTextBox_ReadCfg.Text += ((binary_data_1[8] == 0xFF) ? "随电池" : (binary_data_1[8] == 0) ? "硬件配置" : (binary_data_1[8].ToString() + "V")) + "\r\n";
  554. richTextBox_ReadCfg.Text += "尾灯电压," + binary_data_1[9].ToString() + ",";
  555. richTextBox_ReadCfg.Text += ((binary_data_1[9] == 0xFF) ? "随电池" : (binary_data_1[9] == 0) ? "硬件配置" : (binary_data_1[9].ToString() + "V")) + "\r\n";
  556. }));
  557. }
  558. break;
  559. }
  560. case 0xAE1A://三传感器零点
  561. {
  562. unchecked
  563. {
  564. this.Invoke((EventHandler)(delegate
  565. {
  566. richTextBox_TQ.Text += "传感器" + binary_data_1[2].ToString() + "零点:";
  567. for (int i = 0; i < 12; i++)
  568. {
  569. richTextBox_TQ.Text += ((ushort)((binary_data_1[4 + 2 * i] << 8) + binary_data_1[3 + 2 * i])).ToString() + ", ";
  570. }
  571. richTextBox_TQ.Text = richTextBox_TQ.Text.Substring(0, richTextBox_TQ.Text.Length - 2) + "\r\n";
  572. }));
  573. }
  574. break;
  575. }
  576. case 0xAF02://三传感器校正系数
  577. {
  578. unchecked
  579. {
  580. this.Invoke((EventHandler)(delegate
  581. {
  582. richTextBox_TQ.Text += "传感器" + binary_data_1[2].ToString() + "校正系数:";
  583. richTextBox_TQ.Text += ((float)binary_data_1[3] / 100f).ToString("#0.00") + "\r\n";
  584. }));
  585. }
  586. break;
  587. }
  588. case 0xB002://三传感器启动值
  589. {
  590. unchecked
  591. {
  592. this.Invoke((EventHandler)(delegate
  593. {
  594. richTextBox_TQ.Text += "启动值:";
  595. richTextBox_TQ.Text += ((ushort)((binary_data_1[3] << 8) + binary_data_1[2])).ToString() + "\r\n";
  596. }));
  597. }
  598. break;
  599. }
  600. default: break;
  601. }
  602. }
  603. #endregion
  604. #region TE信息
  605. else if ((ID == 0x615) || (ID == 0x610))
  606. {
  607. switch (CmdTemp)
  608. {
  609. case 0x1240://TE APP版本信息
  610. {
  611. unchecked
  612. {
  613. this.Invoke((EventHandler)(delegate
  614. {
  615. //TE 软件版本
  616. richTextBox_VerInfo.Text += "TE软件版本:";
  617. for (ushort i = 0; i < 16; i++)
  618. {
  619. if (binary_data_1[50 + i] == 0x2E)
  620. {
  621. break;
  622. }
  623. if (binary_data_1[50 + i] == 'r')
  624. {
  625. binary_data_1[50 + i] = (byte)'.';
  626. }
  627. richTextBox_VerInfo.Text += ((char)binary_data_1[50 + i]).ToString();
  628. }
  629. }));
  630. }
  631. break;
  632. }
  633. default: break;
  634. }
  635. #endregion
  636. }
  637. }
  638. #endregion
  639. #region 20ms定时器
  640. private void timer1_Tick(object sender, EventArgs e)
  641. {
  642. //串口数据解析
  643. Serial_DataReceived_Process();
  644. }
  645. #endregion
  646. #region 非独占性延时函数
  647. public static void Delay_ms(int milliSecond)
  648. {
  649. int start = Environment.TickCount;
  650. while (Math.Abs(Environment.TickCount - start) < milliSecond)//毫秒
  651. {
  652. Application.DoEvents();
  653. }
  654. }
  655. #endregion
  656. #region 获取配置文件日期
  657. private void GetDateList()
  658. {
  659. string[] DateList = { "" };
  660. if (checkBox_LocalFile.Checked == true)//从本地更新日期列表
  661. {
  662. DateList = System.IO.Directory.GetDirectories(LocalCfgFilePath);
  663. }
  664. else//从服务器更新日期列表
  665. {
  666. DateList = StartForm.myFtp.GetDirectoryList(StartForm.myServerCfg.RootPath + "/cfg");
  667. }
  668. comboBox_cfgFileDate.Items.Clear();
  669. foreach (var file in DateList)
  670. {
  671. comboBox_cfgFileDate.Items.Add(file.Substring(file.LastIndexOf("\\") + 1));
  672. }
  673. }
  674. #endregion
  675. #region 获取配置文件列表
  676. private void GetFileListFrom(string date)
  677. {
  678. string[] FileList = { "" };
  679. if (checkBox_LocalFile.Checked == true)//从本地更新文件列表
  680. {
  681. FileList = System.IO.Directory.GetFiles(LocalCfgFilePath + date);
  682. }
  683. else//从服务器更新文件列表
  684. {
  685. FileList = StartForm.myFtp.GetFileNameList(StartForm.myServerCfg.RootPath + "/cfg/" + date);
  686. }
  687. comboBox_File.Items.Clear();
  688. foreach (var file in FileList)
  689. {
  690. comboBox_File.Items.Add(file.Substring(file.LastIndexOf("\\") + 1));
  691. }
  692. }
  693. #endregion
  694. #region 选择文件日期后更新文件列表
  695. private void comboBox_cfgFileDate_SelectionChangeCommitted(object sender, EventArgs e)
  696. {
  697. comboBox_File.Text = "";
  698. GetFileListFrom(comboBox_cfgFileDate.SelectedItem.ToString());
  699. }
  700. #endregion
  701. #region 选择文件后加载文件
  702. private void comboBox_File_SelectionChangeCommitted(object sender, EventArgs e)
  703. {
  704. //清空原来数据
  705. array_CfgInfo.Clear();
  706. string SelectFile = "";
  707. if (checkBox_LocalFile.Checked == false)//从服务器下载文件
  708. {
  709. SelectFile = comboBox_cfgFileDate.SelectedItem.ToString() + "\\" + comboBox_File.SelectedItem.ToString();
  710. if (!File.Exists(LocalCfgFilePath + SelectFile))//检查本地是否存在选定文件
  711. {
  712. SelectFile = comboBox_cfgFileDate.SelectedItem.ToString() + "/" + comboBox_File.SelectedItem.ToString();
  713. StartForm.myFtp.DownLoadNotReName(StartForm.myServerCfg.RootPath + "/cfg/" + SelectFile, LocalCfgFilePath + comboBox_cfgFileDate.SelectedItem.ToString());
  714. }
  715. }
  716. SelectFile = comboBox_cfgFileDate.SelectedItem.ToString() + "\\" + comboBox_File.SelectedItem.ToString();
  717. //打开选中文件
  718. StreamReader objReader = new StreamReader(LocalCfgFilePath + SelectFile);
  719. string sLine = "";
  720. while (sLine != null)
  721. {
  722. sLine = objReader.ReadLine();
  723. array_CfgInfo.Add(sLine);
  724. }
  725. objReader.Close();
  726. //解析显示配置文件
  727. richTextBox_CfgFile.Text = "";
  728. try
  729. {
  730. foreach (string sOutput in array_CfgInfo)
  731. {
  732. if (sOutput.Contains("[测试阈值]"))
  733. {
  734. //删除最后一行,不显示后面内容
  735. richTextBox_CfgFile.Text = richTextBox_CfgFile.Text.Remove(richTextBox_CfgFile.Text.Length - 1);
  736. break;
  737. }
  738. richTextBox_CfgFile.AppendText(sOutput + "\r\n");
  739. }
  740. }
  741. catch (System.Exception)
  742. {
  743. return;
  744. }
  745. //加载力矩传感器判断标准
  746. TorqueCheck.CheckParam_Load(LocalCfgFilePath + SelectFile);
  747. textBox_TQ_Zero.Text = TorqueCheck.Zero_Min.ToString() + " - " + TorqueCheck.Zero_Max.ToString();
  748. textBox_TQ_K.Text = TorqueCheck.Cal_K_Min.ToString("#0.00") + " - " + TorqueCheck.Cal_K_Max.ToString("#0.00");
  749. textBox_TQ_Start.Text = TorqueCheck.Start_Min.ToString() + " - " + TorqueCheck.Start_Max.ToString();
  750. }
  751. #endregion
  752. #region 修改获取文件方式,本地或服务器
  753. private void checkBox_LocalFile_CheckedChanged(object sender, EventArgs e)
  754. {
  755. comboBox_cfgFileDate.Text = "";
  756. comboBox_File.Text = "";
  757. GetDateList();
  758. }
  759. #endregion
  760. public CheckForm()
  761. {
  762. InitializeComponent();
  763. System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
  764. }
  765. #region 端口设置
  766. private void toolStripMenuIte_PortSet_Click(object sender, EventArgs e)
  767. {
  768. portsettings_form.Show();
  769. }
  770. #endregion
  771. #region 打开电源
  772. private void 开ToolStripMenuItem_Click(object sender, EventArgs e)
  773. {
  774. byte[] Data = new byte[1];
  775. if (portsettings_form.serialPort1.IsOpen)
  776. {
  777. //开机
  778. Data[0] = 0xF1;
  779. SendCmd(0x7FF, 0x16, 0x2201, Data);
  780. 开ToolStripMenuItem.Checked = true;
  781. 关ToolStripMenuItem.Checked = false;
  782. }
  783. else
  784. {
  785. MessageBox.Show("串口未连接");
  786. return;
  787. }
  788. }
  789. #endregion
  790. #region 关闭电源
  791. private void 关ToolStripMenuItem_Click(object sender, EventArgs e)
  792. {
  793. byte[] Data = new byte[1];
  794. if (portsettings_form.serialPort1.IsOpen)
  795. {
  796. //关机
  797. Data[0] = 0xF0;
  798. SendCmd(0x7FF, 0x16, 0x2201, Data);
  799. 开ToolStripMenuItem.Checked = false;
  800. 关ToolStripMenuItem.Checked = true;
  801. }
  802. else
  803. {
  804. MessageBox.Show("串口未连接");
  805. return;
  806. }
  807. }
  808. #endregion
  809. #region 1s定时器更新时间
  810. private void timer2_Tick(object sender, EventArgs e)
  811. {
  812. label_SystemTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  813. //MC故障信息超时清除
  814. if (MC_ErrorCode_Refresh == false)
  815. {
  816. MC_ErrorCode_Refresh_Cnt++;
  817. if (MC_ErrorCode_Refresh_Cnt > 5)
  818. {
  819. label_error_code.Text = "OK";
  820. label_error_code.BackColor = Color.Green;
  821. MC_ErrorCode_Refresh_Cnt = 0;
  822. }
  823. }
  824. else
  825. {
  826. MC_ErrorCode_Refresh_Cnt = 0;
  827. }
  828. MC_ErrorCode_Refresh = false;
  829. }
  830. #endregion
  831. #region 故障码标签鼠标滑过显示故障内容
  832. private void label_error_code_MouseHover(object sender, EventArgs e)
  833. {
  834. // 创建the ToolTip
  835. ToolTip toolTip1 = new ToolTip();
  836. // 设置显示样式
  837. toolTip1.AutoPopDelay = 30000;//提示信息的可见时间
  838. toolTip1.InitialDelay = 50;//事件触发多久后出现提示
  839. toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  840. toolTip1.ShowAlways = true;//是否显示提示框
  841. // 设置伴随的对象.
  842. string ErrorCode = this.label_error_code.Text;
  843. UInt32 Code = ErrorCode.Contains("H") ? Convert.ToUInt32(ErrorCode.Split('H')[0], 16) : 0;
  844. toolTip1.SetToolTip(this.label_error_code, ErrorInfoDisplay(Code));//设置故障内容显示
  845. }
  846. #endregion
  847. //创建线程执行一次测试任务
  848. /*
  849. 执行流程如下:
  850. 1、读取电机版本信息 -> 确认SN是否存在为0、确认版本和cfg文件是否一致;
  851. 2、读取力矩传感器标定值 -> 依次判断零点、校正系数、启动值是否在允许范围内,判断零点值差异是否过大
  852. 3、依次读取配置参数,严格按照cfg文件的顺序读取 -> 对比两个richbox内容是否一致
  853. 4、显示测试结果
  854. 5、存储测试结果,上传ftp服务器
  855. */
  856. private void Thread_Task()
  857. {
  858. //开始测试
  859. richTextBox_TestRecord.Text += "电机信息:" + textBox_PDinfo.Text + "\r\n";
  860. richTextBox_TestRecord.Text += "配置文件:" + comboBox_File.Text + "\r\n";
  861. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "开始测试" + "\r\n";
  862. label_Result.Text = "不合格";
  863. label_Result.BackColor = Color.Red;
  864. label_Step.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(48)))), ((int)(((byte)(113)))), ((int)(((byte)(185)))));
  865. //公有变量
  866. var Code = new byte[32];//发送的指令数据
  867. #region 版本查询和确认
  868. label_Step.Text = "检查版本信息";
  869. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "发送指令查询版本信息" + "\r\n";
  870. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
  871. Delay_ms(200);
  872. SendCmd((ushort)0x651, (byte)0x11, (ushort)0x1200, null);
  873. Delay_ms(200);
  874. string FW_Version_Check = comboBox_File.Text.Split('_')[2] + "_" + comboBox_File.Text.Split('_')[3];
  875. string FW_Version_Result = "";
  876. string TE_FW_Ver_Check = comboBox_File.Text.Split('_')[4] + "_" + comboBox_File.Text.Split('_')[5];
  877. string TE_FW_Ver_Result = "";
  878. try
  879. {
  880. FW_Version_Result = richTextBox_VerInfo.Lines[3].Split(':')[1];
  881. FW_Version_Result = FW_Version_Result.Split('_')[0] + richTextBox_VerInfo.Lines[4].Split('-')[2].Replace('V', '.') + "_" + FW_Version_Result.Split('_')[1];
  882. TE_FW_Ver_Result = richTextBox_VerInfo.Lines[5].Split(':')[1];
  883. }
  884. catch (System.Exception)
  885. {
  886. label_Step.Text = "版本错误,,测试结束!";
  887. label_Step.ForeColor = Color.Red;
  888. }
  889. if (string.Equals(FW_Version_Check, FW_Version_Result) == false) //主控版本错误,直接结束
  890. {
  891. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "主控版本错误" + "\r\n";
  892. label_Step.Text = "主控软件版本错误,测试结束!";
  893. label_Step.ForeColor = Color.Red;
  894. button_Start.Enabled = true;
  895. ErrorSaveLog();
  896. return;
  897. }
  898. else
  899. {
  900. if (string.Equals(TE_FW_Ver_Check, TE_FW_Ver_Result) == false) //TE版本错误,给出提示
  901. {
  902. if (MessageBox.Show("TE版本错误,请确认主控板是否为HK?", "TE版本错误", MessageBoxButtons.YesNo) == DialogResult.No)
  903. {
  904. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "TE版本错误" + "\r\n";
  905. label_Step.Text = "TE软件版本错误,测试结束!";
  906. label_Step.ForeColor = Color.Red;
  907. button_Start.Enabled = true;
  908. ErrorSaveLog();
  909. return;
  910. }
  911. }
  912. if (string.Equals(richTextBox_VerInfo.Lines[0].Split(':')[1], textBox_PDinfo.Text.Substring(0, textBox_PDinfo.Text.LastIndexOf(' '))) == false)//型号不一致
  913. {
  914. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "型号错误" + "\r\n";
  915. label_Step.Text = "型号不一致,测试结束!";
  916. label_Step.ForeColor = Color.Red;
  917. button_Start.Enabled = true;
  918. ErrorSaveLog();
  919. return;
  920. }
  921. if (string.Equals(richTextBox_VerInfo.Lines[1].Split(':')[1], textBox_PDinfo.Text.Remove(0, textBox_PDinfo.Text.LastIndexOf(' ') + 1)) == false)//SN不一致
  922. {
  923. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "SN错误" + "\r\n";
  924. label_Step.Text = "SN不一致,测试结束!";
  925. label_Step.ForeColor = Color.Red;
  926. button_Start.Enabled = true;
  927. ErrorSaveLog();
  928. return;
  929. }
  930. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "版本信息正确" + "\r\n";
  931. }
  932. Delay_ms(1000);
  933. #endregion
  934. #region 判断力矩传感器的标定值是否在允许范围内
  935. label_Step.Text = "判断力矩传感器";
  936. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "发送指令查询力矩传感器" + "\r\n";
  937. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2000, null);//单传感器零点
  938. Delay_ms(200);
  939. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2900, null);//单传感器校正信息
  940. Delay_ms(200);
  941. Code[0] = 1;
  942. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, Code);//传感器1零点
  943. Delay_ms(200);
  944. Code[0] = 2;
  945. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, Code);//传感器2零点
  946. Delay_ms(200);
  947. Code[0] = 3;
  948. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, Code);//传感器3零点
  949. Delay_ms(200);
  950. Code[0] = 1;
  951. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, Code);//传感器1校正系数
  952. Delay_ms(200);
  953. Code[0] = 2;
  954. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, Code);//传感器2校正系数
  955. Delay_ms(200);
  956. Code[0] = 3;
  957. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, Code);//传感器3校正系数
  958. Delay_ms(200);
  959. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3500, null);//三传感器启动值
  960. Delay_ms(200);
  961. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "判断力矩传感器" + "\r\n";
  962. for (int i = 0; i < richTextBox_TQ.Lines.Length; i++)
  963. {
  964. switch (richTextBox_TQ.Lines[i].Split(':')[0])
  965. {
  966. case "零点": case "传感器1零点": case "传感器2零点": case "传感器3零点":
  967. {
  968. string[] zero = richTextBox_TQ.Lines[i].Split(':')[1].Split(new string[] { ", " }, StringSplitOptions.None);
  969. for (int j = 0; j < zero.Length; j++)
  970. {
  971. if ((Convert.ToUInt16(zero[j]) > TorqueCheck.Zero_Max) || (Convert.ToUInt16(zero[j]) < TorqueCheck.Zero_Min))//零点超差
  972. {
  973. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "传感器零点超范围" + "\r\n";
  974. label_Step.Text = "传感器零点超范围,测试结束!";
  975. label_Step.ForeColor = Color.Red;
  976. button_Start.Enabled = true;
  977. ErrorSaveLog();
  978. return;
  979. }
  980. }
  981. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + richTextBox_TQ.Lines[i].Split(':')[0] + "正常" + "\r\n";
  982. break;
  983. }
  984. case "校正系数": case "传感器1校正系数": case "传感器2校正系数": case "传感器3校正系数":
  985. {
  986. ushort k = (ushort)(Convert.ToSingle(richTextBox_TQ.Lines[i].Split(':')[1]) * 100);
  987. if ((k > (ushort)(TorqueCheck.Cal_K_Max * 100f)) || (k < (ushort)(TorqueCheck.Cal_K_Min * 100f)))//校正系数超差
  988. {
  989. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "校正系数超范围" + "\r\n";
  990. label_Step.Text = "校正系数超范围,测试结束!";
  991. label_Step.ForeColor = Color.Red;
  992. button_Start.Enabled = true;
  993. ErrorSaveLog();
  994. return;
  995. }
  996. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + richTextBox_TQ.Lines[i].Split(':')[0] + "正常" + "\r\n";
  997. break;
  998. }
  999. case "启动值":
  1000. {
  1001. string start = richTextBox_TQ.Lines[i].Split(':')[1];
  1002. if ((Convert.ToUInt16(start) > TorqueCheck.Start_Max) || (Convert.ToUInt16(start) < TorqueCheck.Start_Min))//启动值超差
  1003. {
  1004. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "启动值超范围" + "\r\n";
  1005. label_Step.Text = "启动值超范围,测试结束!";
  1006. label_Step.ForeColor = Color.Red;
  1007. button_Start.Enabled = true;
  1008. ErrorSaveLog();
  1009. return;
  1010. }
  1011. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + richTextBox_TQ.Lines[i].Split(':')[0] + "正常" + "\r\n";
  1012. break;
  1013. }
  1014. default:break;
  1015. }
  1016. }
  1017. #endregion
  1018. #region 依次读取配置参数
  1019. label_Step.Text = "判断配置参数";
  1020. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "发送指令查询电机配置参数" + "\r\n";
  1021. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1A00, null);//用户参数1
  1022. Delay_ms(200);
  1023. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3000, null);//用户参数2
  1024. Delay_ms(200);
  1025. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1C00, null);//马达参数
  1026. Delay_ms(200);
  1027. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1F00, null);//生产信息
  1028. Delay_ms(200);
  1029. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1000, null);//校验秘钥
  1030. Delay_ms(200);
  1031. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1300, null);//自定义1
  1032. Delay_ms(200);
  1033. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1500, null);//自定义2
  1034. Delay_ms(200);
  1035. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1700, null);//自定义3
  1036. Delay_ms(200);
  1037. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "判断配置参数" + "\r\n";
  1038. for (int line = 0; line < richTextBox_CfgFile.Lines.Length; line++)
  1039. {
  1040. try
  1041. {
  1042. if (richTextBox_ReadCfg.Lines[line].Equals(richTextBox_CfgFile.Lines[line]) == false)
  1043. {
  1044. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + richTextBox_CfgFile.Lines[line].Split(',')[0] + "错误" + "\r\n";
  1045. label_Step.Text = "检查配置参数失败,测试结束!";
  1046. label_Step.ForeColor = Color.Red;
  1047. button_Start.Enabled = true;
  1048. ErrorSaveLog();
  1049. return;
  1050. }
  1051. }
  1052. catch (System.Exception)
  1053. {
  1054. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "读取电机配置参数失败" + "\r\n";
  1055. label_Step.Text = "检查配置参数失败,测试结束!";
  1056. label_Step.ForeColor = Color.Red;
  1057. button_Start.Enabled = true;
  1058. ErrorSaveLog();
  1059. return;
  1060. }
  1061. }
  1062. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机配置参数正确" + "\r\n";
  1063. #endregion
  1064. #region 测试结束
  1065. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "测试结束" + "\r\n";
  1066. label_Step.Text = "测试结束!";
  1067. label_Step.ForeColor = Color.Green;
  1068. label_Result.Text = "合格";
  1069. label_Result.BackColor = Color.Green;
  1070. button_Start.Enabled = true;
  1071. #region 按照产品信息设置测试文件路径
  1072. string ResultPathName = textBox_PDinfo.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_");
  1073. if (System.IO.Directory.Exists(LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName) == false)
  1074. System.IO.Directory.CreateDirectory(LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName);
  1075. #endregion
  1076. //页面存储
  1077. Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap
  1078. Graphics g = Graphics.FromImage(bit);
  1079. g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片
  1080. LogSaveFileName = LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName + "\\测试记录" + ".png";
  1081. bit.Save(LogSaveFileName);
  1082. //测试页面上传服务器
  1083. if (StartForm.myFtp.IsNetConnected == true)
  1084. {
  1085. string DateNow = DateTime.Now.ToString("yyyy-MM-dd");
  1086. if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/qcTest/log/", DateNow) == false)
  1087. {
  1088. StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow);
  1089. }
  1090. if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/qcTest/log/", DateNow + "/" + ResultPathName) == false)
  1091. {
  1092. StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow + "/" + ResultPathName);
  1093. }
  1094. bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow + "/" + ResultPathName);
  1095. if (result == true)
  1096. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "测试页面上传服务器成功" + "\r\n";
  1097. }
  1098. //电机数据存储
  1099. LogSaveFileName = LogSavePath + "\\log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName + "\\电机参数" + ".txt";
  1100. string fileInfo = "";
  1101. fileInfo += "检验记录:\r\n" + richTextBox_TestRecord.Text.Replace("\n", "\r\n") + "\r\n\r\n";
  1102. fileInfo += "电机生产参数:\r\n";
  1103. fileInfo += richTextBox_CfgFile.Text.Replace("\n", "\r\n") + "\r\n\r\n";
  1104. fileInfo += "电机版本信息查询:\r\n";
  1105. fileInfo += richTextBox_VerInfo.Text.Replace("\n", "\r\n") + "\r\n\r\n";
  1106. fileInfo += "力矩传感器标定结果:\r\n";
  1107. fileInfo += richTextBox_TQ.Text.Replace("\n", "\r\n") + "\r\n\r\n";
  1108. fileInfo += "电机参数查询:\r\n";
  1109. fileInfo += richTextBox_ReadCfg.Text.Replace("\n", "\r\n") + "\r\n\r\n";
  1110. fileInfo += "电机故障码:\r\n";
  1111. fileInfo += label_error_code.Text.Replace("\n", "\r\n") + "\r\n\r\n";
  1112. fileInfo += "测试结果:\r\n";
  1113. fileInfo += label_Result.Text.Replace("\n", "\r\n") + "\r\n\r\n";
  1114. System.IO.File.WriteAllText(LogSaveFileName, fileInfo);
  1115. //电机数据上传服务器
  1116. if (StartForm.myFtp.IsNetConnected == true)
  1117. {
  1118. string DateNow = DateTime.Now.ToString("yyyy-MM-dd");
  1119. if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/qcTest/log/", DateNow) == false)
  1120. {
  1121. StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow);
  1122. }
  1123. if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow, ResultPathName) == false)
  1124. {
  1125. StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow + "/" + ResultPathName);
  1126. }
  1127. bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow + "/" + ResultPathName);
  1128. if (result == true)
  1129. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机数据上传服务器成功" + "\r\n";
  1130. }
  1131. #endregion
  1132. }
  1133. private void CheckForm_Load(object sender, EventArgs e)
  1134. {
  1135. timer1.Enabled = true;
  1136. timer2.Enabled = true;
  1137. //窗口加载时,从FTP服务器或本地获取文件日期列表
  1138. GetDateList();
  1139. //装载序列号文件
  1140. string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\SerialNum";
  1141. StreamReader objReader = new StreamReader(cfg_file);
  1142. string sLine = "";
  1143. ArrayList arrText = new ArrayList();//创建一个动态数组
  1144. while (sLine != null)
  1145. {
  1146. sLine = objReader.ReadLine();
  1147. arrText.Add(sLine);
  1148. }
  1149. objReader.Close();
  1150. label_SerialNum.Text = arrText[1].ToString();//流水号
  1151. }
  1152. //测试失败存储测试界面
  1153. private void ErrorSaveLog()
  1154. {
  1155. //页面存储
  1156. Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap
  1157. Graphics g = Graphics.FromImage(bit);
  1158. g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片
  1159. string LogSaveFileName = LogSavePath + "\\errorlog\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\";
  1160. if (System.IO.Directory.Exists(LogSaveFileName) == false)
  1161. System.IO.Directory.CreateDirectory(LogSaveFileName);
  1162. LogSaveFileName += textBox_PDinfo.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_测试记录" + ".png";
  1163. bit.Save(LogSaveFileName);
  1164. //测试页面上传服务器
  1165. if (StartForm.myFtp.IsNetConnected == true)
  1166. {
  1167. string DateNow = DateTime.Now.ToString("yyyy-MM-dd");
  1168. if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/qcTest/errorlog/", DateNow) == false)
  1169. {
  1170. StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/qcTest/errorlog/" + DateNow);
  1171. }
  1172. bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/qcTest/errorlog/" + DateNow);
  1173. if (result == true)
  1174. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "测试异常页面上传服务器成功" + "\r\n";
  1175. }
  1176. }
  1177. //开始测试按钮点击
  1178. private void button_Start_Click(object sender, EventArgs e)
  1179. {
  1180. TestStart();
  1181. }
  1182. //开始一次测试
  1183. private void TestStart()
  1184. {
  1185. //检查是否有故障
  1186. if (label_error_code.Text != "OK")
  1187. {
  1188. label_Step.Text = "电机存在故障!";
  1189. ErrorSaveLog();
  1190. return;
  1191. }
  1192. //显示数据清空
  1193. richTextBox_ReadCfg.Text = "";
  1194. richTextBox_TestRecord.Text = "";
  1195. richTextBox_TQ.Text = "";
  1196. richTextBox_VerInfo.Text = "";
  1197. if (!portsettings_form.serialPort1.IsOpen)
  1198. {
  1199. label_Step.Text = "串口未连接!";
  1200. return;
  1201. }
  1202. #region 检查配置文件
  1203. if (comboBox_File.Items.Count == 0) //配置文件目录为空
  1204. {
  1205. label_Step.Text = "无可用配置文件!";
  1206. return;
  1207. }
  1208. if (comboBox_File.Text.Trim().Length == 0)//未选择配置文件
  1209. {
  1210. label_Step.Text = "请选择配置文件!";
  1211. return;
  1212. }
  1213. #endregion
  1214. #region 检查产品信息是否符合格式要求
  1215. try
  1216. {
  1217. string Mode = textBox_PDinfo.Text.Split(new string[] { " " }, StringSplitOptions.None)[0];
  1218. string Sn = textBox_PDinfo.Text.Split(new string[] { " " }, StringSplitOptions.None)[1];
  1219. 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]$");
  1220. if (regex.IsMatch(Sn) == false)
  1221. {
  1222. label_Step.Text = "SN格式错误!";
  1223. label_Step.ForeColor = Color.Red;
  1224. return;
  1225. }
  1226. }
  1227. catch (System.Exception ex)
  1228. {
  1229. label_Step.Text = "标签格式错误!";
  1230. label_Step.ForeColor = Color.Red;
  1231. return;
  1232. }
  1233. #endregion
  1234. //流水号累加和存储
  1235. label_SerialNum.Text = (Convert.ToInt32(label_SerialNum.Text) + 1).ToString();
  1236. string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\SerialNum";
  1237. StreamReader objReader = new StreamReader(cfg_file);
  1238. string sLine = "";
  1239. ArrayList arrText = new ArrayList();//创建一个动态数组
  1240. while (sLine != null)
  1241. {
  1242. sLine = objReader.ReadLine();
  1243. arrText.Add(sLine);
  1244. }
  1245. arrText.RemoveAt(arrText.Count - 1);
  1246. objReader.Close();
  1247. arrText[1] = label_SerialNum.Text;
  1248. using (StreamWriter file = new StreamWriter(cfg_file))
  1249. {
  1250. foreach (var entry in arrText)
  1251. {
  1252. file.WriteLine(entry);
  1253. }
  1254. }
  1255. button_Start.Enabled = false;
  1256. //创建线程,执行测试任务
  1257. Thread th = new Thread(Thread_Task);
  1258. th.Start();
  1259. }
  1260. //双击清零流水号
  1261. private void label32_DoubleClick(object sender, EventArgs e)
  1262. {
  1263. //流水号清零
  1264. if (MessageBox.Show("流水号清零", "确认清零?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1265. {
  1266. label_SerialNum.Text = "0";
  1267. //存储
  1268. string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\SerialNum";
  1269. StreamReader objReader = new StreamReader(cfg_file);
  1270. string sLine = "";
  1271. ArrayList arrText = new ArrayList();//创建一个动态数组
  1272. while (sLine != null)
  1273. {
  1274. sLine = objReader.ReadLine();
  1275. arrText.Add(sLine);
  1276. }
  1277. arrText.RemoveAt(arrText.Count - 1);
  1278. objReader.Close();
  1279. arrText[1] = label_SerialNum.Text;
  1280. using (StreamWriter file = new StreamWriter(cfg_file))
  1281. {
  1282. foreach (var entry in arrText)
  1283. {
  1284. file.WriteLine(entry);
  1285. }
  1286. }
  1287. }
  1288. }
  1289. private void textBox_PDinfo_KeyUp(object sender, KeyEventArgs e)
  1290. {
  1291. if (e.KeyCode == Keys.Enter)
  1292. TestStart();
  1293. }
  1294. private void textBox_PDinfo_Click(object sender, EventArgs e)
  1295. {
  1296. textBox_PDinfo.SelectAll();
  1297. }
  1298. }
  1299. }