Form1.cs 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  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() + " Degree C\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. }));
  364. }
  365. break;
  366. }
  367. case 0x1120://BMS电芯电压
  368. {
  369. unchecked
  370. {
  371. this.Invoke((EventHandler)(delegate
  372. {
  373. for (int i = 0; i < 16; i++)
  374. {
  375. DataTemp = (ushort)(binary_data_1[3 + i * 2] * 256 + binary_data_1[2 + i * 2]);//电压
  376. if (DataTemp == 0)
  377. break;
  378. richTextBox_Data.Text += "电芯" + (i + 1).ToString() + ": " + DataTemp.ToString() + " mV\r\n";
  379. }
  380. }));
  381. }
  382. break;
  383. }
  384. case 0x1204://故障码
  385. {
  386. unchecked
  387. {
  388. this.Invoke((EventHandler)(delegate
  389. {
  390. }));
  391. }
  392. break;
  393. }
  394. case 0x1308://关机指令
  395. {
  396. unchecked
  397. {
  398. this.Invoke((EventHandler)(delegate
  399. {
  400. richTextBox_Data.Text += "接收数据:";
  401. for (int i = 0; i < 8; i++)
  402. richTextBox_Data.Text += ((char)(binary_data_1[2 + i])).ToString();
  403. }));
  404. }
  405. break;
  406. }
  407. case 0x1410://BMS设计信息
  408. {
  409. unchecked
  410. {
  411. this.Invoke((EventHandler)(delegate
  412. {
  413. DataTemp = (ushort)(binary_data_1[3] * 256 + binary_data_1[2]);
  414. richTextBox_Data.Text = "设计容量: " + DataTemp.ToString() + " mAh\r\n";
  415. DataTemp = (ushort)binary_data_1[4];
  416. richTextBox_Data.Text += "设计电压: " + DataTemp.ToString() + " V\r\n";
  417. richTextBox_Data.Text += "电芯型号: ";
  418. for (ushort i = 0; i < 8; i++)
  419. {
  420. richTextBox_Data.Text += ((char)binary_data_1[5 + i]).ToString();
  421. }
  422. richTextBox_Data.Text += "\r\n";
  423. }));
  424. }
  425. break;
  426. }
  427. case 0x1540://BMS版本信息
  428. {
  429. unchecked
  430. {
  431. this.Invoke((EventHandler)(delegate
  432. {
  433. //型号
  434. richTextBox_Data.Text += "型号: ";
  435. for (ushort i = 0; i < 16; i++)
  436. {
  437. richTextBox_Data.Text += ((char)binary_data_1[2 + i]).ToString();
  438. }
  439. richTextBox_Data.Text += "\r\n";
  440. //SN
  441. richTextBox_Data.Text += "序列号: ";
  442. for (ushort i = 0; i < 16; i++)
  443. {
  444. richTextBox_Data.Text += ((char)binary_data_1[18 + i]).ToString();
  445. }
  446. richTextBox_Data.Text += "\r\n";
  447. //HW
  448. richTextBox_Data.Text += "硬件版本: ";
  449. for (ushort i = 0; i < 16; i++)
  450. {
  451. richTextBox_Data.Text += ((char)binary_data_1[34 + i]).ToString();
  452. }
  453. richTextBox_Data.Text += "\r\n";
  454. //FW
  455. richTextBox_Data.Text += "软件版本: ";
  456. for (ushort i = 0; i < 16; i++)
  457. {
  458. richTextBox_Data.Text += ((char)binary_data_1[50 + i]).ToString();
  459. }
  460. richTextBox_Data.Text += "\r\n";
  461. }));
  462. }
  463. break;
  464. }
  465. case 0x160C://BMS物理ID
  466. {
  467. unchecked
  468. {
  469. this.Invoke((EventHandler)(delegate
  470. {
  471. richTextBox_Data.Text += "物理ID: ";
  472. richTextBox_Data.Text += "0x";
  473. for (ushort i = 0; i < 12; i++)
  474. {
  475. richTextBox_Data.Text += Convert.ToString(binary_data_1[2 + i], 16).PadLeft(2, '0').ToUpper();
  476. }
  477. richTextBox_Data.Text += "\r\n";
  478. }));
  479. }
  480. break;
  481. }
  482. case 0x170C://BMS校验码
  483. {
  484. unchecked
  485. {
  486. this.Invoke((EventHandler)(delegate
  487. {
  488. richTextBox_Data.Text += "校验码: ";
  489. richTextBox_Data.Text += "0x";
  490. for (ushort i = 0; i < 12; i++)
  491. {
  492. richTextBox_Data.Text += Convert.ToString(binary_data_1[2 + i], 16).PadLeft(2, '0').ToUpper();
  493. }
  494. richTextBox_Data.Text += "\r\n";
  495. }));
  496. }
  497. break;
  498. }
  499. case 0x3005://MC在线反馈
  500. {
  501. unchecked
  502. {
  503. this.Invoke((EventHandler)(delegate
  504. {
  505. richTextBox_Data.Text += "接收数据:";
  506. for (int i = 0; i < 5; i++)
  507. richTextBox_Data.Text += ((char)(binary_data_1[2 + i])).ToString();
  508. }));
  509. }
  510. break;
  511. }
  512. case 0x5028://电池历史信息
  513. {
  514. unchecked
  515. {
  516. this.Invoke((EventHandler)(delegate
  517. {
  518. Data_Int8 = (sbyte)(binary_data_1[2]);//电芯最高温
  519. richTextBox_Data.Text += "电芯最高温: " + (Data_Int8 - 40).ToString() + " ℃\r\n";
  520. Data_Int8 = (sbyte)(binary_data_1[3]);//电芯最低温
  521. richTextBox_Data.Text += "电芯最低温: " + (Data_Int8 - 40).ToString() + " ℃\r\n";
  522. DataTemp = (ushort)(binary_data_1[5] * 256 + binary_data_1[4]);//最大放电电流
  523. richTextBox_Data.Text += "最大放电电流: " + DataTemp.ToString() + " mA\r\n";
  524. DataTemp = (ushort)(binary_data_1[7] * 256 + binary_data_1[6]);//最大放电电流
  525. richTextBox_Data.Text += "最大充电电流: " + DataTemp.ToString() + " mA\r\n";
  526. DataTemp = (ushort)(binary_data_1[9] * 256 + binary_data_1[8]);//最大放电电流
  527. richTextBox_Data.Text += "循环次数: " + DataTemp.ToString() + " 次\r\n";
  528. DataTemp = (ushort)(binary_data_1[11] * 256 + binary_data_1[10]);//最近充电间隔
  529. richTextBox_Data.Text += "最近充电间隔时间: " + DataTemp.ToString() + " h\r\n";
  530. DataTemp = (ushort)(binary_data_1[13] * 256 + binary_data_1[12]);//最大充电间隔
  531. richTextBox_Data.Text += "最大充电间隔时间: " + DataTemp.ToString() + " h\r\n";
  532. DataTemp = (ushort)(binary_data_1[15] * 256 + binary_data_1[14]);//充电过流保护次数
  533. richTextBox_Data.Text += "充电过流保护次数: " + DataTemp.ToString() + " 次\r\n";
  534. DataTemp = (ushort)(binary_data_1[17] * 256 + binary_data_1[16]);//放电过流保护次数
  535. richTextBox_Data.Text += "放电过流保护次数: " + DataTemp.ToString() + " 次\r\n";
  536. DataTemp = (ushort)(binary_data_1[19] * 256 + binary_data_1[18]);//过充保护次数
  537. richTextBox_Data.Text += "过充保护次数: " + DataTemp.ToString() + " 次\r\n";
  538. DataTemp = (ushort)(binary_data_1[21] * 256 + binary_data_1[20]);//过放保护次数
  539. richTextBox_Data.Text += "过放保护次数: " + DataTemp.ToString() + " 次\r\n";
  540. DataTemp = (ushort)(binary_data_1[23] * 256 + binary_data_1[22]);//短路保护次数
  541. richTextBox_Data.Text += "短路保护次数: " + DataTemp.ToString() + " 次\r\n";
  542. DataTemp = (ushort)(binary_data_1[25] * 256 + binary_data_1[24]);//充电低温保护次数
  543. richTextBox_Data.Text += "充电低温保护次数: " + DataTemp.ToString() + " 次\r\n";
  544. DataTemp = (ushort)(binary_data_1[27] * 256 + binary_data_1[26]);//充电高温保护次数
  545. richTextBox_Data.Text += "充电高温保护次数: " + DataTemp.ToString() + " 次\r\n";
  546. DataTemp = (ushort)(binary_data_1[29] * 256 + binary_data_1[28]);//放电低温保护次数
  547. richTextBox_Data.Text += "放电低温保护次数: " + DataTemp.ToString() + " 次\r\n";
  548. DataTemp = (ushort)(binary_data_1[31] * 256 + binary_data_1[30]);//放电高温保护次数
  549. richTextBox_Data.Text += "放电高温保护次数: " + DataTemp.ToString() + " 次\r\n";
  550. DataTemp32 = (UInt32)((binary_data_1[35] << 24) + (binary_data_1[34]) << 16 + binary_data_1[33] * 256 + binary_data_1[32]);//运行时间
  551. richTextBox_Data.Text += "运行时间: " + DataTemp32.ToString() + " min\r\n";
  552. DataTemp = (ushort)(binary_data_1[36]);//SOH
  553. richTextBox_Data.Text += "剩余寿命: " + DataTemp.ToString() + " %\r\n";
  554. }));
  555. }
  556. break;
  557. }
  558. case 0x5120://电池生产信息
  559. {
  560. unchecked
  561. {
  562. this.Invoke((EventHandler)(delegate
  563. {
  564. richTextBox_Data.Text += "生产商:";
  565. for (int i = 0; i < 8; i++)
  566. richTextBox_Data.Text += ((char)(binary_data_1[2 + i])).ToString();
  567. richTextBox_Data.Text += "\r\n";
  568. richTextBox_Data.Text += "生产地:";
  569. for (int i = 0; i < 8; i++)
  570. richTextBox_Data.Text += ((char)(binary_data_1[10 + 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[18 + i])).ToString();
  575. }));
  576. }
  577. break;
  578. }
  579. case 0x5210://自定义1
  580. {
  581. unchecked
  582. {
  583. this.Invoke((EventHandler)(delegate
  584. {
  585. richTextBox_Data.Text += "接收数据:";
  586. for (int i = 0; i < 16; i++)
  587. richTextBox_Data.Text += ((char)(binary_data_1[2 + i])).ToString();
  588. }));
  589. }
  590. break;
  591. }
  592. case 0x5310://自定义2
  593. {
  594. unchecked
  595. {
  596. this.Invoke((EventHandler)(delegate
  597. {
  598. richTextBox_Data.Text += "接收数据:";
  599. for (int i = 0; i < 16; i++)
  600. richTextBox_Data.Text += ((char)(binary_data_1[2 + i])).ToString();
  601. }));
  602. }
  603. break;
  604. }
  605. case 0x5410://自定义3
  606. {
  607. unchecked
  608. {
  609. this.Invoke((EventHandler)(delegate
  610. {
  611. richTextBox_Data.Text += "接收数据:";
  612. for (int i = 0; i < 16; i++)
  613. richTextBox_Data.Text += ((char)(binary_data_1[2 + i])).ToString();
  614. }));
  615. }
  616. break;
  617. }
  618. case 0x5503://反馈
  619. {
  620. unchecked
  621. {
  622. this.Invoke((EventHandler)(delegate
  623. {
  624. richTextBox_Data.Text += "接收数据:";
  625. for (int i = 0; i < 3; i++)
  626. richTextBox_Data.Text += ((char)(binary_data_1[2 + i])).ToString();
  627. }));
  628. }
  629. break;
  630. }
  631. case 0x5688://日志
  632. {
  633. unchecked
  634. {
  635. this.Invoke((EventHandler)(delegate
  636. {
  637. }));
  638. }
  639. break;
  640. }
  641. default: break;
  642. }
  643. }
  644. #endregion
  645. #region 模拟指令发送
  646. #region MC模拟指令
  647. /// <summary>
  648. /// MC关机就绪
  649. /// </summary>
  650. /// <param name="sender"></param>
  651. /// <param name="e"></param>
  652. private void button_MC_Ready_Click(object sender, EventArgs e)
  653. {
  654. var Code = new byte[5];
  655. Code[0] = (byte)'R';
  656. Code[1] = (byte)'E';
  657. Code[2] = (byte)'A';
  658. Code[3] = (byte)'D';
  659. Code[4] = (byte)'Y';
  660. SendCmd((ushort)0x710, (byte)0x0C, (ushort)0x1305, Code);
  661. richTextBox_Data.Text = "";
  662. richTextBox_Rev.Text = "";
  663. }
  664. /// <summary>
  665. /// MC在线检测
  666. /// </summary>
  667. /// <param name="sender"></param>
  668. /// <param name="e"></param>
  669. private void button_MC_Check_Click(object sender, EventArgs e)
  670. {
  671. var Code = new byte[9];
  672. Code[0] = (byte)'H';
  673. Code[1] = (byte)'A';
  674. Code[2] = (byte)'N';
  675. Code[3] = (byte)'D';
  676. Code[4] = (byte)'S';
  677. Code[5] = (byte)'H';
  678. Code[6] = (byte)'K';
  679. Code[7] = (byte)'A';
  680. Code[8] = (byte)'E';
  681. SendCmd((ushort)0x712, (byte)0x11, (ushort)0x3009, Code);
  682. richTextBox_Data.Text = "";
  683. richTextBox_Rev.Text = "";
  684. }
  685. /// <summary>
  686. /// 查询物理ID
  687. /// </summary>
  688. /// <param name="sender"></param>
  689. /// <param name="e"></param>
  690. private void button_MC_ReadID_Click(object sender, EventArgs e)
  691. {
  692. SendCmd((ushort)0x712, (byte)0x11, (ushort)0x3100, null);
  693. richTextBox_Data.Text = "";
  694. richTextBox_Rev.Text = "";
  695. }
  696. /// <summary>
  697. /// 查询校验码
  698. /// </summary>
  699. /// <param name="sender"></param>
  700. /// <param name="e"></param>
  701. private void button_MC_ReadCheckNum_Click(object sender, EventArgs e)
  702. {
  703. SendCmd((ushort)0x712, (byte)0x11, (ushort)0x3200, null);
  704. richTextBox_Data.Text = "";
  705. richTextBox_Rev.Text = "";
  706. }
  707. /// <summary>
  708. /// 查询设计信息
  709. /// </summary>
  710. /// <param name="sender"></param>
  711. /// <param name="e"></param>
  712. private void button_MC_ReadDesignInfo_Click(object sender, EventArgs e)
  713. {
  714. SendCmd((ushort)0x712, (byte)0x11, (ushort)0x3300, null);
  715. richTextBox_Data.Text = "";
  716. richTextBox_Rev.Text = "";
  717. }
  718. #endregion
  719. #region PBU/OBC模拟指令
  720. /// <summary>
  721. /// OBC关机就绪
  722. /// </summary>
  723. /// <param name="sender"></param>
  724. /// <param name="e"></param>
  725. private void button_OBC_Ready_Click(object sender, EventArgs e)
  726. {
  727. var Code = new byte[5];
  728. Code[0] = (byte)'R';
  729. Code[1] = (byte)'E';
  730. Code[2] = (byte)'A';
  731. Code[3] = (byte)'D';
  732. Code[4] = (byte)'Y';
  733. SendCmd((ushort)0x730, (byte)0x0C, (ushort)0x1405, Code);
  734. richTextBox_Data.Text = "";
  735. richTextBox_Rev.Text = "";
  736. }
  737. /// <summary>
  738. /// OBC查询BMS运行信息
  739. /// </summary>
  740. /// <param name="sender"></param>
  741. /// <param name="e"></param>
  742. private void button_OBC_ReadRunInfo_Click(object sender, EventArgs e)
  743. {
  744. SendCmd((ushort)0x732, (byte)0x11, (ushort)0x5000, null);
  745. richTextBox_Data.Text = "";
  746. richTextBox_Rev.Text = "";
  747. }
  748. /// <summary>
  749. /// OBC查询BMS版本信息
  750. /// </summary>
  751. /// <param name="sender"></param>
  752. /// <param name="e"></param>
  753. private void button_OBC_ReadVer_Click(object sender, EventArgs e)
  754. {
  755. SendCmd((ushort)0x732, (byte)0x11, (ushort)0x5100, null);
  756. richTextBox_Data.Text = "";
  757. richTextBox_Rev.Text = "";
  758. }
  759. /// <summary>
  760. /// OBC查询BMS设计信息
  761. /// </summary>
  762. /// <param name="sender"></param>
  763. /// <param name="e"></param>
  764. private void button_OBC_ReadDesign_Click(object sender, EventArgs e)
  765. {
  766. SendCmd((ushort)0x732, (byte)0x11, (ushort)0x5200, null);
  767. richTextBox_Data.Text = "";
  768. richTextBox_Rev.Text = "";
  769. }
  770. /// <summary>
  771. /// OBC读取BMS电芯电压
  772. /// </summary>
  773. /// <param name="sender"></param>
  774. /// <param name="e"></param>
  775. private void button_OBC_ReadCell_Click(object sender, EventArgs e)
  776. {
  777. SendCmd((ushort)0x732, (byte)0x11, (ushort)0x5300, null);
  778. richTextBox_Data.Text = "";
  779. richTextBox_Rev.Text = "";
  780. }
  781. #endregion
  782. #region HMI模拟指令
  783. /// <summary>
  784. /// HMI关机就绪
  785. /// </summary>
  786. /// <param name="sender"></param>
  787. /// <param name="e"></param>
  788. private void button_HMI_Ready_Click(object sender, EventArgs e)
  789. {
  790. var Code = new byte[5];
  791. Code[0] = (byte)'R';
  792. Code[1] = (byte)'E';
  793. Code[2] = (byte)'A';
  794. Code[3] = (byte)'D';
  795. Code[4] = (byte)'Y';
  796. SendCmd((ushort)0x740, (byte)0x0C, (ushort)0x1305, Code);
  797. richTextBox_Data.Text = "";
  798. richTextBox_Rev.Text = "";
  799. }
  800. /// <summary>
  801. /// HMI读取版本信息
  802. /// </summary>
  803. /// <param name="sender"></param>
  804. /// <param name="e"></param>
  805. private void button_HMI_ReadVerInfo_Click(object sender, EventArgs e)
  806. {
  807. SendCmd((ushort)0x742, (byte)0x11, (ushort)0x5000, null);
  808. richTextBox_Data.Text = "";
  809. richTextBox_Rev.Text = "";
  810. }
  811. /// <summary>
  812. /// HMI读取设计信息
  813. /// </summary>
  814. /// <param name="sender"></param>
  815. /// <param name="e"></param>
  816. private void button_HMI_ReadDesignInfo_Click(object sender, EventArgs e)
  817. {
  818. SendCmd((ushort)0x742, (byte)0x11, (ushort)0x5100, null);
  819. richTextBox_Data.Text = "";
  820. richTextBox_Rev.Text = "";
  821. }
  822. /// <summary>
  823. /// HMI读取电芯电压
  824. /// </summary>
  825. /// <param name="sender"></param>
  826. /// <param name="e"></param>
  827. private void button_HMI_ReadCell_Click(object sender, EventArgs e)
  828. {
  829. SendCmd((ushort)0x742, (byte)0x11, (ushort)0x5200, null);
  830. richTextBox_Data.Text = "";
  831. richTextBox_Rev.Text = "";
  832. }
  833. #endregion
  834. #region 模拟CDL发送指令
  835. /// <summary>
  836. /// CDL查询物理ID
  837. /// </summary>
  838. /// <param name="sender"></param>
  839. /// <param name="e"></param>
  840. private void button_CDL_ReadID_Click(object sender, EventArgs e)
  841. {
  842. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3000, null);
  843. richTextBox_Data.Text = "";
  844. richTextBox_Rev.Text = "";
  845. }
  846. /// <summary>
  847. /// CDL查询校验码
  848. /// </summary>
  849. /// <param name="sender"></param>
  850. /// <param name="e"></param>
  851. private void button_CDL_ReadCheckNum_Click(object sender, EventArgs e)
  852. {
  853. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3100, null);
  854. richTextBox_Data.Text = "";
  855. richTextBox_Rev.Text = "";
  856. }
  857. /// <summary>
  858. /// CDL写入校验码
  859. /// </summary>
  860. /// <param name="sender"></param>
  861. /// <param name="e"></param>
  862. private void button_CDL_WriteCheckNum_Click(object sender, EventArgs e)
  863. {
  864. var Code = new byte[12];
  865. if (HexStringToBytes(textBox_SendData.Text, true).Length !=12)
  866. {
  867. MessageBox.Show("Invalid length of bytes!", "Notice", MessageBoxButtons.OK);
  868. return;
  869. }
  870. Code = HexStringToBytes(textBox_SendData.Text, true);
  871. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x320C, Code);
  872. richTextBox_Data.Text = "";
  873. richTextBox_Rev.Text = "";
  874. }
  875. /// <summary>
  876. /// CDL读取版本信息
  877. /// </summary>
  878. /// <param name="sender"></param>
  879. /// <param name="e"></param>
  880. private void button_CDL_ReadVer_Click(object sender, EventArgs e)
  881. {
  882. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3300, null);
  883. richTextBox_Data.Text = "";
  884. richTextBox_Rev.Text = "";
  885. }
  886. /// <summary>
  887. /// CDL读取运行信息
  888. /// </summary>
  889. /// <param name="sender"></param>
  890. /// <param name="e"></param>
  891. private void button_CDL_ReadRunInfo_Click(object sender, EventArgs e)
  892. {
  893. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3400, null);
  894. richTextBox_Data.Text = "";
  895. richTextBox_Rev.Text = "";
  896. }
  897. /// <summary>
  898. /// CDL读取电芯电压
  899. /// </summary>
  900. /// <param name="sender"></param>
  901. /// <param name="e"></param>
  902. private void button_CDL_ReadCell_Click(object sender, EventArgs e)
  903. {
  904. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3500, null);
  905. richTextBox_Data.Text = "";
  906. richTextBox_Rev.Text = "";
  907. }
  908. /// <summary>
  909. /// CDL读取设计信息
  910. /// </summary>
  911. /// <param name="sender"></param>
  912. /// <param name="e"></param>
  913. private void button_CDL_ReadDesign_Click(object sender, EventArgs e)
  914. {
  915. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3600, null);
  916. richTextBox_Data.Text = "";
  917. richTextBox_Rev.Text = "";
  918. }
  919. /// <summary>
  920. /// CDL读取生产信息
  921. /// </summary>
  922. /// <param name="sender"></param>
  923. /// <param name="e"></param>
  924. private void button_CDL_ReadFac_Click(object sender, EventArgs e)
  925. {
  926. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3700, null);
  927. richTextBox_Data.Text = "";
  928. richTextBox_Rev.Text = "";
  929. }
  930. /// <summary>
  931. /// CDL读取历史信息
  932. /// </summary>
  933. /// <param name="sender"></param>
  934. /// <param name="e"></param>
  935. private void button_CDL_ReadHistory_Click(object sender, EventArgs e)
  936. {
  937. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3800, null);
  938. richTextBox_Data.Text = "";
  939. richTextBox_Rev.Text = "";
  940. }
  941. /// <summary>
  942. /// CDL读取自定义信息1
  943. /// </summary>
  944. /// <param name="sender"></param>
  945. /// <param name="e"></param>
  946. private void button_CDL_ReadUser1_Click(object sender, EventArgs e)
  947. {
  948. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3900, null);
  949. richTextBox_Data.Text = "";
  950. richTextBox_Rev.Text = "";
  951. }
  952. /// <summary>
  953. /// CDL写入自定义信息1
  954. /// </summary>
  955. /// <param name="sender"></param>
  956. /// <param name="e"></param>
  957. private void button_CDL_Write_User1_Click(object sender, EventArgs e)
  958. {
  959. var Code = new byte[16];
  960. if (textBox_SendData.Text.Length > 16)
  961. {
  962. MessageBox.Show("Length should be less 16 bytes!", "Notice", MessageBoxButtons.OK);
  963. return;
  964. }
  965. for (ushort i = 0; i < textBox_SendData.Text.Length; i++)
  966. {
  967. Code[i] = (byte)textBox_SendData.Text[i];
  968. }
  969. if (textBox_SendData.Text.Length < 16)
  970. {
  971. Code[textBox_SendData.Text.Length] = (byte)'.';
  972. for (ushort i = 0; i < 16 - textBox_SendData.Text.Length - 1; i++)
  973. {
  974. Code[textBox_SendData.Text.Length + 1 + i] = 0x20;
  975. }
  976. }
  977. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x3A10, Code);
  978. richTextBox_Data.Text = "";
  979. richTextBox_Rev.Text = "";
  980. }
  981. /// <summary>
  982. /// CDL读取自定义信息2
  983. /// </summary>
  984. /// <param name="sender"></param>
  985. /// <param name="e"></param>
  986. private void button_CDL_ReadUser2_Click(object sender, EventArgs e)
  987. {
  988. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3B00, null);
  989. richTextBox_Data.Text = "";
  990. richTextBox_Rev.Text = "";
  991. }
  992. /// <summary>
  993. /// CDL写入自定义信息2
  994. /// </summary>
  995. /// <param name="sender"></param>
  996. /// <param name="e"></param>
  997. private void button_CDL_Write_User2_Click(object sender, EventArgs e)
  998. {
  999. var Code = new byte[16];
  1000. if (textBox_SendData.Text.Length > 16)
  1001. {
  1002. MessageBox.Show("Length should be less 16 bytes!", "Notice", MessageBoxButtons.OK);
  1003. return;
  1004. }
  1005. for (ushort i = 0; i < textBox_SendData.Text.Length; i++)
  1006. {
  1007. Code[i] = (byte)textBox_SendData.Text[i];
  1008. }
  1009. if (textBox_SendData.Text.Length < 16)
  1010. {
  1011. Code[textBox_SendData.Text.Length] = (byte)'.';
  1012. for (ushort i = 0; i < 16 - textBox_SendData.Text.Length - 1; i++)
  1013. {
  1014. Code[textBox_SendData.Text.Length + 1 + i] = 0x20;
  1015. }
  1016. }
  1017. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x3C10, Code);
  1018. richTextBox_Data.Text = "";
  1019. richTextBox_Rev.Text = "";
  1020. }
  1021. /// <summary>
  1022. /// CDL查询自定义信息3
  1023. /// </summary>
  1024. /// <param name="sender"></param>
  1025. /// <param name="e"></param>
  1026. private void button_CDL_ReadUser3_Click(object sender, EventArgs e)
  1027. {
  1028. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3D00, null);
  1029. richTextBox_Data.Text = "";
  1030. richTextBox_Rev.Text = "";
  1031. }
  1032. /// <summary>
  1033. /// CDL写入自定义信息3
  1034. /// </summary>
  1035. /// <param name="sender"></param>
  1036. /// <param name="e"></param>
  1037. private void button_CDL_Write_User3_Click(object sender, EventArgs e)
  1038. {
  1039. var Code = new byte[16];
  1040. if (textBox_SendData.Text.Length > 16)
  1041. {
  1042. MessageBox.Show("Length should be less 16 bytes!", "Notice", MessageBoxButtons.OK);
  1043. return;
  1044. }
  1045. for (ushort i = 0; i < textBox_SendData.Text.Length; i++)
  1046. {
  1047. Code[i] = (byte)textBox_SendData.Text[i];
  1048. }
  1049. if (textBox_SendData.Text.Length < 16)
  1050. {
  1051. Code[textBox_SendData.Text.Length] = (byte)'.';
  1052. for (ushort i = 0; i < 16 - textBox_SendData.Text.Length - 1; i++)
  1053. {
  1054. Code[textBox_SendData.Text.Length + 1 + i] = 0x20;
  1055. }
  1056. }
  1057. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x3E10, Code);
  1058. richTextBox_Data.Text = "";
  1059. richTextBox_Rev.Text = "";
  1060. }
  1061. /// <summary>
  1062. /// CDL写入生产信息
  1063. /// </summary>
  1064. /// <param name="sender"></param>
  1065. /// <param name="e"></param>
  1066. private void button_CDL_WriteFac_Click(object sender, EventArgs e)
  1067. {
  1068. var Code = new byte[32];
  1069. if (textBox_SendData.Text.Length == 0)
  1070. {
  1071. MessageBox.Show("Invalid length of bytes!", "Notice", MessageBoxButtons.OK);
  1072. return;
  1073. }
  1074. //填入生产商
  1075. for (ushort i = 0; i < 8; i++)
  1076. {
  1077. Code[i] = (byte)textBox_SendData.Text[i];
  1078. }
  1079. if (textBox_SendData.Text.Length < 8)
  1080. {
  1081. Code[textBox_SendData.Text.Length] = (byte)'.';
  1082. for (ushort i = 0; i < 8 - textBox_SendData.Text.Length - 1; i++)
  1083. {
  1084. Code[textBox_SendData.Text.Length + 1 + i] = 0x20;
  1085. }
  1086. }
  1087. //填入生产地
  1088. for (ushort i = 0; i < 8; i++)
  1089. {
  1090. Code[8 + i] = (byte)textBox_SendData.Text[i];
  1091. }
  1092. if (textBox_SendData.Text.Length < 8)
  1093. {
  1094. Code[textBox_SendData.Text.Length + 8] = (byte)'.';
  1095. for (ushort i = 0; i < 8 - textBox_SendData.Text.Length - 1; i++)
  1096. {
  1097. Code[textBox_SendData.Text.Length + 1 + i + 8] = 0x20;
  1098. }
  1099. }
  1100. //填入生产日期
  1101. for (ushort i = 0; i < 8; i++)
  1102. {
  1103. Code[16 + i] = (byte)textBox_SendData.Text[i];
  1104. }
  1105. if (textBox_SendData.Text.Length < 8)
  1106. {
  1107. Code[textBox_SendData.Text.Length + 16] = (byte)'.';
  1108. for (ushort i = 0; i < 8 - textBox_SendData.Text.Length - 1; i++)
  1109. {
  1110. Code[textBox_SendData.Text.Length + 1 + i + 16] = 0x20;
  1111. }
  1112. }
  1113. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x3F20, Code);
  1114. richTextBox_Data.Text = "";
  1115. richTextBox_Rev.Text = "";
  1116. }
  1117. /// <summary>
  1118. /// CDL写入型号
  1119. /// </summary>
  1120. /// <param name="sender"></param>
  1121. /// <param name="e"></param>
  1122. private void button_CDL_WriteModel_Click(object sender, EventArgs e)
  1123. {
  1124. var Code = new byte[16];
  1125. if (textBox_SendData.Text.Length > 16)
  1126. {
  1127. MessageBox.Show("Length should be less 16 bytes!", "Notice", MessageBoxButtons.OK);
  1128. return;
  1129. }
  1130. for (ushort i = 0; i < textBox_SendData.Text.Length; i++)
  1131. {
  1132. Code[i] = (byte)textBox_SendData.Text[i];
  1133. }
  1134. if (textBox_SendData.Text.Length < 16)
  1135. {
  1136. Code[textBox_SendData.Text.Length] = (byte)'.';
  1137. for (ushort i = 0; i < 16 - textBox_SendData.Text.Length - 1; i++)
  1138. {
  1139. Code[textBox_SendData.Text.Length + 1 + i] = 0x20;
  1140. }
  1141. }
  1142. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x4010, Code);
  1143. richTextBox_Data.Text = "";
  1144. richTextBox_Rev.Text = "";
  1145. }
  1146. /// <summary>
  1147. /// CDL写入SN
  1148. /// </summary>
  1149. /// <param name="sender"></param>
  1150. /// <param name="e"></param>
  1151. private void button_CDL_WriteSN_Click(object sender, EventArgs e)
  1152. {
  1153. var Code = new byte[16];
  1154. if (textBox_SendData.Text.Length > 16)
  1155. {
  1156. MessageBox.Show("Length should be less 16 bytes!", "Notice", MessageBoxButtons.OK);
  1157. return;
  1158. }
  1159. for (ushort i = 0; i < textBox_SendData.Text.Length; i++)
  1160. {
  1161. Code[i] = (byte)textBox_SendData.Text[i];
  1162. }
  1163. if (textBox_SendData.Text.Length < 16)
  1164. {
  1165. Code[textBox_SendData.Text.Length] = (byte)'.';
  1166. for (ushort i = 0; i < 16 - textBox_SendData.Text.Length - 1; i++)
  1167. {
  1168. Code[textBox_SendData.Text.Length + 1 + i] = 0x20;
  1169. }
  1170. }
  1171. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x4110, Code);
  1172. richTextBox_Data.Text = "";
  1173. richTextBox_Rev.Text = "";
  1174. }
  1175. /// <summary>
  1176. /// CDL发送复位
  1177. /// </summary>
  1178. /// <param name="sender"></param>
  1179. /// <param name="e"></param>
  1180. private void button_CDL_Reset_Click(object sender, EventArgs e)
  1181. {
  1182. var Code = new byte[5];
  1183. Code[0] = (byte)'R';
  1184. Code[1] = (byte)'E';
  1185. Code[2] = (byte)'S';
  1186. Code[3] = (byte)'E';
  1187. Code[4] = (byte)'T';
  1188. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x4205, Code);
  1189. richTextBox_Data.Text = "";
  1190. richTextBox_Rev.Text = "";
  1191. }
  1192. /// <summary>
  1193. /// 读取日志
  1194. /// </summary>
  1195. /// <param name="sender"></param>
  1196. /// <param name="e"></param>
  1197. private void button_CDL_ReadLog_Click(object sender, EventArgs e)
  1198. {
  1199. var Code = new byte[8];
  1200. string AddBegin, AddEnd;
  1201. var CodeTemp = new byte[4];
  1202. if (textBox_SendData.Text.Length != 17)
  1203. {
  1204. MessageBox.Show("Length should be 17 bytes!", "Notice", MessageBoxButtons.OK);
  1205. return;
  1206. }
  1207. AddBegin = textBox_SendData.Text.Split(' ')[0];
  1208. AddEnd = textBox_SendData.Text.Split(' ')[1];
  1209. CodeTemp = HexStringToBytes(AddBegin, false);
  1210. for (int i = 0; i < 4; i++)
  1211. Code[i] = CodeTemp[3 - i];
  1212. CodeTemp = HexStringToBytes(AddEnd, false);
  1213. for (int i = 0; i < 4; i++)
  1214. Code[4 + i] = CodeTemp[3 - i];
  1215. SendCmd((ushort)0x712, (byte)0x11, (ushort)0x4308, Code);
  1216. richTextBox_Data.Text = "";
  1217. richTextBox_Rev.Text = "";
  1218. }
  1219. }
  1220. #endregion
  1221. #endregion
  1222. }