public static bool GetResult(Command cd) { bool result = false; //bool isError = false; PLAASerialPort.GetInstance().Write(cd.SendByte); for (int i = 0; i < (delay / 50); i++) { Thread.Sleep(50); // 错误码 if (PLAASerialPort.GetInstance().DataBufferList.Count == 5) { byte[] ebyte = PLAASerialPort.GetInstance().DataBufferList.ToArray(); byte[] crcByte = CRC.GetCRC(ebyte); if (BitConverter.ToString(crcByte) == BitConverter.ToString(ebyte, ebyte.Length - 2)) { cd.ErrorByte = PLAASerialPort.GetInstance().DataBufferList.ToArray(); Trace.WriteLine("错误码"); LogLib.Log.GetLogger("CommandResult").Warn(string.Format("错误命令:{0},发送命令:{1}", PLAASerialPort.byteToHexStr(cd.ErrorByte), PLAASerialPort.byteToHexStr(cd.SendByte))); return(false); } } else if (PLAASerialPort.GetInstance().DataBufferList.Count >= cd.ResultLength) { cd.ResultByte = PLAASerialPort.GetInstance().DataBufferList.ToArray(); return(true); } } LogLib.Log.GetLogger("CommandResult").Warn(string.Format("命令超时:{0}", PLAASerialPort.byteToHexStr(cd.SendByte))); Trace.WriteLine("超时"); return(result); }
/// <summary> /// 和校验 /// </summary> /// <param name="data"></param> /// <returns></returns> private bool checkSum(byte[] data) { try { // 最小是5个字节才能校验 if (data.Length < 5) { return(false); } byte[] newbyte = new byte[data.Length - 2]; Array.Copy(data, 1, newbyte, 0, data.Length - 2); byte[] crcByte = CRC.GetCRC(newbyte); if (BitConverter.ToString(crcByte) == BitConverter.ToString(newbyte, newbyte.Length - 2)) { return(true); } LogLib.Log.GetLogger("PLAASerialPort").Warn("和校验失败"); } catch (Exception) { throw; } return(false); }
/// <summary> /// 和校验 /// </summary> /// <param name="data"></param> /// <returns></returns> private static bool checkSum(byte[] data) { try { // 最小是5个字节才能校验 if (data.Length < 5) { return(false); } //byte[] newbyte = new byte[data.Length - 2]; //Array.Copy(data, 0, newbyte, 0, data.Length - 2); byte[] crcByte = CRC.GetCRC(data); if (BitConverter.ToString(crcByte) == BitConverter.ToString(data, data.Length - 2)) { return(true); } LogLib.Log.GetLogger("PLAASerialPort").Warn("和校验失败"); } catch (Exception ex) { LogLib.Log.GetLogger("CommandResult").Warn(ex); } return(false); }
/// <summary> /// 获取发送帧 /// </summary> /// <param name="address"></param> /// <param name="highAdr"></param> /// <param name="lowAdr"></param> /// <param name="num"></param> /// <returns></returns> private byte[] GetSendByte(byte address, byte highAdr, byte lowAdr, short num) { byte[] sendb = new byte[8]; sendb[0] = address; sendb[1] = 0x03; sendb[2] = highAdr; sendb[3] = lowAdr; Array.Copy(BitConverter.GetBytes(num), 0, sendb, 4, 2); Array.Copy(CRC.GetCRC(sendb), 0, sendb, 6, 2); ResultLength = 5 + 2 * num; return(sendb); }
public static byte[] GetReadSendByte(byte address, byte highAdr, byte lowAdr, short num) { byte[] sendb = new byte[8]; sendb[0] = address; sendb[1] = 0x03; sendb[2] = highAdr; sendb[3] = lowAdr; byte[] bb2 = BitConverter.GetBytes(num); Array.Reverse(bb2); Array.Copy(bb2, 0, sendb, 4, 2); Array.Copy(CRC.GetCRC(sendb), 0, sendb, 6, 2); return(sendb); }
/// <summary> /// 获取发送帧 /// </summary> /// <param name="address"></param> /// <param name="highAdr"></param> /// <param name="lowAdr"></param> /// <param name="num"></param> /// <returns></returns> private byte[] SetReadByte(byte address, byte highAdr, byte lowAdr, short num, float value) { byte[] sendb = new byte[9 + 2 * num]; sendb[0] = address; sendb[1] = 0x03; // 功能码 sendb[2] = highAdr; sendb[3] = lowAdr; Array.Copy(BitConverter.GetBytes(num), 0, sendb, 4, 2); sendb[6] = Convert.ToByte(2 * num); Array.Copy(BitConverter.GetBytes(value), 0, sendb, 7, 4); Array.Copy(CRC.GetCRC(sendb), 0, sendb, 11, 2); ResultLength = 5 + 2 * num; return(sendb); }
/// <summary> /// 通过构造函数重载来区别是读命令还是写命令 /// </summary> /// <param name="address">通信地址</param> /// <param name="highAdr">寄存器起始地址高位</param> /// <param name="lowAdr">寄存器起始地址地位</param> /// <param name="num">寄存器个数</param> public Command(byte address, byte highAdr, byte lowAdr, short num) { byte[] sendb = new byte[8]; sendb[0] = address; sendb[1] = 0x03; sendb[2] = highAdr; sendb[3] = lowAdr; byte[] bb2 = BitConverter.GetBytes(num); Array.Reverse(bb2); Array.Copy(bb2, 0, sendb, 4, 2); Array.Copy(CRC.GetCRC(sendb), 0, sendb, 6, 2); ResultLength = 5 + 2 * num; SendByte = sendb; }
public static byte[] GetWiteSendByte(byte address, byte highAdr, byte lowAdr, byte[] content) { byte[] sendb = new byte[9 + content.Length]; sendb[0] = address; sendb[1] = 0x10; sendb[2] = highAdr; sendb[3] = lowAdr; byte[] num = BitConverter.GetBytes(Convert.ToInt16(content.Length / 2)); Array.Reverse(num); Array.Copy(num, 0, sendb, 4, 2); sendb[6] = Convert.ToByte(content.Length); Array.Copy(content, 0, sendb, 7, content.Length); Array.Copy(CRC.GetCRC(sendb), 0, sendb, sendb.Length - 2, 2); return(sendb); }
/// <summary> /// 通过构造函数重载来区别是读命令还是写命令 /// </summary> /// <param name="address">通信地址</param> /// <param name="highAdr">寄存器起始地址高位</param> /// <param name="lowAdr">寄存器起始地址地位</param> /// <param name="num">寄存器个数</param> //public Command(byte address, byte highAdr, byte lowAdr, short num) //{ // byte[] sendb = new byte[8]; // sendb[0] = address; // sendb[1] = 0x03; // sendb[2] = highAdr; // sendb[3] = lowAdr; // byte[] bb2 = BitConverter.GetBytes(num); // Array.Reverse(bb2); // Array.Copy(bb2, 0, sendb, 4, 2); // Array.Copy(CRC.GetCRC(sendb), 0, sendb, 6, 2); // ResultLength = 5 + 2 * num; // SendByte = sendb; //} //public Command(byte address, byte highAdr, byte lowAdr,byte[] content) //{ // byte[] sendb = new byte[9+content.Length]; // sendb[0] = address; // sendb[1] = 0x10; // sendb[2] = highAdr; // sendb[3] = lowAdr; // byte[] num = BitConverter.GetBytes(Convert.ToInt16(content.Length / 2)); // Array.Reverse(num); // Array.Copy(num, 0, sendb, 4, 2); // sendb[6] = Convert.ToByte(content.Length); // Array.Copy(content, 0, sendb, 7, content.Length); // Array.Copy(CRC.GetCRC(sendb), 0, sendb, sendb.Length-2, 2); // ResultLength = 8; // SendByte = sendb; //} // byte relayNum = 4; public Command(byte address, short Adr, short num) { byte[] sendb = new byte[10]; sendb[0] = 0xaa; if (address == 6 | address == 7 | address == 9 | address == 10 | address == 12) { sendb[1] = 0x04; } else if (address == 3) { sendb[1] = 0x02; } else if (address == 8) { sendb[1] = 0x03; } else if (address == 1 | address == 2 | address == 4 | address == 5 | address == 11) { sendb[1] = 0x01; } else if (address >= 13 && address <= 29) { sendb[1] = 0x06; } byte[] sendb2 = new byte[8]; sendb2[0] = address; sendb2[1] = 0x03; byte[] bb1 = BitConverter.GetBytes(Adr); Array.Reverse(bb1); Array.Copy(bb1, 0, sendb2, 2, 2); byte[] bb2 = BitConverter.GetBytes(num); Array.Reverse(bb2); Array.Copy(bb2, 0, sendb2, 4, 2); Array.Copy(CRC.GetCRC(sendb2), 0, sendb2, 6, 2); Array.Copy(sendb2, 0, sendb, 2, 8); ResultLength = 5 + 2 * num; SendByte = sendb; }
public Command(byte address, short Adr, byte[] content) { byte[] sendb = new byte[9 + content.Length]; sendb[0] = address; sendb[1] = 0x10; //sendb[2] = highAdr; //sendb[3] = lowAdr; byte[] bb1 = BitConverter.GetBytes(Adr); Array.Reverse(bb1); Array.Copy(bb1, 0, sendb, 2, 2); byte[] num = BitConverter.GetBytes(Convert.ToInt16(content.Length / 2)); Array.Reverse(num); Array.Copy(num, 0, sendb, 4, 2); sendb[6] = Convert.ToByte(content.Length); Array.Copy(content, 0, sendb, 7, content.Length); Array.Copy(CRC.GetCRC(sendb), 0, sendb, sendb.Length - 2, 2); ResultLength = 8; SendByte = sendb; }
private static byte[] TryReadBytes(byte address, int backCount) { List <byte> result = new List <byte>(); for (int i = 0; i < (delay / 50); i++) { Thread.Sleep(50); int count = serialport.BytesToRead; byte[] rbytes = new byte[count]; serialport.Read(rbytes, 0, count); result.AddRange(rbytes); int allCount = result.Count; byte[] allbytes = result.ToArray(); // 错误码 if (allCount == 5) { byte[] crcByte = CRC.GetCRC(allbytes); if (BitConverter.ToString(crcByte) == BitConverter.ToString(allbytes, allCount - 2)) { CommandUnits.IsCommandding = false; if (allbytes[0] != address) { throw new CommandException("从机地址不匹配"); } if (allbytes[1] != 0x90 && allbytes[1] != 0x83) { throw new CommandException("未知异常"); } switch (allbytes[2]) { case 0x01: throw new CommandException("功能码错位"); case 0x02: throw new CommandException("寄存器地址错位"); case 0x03: throw new CommandException("寄存器数量错误"); case 0x04: throw new CommandException("寄存器写入错误"); default: throw new CommandException("未知异常"); } } } else if (allCount == backCount) { break; } } byte[] rrr = result.ToArray(); if (result.Count < 5) { CommandUnits.IsCommandding = false; throw new CommandException("命令超时"); } else { byte[] crcByte = CRC.GetCRC(rrr); if (BitConverter.ToString(crcByte) != BitConverter.ToString(rrr, rrr.Length - 2)) { CommandUnits.IsCommandding = false; throw new CommandException("CRC校验异常"); } } return(result.ToArray()); }
public static bool GetResult(Command cd) { bool result = false; //bool isError = false; PLAASerialPort.GetInstance().Write(cd.SendByte); for (int i = 0; i < (delay / 1000); i++) { if (cd.SendByte[2] == 6 | cd.SendByte[2] == 7 | cd.SendByte[2] == 9 | cd.SendByte[2] == 10 | cd.SendByte[2] == 12) { Thread.Sleep(1600);//一个中继器来回400ms,4个中00继 } else if (cd.SendByte[2] == 3 | cd.SendByte[2] == 13 | cd.SendByte[2] == 14 | cd.SendByte[2] == 15 | (cd.SendByte[2] >= 25 && cd.SendByte[2] <= 29)) { Thread.Sleep(800); } else if (cd.SendByte[2] == 8 | (cd.SendByte[2] >= 16 && cd.SendByte[2] <= 22)) { Thread.Sleep(1200); } else if (cd.SendByte[2] == 1 | cd.SendByte[2] == 2 | cd.SendByte[2] == 4 | cd.SendByte[2] == 5 | cd.SendByte[2] == 11) { Thread.Sleep(400); } else if (cd.SendByte[2] == 23 | cd.SendByte[2] == 24) { Thread.Sleep(1000); } Thread.Sleep(delay2); // 错误码 if (PLAASerialPort.GetInstance().DataBufferList.Count == 5) { byte[] ebyte = PLAASerialPort.GetInstance().DataBufferList.ToArray(); byte[] crcByte = CRC.GetCRC(ebyte); if (BitConverter.ToString(crcByte) == BitConverter.ToString(ebyte, ebyte.Length - 2)) { cd.ErrorByte = PLAASerialPort.GetInstance().DataBufferList.ToArray(); LogLib.Log.GetLogger("CommandResult").Warn(string.Format("错误命令:{0},发送命令:{1}", PLAASerialPort.byteToHexStr(cd.ErrorByte), PLAASerialPort.byteToHexStr(cd.SendByte))); return(false); } } else if (PLAASerialPort.GetInstance().DataBufferList.Count >= cd.ResultLength) { //for (int a = 0; a < 2; a++) //{ byte[] tbyte = PLAASerialPort.GetInstance().DataBufferList.ToArray(); for (int ii = 0; ii < tbyte.Length; ii++) { if (tbyte[ii] != 0xaa) { continue; } try { if (tbyte[ii + 1] == (cd.SendByte[1] - 1) && tbyte[ii + 2] == cd.SendByte[2] && tbyte[ii + 3] == cd.SendByte[3] && tbyte[ii + 4] == 0x06) { byte[] mbyte = new byte[tbyte.Length - ii - 2]; Array.Copy(tbyte, ii + 2, mbyte, 0, tbyte.Length - ii - 2); //byte[] mbyte = new byte[tbyte.Length - ii]; //Array.Copy(tbyte, ii, mbyte, 0, tbyte.Length - ii); if (mbyte.Length >= cd.ResultLength) { byte[] ybyte = new byte[cd.ResultLength]; Array.Copy(mbyte, 0, ybyte, 0, cd.ResultLength); cd.ResultByte = ybyte; // cd.ResultByte = PLAASerialPort.GetInstance().DataBufferList.ToArray(); return(true); } } } catch { break; } } // } //if (tbyte[0] == 0xaa && tbyte[1] == (cd.SendByte[1] - 1) && tbyte[2] == cd.SendByte[2] && tbyte[3] == cd.SendByte[3]) //{ // cd.ResultByte = PLAASerialPort.GetInstance().DataBufferList.ToArray(); // return true; //} } } LogLib.Log.GetLogger("CommandResult").Warn(string.Format("命令超时:{0}", PLAASerialPort.byteToHexStr(cd.SendByte))); Trace.WriteLine("超时"); return(result); }