CheckForm.cs 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888
  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. Delay_ms(2000);
  1011. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "发送指令查询版本信息" + "\r\n";
  1012. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
  1013. Delay_ms(500);
  1014. SendCmd((ushort)0x651, (byte)0x11, (ushort)0x1200, null);
  1015. Delay_ms(500);
  1016. string FW_Version_Check = comboBox_File.Text.Split('_')[2] + "_" + comboBox_File.Text.Split('_')[3];
  1017. string FW_Version_Result = "";
  1018. string TE_FW_Ver_Check = comboBox_File.Text.Split('_')[4] + "_" + comboBox_File.Text.Split('_')[5];
  1019. string TE_FW_Ver_Result = "";
  1020. string HW_Version_Result = "";
  1021. try
  1022. {
  1023. HW_Version_Result = richTextBox_VerInfo.Lines[2].Split(':')[1];
  1024. FW_Version_Result = richTextBox_VerInfo.Lines[3].Split(':')[1];
  1025. FW_Version_Result = FW_Version_Result.Split('_')[0] + richTextBox_VerInfo.Lines[4].Split('-')[2].Replace('V', '.') + "_" + FW_Version_Result.Split('_')[1];
  1026. TE_FW_Ver_Result = richTextBox_VerInfo.Lines[5].Split(':')[1];
  1027. }
  1028. catch (System.Exception)
  1029. {
  1030. //label_Step.Text = "软件版本信息不完整!";
  1031. }
  1032. //PCBA版本确认
  1033. if (HW_Version_Result.Contains(HW_Version_Check) == false)//PCBA版号错误,直接结束
  1034. {
  1035. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "PCBA版本错误" + "\r\n";
  1036. label_Step.Text = "PCBA版本错误,测试结束!";
  1037. label_Step.ForeColor = Color.Red;
  1038. button_Start.Enabled = true;
  1039. textBox_PDinfo.Enabled = true;
  1040. ErrorSaveLog();
  1041. //关机
  1042. Code[0] = 0xF0;
  1043. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1044. 开ToolStripMenuItem.Checked = false;
  1045. 关ToolStripMenuItem.Checked = true;
  1046. Delay_ms(500);
  1047. //关闭串口
  1048. portsettings_form.closePort();
  1049. return;
  1050. }
  1051. //主控软件版本检查
  1052. if (string.Equals(FW_Version_Check, FW_Version_Result) == false) //主控版本错误,直接结束
  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. textBox_PDinfo.Enabled = true;
  1059. ErrorSaveLog();
  1060. //关机
  1061. Code[0] = 0xF0;
  1062. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1063. 开ToolStripMenuItem.Checked = false;
  1064. 关ToolStripMenuItem.Checked = true;
  1065. Delay_ms(500);
  1066. //关闭串口
  1067. portsettings_form.closePort();
  1068. return;
  1069. }
  1070. else
  1071. {
  1072. //TE软件版本检查
  1073. if (TE_FW_Ver_Check != "N_A")//单MCU主控板不检测TE版本
  1074. {
  1075. if (string.Equals(TE_FW_Ver_Check, TE_FW_Ver_Result) == false) //TE版本错误,给出提示
  1076. {
  1077. if (MessageBox.Show("TE版本错误,请确认主控板是否为HK?", "TE版本错误", MessageBoxButtons.YesNo) == DialogResult.No)
  1078. {
  1079. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "TE版本错误" + "\r\n";
  1080. label_Step.Text = "TE软件版本错误,测试结束!";
  1081. label_Step.ForeColor = Color.Red;
  1082. button_Start.Enabled = true;
  1083. textBox_PDinfo.Enabled = true;
  1084. ErrorSaveLog();
  1085. //关机
  1086. Code[0] = 0xF0;
  1087. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1088. 开ToolStripMenuItem.Checked = false;
  1089. 关ToolStripMenuItem.Checked = true;
  1090. Delay_ms(500);
  1091. //关闭串口
  1092. portsettings_form.closePort();
  1093. return;
  1094. }
  1095. }
  1096. }
  1097. //型号和SN确认
  1098. try
  1099. {
  1100. string mode1 = "", mode2 = "";
  1101. mode1 = richTextBox_VerInfo.Lines[0].Split(':')[1].Trim();
  1102. mode2 = textBox_PDinfo.Text.Contains("%#") ? textBox_PDinfo.Text.Substring(0, textBox_PDinfo.Text.LastIndexOf('%'))
  1103. : textBox_PDinfo.Text.Substring(0, textBox_PDinfo.Text.LastIndexOf(' ')).Trim();
  1104. if (string.Equals(mode1, mode2) == false)//型号不一致
  1105. {
  1106. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "型号错误" + "\r\n";
  1107. label_Step.Text = "型号不一致,测试结束!";
  1108. label_Step.ForeColor = Color.Red;
  1109. button_Start.Enabled = true;
  1110. textBox_PDinfo.Enabled = true;
  1111. ErrorSaveLog();
  1112. //关机
  1113. Code[0] = 0xF0;
  1114. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1115. 开ToolStripMenuItem.Checked = false;
  1116. 关ToolStripMenuItem.Checked = true;
  1117. Delay_ms(500);
  1118. //关闭串口
  1119. portsettings_form.closePort();
  1120. return;
  1121. }
  1122. string SN1 = "", SN2 = "";
  1123. SN1 = richTextBox_VerInfo.Lines[1].Split(':')[1].Trim();
  1124. SN2 = textBox_PDinfo.Text.Contains("%#") ? textBox_PDinfo.Text.Remove(0, textBox_PDinfo.Text.LastIndexOf('#') + 1)
  1125. : textBox_PDinfo.Text.Remove(0, textBox_PDinfo.Text.LastIndexOf(' ') + 1);
  1126. if (string.Equals(SN1, SN2) == false)//SN不一致
  1127. {
  1128. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "SN错误" + "\r\n";
  1129. label_Step.Text = "SN不一致,测试结束!";
  1130. label_Step.ForeColor = Color.Red;
  1131. button_Start.Enabled = true;
  1132. textBox_PDinfo.Enabled = true;
  1133. ErrorSaveLog();
  1134. //关机
  1135. Code[0] = 0xF0;
  1136. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1137. 开ToolStripMenuItem.Checked = false;
  1138. 关ToolStripMenuItem.Checked = true;
  1139. Delay_ms(500);
  1140. //关闭串口
  1141. portsettings_form.closePort();
  1142. return;
  1143. }
  1144. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "版本信息正确" + "\r\n";
  1145. }
  1146. catch (Exception)
  1147. {
  1148. label_Step.Text = "电机信息错误,测试结束!";
  1149. label_Step.ForeColor = Color.Red;
  1150. button_Start.Enabled = true;
  1151. textBox_PDinfo.Enabled = true;
  1152. ErrorSaveLog();
  1153. //关机
  1154. Code[0] = 0xF0;
  1155. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1156. 开ToolStripMenuItem.Checked = false;
  1157. 关ToolStripMenuItem.Checked = true;
  1158. Delay_ms(500);
  1159. //关闭串口
  1160. portsettings_form.closePort();
  1161. return;
  1162. }
  1163. }
  1164. #endregion
  1165. #region 判断力矩传感器的标定值是否在允许范围内
  1166. label_Step.Text = "判断力矩传感器";
  1167. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "发送指令查询力矩传感器" + "\r\n";
  1168. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2000, null);//单传感器零点
  1169. Delay_ms(200);
  1170. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2900, null);//单传感器校正信息
  1171. Delay_ms(200);
  1172. Code[0] = 1;
  1173. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, Code);//传感器1零点
  1174. Delay_ms(200);
  1175. Code[0] = 2;
  1176. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, Code);//传感器2零点
  1177. Delay_ms(200);
  1178. Code[0] = 3;
  1179. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, Code);//传感器3零点
  1180. Delay_ms(200);
  1181. Code[0] = 1;
  1182. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, Code);//传感器1校正系数
  1183. Delay_ms(200);
  1184. Code[0] = 2;
  1185. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, Code);//传感器2校正系数
  1186. Delay_ms(200);
  1187. Code[0] = 3;
  1188. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, Code);//传感器3校正系数
  1189. Delay_ms(200);
  1190. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3500, null);//三传感器启动值
  1191. Delay_ms(200);
  1192. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "判断力矩传感器" + "\r\n";
  1193. for (int i = 0; i < richTextBox_TQ.Lines.Length; i++)
  1194. {
  1195. switch (richTextBox_TQ.Lines[i].Split(':')[0])
  1196. {
  1197. case "零点": case "传感器1零点": case "传感器2零点": case "传感器3零点":
  1198. {
  1199. string[] zero = richTextBox_TQ.Lines[i].Split(':')[1].Split(new string[] { ", " }, StringSplitOptions.None);
  1200. for (int j = 0; j < zero.Length; j++)
  1201. {
  1202. if ((Convert.ToUInt16(zero[j]) > TorqueCheck.Zero_Max) || (Convert.ToUInt16(zero[j]) < TorqueCheck.Zero_Min))//零点超差
  1203. {
  1204. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "传感器零点超范围" + "\r\n";
  1205. label_Step.Text = "传感器零点超范围,测试结束!";
  1206. label_Step.ForeColor = Color.Red;
  1207. button_Start.Enabled = true;
  1208. textBox_PDinfo.Enabled = true;
  1209. ErrorSaveLog();
  1210. //关机
  1211. Code[0] = 0xF0;
  1212. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1213. 开ToolStripMenuItem.Checked = false;
  1214. 关ToolStripMenuItem.Checked = true;
  1215. Delay_ms(500);
  1216. //关闭串口
  1217. portsettings_form.closePort();
  1218. return;
  1219. }
  1220. }
  1221. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + richTextBox_TQ.Lines[i].Split(':')[0] + "正常" + "\r\n";
  1222. break;
  1223. }
  1224. case "校正系数": case "传感器1校正系数": case "传感器2校正系数": case "传感器3校正系数":
  1225. {
  1226. ushort k = (ushort)(Convert.ToSingle(richTextBox_TQ.Lines[i].Split(':')[1]) * 100);
  1227. if ((k > (ushort)(TorqueCheck.Cal_K_Max * 100f)) || (k < (ushort)(TorqueCheck.Cal_K_Min * 100f)))//校正系数超差
  1228. {
  1229. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "校正系数超范围" + "\r\n";
  1230. label_Step.Text = "校正系数超范围,测试结束!";
  1231. label_Step.ForeColor = Color.Red;
  1232. button_Start.Enabled = true;
  1233. textBox_PDinfo.Enabled = true;
  1234. ErrorSaveLog();
  1235. //关机
  1236. Code[0] = 0xF0;
  1237. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1238. 开ToolStripMenuItem.Checked = false;
  1239. 关ToolStripMenuItem.Checked = true;
  1240. Delay_ms(500);
  1241. //关闭串口
  1242. portsettings_form.closePort();
  1243. return;
  1244. }
  1245. if (k == 100)
  1246. {
  1247. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "传感器未标定" + "\r\n";
  1248. label_Step.Text = "传感器未标定,测试结束!";
  1249. label_Step.ForeColor = Color.Red;
  1250. button_Start.Enabled = true;
  1251. textBox_PDinfo.Enabled = true;
  1252. ErrorSaveLog();
  1253. //关机
  1254. Code[0] = 0xF0;
  1255. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1256. 开ToolStripMenuItem.Checked = false;
  1257. 关ToolStripMenuItem.Checked = true;
  1258. Delay_ms(500);
  1259. //关闭串口
  1260. portsettings_form.closePort();
  1261. return;
  1262. }
  1263. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + richTextBox_TQ.Lines[i].Split(':')[0] + "正常" + "\r\n";
  1264. break;
  1265. }
  1266. case "校正系数标准差":
  1267. {
  1268. double std = Convert.ToDouble(richTextBox_TQ.Lines[i].Split(':')[1]);
  1269. if (std > 0.3f)
  1270. {
  1271. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "校正系数差异大" + "\r\n";
  1272. label_Step.Text = "校正系数标准差超范围,测试结束!";
  1273. label_Step.ForeColor = Color.Red;
  1274. button_Start.Enabled = true;
  1275. textBox_PDinfo.Enabled = true;
  1276. ErrorSaveLog();
  1277. //关机
  1278. Code[0] = 0xF0;
  1279. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1280. 开ToolStripMenuItem.Checked = false;
  1281. 关ToolStripMenuItem.Checked = true;
  1282. Delay_ms(500);
  1283. //关闭串口
  1284. portsettings_form.closePort();
  1285. return;
  1286. }
  1287. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + richTextBox_TQ.Lines[i].Split(':')[0] + "正常" + "\r\n";
  1288. break;
  1289. }
  1290. case "启动值":
  1291. {
  1292. string start = richTextBox_TQ.Lines[i].Split(':')[1];
  1293. if ((Convert.ToUInt16(start) > TorqueCheck.Start_Max) || (Convert.ToUInt16(start) < TorqueCheck.Start_Min))//启动值超差
  1294. {
  1295. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "启动值超范围" + "\r\n";
  1296. label_Step.Text = "启动值超范围,测试结束!";
  1297. label_Step.ForeColor = Color.Red;
  1298. button_Start.Enabled = true;
  1299. textBox_PDinfo.Enabled = true;
  1300. ErrorSaveLog();
  1301. //关机
  1302. Code[0] = 0xF0;
  1303. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1304. 开ToolStripMenuItem.Checked = false;
  1305. 关ToolStripMenuItem.Checked = true;
  1306. Delay_ms(500);
  1307. //关闭串口
  1308. portsettings_form.closePort();
  1309. return;
  1310. }
  1311. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + richTextBox_TQ.Lines[i].Split(':')[0] + "正常" + "\r\n";
  1312. break;
  1313. }
  1314. default:break;
  1315. }
  1316. }
  1317. #endregion
  1318. #region 依次读取配置参数
  1319. label_Step.Text = "判断配置参数";
  1320. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "发送指令查询电机配置参数" + "\r\n";
  1321. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1A00, null);//用户参数1
  1322. Delay_ms(200);
  1323. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3000, null);//用户参数2
  1324. Delay_ms(200);
  1325. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1C00, null);//马达参数
  1326. Delay_ms(200);
  1327. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1F00, null);//生产信息
  1328. Delay_ms(200);
  1329. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1000, null);//校验秘钥
  1330. Delay_ms(200);
  1331. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1300, null);//自定义1
  1332. Delay_ms(200);
  1333. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1500, null);//自定义2
  1334. Delay_ms(200);
  1335. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1700, null);//自定义3
  1336. Delay_ms(200);
  1337. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "判断配置参数" + "\r\n";
  1338. for (int line = 0; line < richTextBox_CfgFile.Lines.Length; line++)
  1339. {
  1340. try
  1341. {
  1342. if (richTextBox_ReadCfg.Lines[line].Equals(richTextBox_CfgFile.Lines[line]) == false)
  1343. {
  1344. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + richTextBox_CfgFile.Lines[line].Split(',')[0] + "错误" + "\r\n";
  1345. label_Step.Text = "检查配置参数失败,测试结束!";
  1346. label_Step.ForeColor = Color.Red;
  1347. button_Start.Enabled = true;
  1348. textBox_PDinfo.Enabled = true;
  1349. ErrorSaveLog();
  1350. //关机
  1351. Code[0] = 0xF0;
  1352. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1353. 开ToolStripMenuItem.Checked = false;
  1354. 关ToolStripMenuItem.Checked = true;
  1355. Delay_ms(500);
  1356. //关闭串口
  1357. portsettings_form.closePort();
  1358. return;
  1359. }
  1360. }
  1361. catch (System.Exception)
  1362. {
  1363. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "读取电机配置参数失败" + "\r\n";
  1364. label_Step.Text = "检查配置参数失败,测试结束!";
  1365. label_Step.ForeColor = Color.Red;
  1366. button_Start.Enabled = true;
  1367. textBox_PDinfo.Enabled = true;
  1368. ErrorSaveLog();
  1369. //关机
  1370. Code[0] = 0xF0;
  1371. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1372. 开ToolStripMenuItem.Checked = false;
  1373. 关ToolStripMenuItem.Checked = true;
  1374. Delay_ms(500);
  1375. //关闭串口
  1376. portsettings_form.closePort();
  1377. return;
  1378. }
  1379. }
  1380. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机配置参数正确" + "\r\n";
  1381. #endregion
  1382. //检查是否有故障
  1383. if (label_error_code.Text != "OK")
  1384. {
  1385. label_Step.Text = "电机存在故障!";
  1386. button_Start.Enabled = true;
  1387. textBox_PDinfo.Enabled = true;
  1388. //关机
  1389. Code[0] = 0xF0;
  1390. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1391. 开ToolStripMenuItem.Checked = false;
  1392. 关ToolStripMenuItem.Checked = true;
  1393. Delay_ms(500);
  1394. //关闭串口
  1395. portsettings_form.closePort();
  1396. //存储测试数据
  1397. ErrorSaveLog();
  1398. return;
  1399. }
  1400. #region 测试结束
  1401. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "测试结束" + "\r\n";
  1402. label_Step.Text = "测试结束!";
  1403. label_Step.ForeColor = Color.Green;
  1404. label_Result.Text = "合格";
  1405. label_Result.BackColor = Color.Green;
  1406. button_Start.Enabled = true;
  1407. textBox_PDinfo.Enabled = true;
  1408. //关机
  1409. Code[0] = 0xF0;
  1410. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1411. 开ToolStripMenuItem.Checked = false;
  1412. 关ToolStripMenuItem.Checked = true;
  1413. Delay_ms(500);
  1414. //关闭串口
  1415. portsettings_form.closePort();
  1416. //合格计数累加和存储
  1417. label_OK_Count.Text = (Convert.ToInt32(label_OK_Count.Text) + 1).ToString();
  1418. string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\CheckSerialNum";
  1419. StreamReader objReader = new StreamReader(cfg_file);
  1420. string sLine = "";
  1421. ArrayList arrText = new ArrayList();//创建一个动态数组
  1422. while (sLine != null)
  1423. {
  1424. sLine = objReader.ReadLine();
  1425. arrText.Add(sLine);
  1426. }
  1427. arrText.RemoveAt(arrText.Count - 1);
  1428. objReader.Close();
  1429. arrText[3] = label_OK_Count.Text;
  1430. using (StreamWriter file = new StreamWriter(cfg_file))
  1431. {
  1432. foreach (var entry in arrText)
  1433. {
  1434. file.WriteLine(entry);
  1435. }
  1436. }
  1437. #region 按照产品信息设置测试文件路径
  1438. string ResultPathName = textBox_PDinfo.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_");
  1439. if (System.IO.Directory.Exists(LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName) == false)
  1440. System.IO.Directory.CreateDirectory(LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName);
  1441. #endregion
  1442. //页面存储
  1443. Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap
  1444. Graphics g = Graphics.FromImage(bit);
  1445. g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片
  1446. LogSaveFileName = LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName + "\\测试记录" + ".png";
  1447. bit.Save(LogSaveFileName);
  1448. //测试页面上传服务器
  1449. if (StartForm.myFtp.IsNetConnected == true)
  1450. {
  1451. string DateNow = DateTime.Now.ToString("yyyy-MM-dd");
  1452. if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/qcTest/log/", DateNow) == false)
  1453. {
  1454. StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow);
  1455. }
  1456. if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/qcTest/log/", DateNow + "/" + ResultPathName) == false)
  1457. {
  1458. StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow + "/" + ResultPathName);
  1459. }
  1460. bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow + "/" + ResultPathName);
  1461. if (result == true)
  1462. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "测试页面上传服务器成功" + "\r\n";
  1463. }
  1464. //电机数据存储
  1465. LogSaveFileName = LogSavePath + "\\log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName + "\\电机参数" + ".txt";
  1466. string fileInfo = "";
  1467. fileInfo += "检验记录:\r\n" + richTextBox_TestRecord.Text.Replace("\n", "\r\n") + "\r\n\r\n";
  1468. fileInfo += "电机生产参数:\r\n";
  1469. fileInfo += richTextBox_CfgFile.Text.Replace("\n", "\r\n") + "\r\n\r\n";
  1470. fileInfo += "电机版本信息查询:\r\n";
  1471. fileInfo += richTextBox_VerInfo.Text.Replace("\n", "\r\n") + "\r\n\r\n";
  1472. fileInfo += "力矩传感器标定结果:\r\n";
  1473. fileInfo += richTextBox_TQ.Text.Replace("\n", "\r\n") + "\r\n\r\n";
  1474. fileInfo += "电机参数查询:\r\n";
  1475. fileInfo += richTextBox_ReadCfg.Text.Replace("\n", "\r\n") + "\r\n\r\n";
  1476. fileInfo += "电机故障码:\r\n";
  1477. fileInfo += label_error_code.Text.Replace("\n", "\r\n") + "\r\n\r\n";
  1478. fileInfo += "测试结果:\r\n";
  1479. fileInfo += label_Result.Text.Replace("\n", "\r\n") + "\r\n\r\n";
  1480. System.IO.File.WriteAllText(LogSaveFileName, fileInfo);
  1481. //电机数据上传服务器
  1482. if (StartForm.myFtp.IsNetConnected == true)
  1483. {
  1484. string DateNow = DateTime.Now.ToString("yyyy-MM-dd");
  1485. if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/qcTest/log/", DateNow) == false)
  1486. {
  1487. StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow);
  1488. }
  1489. if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow, ResultPathName) == false)
  1490. {
  1491. StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow + "/" + ResultPathName);
  1492. }
  1493. bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow + "/" + ResultPathName);
  1494. if (result == true)
  1495. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机数据上传服务器成功" + "\r\n";
  1496. }
  1497. #endregion
  1498. //测试正常,清空扫码信息
  1499. Delay_ms(1000);
  1500. textBox_PDinfo.Text = "";
  1501. }
  1502. private void CheckForm_Load(object sender, EventArgs e)
  1503. {
  1504. timer1.Enabled = true;
  1505. timer2.Enabled = true;
  1506. //显示测试员信息
  1507. label_TestUser.Text = "测试员:" + StartForm.EnterForm1.comboBox_User.Text;
  1508. //窗口加载时,从FTP服务器或本地获取文件日期列表
  1509. GetDateList(false);
  1510. //装载序列号文件
  1511. string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\CheckSerialNum";
  1512. StreamReader objReader = new StreamReader(cfg_file);
  1513. string sLine = "";
  1514. ArrayList arrText = new ArrayList();//创建一个动态数组
  1515. while (sLine != null)
  1516. {
  1517. sLine = objReader.ReadLine();
  1518. arrText.Add(sLine);
  1519. }
  1520. objReader.Close();
  1521. label_SerialNum.Text = arrText[1].ToString();//流水号
  1522. label_OK_Count.Text = arrText[3].ToString();//合格计数
  1523. }
  1524. //测试失败存储测试界面
  1525. private void ErrorSaveLog()
  1526. {
  1527. //页面存储
  1528. Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap
  1529. Graphics g = Graphics.FromImage(bit);
  1530. g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片
  1531. string LogSaveFileName = LogSavePath + "errorlog\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\";
  1532. if (System.IO.Directory.Exists(LogSaveFileName) == false)
  1533. System.IO.Directory.CreateDirectory(LogSaveFileName);
  1534. LogSaveFileName += textBox_PDinfo.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_测试记录" + ".png";
  1535. bit.Save(LogSaveFileName);
  1536. //测试页面上传服务器
  1537. if (StartForm.myFtp.IsNetConnected == true)
  1538. {
  1539. string DateNow = DateTime.Now.ToString("yyyy-MM-dd");
  1540. if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/qcTest/errorlog/", DateNow) == false)
  1541. {
  1542. StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/qcTest/errorlog/" + DateNow);
  1543. }
  1544. bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/qcTest/errorlog/" + DateNow);
  1545. if (result == true)
  1546. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "测试异常页面上传服务器成功" + "\r\n";
  1547. }
  1548. }
  1549. //开始测试按钮点击
  1550. private void button_Start_Click(object sender, EventArgs e)
  1551. {
  1552. TestStart();
  1553. }
  1554. //开始一次测试
  1555. private void TestStart()
  1556. {
  1557. //显示数据清空
  1558. richTextBox_ReadCfg.Text = "";
  1559. richTextBox_TestRecord.Text = "";
  1560. richTextBox_TQ.Text = "";
  1561. richTextBox_VerInfo.Text = "";
  1562. //Boot版本标志复位
  1563. IsBootVerOK = false;
  1564. #region 检查配置文件
  1565. if (comboBox_File.Items.Count == 0) //配置文件目录为空
  1566. {
  1567. label_Step.Text = "无可用配置文件!";
  1568. label_Step.ForeColor = Color.Red;
  1569. return;
  1570. }
  1571. if (comboBox_File.Text.Trim().Length == 0)//未选择配置文件
  1572. {
  1573. label_Step.Text = "请选择配置文件!";
  1574. label_Step.ForeColor = Color.Red;
  1575. return;
  1576. }
  1577. #endregion
  1578. #region 检查产品信息是否符合格式要求
  1579. try
  1580. {
  1581. 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][A-Z]?$");
  1582. if (textBox_PDinfo.Text.Contains(" "))
  1583. {
  1584. string Mode = textBox_PDinfo.Text.Split(new string[] { " " }, StringSplitOptions.None)[0];
  1585. string Sn = textBox_PDinfo.Text.Split(new string[] { " " }, StringSplitOptions.None)[1];
  1586. if (regex.IsMatch(Sn) == false)
  1587. {
  1588. label_Step.Text = "SN格式错误!";
  1589. label_Step.ForeColor = Color.Red;
  1590. return;
  1591. }
  1592. }
  1593. else if (textBox_PDinfo.Text.Contains("%#"))
  1594. {
  1595. string Mode = textBox_PDinfo.Text.Split(new string[] { "%#" }, StringSplitOptions.None)[0];
  1596. string Sn = textBox_PDinfo.Text.Split(new string[] { "%#" }, StringSplitOptions.None)[1];
  1597. if (regex.IsMatch(Sn) == false)
  1598. {
  1599. label_Step.Text = "SN格式错误!";
  1600. label_Step.ForeColor = Color.Red;
  1601. return;
  1602. }
  1603. }
  1604. else
  1605. {
  1606. label_Step.Text = "标签格式错误!";
  1607. label_Step.ForeColor = Color.Red;
  1608. return;
  1609. }
  1610. }
  1611. catch (System.Exception)
  1612. {
  1613. label_Step.Text = "标签格式错误!";
  1614. label_Step.ForeColor = Color.Red;
  1615. return;
  1616. }
  1617. #endregion
  1618. #region 检查产品型号和配置文件是否一致
  1619. do
  1620. {
  1621. string cfg_mode = "", pd_mode = "";
  1622. cfg_mode = comboBox_File.Text.Split('_')[0];
  1623. if (textBox_PDinfo.Text.Contains(" "))
  1624. {
  1625. pd_mode = textBox_PDinfo.Text.Split(new string[] { " " }, StringSplitOptions.None)[0];
  1626. }
  1627. else if (textBox_PDinfo.Text.Contains("%#"))
  1628. {
  1629. pd_mode = textBox_PDinfo.Text.Split(new string[] { "%#" }, StringSplitOptions.None)[0];
  1630. }
  1631. if (pd_mode != cfg_mode)
  1632. {
  1633. label_Step.Text = "打标信息与配置文件不一致!";
  1634. label_Step.ForeColor = Color.Red;
  1635. return;
  1636. }
  1637. } while (false);
  1638. #endregion
  1639. //连接串口
  1640. if (portsettings_form.openPort() == false)
  1641. {
  1642. label_Step.Text = "串口连接失败!";
  1643. label_Step.ForeColor = Color.Red;
  1644. return;
  1645. }
  1646. //测试状态更新
  1647. label_Step.Text = "准备开始测试";
  1648. label_Step.ForeColor = Color.Red;
  1649. //流水号累加和存储
  1650. label_SerialNum.Text = (Convert.ToInt32(label_SerialNum.Text) + 1).ToString();
  1651. string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\CheckSerialNum";
  1652. StreamReader objReader = new StreamReader(cfg_file);
  1653. string sLine = "";
  1654. ArrayList arrText = new ArrayList();//创建一个动态数组
  1655. while (sLine != null)
  1656. {
  1657. sLine = objReader.ReadLine();
  1658. arrText.Add(sLine);
  1659. }
  1660. arrText.RemoveAt(arrText.Count - 1);
  1661. objReader.Close();
  1662. arrText[1] = label_SerialNum.Text;
  1663. using (StreamWriter file = new StreamWriter(cfg_file))
  1664. {
  1665. foreach (var entry in arrText)
  1666. {
  1667. file.WriteLine(entry);
  1668. }
  1669. }
  1670. button_Start.Enabled = false;
  1671. textBox_PDinfo.Enabled = false;
  1672. //创建线程,执行测试任务
  1673. Thread th = new Thread(Thread_Task);
  1674. th.Start();
  1675. }
  1676. //双击清零流水号
  1677. private void label32_DoubleClick(object sender, EventArgs e)
  1678. {
  1679. StartForm.EnterForm1.ShowDialog();
  1680. try
  1681. {
  1682. if (StartForm.EnterForm1.textBox_Passwd.Text == StartForm.EnterForm1.UserAccount[StartForm.EnterForm1.comboBox_User.Text])
  1683. {
  1684. if (StartForm.EnterForm1.comboBox_User.Text == "工程Engineer")
  1685. {
  1686. //流水号清零
  1687. if (MessageBox.Show("流水号清零", "确认清零?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1688. {
  1689. label_SerialNum.Text = "0";
  1690. label_OK_Count.Text = "0";
  1691. //存储
  1692. string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\CheckSerialNum";
  1693. StreamReader objReader = new StreamReader(cfg_file);
  1694. string sLine = "";
  1695. ArrayList arrText = new ArrayList();//创建一个动态数组
  1696. while (sLine != null)
  1697. {
  1698. sLine = objReader.ReadLine();
  1699. arrText.Add(sLine);
  1700. }
  1701. arrText.RemoveAt(arrText.Count - 1);
  1702. objReader.Close();
  1703. arrText[1] = label_SerialNum.Text;
  1704. arrText[3] = label_OK_Count.Text;
  1705. using (StreamWriter file = new StreamWriter(cfg_file))
  1706. {
  1707. foreach (var entry in arrText)
  1708. {
  1709. file.WriteLine(entry);
  1710. }
  1711. }
  1712. }
  1713. }
  1714. else
  1715. {
  1716. MessageBox.Show("权限不支持", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1717. }
  1718. }
  1719. else
  1720. {
  1721. MessageBox.Show("密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1722. }
  1723. }
  1724. catch (Exception)
  1725. {
  1726. MessageBox.Show("用户不存在或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1727. }
  1728. }
  1729. private void textBox_PDinfo_KeyUp(object sender, KeyEventArgs e)
  1730. {
  1731. if (e.KeyCode == Keys.Enter)
  1732. TestStart();
  1733. }
  1734. private void textBox_PDinfo_Click(object sender, EventArgs e)
  1735. {
  1736. textBox_PDinfo.SelectAll();
  1737. }
  1738. private void 屏幕键盘ToolStripMenuItem_Click_1(object sender, EventArgs e)
  1739. {
  1740. System.Diagnostics.Process.Start("osk.exe");
  1741. }
  1742. private void 端口设置ToolStripMenuItem_Click(object sender, EventArgs e)
  1743. {
  1744. portsettings_form.Show();
  1745. }
  1746. private void 合格ToolStripMenuItem_Click(object sender, EventArgs e)
  1747. {
  1748. System.Diagnostics.Process.Start("explorer.exe", LogSavePath + "log\\");
  1749. }
  1750. private void 不合格ToolStripMenuItem_Click(object sender, EventArgs e)
  1751. {
  1752. System.Diagnostics.Process.Start("explorer.exe", LogSavePath + "errorlog\\");
  1753. }
  1754. private void CheckForm_FormClosing(object sender, FormClosingEventArgs e)
  1755. {
  1756. portsettings_form.serialPort1.Close();
  1757. portsettings_form.comboBox_ComIndex.Enabled = true;
  1758. portsettings_form.button_Enter.Text = "确认";
  1759. portsettings_form.g_blnIsOpen = false;
  1760. #if false
  1761. //针对工程和越南账户,直接退出,不执行删除本地配置
  1762. if ((StartForm.EnterForm1.comboBox_User.Text == "越南Vietnam") || (StartForm.EnterForm1.comboBox_User.Text == "工程Engineer"))
  1763. {
  1764. return;
  1765. }
  1766. //关闭前,删除本地所有配置文件
  1767. string path = StartForm.UserPath + "\\cfg\\";
  1768. try
  1769. {
  1770. DirectoryInfo dir = new DirectoryInfo(path);
  1771. FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录
  1772. foreach (FileSystemInfo i in fileinfo)
  1773. {
  1774. if (i is DirectoryInfo) //判断是否文件夹
  1775. {
  1776. DirectoryInfo subdir = new DirectoryInfo(i.FullName);
  1777. subdir.Delete(true); //删除子目录和文件
  1778. }
  1779. else
  1780. {
  1781. File.Delete(i.FullName); //删除指定文件
  1782. }
  1783. }
  1784. //MessageBox.Show("文件清理成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1785. }
  1786. catch (Exception)
  1787. {
  1788. MessageBox.Show("文件清理失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  1789. }
  1790. #endif
  1791. }
  1792. }
  1793. }