Form1.cs 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO.Ports;
  9. namespace MOTINOVA_MC_Test
  10. {
  11. public partial class Form1 : Form
  12. {
  13. #region------------------------本地变量-----------------------------
  14. #region 串口相关
  15. private List<byte> buffer = new List<byte>(4096);
  16. private byte[] binary_data_1 = new byte[128];
  17. private bool Listening = false;// 侦听串口是否是接收数据标志位
  18. public static string m_strDataBits = "8";
  19. public static string m_strStopBits = "1";
  20. public static string m_strCheckBits = "None";
  21. public static bool g_blnIsOpen = false;
  22. #endregion
  23. #endregion
  24. public Form1()
  25. {
  26. InitializeComponent();
  27. }
  28. #region 打开或关闭串口
  29. private void button_ComOpen_Click(object sender, EventArgs e)
  30. {
  31. try
  32. {
  33. if (!serialPort1.IsOpen)
  34. {
  35. serialPort1.PortName = comboBox_ComIndex.Text;
  36. serialPort1.BaudRate = int.Parse(comboBox_Baudrate.Text);
  37. serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), m_strCheckBits);
  38. serialPort1.DataBits = Int32.Parse(m_strDataBits);
  39. serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), (m_strStopBits == "1.5") ? "3" : m_strStopBits);
  40. serialPort1.Open();
  41. comboBox_ComIndex.Enabled = false;
  42. comboBox_Baudrate.Enabled = false;
  43. button_ComOpen.Text = "断开";
  44. g_blnIsOpen = true;
  45. timer1.Enabled = true;
  46. button_RefreshPort.Enabled = false;
  47. }
  48. else
  49. {
  50. while (Listening) Application.DoEvents();
  51. //打开时点击,则关闭串口
  52. serialPort1.Close();
  53. comboBox_ComIndex.Enabled = true;
  54. comboBox_Baudrate.Enabled = true;
  55. button_ComOpen.Text = "连接";
  56. g_blnIsOpen = false;
  57. timer1.Enabled = false;
  58. button_RefreshPort.Enabled = true;
  59. }
  60. }
  61. catch (System.Exception ex)
  62. {
  63. MessageBox.Show(ex.Message);
  64. return;
  65. }
  66. }
  67. #endregion
  68. private void Form1_Load(object sender, EventArgs e)
  69. {
  70. string[] ports = SerialPort.GetPortNames();
  71. Array.Sort(ports);
  72. comboBox_ComIndex.Items.AddRange(ports);
  73. comboBox_ComIndex.SelectedIndex = comboBox_ComIndex.Items.Count > 0 ? 0 : -1;
  74. comboBox_Baudrate.SelectedIndex = comboBox_Baudrate.Items.IndexOf("115200");
  75. }
  76. #region CRC校验
  77. static UInt32[] crc32_table = new UInt32[256]
  78. {
  79. 0x00000000, 0x04C11DB7, 0x09823B6E, 0x0D4326D9, 0x130476DC, 0x17C56B6B,
  80. 0x1A864DB2, 0x1E475005, 0x2608EDB8, 0x22C9F00F, 0x2F8AD6D6, 0x2B4BCB61,
  81. 0x350C9B64, 0x31CD86D3, 0x3C8EA00A, 0x384FBDBD, 0x4C11DB70, 0x48D0C6C7,
  82. 0x4593E01E, 0x4152FDA9, 0x5F15ADAC, 0x5BD4B01B, 0x569796C2, 0x52568B75,
  83. 0x6A1936C8, 0x6ED82B7F, 0x639B0DA6, 0x675A1011, 0x791D4014, 0x7DDC5DA3,
  84. 0x709F7B7A, 0x745E66CD, 0x9823B6E0, 0x9CE2AB57, 0x91A18D8E, 0x95609039,
  85. 0x8B27C03C, 0x8FE6DD8B, 0x82A5FB52, 0x8664E6E5, 0xBE2B5B58, 0xBAEA46EF,
  86. 0xB7A96036, 0xB3687D81, 0xAD2F2D84, 0xA9EE3033, 0xA4AD16EA, 0xA06C0B5D,
  87. 0xD4326D90, 0xD0F37027, 0xDDB056FE, 0xD9714B49, 0xC7361B4C, 0xC3F706FB,
  88. 0xCEB42022, 0xCA753D95, 0xF23A8028, 0xF6FB9D9F, 0xFBB8BB46, 0xFF79A6F1,
  89. 0xE13EF6F4, 0xE5FFEB43, 0xE8BCCD9A, 0xEC7DD02D, 0x34867077, 0x30476DC0,
  90. 0x3D044B19, 0x39C556AE, 0x278206AB, 0x23431B1C, 0x2E003DC5, 0x2AC12072,
  91. 0x128E9DCF, 0x164F8078, 0x1B0CA6A1, 0x1FCDBB16, 0x018AEB13, 0x054BF6A4,
  92. 0x0808D07D, 0x0CC9CDCA, 0x7897AB07, 0x7C56B6B0, 0x71159069, 0x75D48DDE,
  93. 0x6B93DDDB, 0x6F52C06C, 0x6211E6B5, 0x66D0FB02, 0x5E9F46BF, 0x5A5E5B08,
  94. 0x571D7DD1, 0x53DC6066, 0x4D9B3063, 0x495A2DD4, 0x44190B0D, 0x40D816BA,
  95. 0xACA5C697, 0xA864DB20, 0xA527FDF9, 0xA1E6E04E, 0xBFA1B04B, 0xBB60ADFC,
  96. 0xB6238B25, 0xB2E29692, 0x8AAD2B2F, 0x8E6C3698, 0x832F1041, 0x87EE0DF6,
  97. 0x99A95DF3, 0x9D684044, 0x902B669D, 0x94EA7B2A, 0xE0B41DE7, 0xE4750050,
  98. 0xE9362689, 0xEDF73B3E, 0xF3B06B3B, 0xF771768C, 0xFA325055, 0xFEF34DE2,
  99. 0xC6BCF05F, 0xC27DEDE8, 0xCF3ECB31, 0xCBFFD686, 0xD5B88683, 0xD1799B34,
  100. 0xDC3ABDED, 0xD8FBA05A, 0x690CE0EE, 0x6DCDFD59, 0x608EDB80, 0x644FC637,
  101. 0x7A089632, 0x7EC98B85, 0x738AAD5C, 0x774BB0EB, 0x4F040D56, 0x4BC510E1,
  102. 0x46863638, 0x42472B8F, 0x5C007B8A, 0x58C1663D, 0x558240E4, 0x51435D53,
  103. 0x251D3B9E, 0x21DC2629, 0x2C9F00F0, 0x285E1D47, 0x36194D42, 0x32D850F5,
  104. 0x3F9B762C, 0x3B5A6B9B, 0x0315D626, 0x07D4CB91, 0x0A97ED48, 0x0E56F0FF,
  105. 0x1011A0FA, 0x14D0BD4D, 0x19939B94, 0x1D528623, 0xF12F560E, 0xF5EE4BB9,
  106. 0xF8AD6D60, 0xFC6C70D7, 0xE22B20D2, 0xE6EA3D65, 0xEBA91BBC, 0xEF68060B,
  107. 0xD727BBB6, 0xD3E6A601, 0xDEA580D8, 0xDA649D6F, 0xC423CD6A, 0xC0E2D0DD,
  108. 0xCDA1F604, 0xC960EBB3, 0xBD3E8D7E, 0xB9FF90C9, 0xB4BCB610, 0xB07DABA7,
  109. 0xAE3AFBA2, 0xAAFBE615, 0xA7B8C0CC, 0xA379DD7B, 0x9B3660C6, 0x9FF77D71,
  110. 0x92B45BA8, 0x9675461F, 0x8832161A, 0x8CF30BAD, 0x81B02D74, 0x857130C3,
  111. 0x5D8A9099, 0x594B8D2E, 0x5408ABF7, 0x50C9B640, 0x4E8EE645, 0x4A4FFBF2,
  112. 0x470CDD2B, 0x43CDC09C, 0x7B827D21, 0x7F436096, 0x7200464F, 0x76C15BF8,
  113. 0x68860BFD, 0x6C47164A, 0x61043093, 0x65C52D24, 0x119B4BE9, 0x155A565E,
  114. 0x18197087, 0x1CD86D30, 0x029F3D35, 0x065E2082, 0x0B1D065B, 0x0FDC1BEC,
  115. 0x3793A651, 0x3352BBE6, 0x3E119D3F, 0x3AD08088, 0x2497D08D, 0x2056CD3A,
  116. 0x2D15EBE3, 0x29D4F654, 0xC5A92679, 0xC1683BCE, 0xCC2B1D17, 0xC8EA00A0,
  117. 0xD6AD50A5, 0xD26C4D12, 0xDF2F6BCB, 0xDBEE767C, 0xE3A1CBC1, 0xE760D676,
  118. 0xEA23F0AF, 0xEEE2ED18, 0xF0A5BD1D, 0xF464A0AA, 0xF9278673, 0xFDE69BC4,
  119. 0x89B8FD09, 0x8D79E0BE, 0x803AC667, 0x84FBDBD0, 0x9ABC8BD5, 0x9E7D9662,
  120. 0x933EB0BB, 0x97FFAD0C, 0xAFB010B1, 0xAB710D06, 0xA6322BDF, 0xA2F33668,
  121. 0xBCB4666D, 0xB8757BDA, 0xB5365D03, 0xB1F740B4
  122. };
  123. static UInt32 crc32_cal(byte[] pData, UInt16 Length)
  124. {
  125. UInt32 nReg;
  126. UInt32 nTemp = 0;
  127. UInt16 i, n;
  128. nReg = 0xFFFFFFFF;
  129. for (n = 0; n < Length; n++)
  130. {
  131. nReg ^= (UInt32)pData[n];
  132. for (i = 0; i < 4; i++)
  133. {
  134. nTemp = crc32_table[((nReg >> 24) & 0xFF)];
  135. nReg <<= 8;
  136. nReg ^= nTemp;
  137. }
  138. }
  139. return nReg;
  140. }
  141. #endregion
  142. #region 发送指令函数
  143. private void SendCmd(ushort ID, byte Mode, ushort Cmd, byte[] Data)
  144. {
  145. if (!serialPort1.IsOpen)
  146. {
  147. MessageBox.Show("串口未连接");
  148. return;
  149. }
  150. ushort DataLength = (ushort)(Cmd & 0xFF);
  151. var SendCmdTmp = new byte[DataLength + 13];
  152. //帧头
  153. SendCmdTmp[0] = 0x55;
  154. SendCmdTmp[1] = 0xAA;
  155. //ID
  156. SendCmdTmp[2] = (byte)(ID >> 8);
  157. SendCmdTmp[3] = (byte)(ID & 0xFF);
  158. //MODE
  159. SendCmdTmp[4] = Mode;
  160. //长度
  161. SendCmdTmp[5] = (byte)((Cmd & 0xFF) + 2);
  162. //命令字
  163. SendCmdTmp[6] = (byte)(Cmd >> 8);
  164. SendCmdTmp[7] = (byte)(Cmd & 0xFF);
  165. //数据段
  166. for (UInt16 i = 0; i < (Cmd & 0xFF); i++)
  167. {
  168. SendCmdTmp[8 + i] = Data[i];
  169. }
  170. //校验
  171. UInt32 CRC_Result = crc32_cal(SendCmdTmp, (ushort)(DataLength + 8));
  172. SendCmdTmp[DataLength + 8] = Convert.ToByte(CRC_Result >> 24);
  173. SendCmdTmp[DataLength + 9] = Convert.ToByte((CRC_Result >> 16) % 256);
  174. SendCmdTmp[DataLength + 10] = Convert.ToByte((CRC_Result >> 8) % 256);
  175. SendCmdTmp[DataLength + 11] = Convert.ToByte(CRC_Result % 256);
  176. //帧尾
  177. SendCmdTmp[DataLength + 12] = 0xF0;
  178. //发送数据
  179. serialPort1.Write(SendCmdTmp, 0, SendCmdTmp.Length);
  180. }
  181. #endregion
  182. //串口数据接收
  183. private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  184. {
  185. try
  186. {
  187. Listening = true;
  188. int n = serialPort1.BytesToRead;
  189. byte[] buf = new byte[n];//将一次串口事件中接收到的数据暂存于buf中(注:对于超过8个字节的数据,C#的
  190. //串口接收事件将按每次处理8个字节的方式多次处理
  191. serialPort1.Read(buf, 0, n);//将接收到的数据读入buf
  192. this.Invoke((EventHandler)delegate
  193. {
  194. });
  195. buffer.AddRange(buf);//将读取的数据放入buffer中
  196. }
  197. catch (Exception ex)
  198. {
  199. MessageBox.Show(ex.Message);
  200. return;
  201. }
  202. finally
  203. {
  204. Listening = false;
  205. }
  206. }
  207. //接收计数清零
  208. private void label58_Click(object sender, EventArgs e)
  209. {
  210. textBox_RevCnt.Text = "0";
  211. }
  212. //端口刷新
  213. private void button_RefreshPort_Click(object sender, EventArgs e)
  214. {
  215. string[] ports = SerialPort.GetPortNames();
  216. Array.Sort(ports);
  217. comboBox_ComIndex.Items.Clear();
  218. comboBox_ComIndex.Items.AddRange(ports);
  219. comboBox_ComIndex.SelectedIndex = comboBox_ComIndex.Items.Count > 0 ? 0 : -1;
  220. }
  221. //time1定时任务
  222. private void timer1_Tick(object sender, EventArgs e)
  223. {
  224. //...
  225. //串口解析
  226. byte DataNum = 0; //记录每条命令数据段的长度
  227. while (buffer.Count >= 13)//当buffer中的数据的个数大于等于4个时,即每一帧的数据长度所在位(第4位)出现时
  228. {
  229. if (buffer[0] == 0x55 && buffer[1] == 0xAA)//判断帧头
  230. {
  231. int CmdLen = buffer[5];
  232. if (buffer.Count < CmdLen + 11) break;//如果接收到的数据没有达到一帧数据的指定长度, 则执行下次循环
  233. buffer.RemoveRange(0, 6);//数据达到要求长度后删去帧头,ID,模式,和命令长度
  234. while (CmdLen > 0) //读取命令段
  235. {
  236. DataNum = (byte)(buffer[1]);//命令字的第二个字节的表示数据位的长度
  237. if (DataNum <= (CmdLen - 2))
  238. {
  239. for (int i = 0; i < DataNum + 2; i++)
  240. {
  241. binary_data_1[i] = buffer[i];
  242. }
  243. DataCmdProcess();//对于不同的命令段,做不同的处理
  244. buffer.RemoveRange(0, DataNum + 2);//移除处理过的命令段与数据段
  245. CmdLen -= (DataNum + 2);
  246. if (CmdLen < 2)
  247. {
  248. buffer.RemoveRange(0, CmdLen + 2);
  249. return;
  250. }
  251. }
  252. else
  253. {
  254. buffer.RemoveRange(0, CmdLen + 11);
  255. }
  256. }
  257. }
  258. else
  259. {
  260. buffer.RemoveAt(0);//帧头不对,删除帧头高字节
  261. }
  262. }
  263. }
  264. #region 字符串转换为字节流
  265. public static byte[] HexStringToBytes(string hexStr, bool Flag_Space)
  266. {
  267. if (string.IsNullOrEmpty(hexStr))
  268. {
  269. return new byte[0];
  270. }
  271. if (hexStr.StartsWith("0x"))
  272. {
  273. hexStr = hexStr.Remove(0, 2);
  274. }
  275. var count = hexStr.Length;
  276. var byteCount = 0;
  277. if (Flag_Space == true)//相邻字节含空格
  278. {
  279. if ((count % 3) == 0)//最后字节含空格
  280. {
  281. byteCount = count / 3;
  282. }
  283. else if ((count % 3) == 2)//最后字节不含空格
  284. {
  285. byteCount = (count + 1) / 3;
  286. }
  287. else//数据无效
  288. {
  289. MessageBox.Show("相邻字节请插入空格!", "提示");
  290. }
  291. var result = new byte[byteCount];
  292. for (int ii = 0; ii < byteCount; ++ii)
  293. {
  294. var tempBytes = Byte.Parse(hexStr.Substring(3 * ii, 2), System.Globalization.NumberStyles.HexNumber);
  295. result[ii] = tempBytes;
  296. }
  297. return result;
  298. }
  299. else if (Flag_Space == false)//相邻字节不含空格
  300. {
  301. if (count % 2 == 1)
  302. {
  303. MessageBox.Show("请删除相邻字节之间空格!", "提示");
  304. }
  305. byteCount = count / 2;
  306. var result = new byte[byteCount];
  307. for (int ii = 0; ii < byteCount; ++ii)
  308. {
  309. var tempBytes = Byte.Parse(hexStr.Substring(2 * ii, 2), System.Globalization.NumberStyles.HexNumber);
  310. result[ii] = tempBytes;
  311. }
  312. return result;
  313. }
  314. else
  315. {
  316. return new byte[0];
  317. }
  318. }
  319. #endregion
  320. #region 命令段处理函数
  321. private void DataCmdProcess()
  322. {
  323. ushort CmdTemp = (ushort)(binary_data_1[0] * 256 + binary_data_1[1]);
  324. ushort DataTemp;
  325. short Data_Temp_Int;
  326. sbyte Data_Int8;
  327. UInt32 DataTemp32;
  328. textBox_RevCnt.Text = Convert.ToString(Convert.ToInt16(textBox_RevCnt.Text) + 1);
  329. richTextBox_Rev.Text = "";
  330. richTextBox_Data.Text = "";
  331. //显示接收的数据
  332. richTextBox_Rev.Text += "CMD: " + Convert.ToString(binary_data_1[0], 16).PadLeft(2, '0').ToUpper() + " " + Convert.ToString(binary_data_1[1], 16).PadLeft(2, '0').ToUpper() + "\r\n";
  333. richTextBox_Rev.Text += "DATA: ";
  334. for (int i = 0; i < binary_data_1[1]; i++)
  335. {
  336. richTextBox_Rev.Text += Convert.ToString(binary_data_1[2 + i], 16).PadLeft(2, '0').ToUpper() + " ";
  337. }
  338. //数据解析
  339. switch (CmdTemp)
  340. {
  341. case 0x1010://BMS运行信息
  342. {
  343. unchecked
  344. {
  345. this.Invoke((EventHandler)(delegate
  346. {
  347. DataTemp = (ushort)(binary_data_1[3] * 256 + binary_data_1[2]);//电压
  348. richTextBox_Data.Text = "母线电压: " + DataTemp.ToString() + " mV\r\n";
  349. Data_Temp_Int = (short)(binary_data_1[5] * 256 + binary_data_1[4]);//电流
  350. richTextBox_Data.Text += "母线电流: " + Data_Temp_Int.ToString() + " mA\r\n";
  351. DataTemp = (ushort)(binary_data_1[7] * 256 + binary_data_1[6]);//剩余容量
  352. richTextBox_Data.Text += "剩余容量: " + DataTemp.ToString() + " mAh\r\n";
  353. DataTemp = (ushort)(binary_data_1[9] * 256 + binary_data_1[8]);//满充容量
  354. richTextBox_Data.Text += "满充容量: " + DataTemp.ToString() + " mAh\r\n";
  355. Data_Temp_Int = (short)binary_data_1[10];//电芯温度
  356. richTextBox_Data.Text += "电芯温度: " + (Data_Temp_Int - 40).ToString() + " ℃\r\n";
  357. DataTemp = (ushort)binary_data_1[11];//剩余电量
  358. richTextBox_Data.Text += "剩余电量: " + DataTemp.ToString() + " %\r\n";
  359. DataTemp = (ushort)binary_data_1[12];//运行状态
  360. richTextBox_Data.Text += "运行状态: " + "0x" + Convert.ToString(DataTemp, 16).PadLeft(2, '0').ToUpper() + "\r\n";
  361. DataTemp = (ushort)binary_data_1[13];//SOH
  362. richTextBox_Data.Text += "剩余寿命: " + DataTemp.ToString() + " %\r\n";
  363. DataTemp = (ushort)(binary_data_1[15] * 256 + binary_data_1[14]);//循环次数
  364. richTextBox_Data.Text += "循环次数: " + DataTemp.ToString() + " \r\n";
  365. DataTemp = (ushort)(binary_data_1[17] * 256 + binary_data_1[16]);//剩余充电时间
  366. richTextBox_Data.Text += "剩余充电时间: " + DataTemp.ToString() + " min\r\n";
  367. }));
  368. }
  369. break;
  370. }
  371. case 0x1120://BMS电芯电压
  372. {
  373. unchecked
  374. {
  375. this.Invoke((EventHandler)(delegate
  376. {
  377. for (int i = 0; i < 16; i++)
  378. {
  379. DataTemp = (ushort)(binary_data_1[3 + i * 2] * 256 + binary_data_1[2 + i * 2]);//电压
  380. if (DataTemp == 0)
  381. break;
  382. richTextBox_Data.Text += "电芯" + (i + 1).ToString() + ": " + DataTemp.ToString() + " mV\r\n";
  383. }
  384. }));
  385. }
  386. break;
  387. }
  388. case 0x1204://故障码
  389. {
  390. unchecked
  391. {
  392. this.Invoke((EventHandler)(delegate
  393. {
  394. }));
  395. }
  396. break;
  397. }
  398. case 0x1308://关机指令
  399. {
  400. unchecked
  401. {
  402. this.Invoke((EventHandler)(delegate
  403. {
  404. richTextBox_Data.Text += "接收数据:";
  405. for (int i = 0; i < 8; i++)
  406. richTextBox_Data.Text += ((char)(binary_data_1[2 + i])).ToString();
  407. }));
  408. }
  409. break;
  410. }
  411. case 0x1410://BMS设计信息
  412. {
  413. unchecked
  414. {
  415. this.Invoke((EventHandler)(delegate
  416. {
  417. DataTemp = (ushort)(binary_data_1[3] * 256 + binary_data_1[2]);
  418. richTextBox_Data.Text = "设计容量: " + DataTemp.ToString() + " mAh\r\n";
  419. DataTemp = (ushort)binary_data_1[4];
  420. richTextBox_Data.Text += "设计电压: " + DataTemp.ToString() + " V\r\n";
  421. richTextBox_Data.Text += "电芯型号: ";
  422. for (ushort i = 0; i < 8; i++)
  423. {
  424. richTextBox_Data.Text += ((char)binary_data_1[5 + i]).ToString();
  425. }
  426. richTextBox_Data.Text += "\r\n";
  427. }));
  428. }
  429. break;
  430. }
  431. case 0x1540://BMS版本信息
  432. {
  433. unchecked
  434. {
  435. this.Invoke((EventHandler)(delegate
  436. {
  437. //型号
  438. richTextBox_Data.Text += "型号: ";
  439. for (ushort i = 0; i < 16; i++)
  440. {
  441. richTextBox_Data.Text += ((char)binary_data_1[2 + i]).ToString();
  442. }
  443. richTextBox_Data.Text += "\r\n";
  444. //SN
  445. richTextBox_Data.Text += "序列号: ";
  446. for (ushort i = 0; i < 16; i++)
  447. {
  448. richTextBox_Data.Text += ((char)binary_data_1[18 + i]).ToString();
  449. }
  450. richTextBox_Data.Text += "\r\n";
  451. //HW
  452. richTextBox_Data.Text += "硬件版本: ";
  453. for (ushort i = 0; i < 16; i++)
  454. {
  455. richTextBox_Data.Text += ((char)binary_data_1[34 + i]).ToString();
  456. }
  457. richTextBox_Data.Text += "\r\n";
  458. //FW
  459. richTextBox_Data.Text += "软件版本: ";
  460. for (ushort i = 0; i < 16; i++)
  461. {
  462. richTextBox_Data.Text += ((char)binary_data_1[50 + i]).ToString();
  463. }
  464. richTextBox_Data.Text += "\r\n";
  465. }));
  466. }
  467. break;
  468. }
  469. case 0x160C://BMS物理ID
  470. {
  471. unchecked
  472. {
  473. this.Invoke((EventHandler)(delegate
  474. {
  475. richTextBox_Data.Text += "物理ID: ";
  476. richTextBox_Data.Text += "0x";
  477. for (ushort i = 0; i < 12; i++)
  478. {
  479. richTextBox_Data.Text += Convert.ToString(binary_data_1[2 + i], 16).PadLeft(2, '0').ToUpper();
  480. }
  481. richTextBox_Data.Text += "\r\n";
  482. }));
  483. }
  484. break;
  485. }
  486. case 0x170C://BMS校验码
  487. {
  488. unchecked
  489. {
  490. this.Invoke((EventHandler)(delegate
  491. {
  492. richTextBox_Data.Text += "校验码: ";
  493. richTextBox_Data.Text += "0x";
  494. for (ushort i = 0; i < 12; i++)
  495. {
  496. richTextBox_Data.Text += Convert.ToString(binary_data_1[2 + i], 16).PadLeft(2, '0').ToUpper();
  497. }
  498. richTextBox_Data.Text += "\r\n";
  499. }));
  500. }
  501. break;
  502. }
  503. case 0x3005://MC在线反馈
  504. {
  505. unchecked
  506. {
  507. this.Invoke((EventHandler)(delegate
  508. {
  509. richTextBox_Data.Text += "接收数据:";
  510. for (int i = 0; i < 5; i++)
  511. richTextBox_Data.Text += ((char)(binary_data_1[2 + i])).ToString();
  512. }));
  513. }
  514. break;
  515. }
  516. case 0x5028://电池历史信息
  517. {
  518. unchecked
  519. {
  520. this.Invoke((EventHandler)(delegate
  521. {
  522. Data_Int8 = (sbyte)(binary_data_1[2]);//电芯最高温
  523. richTextBox_Data.Text += "电芯最高温: " + (Data_Int8 - 40).ToString() + " ℃\r\n";
  524. Data_Int8 = (sbyte)(binary_data_1[3]);//电芯最低温
  525. richTextBox_Data.Text += "电芯最低温: " + (Data_Int8 - 40).ToString() + " ℃\r\n";
  526. DataTemp = (ushort)(binary_data_1[5] * 256 + binary_data_1[4]);//最大放电电流
  527. richTextBox_Data.Text += "最大放电电流: " + DataTemp.ToString() + " mA\r\n";
  528. DataTemp = (ushort)(binary_data_1[7] * 256 + binary_data_1[6]);//最大放电电流
  529. richTextBox_Data.Text += "最大充电电流: " + DataTemp.ToString() + " mA\r\n";
  530. DataTemp = (ushort)(binary_data_1[9] * 256 + binary_data_1[8]);//最大放电电流
  531. richTextBox_Data.Text += "循环次数: " + DataTemp.ToString() + " 次\r\n";
  532. DataTemp = (ushort)(binary_data_1[11] * 256 + binary_data_1[10]);//最近充电间隔
  533. richTextBox_Data.Text += "最近充电间隔时间: " + DataTemp.ToString() + " h\r\n";
  534. DataTemp = (ushort)(binary_data_1[13] * 256 + binary_data_1[12]);//最大充电间隔
  535. richTextBox_Data.Text += "最大充电间隔时间: " + DataTemp.ToString() + " h\r\n";
  536. DataTemp = (ushort)(binary_data_1[15] * 256 + binary_data_1[14]);//充电过流保护次数
  537. richTextBox_Data.Text += "充电过流保护次数: " + DataTemp.ToString() + " 次\r\n";
  538. DataTemp = (ushort)(binary_data_1[17] * 256 + binary_data_1[16]);//放电过流保护次数
  539. richTextBox_Data.Text += "放电过流保护次数: " + DataTemp.ToString() + " 次\r\n";
  540. DataTemp = (ushort)(binary_data_1[19] * 256 + binary_data_1[18]);//过充保护次数
  541. richTextBox_Data.Text += "过充保护次数: " + DataTemp.ToString() + " 次\r\n";
  542. DataTemp = (ushort)(binary_data_1[21] * 256 + binary_data_1[20]);//过放保护次数
  543. richTextBox_Data.Text += "过放保护次数: " + DataTemp.ToString() + " 次\r\n";
  544. DataTemp = (ushort)(binary_data_1[23] * 256 + binary_data_1[22]);//短路保护次数
  545. richTextBox_Data.Text += "短路保护次数: " + DataTemp.ToString() + " 次\r\n";
  546. DataTemp = (ushort)(binary_data_1[25] * 256 + binary_data_1[24]);//充电低温保护次数
  547. richTextBox_Data.Text += "充电低温保护次数: " + DataTemp.ToString() + " 次\r\n";
  548. DataTemp = (ushort)(binary_data_1[27] * 256 + binary_data_1[26]);//充电高温保护次数
  549. richTextBox_Data.Text += "充电高温保护次数: " + DataTemp.ToString() + " 次\r\n";
  550. DataTemp = (ushort)(binary_data_1[29] * 256 + binary_data_1[28]);//放电低温保护次数
  551. richTextBox_Data.Text += "放电低温保护次数: " + DataTemp.ToString() + " 次\r\n";
  552. DataTemp = (ushort)(binary_data_1[31] * 256 + binary_data_1[30]);//放电高温保护次数
  553. richTextBox_Data.Text += "放电高温保护次数: " + DataTemp.ToString() + " 次\r\n";
  554. DataTemp32 = (UInt32)((binary_data_1[35] << 24) + (binary_data_1[34]) << 16 + binary_data_1[33] * 256 + binary_data_1[32]);//运行时间
  555. richTextBox_Data.Text += "运行时间: " + DataTemp32.ToString() + " min\r\n";
  556. DataTemp = (ushort)(binary_data_1[36]);//SOH
  557. richTextBox_Data.Text += "剩余寿命: " + DataTemp.ToString() + " %\r\n";
  558. }));
  559. }
  560. break;
  561. }
  562. case 0x5120://电池生产信息
  563. {
  564. unchecked
  565. {
  566. this.Invoke((EventHandler)(delegate
  567. {
  568. richTextBox_Data.Text += "生产商:";
  569. for (int i = 0; i < 8; i++)
  570. richTextBox_Data.Text += ((char)(binary_data_1[2 + i])).ToString();
  571. richTextBox_Data.Text += "\r\n";
  572. richTextBox_Data.Text += "生产地:";
  573. for (int i = 0; i < 8; i++)
  574. richTextBox_Data.Text += ((char)(binary_data_1[10 + i])).ToString();
  575. richTextBox_Data.Text += "\r\n";
  576. richTextBox_Data.Text += "生产日期:";
  577. for (int i = 0; i < 8; i++)
  578. richTextBox_Data.Text += ((char)(binary_data_1[18 + i])).ToString();
  579. }));
  580. }
  581. break;
  582. }
  583. case 0x5210://自定义1
  584. {
  585. unchecked
  586. {
  587. this.Invoke((EventHandler)(delegate
  588. {
  589. richTextBox_Data.Text += "接收数据:";
  590. for (int i = 0; i < 16; i++)
  591. richTextBox_Data.Text += ((char)(binary_data_1[2 + i])).ToString();
  592. }));
  593. }
  594. break;
  595. }
  596. case 0x5310://自定义2
  597. {
  598. unchecked
  599. {
  600. this.Invoke((EventHandler)(delegate
  601. {
  602. richTextBox_Data.Text += "接收数据:";
  603. for (int i = 0; i < 16; i++)
  604. richTextBox_Data.Text += ((char)(binary_data_1[2 + i])).ToString();
  605. }));
  606. }
  607. break;
  608. }
  609. case 0x5410://自定义3
  610. {
  611. unchecked
  612. {
  613. this.Invoke((EventHandler)(delegate
  614. {
  615. richTextBox_Data.Text += "接收数据:";
  616. for (int i = 0; i < 16; i++)
  617. richTextBox_Data.Text += ((char)(binary_data_1[2 + i])).ToString();
  618. }));
  619. }
  620. break;
  621. }
  622. case 0x5503://反馈
  623. {
  624. unchecked
  625. {
  626. this.Invoke((EventHandler)(delegate
  627. {
  628. richTextBox_Data.Text += "接收数据:";
  629. for (int i = 0; i < 3; i++)
  630. richTextBox_Data.Text += ((char)(binary_data_1[2 + i])).ToString();
  631. }));
  632. }
  633. break;
  634. }
  635. case 0x5688://日志
  636. {
  637. unchecked
  638. {
  639. this.Invoke((EventHandler)(delegate
  640. {
  641. }));
  642. }
  643. break;
  644. }
  645. default: break;
  646. }
  647. }
  648. #endregion
  649. #region 模拟指令发送
  650. #region MC模拟指令
  651. /// <summary>
  652. /// MC关机就绪
  653. /// </summary>
  654. /// <param name="sender"></param>
  655. /// <param name="e"></param>
  656. private void button_MC_Ready_Click(object sender, EventArgs e)
  657. {
  658. var Code = new byte[5];
  659. Code[0] = (byte)'R';
  660. Code[1] = (byte)'E';
  661. Code[2] = (byte)'A';
  662. Code[3] = (byte)'D';
  663. Code[4] = (byte)'Y';
  664. SendCmd((ushort)0x710, (byte)0x0C, (ushort)0x1305, Code);
  665. richTextBox_Data.Text = "";
  666. richTextBox_Rev.Text = "";
  667. }
  668. /// <summary>
  669. /// MC在线检测
  670. /// </summary>
  671. /// <param name="sender"></param>
  672. /// <param name="e"></param>
  673. private void button_MC_Check_Click(object sender, EventArgs e)
  674. {
  675. var Code = new byte[9];
  676. Code[0] = (byte)'H';
  677. Code[1] = (byte)'A';
  678. Code[2] = (byte)'N';
  679. Code[3] = (byte)'D';
  680. Code[4] = (byte)'S';
  681. Code[5] = (byte)'H';
  682. Code[6] = (byte)'K';
  683. Code[7] = (byte)'A';
  684. Code[8] = (byte)'E';
  685. SendCmd((ushort)0x712, (byte)0x11, (ushort)0x3009, Code);
  686. richTextBox_Data.Text = "";
  687. richTextBox_Rev.Text = "";
  688. }
  689. /// <summary>
  690. /// 查询物理ID
  691. /// </summary>
  692. /// <param name="sender"></param>
  693. /// <param name="e"></param>
  694. private void button_MC_ReadID_Click(object sender, EventArgs e)
  695. {
  696. SendCmd((ushort)0x712, (byte)0x11, (ushort)0x3100, null);
  697. richTextBox_Data.Text = "";
  698. richTextBox_Rev.Text = "";
  699. }
  700. /// <summary>
  701. /// 查询校验码
  702. /// </summary>
  703. /// <param name="sender"></param>
  704. /// <param name="e"></param>
  705. private void button_MC_ReadCheckNum_Click(object sender, EventArgs e)
  706. {
  707. SendCmd((ushort)0x712, (byte)0x11, (ushort)0x3200, null);
  708. richTextBox_Data.Text = "";
  709. richTextBox_Rev.Text = "";
  710. }
  711. /// <summary>
  712. /// 查询设计信息
  713. /// </summary>
  714. /// <param name="sender"></param>
  715. /// <param name="e"></param>
  716. private void button_MC_ReadDesignInfo_Click(object sender, EventArgs e)
  717. {
  718. SendCmd((ushort)0x712, (byte)0x11, (ushort)0x3300, null);
  719. richTextBox_Data.Text = "";
  720. richTextBox_Rev.Text = "";
  721. }
  722. #endregion
  723. #region PBU/OBC模拟指令
  724. /// <summary>
  725. /// OBC关机就绪
  726. /// </summary>
  727. /// <param name="sender"></param>
  728. /// <param name="e"></param>
  729. private void button_OBC_Ready_Click(object sender, EventArgs e)
  730. {
  731. var Code = new byte[5];
  732. Code[0] = (byte)'R';
  733. Code[1] = (byte)'E';
  734. Code[2] = (byte)'A';
  735. Code[3] = (byte)'D';
  736. Code[4] = (byte)'Y';
  737. SendCmd((ushort)0x730, (byte)0x0C, (ushort)0x1405, Code);
  738. richTextBox_Data.Text = "";
  739. richTextBox_Rev.Text = "";
  740. }
  741. /// <summary>
  742. /// OBC查询BMS运行信息
  743. /// </summary>
  744. /// <param name="sender"></param>
  745. /// <param name="e"></param>
  746. private void button_OBC_ReadRunInfo_Click(object sender, EventArgs e)
  747. {
  748. SendCmd((ushort)0x732, (byte)0x11, (ushort)0x5000, null);
  749. richTextBox_Data.Text = "";
  750. richTextBox_Rev.Text = "";
  751. }
  752. /// <summary>
  753. /// OBC查询BMS版本信息
  754. /// </summary>
  755. /// <param name="sender"></param>
  756. /// <param name="e"></param>
  757. private void button_OBC_ReadVer_Click(object sender, EventArgs e)
  758. {
  759. SendCmd((ushort)0x732, (byte)0x11, (ushort)0x5100, null);
  760. richTextBox_Data.Text = "";
  761. richTextBox_Rev.Text = "";
  762. }
  763. /// <summary>
  764. /// OBC查询BMS设计信息
  765. /// </summary>
  766. /// <param name="sender"></param>
  767. /// <param name="e"></param>
  768. private void button_OBC_ReadDesign_Click(object sender, EventArgs e)
  769. {
  770. SendCmd((ushort)0x732, (byte)0x11, (ushort)0x5200, null);
  771. richTextBox_Data.Text = "";
  772. richTextBox_Rev.Text = "";
  773. }
  774. /// <summary>
  775. /// OBC读取BMS电芯电压
  776. /// </summary>
  777. /// <param name="sender"></param>
  778. /// <param name="e"></param>
  779. private void button_OBC_ReadCell_Click(object sender, EventArgs e)
  780. {
  781. SendCmd((ushort)0x732, (byte)0x11, (ushort)0x5300, null);
  782. richTextBox_Data.Text = "";
  783. richTextBox_Rev.Text = "";
  784. }
  785. #endregion
  786. #region HMI模拟指令
  787. /// <summary>
  788. /// HMI关机就绪
  789. /// </summary>
  790. /// <param name="sender"></param>
  791. /// <param name="e"></param>
  792. private void button_HMI_Ready_Click(object sender, EventArgs e)
  793. {
  794. var Code = new byte[5];
  795. Code[0] = (byte)'R';
  796. Code[1] = (byte)'E';
  797. Code[2] = (byte)'A';
  798. Code[3] = (byte)'D';
  799. Code[4] = (byte)'Y';
  800. SendCmd((ushort)0x740, (byte)0x0C, (ushort)0x1305, Code);
  801. richTextBox_Data.Text = "";
  802. richTextBox_Rev.Text = "";
  803. }
  804. /// <summary>
  805. /// HMI读取版本信息
  806. /// </summary>
  807. /// <param name="sender"></param>
  808. /// <param name="e"></param>
  809. private void button_HMI_ReadVerInfo_Click(object sender, EventArgs e)
  810. {
  811. SendCmd((ushort)0x742, (byte)0x11, (ushort)0x5000, null);
  812. richTextBox_Data.Text = "";
  813. richTextBox_Rev.Text = "";
  814. }
  815. /// <summary>
  816. /// HMI读取设计信息
  817. /// </summary>
  818. /// <param name="sender"></param>
  819. /// <param name="e"></param>
  820. private void button_HMI_ReadDesignInfo_Click(object sender, EventArgs e)
  821. {
  822. SendCmd((ushort)0x742, (byte)0x11, (ushort)0x5100, null);
  823. richTextBox_Data.Text = "";
  824. richTextBox_Rev.Text = "";
  825. }
  826. /// <summary>
  827. /// HMI读取电芯电压
  828. /// </summary>
  829. /// <param name="sender"></param>
  830. /// <param name="e"></param>
  831. private void button_HMI_ReadCell_Click(object sender, EventArgs e)
  832. {
  833. SendCmd((ushort)0x742, (byte)0x11, (ushort)0x5200, null);
  834. richTextBox_Data.Text = "";
  835. richTextBox_Rev.Text = "";
  836. }
  837. #endregion
  838. #region 模拟CDL发送指令
  839. /// <summary>
  840. /// CDL查询物理ID
  841. /// </summary>
  842. /// <param name="sender"></param>
  843. /// <param name="e"></param>
  844. private void button_CDL_ReadID_Click(object sender, EventArgs e)
  845. {
  846. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3000, null);
  847. richTextBox_Data.Text = "";
  848. richTextBox_Rev.Text = "";
  849. }
  850. /// <summary>
  851. /// CDL查询校验码
  852. /// </summary>
  853. /// <param name="sender"></param>
  854. /// <param name="e"></param>
  855. private void button_CDL_ReadCheckNum_Click(object sender, EventArgs e)
  856. {
  857. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3100, null);
  858. richTextBox_Data.Text = "";
  859. richTextBox_Rev.Text = "";
  860. }
  861. /// <summary>
  862. /// CDL写入校验码
  863. /// </summary>
  864. /// <param name="sender"></param>
  865. /// <param name="e"></param>
  866. private void button_CDL_WriteCheckNum_Click(object sender, EventArgs e)
  867. {
  868. var Code = new byte[12];
  869. if (HexStringToBytes(textBox_SendData.Text, true).Length !=12)
  870. {
  871. MessageBox.Show("Invalid length of bytes!", "Notice", MessageBoxButtons.OK);
  872. return;
  873. }
  874. Code = HexStringToBytes(textBox_SendData.Text, true);
  875. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x320C, Code);
  876. richTextBox_Data.Text = "";
  877. richTextBox_Rev.Text = "";
  878. }
  879. /// <summary>
  880. /// CDL读取版本信息
  881. /// </summary>
  882. /// <param name="sender"></param>
  883. /// <param name="e"></param>
  884. private void button_CDL_ReadVer_Click(object sender, EventArgs e)
  885. {
  886. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3300, null);
  887. richTextBox_Data.Text = "";
  888. richTextBox_Rev.Text = "";
  889. }
  890. /// <summary>
  891. /// CDL读取运行信息
  892. /// </summary>
  893. /// <param name="sender"></param>
  894. /// <param name="e"></param>
  895. private void button_CDL_ReadRunInfo_Click(object sender, EventArgs e)
  896. {
  897. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3400, null);
  898. richTextBox_Data.Text = "";
  899. richTextBox_Rev.Text = "";
  900. }
  901. /// <summary>
  902. /// CDL读取电芯电压
  903. /// </summary>
  904. /// <param name="sender"></param>
  905. /// <param name="e"></param>
  906. private void button_CDL_ReadCell_Click(object sender, EventArgs e)
  907. {
  908. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3500, null);
  909. richTextBox_Data.Text = "";
  910. richTextBox_Rev.Text = "";
  911. }
  912. /// <summary>
  913. /// CDL读取设计信息
  914. /// </summary>
  915. /// <param name="sender"></param>
  916. /// <param name="e"></param>
  917. private void button_CDL_ReadDesign_Click(object sender, EventArgs e)
  918. {
  919. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3600, null);
  920. richTextBox_Data.Text = "";
  921. richTextBox_Rev.Text = "";
  922. }
  923. /// <summary>
  924. /// CDL读取生产信息
  925. /// </summary>
  926. /// <param name="sender"></param>
  927. /// <param name="e"></param>
  928. private void button_CDL_ReadFac_Click(object sender, EventArgs e)
  929. {
  930. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3700, null);
  931. richTextBox_Data.Text = "";
  932. richTextBox_Rev.Text = "";
  933. }
  934. /// <summary>
  935. /// CDL读取历史信息
  936. /// </summary>
  937. /// <param name="sender"></param>
  938. /// <param name="e"></param>
  939. private void button_CDL_ReadHistory_Click(object sender, EventArgs e)
  940. {
  941. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3800, null);
  942. richTextBox_Data.Text = "";
  943. richTextBox_Rev.Text = "";
  944. }
  945. /// <summary>
  946. /// CDL读取自定义信息1
  947. /// </summary>
  948. /// <param name="sender"></param>
  949. /// <param name="e"></param>
  950. private void button_CDL_ReadUser1_Click(object sender, EventArgs e)
  951. {
  952. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3900, null);
  953. richTextBox_Data.Text = "";
  954. richTextBox_Rev.Text = "";
  955. }
  956. /// <summary>
  957. /// CDL写入自定义信息1
  958. /// </summary>
  959. /// <param name="sender"></param>
  960. /// <param name="e"></param>
  961. private void button_CDL_Write_User1_Click(object sender, EventArgs e)
  962. {
  963. var Code = new byte[16];
  964. if (textBox_SendData.Text.Length > 16)
  965. {
  966. MessageBox.Show("Length should be less 16 bytes!", "Notice", MessageBoxButtons.OK);
  967. return;
  968. }
  969. for (ushort i = 0; i < textBox_SendData.Text.Length; i++)
  970. {
  971. Code[i] = (byte)textBox_SendData.Text[i];
  972. }
  973. if (textBox_SendData.Text.Length < 16)
  974. {
  975. Code[textBox_SendData.Text.Length] = (byte)'.';
  976. for (ushort i = 0; i < 16 - textBox_SendData.Text.Length - 1; i++)
  977. {
  978. Code[textBox_SendData.Text.Length + 1 + i] = 0x20;
  979. }
  980. }
  981. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x3A10, Code);
  982. richTextBox_Data.Text = "";
  983. richTextBox_Rev.Text = "";
  984. }
  985. /// <summary>
  986. /// CDL读取自定义信息2
  987. /// </summary>
  988. /// <param name="sender"></param>
  989. /// <param name="e"></param>
  990. private void button_CDL_ReadUser2_Click(object sender, EventArgs e)
  991. {
  992. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3B00, null);
  993. richTextBox_Data.Text = "";
  994. richTextBox_Rev.Text = "";
  995. }
  996. /// <summary>
  997. /// CDL写入自定义信息2
  998. /// </summary>
  999. /// <param name="sender"></param>
  1000. /// <param name="e"></param>
  1001. private void button_CDL_Write_User2_Click(object sender, EventArgs e)
  1002. {
  1003. var Code = new byte[16];
  1004. if (textBox_SendData.Text.Length > 16)
  1005. {
  1006. MessageBox.Show("Length should be less 16 bytes!", "Notice", MessageBoxButtons.OK);
  1007. return;
  1008. }
  1009. for (ushort i = 0; i < textBox_SendData.Text.Length; i++)
  1010. {
  1011. Code[i] = (byte)textBox_SendData.Text[i];
  1012. }
  1013. if (textBox_SendData.Text.Length < 16)
  1014. {
  1015. Code[textBox_SendData.Text.Length] = (byte)'.';
  1016. for (ushort i = 0; i < 16 - textBox_SendData.Text.Length - 1; i++)
  1017. {
  1018. Code[textBox_SendData.Text.Length + 1 + i] = 0x20;
  1019. }
  1020. }
  1021. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x3C10, Code);
  1022. richTextBox_Data.Text = "";
  1023. richTextBox_Rev.Text = "";
  1024. }
  1025. /// <summary>
  1026. /// CDL查询自定义信息3
  1027. /// </summary>
  1028. /// <param name="sender"></param>
  1029. /// <param name="e"></param>
  1030. private void button_CDL_ReadUser3_Click(object sender, EventArgs e)
  1031. {
  1032. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3D00, null);
  1033. richTextBox_Data.Text = "";
  1034. richTextBox_Rev.Text = "";
  1035. }
  1036. /// <summary>
  1037. /// CDL写入自定义信息3
  1038. /// </summary>
  1039. /// <param name="sender"></param>
  1040. /// <param name="e"></param>
  1041. private void button_CDL_Write_User3_Click(object sender, EventArgs e)
  1042. {
  1043. var Code = new byte[16];
  1044. if (textBox_SendData.Text.Length > 16)
  1045. {
  1046. MessageBox.Show("Length should be less 16 bytes!", "Notice", MessageBoxButtons.OK);
  1047. return;
  1048. }
  1049. for (ushort i = 0; i < textBox_SendData.Text.Length; i++)
  1050. {
  1051. Code[i] = (byte)textBox_SendData.Text[i];
  1052. }
  1053. if (textBox_SendData.Text.Length < 16)
  1054. {
  1055. Code[textBox_SendData.Text.Length] = (byte)'.';
  1056. for (ushort i = 0; i < 16 - textBox_SendData.Text.Length - 1; i++)
  1057. {
  1058. Code[textBox_SendData.Text.Length + 1 + i] = 0x20;
  1059. }
  1060. }
  1061. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x3E10, Code);
  1062. richTextBox_Data.Text = "";
  1063. richTextBox_Rev.Text = "";
  1064. }
  1065. /// <summary>
  1066. /// CDL写入生产信息
  1067. /// </summary>
  1068. /// <param name="sender"></param>
  1069. /// <param name="e"></param>
  1070. private void button_CDL_WriteFac_Click(object sender, EventArgs e)
  1071. {
  1072. var Code = new byte[32];
  1073. if (textBox_SendData.Text.Length == 0)
  1074. {
  1075. MessageBox.Show("Invalid length of bytes!", "Notice", MessageBoxButtons.OK);
  1076. return;
  1077. }
  1078. //填入生产商
  1079. for (ushort i = 0; i < 8; i++)
  1080. {
  1081. Code[i] = (byte)textBox_SendData.Text[i];
  1082. }
  1083. if (textBox_SendData.Text.Length < 8)
  1084. {
  1085. Code[textBox_SendData.Text.Length] = (byte)'.';
  1086. for (ushort i = 0; i < 8 - textBox_SendData.Text.Length - 1; i++)
  1087. {
  1088. Code[textBox_SendData.Text.Length + 1 + i] = 0x20;
  1089. }
  1090. }
  1091. //填入生产地
  1092. for (ushort i = 0; i < 8; i++)
  1093. {
  1094. Code[8 + i] = (byte)textBox_SendData.Text[i];
  1095. }
  1096. if (textBox_SendData.Text.Length < 8)
  1097. {
  1098. Code[textBox_SendData.Text.Length + 8] = (byte)'.';
  1099. for (ushort i = 0; i < 8 - textBox_SendData.Text.Length - 1; i++)
  1100. {
  1101. Code[textBox_SendData.Text.Length + 1 + i + 8] = 0x20;
  1102. }
  1103. }
  1104. //填入生产日期
  1105. for (ushort i = 0; i < 8; i++)
  1106. {
  1107. Code[16 + i] = (byte)textBox_SendData.Text[i];
  1108. }
  1109. if (textBox_SendData.Text.Length < 8)
  1110. {
  1111. Code[textBox_SendData.Text.Length + 16] = (byte)'.';
  1112. for (ushort i = 0; i < 8 - textBox_SendData.Text.Length - 1; i++)
  1113. {
  1114. Code[textBox_SendData.Text.Length + 1 + i + 16] = 0x20;
  1115. }
  1116. }
  1117. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x3F20, Code);
  1118. richTextBox_Data.Text = "";
  1119. richTextBox_Rev.Text = "";
  1120. }
  1121. /// <summary>
  1122. /// CDL写入型号
  1123. /// </summary>
  1124. /// <param name="sender"></param>
  1125. /// <param name="e"></param>
  1126. private void button_CDL_WriteModel_Click(object sender, EventArgs e)
  1127. {
  1128. var Code = new byte[16];
  1129. if (textBox_SendData.Text.Length > 16)
  1130. {
  1131. MessageBox.Show("Length should be less 16 bytes!", "Notice", MessageBoxButtons.OK);
  1132. return;
  1133. }
  1134. for (ushort i = 0; i < textBox_SendData.Text.Length; i++)
  1135. {
  1136. Code[i] = (byte)textBox_SendData.Text[i];
  1137. }
  1138. if (textBox_SendData.Text.Length < 16)
  1139. {
  1140. Code[textBox_SendData.Text.Length] = (byte)'.';
  1141. for (ushort i = 0; i < 16 - textBox_SendData.Text.Length - 1; i++)
  1142. {
  1143. Code[textBox_SendData.Text.Length + 1 + i] = 0x20;
  1144. }
  1145. }
  1146. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x4010, Code);
  1147. richTextBox_Data.Text = "";
  1148. richTextBox_Rev.Text = "";
  1149. }
  1150. /// <summary>
  1151. /// CDL写入SN
  1152. /// </summary>
  1153. /// <param name="sender"></param>
  1154. /// <param name="e"></param>
  1155. private void button_CDL_WriteSN_Click(object sender, EventArgs e)
  1156. {
  1157. var Code = new byte[16];
  1158. if (textBox_SendData.Text.Length > 16)
  1159. {
  1160. MessageBox.Show("Length should be less 16 bytes!", "Notice", MessageBoxButtons.OK);
  1161. return;
  1162. }
  1163. for (ushort i = 0; i < textBox_SendData.Text.Length; i++)
  1164. {
  1165. Code[i] = (byte)textBox_SendData.Text[i];
  1166. }
  1167. if (textBox_SendData.Text.Length < 16)
  1168. {
  1169. Code[textBox_SendData.Text.Length] = (byte)'.';
  1170. for (ushort i = 0; i < 16 - textBox_SendData.Text.Length - 1; i++)
  1171. {
  1172. Code[textBox_SendData.Text.Length + 1 + i] = 0x20;
  1173. }
  1174. }
  1175. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x4110, Code);
  1176. richTextBox_Data.Text = "";
  1177. richTextBox_Rev.Text = "";
  1178. }
  1179. /// <summary>
  1180. /// CDL发送复位
  1181. /// </summary>
  1182. /// <param name="sender"></param>
  1183. /// <param name="e"></param>
  1184. private void button_CDL_Reset_Click(object sender, EventArgs e)
  1185. {
  1186. var Code = new byte[5];
  1187. Code[0] = (byte)'R';
  1188. Code[1] = (byte)'E';
  1189. Code[2] = (byte)'S';
  1190. Code[3] = (byte)'E';
  1191. Code[4] = (byte)'T';
  1192. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x4205, Code);
  1193. richTextBox_Data.Text = "";
  1194. richTextBox_Rev.Text = "";
  1195. }
  1196. /// <summary>
  1197. /// 读取日志
  1198. /// </summary>
  1199. /// <param name="sender"></param>
  1200. /// <param name="e"></param>
  1201. private void button_CDL_ReadLog_Click(object sender, EventArgs e)
  1202. {
  1203. var Code = new byte[8];
  1204. string AddBegin, AddEnd;
  1205. var CodeTemp = new byte[4];
  1206. if (textBox_SendData.Text.Length != 17)
  1207. {
  1208. MessageBox.Show("Length should be 17 bytes!", "Notice", MessageBoxButtons.OK);
  1209. return;
  1210. }
  1211. AddBegin = textBox_SendData.Text.Split(' ')[0];
  1212. AddEnd = textBox_SendData.Text.Split(' ')[1];
  1213. CodeTemp = HexStringToBytes(AddBegin, false);
  1214. for (int i = 0; i < 4; i++)
  1215. Code[i] = CodeTemp[3 - i];
  1216. CodeTemp = HexStringToBytes(AddEnd, false);
  1217. for (int i = 0; i < 4; i++)
  1218. Code[4 + i] = CodeTemp[3 - i];
  1219. SendCmd((ushort)0x712, (byte)0x11, (ushort)0x4308, Code);
  1220. richTextBox_Data.Text = "";
  1221. richTextBox_Rev.Text = "";
  1222. }
  1223. }
  1224. #endregion
  1225. #endregion
  1226. }