Form1.cs 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  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. byte Data_Uint8;
  328. UInt32 DataTemp32;
  329. textBox_RevCnt.Text = Convert.ToString(Convert.ToInt16(textBox_RevCnt.Text) + 1);
  330. richTextBox_Rev.Text = "";
  331. richTextBox_Data.Text = "";
  332. //显示接收的数据
  333. 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";
  334. richTextBox_Rev.Text += "DATA: ";
  335. for (int i = 0; i < binary_data_1[1]; i++)
  336. {
  337. richTextBox_Rev.Text += Convert.ToString(binary_data_1[2 + i], 16).PadLeft(2, '0').ToUpper() + " ";
  338. }
  339. //数据解析
  340. switch (CmdTemp)
  341. {
  342. case 0x1010://BMS运行信息
  343. {
  344. unchecked
  345. {
  346. this.Invoke((EventHandler)(delegate
  347. {
  348. DataTemp = (ushort)(binary_data_1[3] * 256 + binary_data_1[2]);//电压
  349. richTextBox_Data.Text = "母线电压: " + DataTemp.ToString() + " mV\r\n";
  350. Data_Temp_Int = (short)(binary_data_1[5] * 256 + binary_data_1[4]);//电流
  351. richTextBox_Data.Text += "母线电流: " + Data_Temp_Int.ToString() + " mA\r\n";
  352. DataTemp = (ushort)(binary_data_1[7] * 256 + binary_data_1[6]);//剩余容量
  353. richTextBox_Data.Text += "剩余容量: " + DataTemp.ToString() + " mAh\r\n";
  354. DataTemp = (ushort)(binary_data_1[9] * 256 + binary_data_1[8]);//满充容量
  355. richTextBox_Data.Text += "满充容量: " + DataTemp.ToString() + " mAh\r\n";
  356. Data_Temp_Int = (short)binary_data_1[10];//电芯温度
  357. richTextBox_Data.Text += "电芯温度: " + (Data_Temp_Int - 40).ToString() + " ℃\r\n";
  358. DataTemp = (ushort)binary_data_1[11];//剩余电量
  359. richTextBox_Data.Text += "剩余电量: " + DataTemp.ToString() + " %\r\n";
  360. DataTemp = (ushort)binary_data_1[12];//运行状态
  361. richTextBox_Data.Text += "运行状态: " + "0x" + Convert.ToString(DataTemp, 16).PadLeft(2, '0').ToUpper() + "\r\n";
  362. DataTemp = (ushort)binary_data_1[13];//SOH
  363. richTextBox_Data.Text += "剩余寿命: " + DataTemp.ToString() + " %\r\n";
  364. DataTemp = (ushort)(binary_data_1[15] * 256 + binary_data_1[14]);//循环次数
  365. richTextBox_Data.Text += "循环次数: " + DataTemp.ToString() + " \r\n";
  366. DataTemp = (ushort)(binary_data_1[17] * 256 + binary_data_1[16]);//剩余充电时间
  367. richTextBox_Data.Text += "剩余充电时间: " + DataTemp.ToString() + " min\r\n";
  368. }));
  369. }
  370. break;
  371. }
  372. case 0x1120://BMS电芯电压
  373. {
  374. unchecked
  375. {
  376. this.Invoke((EventHandler)(delegate
  377. {
  378. for (int i = 0; i < 16; i++)
  379. {
  380. DataTemp = (ushort)(binary_data_1[3 + i * 2] * 256 + binary_data_1[2 + i * 2]);//电压
  381. if (DataTemp == 0)
  382. break;
  383. richTextBox_Data.Text += "电芯" + (i + 1).ToString() + ": " + DataTemp.ToString() + " mV\r\n";
  384. }
  385. }));
  386. }
  387. break;
  388. }
  389. case 0x1204://故障码
  390. {
  391. unchecked
  392. {
  393. this.Invoke((EventHandler)(delegate
  394. {
  395. }));
  396. }
  397. break;
  398. }
  399. case 0x1308://关机指令
  400. {
  401. unchecked
  402. {
  403. this.Invoke((EventHandler)(delegate
  404. {
  405. richTextBox_Data.Text += "接收数据:";
  406. for (int i = 0; i < 8; i++)
  407. richTextBox_Data.Text += ((char)(binary_data_1[2 + i])).ToString();
  408. }));
  409. }
  410. break;
  411. }
  412. case 0x1410://BMS设计信息
  413. {
  414. unchecked
  415. {
  416. this.Invoke((EventHandler)(delegate
  417. {
  418. DataTemp = (ushort)(binary_data_1[3] * 256 + binary_data_1[2]);
  419. richTextBox_Data.Text = "设计容量: " + DataTemp.ToString() + " mAh\r\n";
  420. DataTemp = (ushort)binary_data_1[4];
  421. richTextBox_Data.Text += "设计电压: " + DataTemp.ToString() + " V\r\n";
  422. richTextBox_Data.Text += "电芯型号: ";
  423. for (ushort i = 0; i < 8; i++)
  424. {
  425. richTextBox_Data.Text += ((char)binary_data_1[5 + i]).ToString();
  426. }
  427. richTextBox_Data.Text += "\r\n";
  428. }));
  429. }
  430. break;
  431. }
  432. case 0x1540://BMS版本信息
  433. {
  434. unchecked
  435. {
  436. this.Invoke((EventHandler)(delegate
  437. {
  438. //型号
  439. richTextBox_Data.Text += "型号: ";
  440. for (ushort i = 0; i < 16; i++)
  441. {
  442. richTextBox_Data.Text += ((char)binary_data_1[2 + i]).ToString();
  443. }
  444. richTextBox_Data.Text += "\r\n";
  445. //SN
  446. richTextBox_Data.Text += "序列号: ";
  447. for (ushort i = 0; i < 16; i++)
  448. {
  449. richTextBox_Data.Text += ((char)binary_data_1[18 + i]).ToString();
  450. }
  451. richTextBox_Data.Text += "\r\n";
  452. //HW
  453. richTextBox_Data.Text += "硬件版本: ";
  454. for (ushort i = 0; i < 16; i++)
  455. {
  456. richTextBox_Data.Text += ((char)binary_data_1[34 + i]).ToString();
  457. }
  458. richTextBox_Data.Text += "\r\n";
  459. //FW
  460. richTextBox_Data.Text += "软件版本: ";
  461. for (ushort i = 0; i < 16; i++)
  462. {
  463. richTextBox_Data.Text += ((char)binary_data_1[50 + i]).ToString();
  464. }
  465. richTextBox_Data.Text += "\r\n";
  466. }));
  467. }
  468. break;
  469. }
  470. case 0x160C://BMS物理ID
  471. {
  472. unchecked
  473. {
  474. this.Invoke((EventHandler)(delegate
  475. {
  476. richTextBox_Data.Text += "物理ID: ";
  477. richTextBox_Data.Text += "0x";
  478. for (ushort i = 0; i < 12; i++)
  479. {
  480. richTextBox_Data.Text += Convert.ToString(binary_data_1[2 + i], 16).PadLeft(2, '0').ToUpper();
  481. }
  482. richTextBox_Data.Text += "\r\n";
  483. }));
  484. }
  485. break;
  486. }
  487. case 0x170C://BMS校验码
  488. {
  489. unchecked
  490. {
  491. this.Invoke((EventHandler)(delegate
  492. {
  493. richTextBox_Data.Text += "校验码: ";
  494. richTextBox_Data.Text += "0x";
  495. for (ushort i = 0; i < 12; i++)
  496. {
  497. richTextBox_Data.Text += Convert.ToString(binary_data_1[2 + i], 16).PadLeft(2, '0').ToUpper();
  498. }
  499. richTextBox_Data.Text += "\r\n";
  500. }));
  501. }
  502. break;
  503. }
  504. case 0x1810://用户使用记录
  505. {
  506. unchecked
  507. {
  508. this.Invoke((EventHandler)(delegate
  509. {
  510. Data_Uint8 = (binary_data_1[2]);//电芯最高温
  511. richTextBox_Data.Text += "电芯最高温: " +((Int16)(Data_Uint8 - 40)).ToString() + " ℃\r\n";
  512. Data_Uint8 = (binary_data_1[3]);//电芯最低温
  513. richTextBox_Data.Text += "电芯最低温: " + ((Int16)(Data_Uint8 - 40)).ToString() + " ℃\r\n";
  514. DataTemp = (ushort)(binary_data_1[5] * 256 + binary_data_1[4]);//最近充电间隔
  515. richTextBox_Data.Text += "最近充电间隔时间: " + DataTemp.ToString() + " h\r\n";
  516. DataTemp = (ushort)(binary_data_1[7] * 256 + binary_data_1[6]);//最大充电间隔
  517. richTextBox_Data.Text += "最大充电间隔时间: " + DataTemp.ToString() + " h\r\n";
  518. }));
  519. }
  520. break;
  521. }
  522. case 0x1928://补充信息
  523. {
  524. unchecked
  525. {
  526. this.Invoke((EventHandler)(delegate
  527. {
  528. //初次使用时间
  529. richTextBox_Data.Text += "初次使用时间: ";
  530. for (ushort i = 0; i < 8; i++)
  531. {
  532. richTextBox_Data.Text += ((char)binary_data_1[2 + i]).ToString();
  533. }
  534. richTextBox_Data.Text += "\r\n";
  535. //能量吞吐量
  536. DataTemp = (ushort)(binary_data_1[11] * 256 + binary_data_1[10]);
  537. richTextBox_Data.Text += "能量吞吐量: " + ((float)DataTemp / 10f).ToString("0.0") + " kWh\r\n";
  538. //容量吞吐量
  539. DataTemp = (ushort)(binary_data_1[13] * 256 + binary_data_1[12]);
  540. richTextBox_Data.Text += "容量吞吐量: " + DataTemp.ToString() + " Ah\r\n";
  541. //深度放电次数
  542. DataTemp = (ushort)(binary_data_1[15] * 256 + binary_data_1[14]);
  543. richTextBox_Data.Text += "深度放电次数: " + DataTemp.ToString() + " 次\r\n";
  544. //极端低温充电累计时间
  545. DataTemp = (ushort)(binary_data_1[17] * 256 + binary_data_1[16]);
  546. richTextBox_Data.Text += "极端低温充电累计时间: " + DataTemp.ToString() + " h\r\n";
  547. //极端低温放电累计时间
  548. DataTemp = (ushort)(binary_data_1[19] * 256 + binary_data_1[18]);
  549. richTextBox_Data.Text += "极端低温放电累计时间: " + DataTemp.ToString() + " h\r\n";
  550. //极端高温充电累计时间
  551. DataTemp = (ushort)(binary_data_1[21] * 256 + binary_data_1[20]);
  552. richTextBox_Data.Text += "极端高温充电累计时间: " + DataTemp.ToString() + " h\r\n";
  553. //极端高温放电累计时间
  554. DataTemp = (ushort)(binary_data_1[23] * 256 + binary_data_1[22]);
  555. richTextBox_Data.Text += "极端高温放电累计时间: " + DataTemp.ToString() + " h\r\n";
  556. //自放电率
  557. DataTemp = (ushort)(binary_data_1[25] * 256 + binary_data_1[24]);
  558. richTextBox_Data.Text += "自放电率: " + DataTemp.ToString() + " mV/d\r\n";
  559. //能量往返效率
  560. DataTemp = (ushort)(binary_data_1[27] * 256 + binary_data_1[26]);
  561. richTextBox_Data.Text += "能量往返效率: " + DataTemp.ToString() + " %\r\n";
  562. //内阻
  563. DataTemp = (ushort)(binary_data_1[29] * 256 + binary_data_1[28]);
  564. richTextBox_Data.Text += "内阻: " + DataTemp.ToString() + " mΩ\r\n";
  565. //剩余功率能力
  566. DataTemp = (ushort)(binary_data_1[31] * 256 + binary_data_1[30]);
  567. richTextBox_Data.Text += "剩余功率能力: " + DataTemp.ToString() + " W\r\n";
  568. }));
  569. }
  570. break;
  571. }
  572. case 0x3005://MC在线反馈
  573. {
  574. unchecked
  575. {
  576. this.Invoke((EventHandler)(delegate
  577. {
  578. richTextBox_Data.Text += "接收数据:";
  579. for (int i = 0; i < 5; i++)
  580. richTextBox_Data.Text += ((char)(binary_data_1[2 + i])).ToString();
  581. }));
  582. }
  583. break;
  584. }
  585. case 0x5028://电池历史信息
  586. {
  587. unchecked
  588. {
  589. this.Invoke((EventHandler)(delegate
  590. {
  591. Data_Int8 = (sbyte)(binary_data_1[2]);//电芯最高温
  592. richTextBox_Data.Text += "电芯最高温: " + (Data_Int8 - 40).ToString() + " ℃\r\n";
  593. Data_Int8 = (sbyte)(binary_data_1[3]);//电芯最低温
  594. richTextBox_Data.Text += "电芯最低温: " + (Data_Int8 - 40).ToString() + " ℃\r\n";
  595. DataTemp = (ushort)(binary_data_1[5] * 256 + binary_data_1[4]);//最大放电电流
  596. richTextBox_Data.Text += "最大放电电流: " + DataTemp.ToString() + " mA\r\n";
  597. DataTemp = (ushort)(binary_data_1[7] * 256 + binary_data_1[6]);//最大放电电流
  598. richTextBox_Data.Text += "最大充电电流: " + DataTemp.ToString() + " mA\r\n";
  599. DataTemp = (ushort)(binary_data_1[9] * 256 + binary_data_1[8]);//最大放电电流
  600. richTextBox_Data.Text += "循环次数: " + DataTemp.ToString() + " 次\r\n";
  601. DataTemp = (ushort)(binary_data_1[11] * 256 + binary_data_1[10]);//最近充电间隔
  602. richTextBox_Data.Text += "最近充电间隔时间: " + DataTemp.ToString() + " h\r\n";
  603. DataTemp = (ushort)(binary_data_1[13] * 256 + binary_data_1[12]);//最大充电间隔
  604. richTextBox_Data.Text += "最大充电间隔时间: " + DataTemp.ToString() + " h\r\n";
  605. DataTemp = (ushort)(binary_data_1[15] * 256 + binary_data_1[14]);//充电过流保护次数
  606. richTextBox_Data.Text += "充电过流保护次数: " + DataTemp.ToString() + " 次\r\n";
  607. DataTemp = (ushort)(binary_data_1[17] * 256 + binary_data_1[16]);//放电过流保护次数
  608. richTextBox_Data.Text += "放电过流保护次数: " + DataTemp.ToString() + " 次\r\n";
  609. DataTemp = (ushort)(binary_data_1[19] * 256 + binary_data_1[18]);//过充保护次数
  610. richTextBox_Data.Text += "过充保护次数: " + DataTemp.ToString() + " 次\r\n";
  611. DataTemp = (ushort)(binary_data_1[21] * 256 + binary_data_1[20]);//过放保护次数
  612. richTextBox_Data.Text += "过放保护次数: " + DataTemp.ToString() + " 次\r\n";
  613. DataTemp = (ushort)(binary_data_1[23] * 256 + binary_data_1[22]);//短路保护次数
  614. richTextBox_Data.Text += "短路保护次数: " + DataTemp.ToString() + " 次\r\n";
  615. DataTemp = (ushort)(binary_data_1[25] * 256 + binary_data_1[24]);//充电低温保护次数
  616. richTextBox_Data.Text += "充电低温保护次数: " + DataTemp.ToString() + " 次\r\n";
  617. DataTemp = (ushort)(binary_data_1[27] * 256 + binary_data_1[26]);//充电高温保护次数
  618. richTextBox_Data.Text += "充电高温保护次数: " + DataTemp.ToString() + " 次\r\n";
  619. DataTemp = (ushort)(binary_data_1[29] * 256 + binary_data_1[28]);//放电低温保护次数
  620. richTextBox_Data.Text += "放电低温保护次数: " + DataTemp.ToString() + " 次\r\n";
  621. DataTemp = (ushort)(binary_data_1[31] * 256 + binary_data_1[30]);//放电高温保护次数
  622. richTextBox_Data.Text += "放电高温保护次数: " + DataTemp.ToString() + " 次\r\n";
  623. DataTemp32 = (UInt32)((binary_data_1[35] << 24) + (binary_data_1[34] << 16) + (binary_data_1[33] <<8) + binary_data_1[32]);//运行时间
  624. richTextBox_Data.Text += "运行时间: " + DataTemp32.ToString() + " min\r\n";
  625. DataTemp = (ushort)(binary_data_1[36]);//SOH
  626. richTextBox_Data.Text += "剩余寿命: " + DataTemp.ToString() + " %\r\n";
  627. }));
  628. }
  629. break;
  630. }
  631. case 0x5120://电池生产信息
  632. {
  633. unchecked
  634. {
  635. this.Invoke((EventHandler)(delegate
  636. {
  637. richTextBox_Data.Text += "生产商:";
  638. for (int i = 0; i < 8; i++)
  639. richTextBox_Data.Text += ((char)(binary_data_1[2 + i])).ToString();
  640. richTextBox_Data.Text += "\r\n";
  641. richTextBox_Data.Text += "生产地:";
  642. for (int i = 0; i < 8; i++)
  643. richTextBox_Data.Text += ((char)(binary_data_1[10 + i])).ToString();
  644. richTextBox_Data.Text += "\r\n";
  645. richTextBox_Data.Text += "生产日期:";
  646. for (int i = 0; i < 8; i++)
  647. richTextBox_Data.Text += ((char)(binary_data_1[18 + i])).ToString();
  648. }));
  649. }
  650. break;
  651. }
  652. case 0x5210://自定义1
  653. {
  654. unchecked
  655. {
  656. this.Invoke((EventHandler)(delegate
  657. {
  658. richTextBox_Data.Text += "接收数据:";
  659. for (int i = 0; i < 16; i++)
  660. richTextBox_Data.Text += ((char)(binary_data_1[2 + i])).ToString();
  661. }));
  662. }
  663. break;
  664. }
  665. case 0x5310://自定义2
  666. {
  667. unchecked
  668. {
  669. this.Invoke((EventHandler)(delegate
  670. {
  671. richTextBox_Data.Text += "接收数据:";
  672. for (int i = 0; i < 16; i++)
  673. richTextBox_Data.Text += ((char)(binary_data_1[2 + i])).ToString();
  674. }));
  675. }
  676. break;
  677. }
  678. case 0x5410://自定义3
  679. {
  680. unchecked
  681. {
  682. this.Invoke((EventHandler)(delegate
  683. {
  684. richTextBox_Data.Text += "接收数据:";
  685. for (int i = 0; i < 16; i++)
  686. richTextBox_Data.Text += ((char)(binary_data_1[2 + i])).ToString();
  687. }));
  688. }
  689. break;
  690. }
  691. case 0x5503://反馈
  692. {
  693. unchecked
  694. {
  695. this.Invoke((EventHandler)(delegate
  696. {
  697. richTextBox_Data.Text += "接收数据:";
  698. for (int i = 0; i < 3; i++)
  699. richTextBox_Data.Text += ((char)(binary_data_1[2 + i])).ToString();
  700. }));
  701. }
  702. break;
  703. }
  704. case 0x5688://日志
  705. {
  706. unchecked
  707. {
  708. this.Invoke((EventHandler)(delegate
  709. {
  710. }));
  711. }
  712. break;
  713. }
  714. case 0x5720://产品条码
  715. {
  716. unchecked
  717. {
  718. this.Invoke((EventHandler)(delegate
  719. {
  720. richTextBox_Data.Text += "接收数据:";
  721. for (int i = 0; i < 32; i++)
  722. richTextBox_Data.Text += ((char)(binary_data_1[2 + i])).ToString();
  723. }));
  724. }
  725. break;
  726. }
  727. case 0x5808://故障日志地址
  728. {
  729. unchecked
  730. {
  731. this.Invoke((EventHandler)(delegate
  732. {
  733. uint Addr_Begin = 0, Addr_End = 0;
  734. Addr_Begin = (uint)((binary_data_1[2] << 24) + (binary_data_1[3] << 16) + (binary_data_1[4] << 8) + binary_data_1[5]);
  735. Addr_End = (uint)((binary_data_1[6] << 24) + (binary_data_1[7] << 16) + (binary_data_1[8] << 8) + binary_data_1[9]);
  736. richTextBox_Data.Text += "起始地址:0x";
  737. richTextBox_Data.Text += Convert.ToString(Addr_Begin, 16).PadLeft(8, '0').ToUpper() + "\r\n";
  738. richTextBox_Data.Text += "结束数据:0x";
  739. richTextBox_Data.Text += Convert.ToString(Addr_End, 16).PadLeft(8, '0').ToUpper();
  740. }));
  741. }
  742. break;
  743. }
  744. default: break;
  745. }
  746. }
  747. #endregion
  748. #region 模拟指令发送
  749. #region MC模拟指令
  750. /// <summary>
  751. /// MC关机就绪
  752. /// </summary>
  753. /// <param name="sender"></param>
  754. /// <param name="e"></param>
  755. private void button_MC_Ready_Click(object sender, EventArgs e)
  756. {
  757. var Code = new byte[5];
  758. Code[0] = (byte)'R';
  759. Code[1] = (byte)'E';
  760. Code[2] = (byte)'A';
  761. Code[3] = (byte)'D';
  762. Code[4] = (byte)'Y';
  763. SendCmd((ushort)0x710, (byte)0x0C, (ushort)0x1305, Code);
  764. richTextBox_Data.Text = "";
  765. richTextBox_Rev.Text = "";
  766. }
  767. /// <summary>
  768. /// MC在线检测
  769. /// </summary>
  770. /// <param name="sender"></param>
  771. /// <param name="e"></param>
  772. private void button_MC_Check_Click(object sender, EventArgs e)
  773. {
  774. var Code = new byte[9];
  775. Code[0] = (byte)'H';
  776. Code[1] = (byte)'A';
  777. Code[2] = (byte)'N';
  778. Code[3] = (byte)'D';
  779. Code[4] = (byte)'S';
  780. Code[5] = (byte)'H';
  781. Code[6] = (byte)'K';
  782. Code[7] = (byte)'A';
  783. Code[8] = (byte)'E';
  784. SendCmd((ushort)0x712, (byte)0x11, (ushort)0x3009, Code);
  785. richTextBox_Data.Text = "";
  786. richTextBox_Rev.Text = "";
  787. }
  788. /// <summary>
  789. /// 查询物理ID
  790. /// </summary>
  791. /// <param name="sender"></param>
  792. /// <param name="e"></param>
  793. private void button_MC_ReadID_Click(object sender, EventArgs e)
  794. {
  795. SendCmd((ushort)0x712, (byte)0x11, (ushort)0x3100, null);
  796. richTextBox_Data.Text = "";
  797. richTextBox_Rev.Text = "";
  798. }
  799. /// <summary>
  800. /// 查询校验码
  801. /// </summary>
  802. /// <param name="sender"></param>
  803. /// <param name="e"></param>
  804. private void button_MC_ReadCheckNum_Click(object sender, EventArgs e)
  805. {
  806. SendCmd((ushort)0x712, (byte)0x11, (ushort)0x3200, null);
  807. richTextBox_Data.Text = "";
  808. richTextBox_Rev.Text = "";
  809. }
  810. /// <summary>
  811. /// 查询设计信息
  812. /// </summary>
  813. /// <param name="sender"></param>
  814. /// <param name="e"></param>
  815. private void button_MC_ReadDesignInfo_Click(object sender, EventArgs e)
  816. {
  817. SendCmd((ushort)0x712, (byte)0x11, (ushort)0x3300, null);
  818. richTextBox_Data.Text = "";
  819. richTextBox_Rev.Text = "";
  820. }
  821. #endregion
  822. #region PBU/OBC模拟指令
  823. /// <summary>
  824. /// OBC关机就绪
  825. /// </summary>
  826. /// <param name="sender"></param>
  827. /// <param name="e"></param>
  828. private void button_OBC_Ready_Click(object sender, EventArgs e)
  829. {
  830. var Code = new byte[5];
  831. Code[0] = (byte)'R';
  832. Code[1] = (byte)'E';
  833. Code[2] = (byte)'A';
  834. Code[3] = (byte)'D';
  835. Code[4] = (byte)'Y';
  836. SendCmd((ushort)0x730, (byte)0x0C, (ushort)0x1405, Code);
  837. richTextBox_Data.Text = "";
  838. richTextBox_Rev.Text = "";
  839. }
  840. /// <summary>
  841. /// OBC查询BMS运行信息
  842. /// </summary>
  843. /// <param name="sender"></param>
  844. /// <param name="e"></param>
  845. private void button_OBC_ReadRunInfo_Click(object sender, EventArgs e)
  846. {
  847. SendCmd((ushort)0x732, (byte)0x11, (ushort)0x5000, null);
  848. richTextBox_Data.Text = "";
  849. richTextBox_Rev.Text = "";
  850. }
  851. /// <summary>
  852. /// OBC查询BMS版本信息
  853. /// </summary>
  854. /// <param name="sender"></param>
  855. /// <param name="e"></param>
  856. private void button_OBC_ReadVer_Click(object sender, EventArgs e)
  857. {
  858. SendCmd((ushort)0x732, (byte)0x11, (ushort)0x5100, null);
  859. richTextBox_Data.Text = "";
  860. richTextBox_Rev.Text = "";
  861. }
  862. /// <summary>
  863. /// OBC查询BMS设计信息
  864. /// </summary>
  865. /// <param name="sender"></param>
  866. /// <param name="e"></param>
  867. private void button_OBC_ReadDesign_Click(object sender, EventArgs e)
  868. {
  869. SendCmd((ushort)0x732, (byte)0x11, (ushort)0x5200, null);
  870. richTextBox_Data.Text = "";
  871. richTextBox_Rev.Text = "";
  872. }
  873. /// <summary>
  874. /// OBC读取BMS电芯电压
  875. /// </summary>
  876. /// <param name="sender"></param>
  877. /// <param name="e"></param>
  878. private void button_OBC_ReadCell_Click(object sender, EventArgs e)
  879. {
  880. SendCmd((ushort)0x732, (byte)0x11, (ushort)0x5300, null);
  881. richTextBox_Data.Text = "";
  882. richTextBox_Rev.Text = "";
  883. }
  884. #endregion
  885. /// <summary>
  886. /// OBC读取使用记录
  887. /// </summary>
  888. /// <param name="sender"></param>
  889. /// <param name="e"></param>
  890. private void button_OBC_ReadLog_Click(object sender, EventArgs e)
  891. {
  892. SendCmd((ushort)0x732, (byte)0x11, (ushort)0x5400, null);
  893. richTextBox_Data.Text = "";
  894. richTextBox_Rev.Text = "";
  895. }
  896. #region HMI模拟指令
  897. /// <summary>
  898. /// HMI关机就绪
  899. /// </summary>
  900. /// <param name="sender"></param>
  901. /// <param name="e"></param>
  902. private void button_HMI_Ready_Click(object sender, EventArgs e)
  903. {
  904. var Code = new byte[5];
  905. Code[0] = (byte)'R';
  906. Code[1] = (byte)'E';
  907. Code[2] = (byte)'A';
  908. Code[3] = (byte)'D';
  909. Code[4] = (byte)'Y';
  910. SendCmd((ushort)0x740, (byte)0x0C, (ushort)0x1305, Code);
  911. richTextBox_Data.Text = "";
  912. richTextBox_Rev.Text = "";
  913. }
  914. /// <summary>
  915. /// HMI读取版本信息
  916. /// </summary>
  917. /// <param name="sender"></param>
  918. /// <param name="e"></param>
  919. private void button_HMI_ReadVerInfo_Click(object sender, EventArgs e)
  920. {
  921. SendCmd((ushort)0x742, (byte)0x11, (ushort)0x5000, null);
  922. richTextBox_Data.Text = "";
  923. richTextBox_Rev.Text = "";
  924. }
  925. /// <summary>
  926. /// HMI读取设计信息
  927. /// </summary>
  928. /// <param name="sender"></param>
  929. /// <param name="e"></param>
  930. private void button_HMI_ReadDesignInfo_Click(object sender, EventArgs e)
  931. {
  932. SendCmd((ushort)0x742, (byte)0x11, (ushort)0x5100, null);
  933. richTextBox_Data.Text = "";
  934. richTextBox_Rev.Text = "";
  935. }
  936. /// <summary>
  937. /// HMI读取电芯电压
  938. /// </summary>
  939. /// <param name="sender"></param>
  940. /// <param name="e"></param>
  941. private void button_HMI_ReadCell_Click(object sender, EventArgs e)
  942. {
  943. SendCmd((ushort)0x742, (byte)0x11, (ushort)0x5200, null);
  944. richTextBox_Data.Text = "";
  945. richTextBox_Rev.Text = "";
  946. }
  947. /// <summary>
  948. ///
  949. /// </summary>
  950. /// <param name="sender"></param>
  951. /// <param name="e"></param>
  952. private void button_HMI_ReadLog_Click(object sender, EventArgs e)
  953. {
  954. SendCmd((ushort)0x742, (byte)0x11, (ushort)0x5300, null);
  955. richTextBox_Data.Text = "";
  956. richTextBox_Rev.Text = "";
  957. }
  958. #endregion
  959. #region 模拟CDL发送指令
  960. /// <summary>
  961. /// CDL查询物理ID
  962. /// </summary>
  963. /// <param name="sender"></param>
  964. /// <param name="e"></param>
  965. private void button_CDL_ReadID_Click(object sender, EventArgs e)
  966. {
  967. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3000, null);
  968. richTextBox_Data.Text = "";
  969. richTextBox_Rev.Text = "";
  970. }
  971. /// <summary>
  972. /// CDL查询校验码
  973. /// </summary>
  974. /// <param name="sender"></param>
  975. /// <param name="e"></param>
  976. private void button_CDL_ReadCheckNum_Click(object sender, EventArgs e)
  977. {
  978. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3100, null);
  979. richTextBox_Data.Text = "";
  980. richTextBox_Rev.Text = "";
  981. }
  982. /// <summary>
  983. /// CDL写入校验码
  984. /// </summary>
  985. /// <param name="sender"></param>
  986. /// <param name="e"></param>
  987. private void button_CDL_WriteCheckNum_Click(object sender, EventArgs e)
  988. {
  989. var Code = new byte[12];
  990. if (HexStringToBytes(textBox_SendData.Text, true).Length !=12)
  991. {
  992. MessageBox.Show("Invalid length of bytes!", "Notice", MessageBoxButtons.OK);
  993. return;
  994. }
  995. Code = HexStringToBytes(textBox_SendData.Text, true);
  996. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x320C, Code);
  997. richTextBox_Data.Text = "";
  998. richTextBox_Rev.Text = "";
  999. }
  1000. /// <summary>
  1001. /// CDL读取版本信息
  1002. /// </summary>
  1003. /// <param name="sender"></param>
  1004. /// <param name="e"></param>
  1005. private void button_CDL_ReadVer_Click(object sender, EventArgs e)
  1006. {
  1007. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3300, null);
  1008. richTextBox_Data.Text = "";
  1009. richTextBox_Rev.Text = "";
  1010. }
  1011. /// <summary>
  1012. /// CDL读取运行信息
  1013. /// </summary>
  1014. /// <param name="sender"></param>
  1015. /// <param name="e"></param>
  1016. private void button_CDL_ReadRunInfo_Click(object sender, EventArgs e)
  1017. {
  1018. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3400, null);
  1019. richTextBox_Data.Text = "";
  1020. richTextBox_Rev.Text = "";
  1021. }
  1022. /// <summary>
  1023. /// CDL读取电芯电压
  1024. /// </summary>
  1025. /// <param name="sender"></param>
  1026. /// <param name="e"></param>
  1027. private void button_CDL_ReadCell_Click(object sender, EventArgs e)
  1028. {
  1029. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3500, null);
  1030. richTextBox_Data.Text = "";
  1031. richTextBox_Rev.Text = "";
  1032. }
  1033. /// <summary>
  1034. /// CDL读取设计信息
  1035. /// </summary>
  1036. /// <param name="sender"></param>
  1037. /// <param name="e"></param>
  1038. private void button_CDL_ReadDesign_Click(object sender, EventArgs e)
  1039. {
  1040. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3600, null);
  1041. richTextBox_Data.Text = "";
  1042. richTextBox_Rev.Text = "";
  1043. }
  1044. /// <summary>
  1045. /// CDL读取生产信息
  1046. /// </summary>
  1047. /// <param name="sender"></param>
  1048. /// <param name="e"></param>
  1049. private void button_CDL_ReadFac_Click(object sender, EventArgs e)
  1050. {
  1051. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3700, null);
  1052. richTextBox_Data.Text = "";
  1053. richTextBox_Rev.Text = "";
  1054. }
  1055. /// <summary>
  1056. /// CDL读取历史信息
  1057. /// </summary>
  1058. /// <param name="sender"></param>
  1059. /// <param name="e"></param>
  1060. private void button_CDL_ReadHistory_Click(object sender, EventArgs e)
  1061. {
  1062. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3800, null);
  1063. richTextBox_Data.Text = "";
  1064. richTextBox_Rev.Text = "";
  1065. }
  1066. /// <summary>
  1067. /// CDL读取自定义信息1
  1068. /// </summary>
  1069. /// <param name="sender"></param>
  1070. /// <param name="e"></param>
  1071. private void button_CDL_ReadUser1_Click(object sender, EventArgs e)
  1072. {
  1073. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3900, null);
  1074. richTextBox_Data.Text = "";
  1075. richTextBox_Rev.Text = "";
  1076. }
  1077. /// <summary>
  1078. /// CDL写入自定义信息1
  1079. /// </summary>
  1080. /// <param name="sender"></param>
  1081. /// <param name="e"></param>
  1082. private void button_CDL_Write_User1_Click(object sender, EventArgs e)
  1083. {
  1084. var Code = new byte[16];
  1085. if (textBox_SendData.Text.Length > 16)
  1086. {
  1087. MessageBox.Show("Length should be less 16 bytes!", "Notice", MessageBoxButtons.OK);
  1088. return;
  1089. }
  1090. for (ushort i = 0; i < textBox_SendData.Text.Length; i++)
  1091. {
  1092. Code[i] = (byte)textBox_SendData.Text[i];
  1093. }
  1094. if (textBox_SendData.Text.Length < 16)
  1095. {
  1096. Code[textBox_SendData.Text.Length] = (byte)'.';
  1097. for (ushort i = 0; i < 16 - textBox_SendData.Text.Length - 1; i++)
  1098. {
  1099. Code[textBox_SendData.Text.Length + 1 + i] = 0x20;
  1100. }
  1101. }
  1102. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x3A10, Code);
  1103. richTextBox_Data.Text = "";
  1104. richTextBox_Rev.Text = "";
  1105. }
  1106. /// <summary>
  1107. /// CDL读取自定义信息2
  1108. /// </summary>
  1109. /// <param name="sender"></param>
  1110. /// <param name="e"></param>
  1111. private void button_CDL_ReadUser2_Click(object sender, EventArgs e)
  1112. {
  1113. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3B00, null);
  1114. richTextBox_Data.Text = "";
  1115. richTextBox_Rev.Text = "";
  1116. }
  1117. /// <summary>
  1118. /// CDL写入自定义信息2
  1119. /// </summary>
  1120. /// <param name="sender"></param>
  1121. /// <param name="e"></param>
  1122. private void button_CDL_Write_User2_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)0x3C10, Code);
  1143. richTextBox_Data.Text = "";
  1144. richTextBox_Rev.Text = "";
  1145. }
  1146. /// <summary>
  1147. /// CDL查询自定义信息3
  1148. /// </summary>
  1149. /// <param name="sender"></param>
  1150. /// <param name="e"></param>
  1151. private void button_CDL_ReadUser3_Click(object sender, EventArgs e)
  1152. {
  1153. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x3D00, null);
  1154. richTextBox_Data.Text = "";
  1155. richTextBox_Rev.Text = "";
  1156. }
  1157. /// <summary>
  1158. /// CDL写入自定义信息3
  1159. /// </summary>
  1160. /// <param name="sender"></param>
  1161. /// <param name="e"></param>
  1162. private void button_CDL_Write_User3_Click(object sender, EventArgs e)
  1163. {
  1164. var Code = new byte[16];
  1165. if (textBox_SendData.Text.Length > 16)
  1166. {
  1167. MessageBox.Show("Length should be less 16 bytes!", "Notice", MessageBoxButtons.OK);
  1168. return;
  1169. }
  1170. for (ushort i = 0; i < textBox_SendData.Text.Length; i++)
  1171. {
  1172. Code[i] = (byte)textBox_SendData.Text[i];
  1173. }
  1174. if (textBox_SendData.Text.Length < 16)
  1175. {
  1176. Code[textBox_SendData.Text.Length] = (byte)'.';
  1177. for (ushort i = 0; i < 16 - textBox_SendData.Text.Length - 1; i++)
  1178. {
  1179. Code[textBox_SendData.Text.Length + 1 + i] = 0x20;
  1180. }
  1181. }
  1182. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x3E10, Code);
  1183. richTextBox_Data.Text = "";
  1184. richTextBox_Rev.Text = "";
  1185. }
  1186. /// <summary>
  1187. /// CDL写入生产信息
  1188. /// </summary>
  1189. /// <param name="sender"></param>
  1190. /// <param name="e"></param>
  1191. private void button_CDL_WriteFac_Click(object sender, EventArgs e)
  1192. {
  1193. var Code = new byte[32];
  1194. if (textBox_SendData.Text.Length == 0)
  1195. {
  1196. MessageBox.Show("Invalid length of bytes!", "Notice", MessageBoxButtons.OK);
  1197. return;
  1198. }
  1199. //填入生产商
  1200. for (ushort i = 0; i < 8; i++)
  1201. {
  1202. Code[i] = (byte)textBox_SendData.Text[i];
  1203. }
  1204. if (textBox_SendData.Text.Length < 8)
  1205. {
  1206. Code[textBox_SendData.Text.Length] = (byte)'.';
  1207. for (ushort i = 0; i < 8 - textBox_SendData.Text.Length - 1; i++)
  1208. {
  1209. Code[textBox_SendData.Text.Length + 1 + i] = 0x20;
  1210. }
  1211. }
  1212. //填入生产地
  1213. for (ushort i = 0; i < 8; i++)
  1214. {
  1215. Code[8 + i] = (byte)textBox_SendData.Text[i];
  1216. }
  1217. if (textBox_SendData.Text.Length < 8)
  1218. {
  1219. Code[textBox_SendData.Text.Length + 8] = (byte)'.';
  1220. for (ushort i = 0; i < 8 - textBox_SendData.Text.Length - 1; i++)
  1221. {
  1222. Code[textBox_SendData.Text.Length + 1 + i + 8] = 0x20;
  1223. }
  1224. }
  1225. //填入生产日期
  1226. for (ushort i = 0; i < 8; i++)
  1227. {
  1228. Code[16 + i] = (byte)textBox_SendData.Text[i];
  1229. }
  1230. if (textBox_SendData.Text.Length < 8)
  1231. {
  1232. Code[textBox_SendData.Text.Length + 16] = (byte)'.';
  1233. for (ushort i = 0; i < 8 - textBox_SendData.Text.Length - 1; i++)
  1234. {
  1235. Code[textBox_SendData.Text.Length + 1 + i + 16] = 0x20;
  1236. }
  1237. }
  1238. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x3F20, Code);
  1239. richTextBox_Data.Text = "";
  1240. richTextBox_Rev.Text = "";
  1241. }
  1242. /// <summary>
  1243. /// CDL写入型号
  1244. /// </summary>
  1245. /// <param name="sender"></param>
  1246. /// <param name="e"></param>
  1247. private void button_CDL_WriteModel_Click(object sender, EventArgs e)
  1248. {
  1249. var Code = new byte[16];
  1250. if (textBox_SendData.Text.Length > 16)
  1251. {
  1252. MessageBox.Show("Length should be less 16 bytes!", "Notice", MessageBoxButtons.OK);
  1253. return;
  1254. }
  1255. for (ushort i = 0; i < textBox_SendData.Text.Length; i++)
  1256. {
  1257. Code[i] = (byte)textBox_SendData.Text[i];
  1258. }
  1259. if (textBox_SendData.Text.Length < 16)
  1260. {
  1261. Code[textBox_SendData.Text.Length] = (byte)'.';
  1262. for (ushort i = 0; i < 16 - textBox_SendData.Text.Length - 1; i++)
  1263. {
  1264. Code[textBox_SendData.Text.Length + 1 + i] = 0x20;
  1265. }
  1266. }
  1267. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x4010, Code);
  1268. richTextBox_Data.Text = "";
  1269. richTextBox_Rev.Text = "";
  1270. }
  1271. /// <summary>
  1272. /// CDL写入SN
  1273. /// </summary>
  1274. /// <param name="sender"></param>
  1275. /// <param name="e"></param>
  1276. private void button_CDL_WriteSN_Click(object sender, EventArgs e)
  1277. {
  1278. var Code = new byte[16];
  1279. if (textBox_SendData.Text.Length > 16)
  1280. {
  1281. MessageBox.Show("Length should be less 16 bytes!", "Notice", MessageBoxButtons.OK);
  1282. return;
  1283. }
  1284. for (ushort i = 0; i < textBox_SendData.Text.Length; i++)
  1285. {
  1286. Code[i] = (byte)textBox_SendData.Text[i];
  1287. }
  1288. if (textBox_SendData.Text.Length < 16)
  1289. {
  1290. Code[textBox_SendData.Text.Length] = (byte)'.';
  1291. for (ushort i = 0; i < 16 - textBox_SendData.Text.Length - 1; i++)
  1292. {
  1293. Code[textBox_SendData.Text.Length + 1 + i] = 0x20;
  1294. }
  1295. }
  1296. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x4110, Code);
  1297. richTextBox_Data.Text = "";
  1298. richTextBox_Rev.Text = "";
  1299. }
  1300. /// <summary>
  1301. /// CDL发送复位
  1302. /// </summary>
  1303. /// <param name="sender"></param>
  1304. /// <param name="e"></param>
  1305. private void button_CDL_Reset_Click(object sender, EventArgs e)
  1306. {
  1307. var Code = new byte[5];
  1308. Code[0] = (byte)'R';
  1309. Code[1] = (byte)'E';
  1310. Code[2] = (byte)'S';
  1311. Code[3] = (byte)'E';
  1312. Code[4] = (byte)'T';
  1313. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x4205, Code);
  1314. richTextBox_Data.Text = "";
  1315. richTextBox_Rev.Text = "";
  1316. }
  1317. /// <summary>
  1318. /// 读取日志
  1319. /// </summary>
  1320. /// <param name="sender"></param>
  1321. /// <param name="e"></param>
  1322. private void button_CDL_ReadLog_Click(object sender, EventArgs e)
  1323. {
  1324. var Code = new byte[8];
  1325. string AddBegin, AddEnd;
  1326. var CodeTemp = new byte[4];
  1327. if (textBox_SendData.Text.Length != 17)
  1328. {
  1329. MessageBox.Show("Length should be 17 bytes!", "Notice", MessageBoxButtons.OK);
  1330. return;
  1331. }
  1332. AddBegin = textBox_SendData.Text.Split(' ')[0];
  1333. AddEnd = textBox_SendData.Text.Split(' ')[1];
  1334. CodeTemp = HexStringToBytes(AddBegin, false);
  1335. for (int i = 0; i < 4; i++)
  1336. Code[i] = CodeTemp[3 - i];
  1337. CodeTemp = HexStringToBytes(AddEnd, false);
  1338. for (int i = 0; i < 4; i++)
  1339. Code[4 + i] = CodeTemp[3 - i];
  1340. SendCmd((ushort)0x712, (byte)0x11, (ushort)0x4308, Code);
  1341. richTextBox_Data.Text = "";
  1342. richTextBox_Rev.Text = "";
  1343. }
  1344. /// <summary>
  1345. /// CDL读取产品条码
  1346. /// </summary>
  1347. /// <param name="sender"></param>
  1348. /// <param name="e"></param>
  1349. private void button_CDL_ReadTag_Click(object sender, EventArgs e)
  1350. {
  1351. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x4500, null);
  1352. richTextBox_Data.Text = "";
  1353. richTextBox_Rev.Text = "";
  1354. }
  1355. /// <summary>
  1356. /// CDL写入产品条码
  1357. /// </summary>
  1358. /// <param name="sender"></param>
  1359. /// <param name="e"></param>
  1360. private void button_CDL_WriteTag_Click(object sender, EventArgs e)
  1361. {
  1362. var Code = new byte[32];
  1363. for (int i = 0; i < 32; i++)
  1364. Code[i] = 0x00;
  1365. if (textBox_SendData.Text.Length > 32)
  1366. {
  1367. MessageBox.Show("Length should be less 32 bytes!", "Notice", MessageBoxButtons.OK);
  1368. return;
  1369. }
  1370. for (ushort i = 0; i < textBox_SendData.Text.Length; i++)
  1371. {
  1372. Code[i] = (byte)textBox_SendData.Text[i];
  1373. }
  1374. SendCmd((ushort)0x752, (byte)0x16, (ushort)0x4620, Code);
  1375. richTextBox_Data.Text = "";
  1376. richTextBox_Rev.Text = "";
  1377. }
  1378. /// <summary>
  1379. /// 读取故障日志地址
  1380. /// </summary>
  1381. /// <param name="sender"></param>
  1382. /// <param name="e"></param>
  1383. private void button_CDL_ReadLogAddr_Click(object sender, EventArgs e)
  1384. {
  1385. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x4700, null);
  1386. richTextBox_Data.Text = "";
  1387. richTextBox_Rev.Text = "";
  1388. }
  1389. /// <summary>
  1390. /// CDL读取BMS补充信息
  1391. /// </summary>
  1392. /// <param name="sender"></param>
  1393. /// <param name="e"></param>
  1394. private void button_CDL_ReadOtherInfo_Click(object sender, EventArgs e)
  1395. {
  1396. SendCmd((ushort)0x752, (byte)0x11, (ushort)0x4800, null);
  1397. richTextBox_Data.Text = "";
  1398. richTextBox_Rev.Text = "";
  1399. }
  1400. /// <summary>
  1401. /// OBC读取BMS补充信息
  1402. /// </summary>
  1403. /// <param name="sender"></param>
  1404. /// <param name="e"></param>
  1405. private void button_OBC_ReadOtherInfo_Click(object sender, EventArgs e)
  1406. {
  1407. SendCmd((ushort)0x732, (byte)0x11, (ushort)0x5600, null);
  1408. richTextBox_Data.Text = "";
  1409. richTextBox_Rev.Text = "";
  1410. }
  1411. }
  1412. #endregion
  1413. #endregion
  1414. }