using System; using System.Collections.Generic; using System.Configuration; using System.IO.Ports; using System.Threading; using ZJ_BYD.ViewModel; namespace ZJ_BYD.Untils { public class CAN1DeviceHelper : Singleton { public DeviceInfo DeviceInfo { get; set; } = new DeviceInfo(); List> _dataList = new List>(); List _data = new List(); SerialPort _serialPort; public void Run() { try { string PortName = ConfigurationManager.AppSettings["PortName"]; int BaudRate = int.Parse(ConfigurationManager.AppSettings["BaudRate"]); int Parity = int.Parse(ConfigurationManager.AppSettings["Parity"]); int DataBits = int.Parse(ConfigurationManager.AppSettings["DataBits"]); int StopBits = int.Parse(ConfigurationManager.AppSettings["StopBits"]); if (_serialPort != null && _serialPort.IsOpen) { _serialPort.Close(); } _serialPort = null; _dataList = new List>(); _data = new List(); _serialPort = new SerialPort { PortName = PortName, BaudRate = BaudRate, Parity = (Parity)Parity, DataBits = DataBits, StopBits = (StopBits)StopBits }; _serialPort.DataReceived += _serialPort_DataReceived; _serialPort.Open(); string sendString = "68 02 00 41 6C 17 16"; writeData(sendString); Thread.Sleep(10); sendString = "68 07 00 04 00 20 A1 07 00 00 3B 16"; writeData(sendString); //1.发送软件编码 sendString = "68 07 00 04 00 20 A1 07 00 00 3B 16 68 13 00 02 00 01 00 00 08 00 B3 07 00 00 03 22 F1 94 55 55 55 55 3E 16 68 13 00 02 00 01 00 00 08 00 B3 07 00 00 30 00 00 00 00 00 00 00 70 16"; Info($"1.发送-->软件编码:{sendString}"); var writeResult1 = writeData(sendString); if (!writeResult1.isok) { Info($"1.发送-->软件编码异常:{writeResult1.msg}"); } getSoftwareCode();//解析软件编码 Info($"1.解析软件编码结果:{DeviceInfo.SoftwareCode}"); //2.发送引导报文 sendString = "68 13 00 02 00 01 00 00 08 00 B3 07 00 00 03 22 F1 80 55 55 55 55 2A 16 68 13 00 02 00 01 00 00 08 00 B3 07 00 00 30 00 00 00 00 00 00 00 70 16"; Info($"2.发送-->引导报文:{sendString}"); var writeResult2 = writeData(sendString); if (!writeResult2.isok) { Info($"2.发送-->引导报文异常:{writeResult2.msg}"); } getGuideVersion();//解析引导报文 Info($"2.解析引导报文结果:{DeviceInfo.GuideVersion}"); //3.清除故障 sendString = "68 13 00 02 00 01 00 00 08 00 B3 07 00 00 04 14 FF FF FF AA AA AA 53 16"; Info($"3.1发送-->清除故障:{sendString}"); byte[] sendBytes = HexStrToByteArray(sendString); if (sendBytes == null) { return; } _serialPort.Write(sendBytes, 0, sendBytes.Length); Thread.Sleep(500); sendString = "68 13 00 02 00 01 00 00 08 00 B3 07 00 00 03 19 01 09 55 55 55 55 BA 16 68 13 00 02 00 01 00 00 08 00 B3 07 00 00 30 00 00 00 00 00 00 00 70 16"; Info($"3.2发送-->清除故障:{sendString}"); var writeResult3 = writeData(sendString); if (!writeResult3.isok) { Info($"3.2发送-->清除故障异常:{writeResult3.msg}"); } getClearFault();//解析清除故障 Info($"3.2解析清除故障结果:{DeviceInfo.ClearFault}"); //4.软件版本号 sendString = "68 13 00 02 00 01 00 00 08 00 72 06 00 00 00 01 00 00 00 00 00 00 FF 16"; Info($"4.发送-->软件版本号:{sendString}"); var writeResult4 = writeData(sendString); if (!writeResult4.isok) { Info($"4.发送-->软件版本号异常:{writeResult4.msg}"); } getSoftwareVersion();//解析软件版本号 Info($"4.解析软件版本号结果:{DeviceInfo.SoftwareVersion}"); //5.电压 getVoltage(); Info($"5.接收--电压:{DeviceInfo.Voltage}"); } catch (Exception ex) { var msg = ex == null ? "与232模块通讯异常" : ex.Message; Info($"DeviceHelper类中Run方法异常:{msg}"); } } private (bool isok, string msg) writeData(string sendString) { try { _dataList.Clear(); byte[] sendBytes = HexStrToByteArray(sendString); if (sendBytes == null) { return (false, $"将{sendString}字符串转成16进制时,异常"); } _serialPort.Write(sendBytes, 0, sendBytes.Length); Thread.Sleep(200); return (true, ""); } catch (Exception ex) { var msg = ex == null ? "writeData异常" : $"writeData异常,{ex.Message}"; return (false, msg); } } private void getSoftwareCode() { try { DeviceInfo.SoftwareCode = string.Empty; if (_dataList.Count > 0) { for (int i = 0; i < _dataList.Count; i++) { var item = _dataList[i]; if (item[14] == 0xBB && item[15] == 0x07) { if (item[18] == 0x10) { DeviceInfo.SoftwareCode += item[23].ToString("X2"); DeviceInfo.SoftwareCode += item[24].ToString("X2"); DeviceInfo.SoftwareCode += item[25].ToString("X2"); } else if (item[18] == 0x21) { DeviceInfo.SoftwareCode += item[19].ToString("X2"); DeviceInfo.SoftwareCode += item[20].ToString("X2"); DeviceInfo.SoftwareCode += item[21].ToString("X2"); DeviceInfo.SoftwareCode += item[22].ToString("X2"); DeviceInfo.SoftwareCode += item[23].ToString("X2"); DeviceInfo.SoftwareCode += item[24].ToString("X2"); } } } } } catch (Exception ex) { var msg = ex == null ? "操作异常" : ex.Message; Info($"getSoftwareCode方法异常:{msg}"); } } private void getGuideVersion() { try { DeviceInfo.GuideVersion = string.Empty; if (_dataList.Count > 0) { for (int i = 0; i < _dataList.Count; i++) { var item = _dataList[i]; if (item[14] == 0xBB && item[15] == 0x07) { if (item[18] == 0x10) { DeviceInfo.GuideVersion += item[23].ToString() + "."; DeviceInfo.GuideVersion += item[24].ToString() + "."; DeviceInfo.GuideVersion += item[25].ToString() + "."; } else if (item[18] == 0x21) { DeviceInfo.GuideVersion += item[19].ToString() + "."; DeviceInfo.GuideVersion += item[20].ToString() + "."; DeviceInfo.GuideVersion += item[21].ToString() + "."; DeviceInfo.GuideVersion += item[22].ToString() + "."; DeviceInfo.GuideVersion += item[23].ToString() + "."; DeviceInfo.GuideVersion += item[24].ToString(); } } } } } catch (Exception ex) { var msg = ex == null ? "操作异常" : ex.Message; Info($"getGuideVersion方法异常:{msg}"); } } private void getClearFault() { try { DeviceInfo.ClearFault = string.Empty; if (_dataList.Count > 0) { for (int i = 0; i < _dataList.Count; i++) { var item = _dataList[i]; if (item[14] == 0xBB && item[15] == 0x07 && item[18] == 0x06 && item[19] == 0x59) { DeviceInfo.ClearFault += item[23].ToString(); DeviceInfo.ClearFault += item[24].ToString(); break; } } } } catch (Exception ex) { var msg = ex == null ? "操作异常" : ex.Message; Info($"getClearFault方法异常:{msg}"); } } private void getSoftwareVersion() { try { DeviceInfo.SoftwareVersion = string.Empty; if (_dataList.Count > 1) { string year = string.Empty, month = string.Empty, day = string.Empty, times = string.Empty, version = string.Empty, version1 = string.Empty, version2 = string.Empty, version3 = string.Empty; for (int i = 0; i < _dataList.Count; i++) { var item = _dataList[i]; if (item[18] == 0x51) { var yearVal = ByteArrayToInt(new byte[] { item[20], item[19] }); if (yearVal.HasValue) { year = yearVal.Value.ToString(); } var monthVal = ByteArrayToInt(new byte[] { item[22], item[21] }); if (monthVal.HasValue) { month = monthVal.Value.ToString(); } var dayVal = ByteArrayToInt(new byte[] { item[24], item[23] }); if (dayVal.HasValue) { day = dayVal.Value.ToString(); } } if (item[18] == 0x52) { var timsVal = ByteArrayToInt(new byte[] { item[20], item[19] }); if (timsVal.HasValue) { times = timsVal.Value.ToString(); } var versionVal = ByteArrayToInt(new byte[] { item[21], item[23] }); if (versionVal.HasValue) { version = versionVal.Value.ToString().PadLeft(5, '0'); version1 = version.Substring(0, 1); version2 = version.Substring(1, 2); version3 = version.Substring(3, 2); } } DeviceInfo.SoftwareVersion = $"{version1}.{version2}.{version3}.{year}.{month}.{day}.{times}"; } } } catch (Exception ex) { var msg = ex == null ? "操作异常" : ex.Message; Info($"getSoftwareVersion方法异常:{msg}"); } } private void getVoltage() { try { DeviceInfo.Voltage = string.Empty; if (_dataList.Count > 0) { String voltage1 = String.Empty, voltage2 = String.Empty; foreach (List item in _dataList) { if (item.Count == 28 && item[14] == 0xDB && item[15] == 0x02) { voltage1 = ((double)item[22] / 10).ToString() + "V"; voltage2 = ((double)item[23] / 10).ToString() + "V"; DeviceInfo.Voltage = $"{voltage1},{voltage2}"; } } } } catch (Exception ex) { var msg = ex == null ? "操作异常" : ex.Message; Info($"getVoltage方法异常:{msg}"); } } private void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { try { byte[] read = new byte[_serialPort.BytesToRead]; _serialPort.Read(read, 0, read.Length); foreach (byte b in read) { if (b == 0x68) { if (_data.Count == 28) { if (_data[14] == 0xBB && _data[15] == 0x07) { _dataList.Add(new List(_data)); } else if (_data[14] == 0x73 && _data[15] == 0x06 && _data[16] == 0x00 && _data[17] == 0x00 && (_data[18] == 0x51 || _data[18] == 0x52)) { _dataList.Add(new List(_data)); } else if (_data[14] == 0xDB && _data[15] == 0x02) { _dataList.Add(new List(_data)); } } _data.Clear(); } _data.Add(b); } } catch (Exception ex) { var msg = ex == null ? "操作异常" : ex.Message; Info($"_serialPort_DataReceived方法异常:{msg}"); } } private static void Info(string info = "") { LogHelper.WriteLog(info); } /// /// 字符串转16进制字节数组 /// /// /// public byte[] HexStrToByteArray(string hexString) { try { hexString = hexString.Replace(" ", ""); if ((hexString.Length % 2) != 0) hexString += " "; byte[] returnBytes = new byte[hexString.Length / 2]; for (int i = 0; i < returnBytes.Length; i++) returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); return returnBytes; } catch (Exception ex) { var msg = ex == null ? "操作异常" : ex.Message; Info($"HexStrToByteArray方法将({hexString})字符串转成16进制时,异常:{msg}"); return null; } } /// /// 字节数组转Int32 /// /// /// public static int? ByteArrayToInt(byte[] bytes) { try { int mask = 0xff; int temp = 0; int n = 0; for (int i = 0; i < bytes.Length; i++) { n <<= 8; temp = bytes[i] & mask; n |= temp; } return n; } catch (Exception ex) { var msg = ex == null ? "操作异常" : ex.Message; Info($"ByteArrayToInt方法异常:{msg}"); return null; } } } }