public byte[] ToBytes() { byte[] bytes = null; int SoleCoding = 0; bytes = new byte[18]; bytes[0] = 0x5A; bytes[1] = 0xA5; bytes[2] = 0x7E; bytes[3] = 0xE7; bytes[4] = (byte)def.Fzh; bytes[5] = 0x58; CommandUtil.ConvertInt16ToByte((ushort)(bytes.Length - 4), bytes, 6); //长度,6,7 bytes[8] = (byte)(this.LastAcceptFlag + 0x04); bytes[9] = RandomCode; //随机数 if (ModificationItems.Count > 0) { int tempInt = ModificationItems[0].DeviceType; //todo:服务端下发时填大类 SoleCoding += (tempInt << 24); //设备大类 tempInt = Convert.ToInt32(ModificationItems[0].SoleCoding.Substring(0, 4)); SoleCoding += ((tempInt - 2018) << 19); //年 tempInt = Convert.ToInt32(ModificationItems[0].SoleCoding.Substring(4, 2)); SoleCoding += (tempInt << 15); //月 tempInt = Convert.ToInt32(ModificationItems[0].SoleCoding.Substring(6, 2)); SoleCoding += (tempInt << 10); //日 tempInt = Convert.ToInt32(ModificationItems[0].SoleCoding.Substring(8, 4)); SoleCoding += tempInt; //序列号 CommandUtil.ConvertInt32ToByte(Convert.ToUInt32(SoleCoding), bytes, 10); //610 11 12 13 bytes[14] = ModificationItems[0].BeforeModification; bytes[15] = ModificationItems[0].AfterModification; } CommandUtil.CRC16_CCITT(bytes, 4, bytes.Length - 2);//累加和 return(bytes); }
public byte[] ToBytes() { byte[] bytes = new byte[11]; bytes[0] = 0x5A; bytes[1] = 0xA5; bytes[2] = 0x7E; bytes[3] = 0xE7; bytes[4] = 252; bytes[5] = 0x46; CommandUtil.ConvertInt16ToByte((ushort)(bytes.Length - 4), bytes, 6); //长度,6,7 bytes[8] = 0; CommandUtil.CRC16_CCITT(bytes, 4, bytes.Length - 2); //累加和 return(bytes); }
public byte[] ToBytes() { byte[] bytes = null; bytes = new byte[11]; bytes[0] = 0x5A; bytes[1] = 0xA5; bytes[2] = 0x7E; bytes[3] = 0xE7; bytes[4] = (byte)def.Fzh; bytes[5] = 0x58; CommandUtil.ConvertInt16ToByte((ushort)(bytes.Length - 4), bytes, 6); //长度,6,7 bytes[8] = (byte)(this.LastAcceptFlag); CommandUtil.CRC16_CCITT(bytes, 4, bytes.Length - 2); //累加和 return(bytes); }
public byte[] ToBytes() { byte[] bytes = null; bytes = new byte[13]; bytes[0] = 0x5A; bytes[1] = 0xA5; bytes[2] = 0x7E; bytes[3] = 0xE7; bytes[4] = (byte)def.Fzh; bytes[5] = 0x58; CommandUtil.ConvertInt16ToByte((ushort)(bytes.Length - 4), bytes, 6);//长度,6,7 bytes[8] = (byte)(this.LastAcceptFlag + (3 << 1)); bytes[9] = (byte)(QueryMinute == 0 ? 48 : QueryMinute); bytes[10] = SerialNumber; CommandUtil.CRC16_CCITT(bytes, 4, bytes.Length - 2);//累加和 return(bytes); }
public static void HandleSwitchInfo(byte[] data, ProtocolDataCreatedEventArgs upData, NetworkDeviceInfo net) { GetSwitchboardParamCommResponse cData = new GetSwitchboardParamCommResponse();//交换机基本信息; byte[] buffer = new byte[data.Length - 4]; ushort startindex = 32760; //数据开始位置 int receivelength = 0; //下标|接收数据长度 ushort crcvalue = 0, receivevalue; //crc累加和 回发累加和 byte curByte = 0, SignCount = 0; cData.DeviceCode = net.MAC; cData.RealDataItems = new List <RealDataItem>(); for (int i = 4; i < data.Length; i++) { buffer[i - 4] = data[i]; } if (buffer[0] == 252) { startindex = 0; } if (startindex == 32760) { LogHelper.Error("【HandleSwitchInfo】" + "没有长到分站地址引导符【252】"); return; } receivelength = CommandUtil.ConvertByteToInt16(buffer, startindex + 2); if (receivelength > buffer.Length) { LogHelper.Error("【HandleSwitchInfo】" + "回发长度不足【" + receivelength + "】" + "【" + data.Length + "】"); return; } receivevalue = CommandUtil.ConvertByteToInt16(buffer, startindex + receivelength - 2); crcvalue = CommandUtil.CRC16_CCITT(buffer, startindex, startindex + receivelength - 2); if (crcvalue != receivevalue) { LogHelper.Error("【HandleSwitchInfo】" + "通讯误码【" + crcvalue + "】" + "【" + receivevalue + "】"); return; } #region 协议解析 //5A A5 3C C3 FC 46 00 12 C1 04 64 FF FF 00 F8 C1 C1 00 00 00 0B 84 curByte = buffer[startindex + 5]; SignCount = (byte)(curByte >> 2); cData.BatteryControlState = (byte)(curByte & 0x01); cData.BatteryState = (byte)((curByte >> 1) & 0x01); startindex = (ushort)(startindex + 6); cData.BatteryCapacity = buffer[startindex++]; curByte = buffer[startindex++]; cData.SerialPortBatteryState = (byte)((curByte >> 6) & 0x01); //(串口服务器-供电电源) cData.SerialPortRunState = (byte)((curByte >> 5) & 0x01); //(串口服务器-运行状态) cData.SwitchRunState = (byte)((curByte >> 1) & 0x01); //(交换机-运行状态) cData.SwitchBatteryState = (byte)(curByte & 0x01); //(交换机-供电电源) cData.Switch1000State = new byte[3]; cData.Switch1000State[0] = (byte)((curByte >> 2) & 0x01); //Bit2位(千兆光口1) cData.Switch1000State[1] = (byte)((curByte >> 3) & 0x01); //Bit2位(千兆光口2) cData.Switch1000State[2] = (byte)((curByte >> 4) & 0x01); //Bit2位(千兆光口3) curByte = buffer[startindex++]; cData.Switch100State = new byte[7]; for (int i = 0; i < 7; i++) { cData.Switch100State[i] = (byte)((curByte >> i) & 0x01); //Bit2位(百兆接口1~7) } startindex++; //预留字节 if (SignCount > 0) { HandleSoleCoding(SignCount, buffer, startindex, cData); } #endregion upData.MasProtocol.Protocol = cData; }