ImportForm.cs 63 KB

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