ImportForm.cs 64 KB

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