private const int IDX_CRC16_RESP = 5; // 2 byte 应答包校验 public void Request(ref SensorAcqResult sensorAcq) { try { byte m; if (byte.TryParse(sensorAcq.Sensor.ModuleNo.ToString().Trim(), out m)) { var packet = new byte[8]; packet[IDX_ADDRESS] = Convert.ToByte(sensorAcq.Sensor.ModuleNo); packet[IDX_CMD] = 0x04; //输入寄存器起始地址为 0x0004,寄存器数量为 2 packet[3] = 0x04; // 目的寄存器长度低8位 packet[5] = 0x02; ValueHelper.CheckCRC16(packet, 0, IDX_CRC16_REQ, out packet[IDX_CRC16_REQ + 1], out packet[IDX_CRC16_REQ]); sensorAcq.ErrorCode = (int)Errors.SUCCESS; sensorAcq.Request = packet; } else { sensorAcq.ErrorCode = (int)Errors.ERR_INVALID_MODULE; sensorAcq.Request = null; } } catch { sensorAcq.Request = null; sensorAcq.ErrorCode = (int)Errors.ERR_UNKNOW; } }
public int IsValid(byte[] data) { if (data == null) { return((int)Errors.ERR_INVALID_DATA); } if ((data[IDX_DevType] << 8 | data[IDX_DevType + 1]) != SensorType) { return((int)Errors.ERR_INVALID_DATA); } if (data[IDX_CMD] != 0x81 && data[IDX_CMD] != 0xC0) { return((int)Errors.ERR_INVALID_DATA); } byte[] crc16 = { data[data.Length - 2], data[data.Length - 1] }; byte crcHi; byte crcLo; ValueHelper.CheckCRC16(data, 0, data.Length - 2, out crcHi, out crcLo); if ((crc16[0] != crcHi) || (crc16[1] != crcLo)) { return((int)Errors.ERR_CRC); } return((int)Errors.SUCCESS); }
public void Request(ref SensorAcqResult sensorAcq) { try { byte m; if (byte.TryParse(sensorAcq.Sensor.ModuleNo.ToString().Trim(), out m)) { var package = new byte[8]; package[IDX_ADRESS] = Convert.ToByte(sensorAcq.Sensor.ModuleNo); package[IDX_CMD] = 0x04; package[2] = 0x10; package[3] = 0x04; package[5] = 0x08; ValueHelper.CheckCRC16(package, 0, IDX_CRC16_REQ, out package[IDX_CRC16_REQ + 1], out package[IDX_CRC16_REQ]); sensorAcq.ErrorCode = (int)Errors.SUCCESS; sensorAcq.Request = package; } else { sensorAcq.ErrorCode = (int)Errors.ERR_INVALID_MODULE; sensorAcq.Request = null; } } catch { sensorAcq.Request = null; sensorAcq.ErrorCode = (int)Errors.ERR_UNKNOW; } }
private const int SensorType = 1; //温度 public void Request(ref SensorAcqResult sensorAcq) { try { if (sensorAcq.Sensor.ModuleNo > ushort.MaxValue) { sensorAcq.ErrorCode = (int)Errors.ERR_INVALID_MODULE; sensorAcq.Request = null; return; } var packet = new byte[8]; ValueHelper.WriteShort_BE(packet, IDX_DevType, (short)SensorType); ValueHelper.WriteUShort_BE(packet, IDX_DevId, (ushort)sensorAcq.Sensor.ModuleNo); packet[IDX_CMD] = 0x01; // 采集命令 ValueHelper.CheckCRC16(packet, 0, IDX_DataField + 1, out packet[IDX_DataField + 1], out packet[IDX_DataField + 2]); sensorAcq.ErrorCode = (int)Errors.SUCCESS; sensorAcq.Request = packet; } catch { sensorAcq.Request = null; sensorAcq.ErrorCode = (int)Errors.ERR_UNKNOW; } }
private const int IDX_FrameLength_Res = 7; // byte 应答包长度 public void Request(ref SensorAcqResult sensorAcq) { try { byte m; if (byte.TryParse(sensorAcq.Sensor.ModuleNo.ToString().Trim(), out m)) { var packet = new byte[8]; packet[IDX_Adrress] = Convert.ToByte(sensorAcq.Sensor.ModuleNo); packet[IDX_CMD] = 0x03; // 采集命令 = 0x03 packet[5] = 0x01; // 目的寄存器长度低8位 ValueHelper.CheckCRC16(packet, 0, 6, out packet[IDX_CRC16_Req + 1], out packet[IDX_CRC16_Req]); //start, end sensorAcq.ErrorCode = (int)Errors.SUCCESS; sensorAcq.Request = packet; } else { sensorAcq.ErrorCode = (int)Errors.ERR_INVALID_MODULE; sensorAcq.Request = null; } } catch { sensorAcq.Request = null; sensorAcq.ErrorCode = (int)Errors.ERR_UNKNOW; } }
private const int SensorType = 10; //振弦 public void Request(ref SensorAcqResult sensorAcq) { try { if (sensorAcq.Sensor.ModuleNo > ushort.MaxValue) { sensorAcq.ErrorCode = (int)Errors.ERR_INVALID_MODULE; sensorAcq.Request = null; return; } var packet = new byte[9]; ValueHelper.WriteShort_BE(packet, IDX_DevType, (short)SensorType); ValueHelper.WriteUShort_BE(packet, IDX_DevId, (ushort)sensorAcq.Sensor.ModuleNo); packet[IDX_CMD] = 0x01; //采集命令 packet[IDX_DataField] = (byte)sensorAcq.Sensor.ChannelNo; packet[IDX_DataField + 1] = 0x67; // 默认采集 频率、温度、幅值 、发送激励信号、检测线状态 返回包长度为22 // 如果采集温度编号 0x6f 返回包长度为26 ValueHelper.CheckCRC16(packet, 0, IDX_DataField + 2, out packet[IDX_DataField + 2], out packet[IDX_DataField + 3]); sensorAcq.ErrorCode = (int)Errors.SUCCESS; sensorAcq.Request = packet; } catch { sensorAcq.Request = null; sensorAcq.ErrorCode = (int)Errors.ERR_UNKNOW; } }
private int IsValid(byte[] data) { if ((data[IDX_DevType] << 8 | data[IDX_DevType + 1]) != SensorType) { return((int)Errors.ERR_INVALID_DATA); } byte[] crc16 = { data[data.Length - 2], data[data.Length - 1] }; byte crcHi; byte crcLo; ValueHelper.CheckCRC16(data, 0, data.Length - 2, out crcHi, out crcLo); if ((crc16[0] == crcHi) && (crc16[1] == crcLo)) { return((int)Errors.SUCCESS); } return((int)Errors.ERR_CRC); }
int IsValid(byte[] data) { if (data == null || data.Length != IDX_CRC16_RESP + 2) { return((int)Errors.ERR_INVALID_DATA); } if (data[IDX_CMD] != 0x04) { return((int)Errors.ERR_INVALID_DATA); } byte[] crc16 = { data[IDX_CRC16_RESP], data[IDX_CRC16_RESP + 1] }; byte crcHi; byte crcLo; ValueHelper.CheckCRC16(data, 0, IDX_CRC16_RESP, out crcHi, out crcLo); if ((crc16[0] == crcLo) && (crc16[1] == crcHi)) { return((int)Errors.SUCCESS); } return((int)Errors.ERR_CRC); }
public int IsValid(byte[] data) { if (data == null || (data.Length != 9 && data.Length != 5)) { return((int)Errors.ERR_INVALID_DATA); } if (data[IDX_CMD] != 0x04 && data[IDX_CMD] != 0x84) { return((int)Errors.ERR_INVALID_DATA); } byte[] crc16 = { data[data.Length - 2], data[data.Length - 1] }; byte crcHi; byte crcLo; ValueHelper.CheckCRC16(data, 0, data.Length - 2, out crcLo, out crcHi); if ((crc16[0] == crcHi) && (crc16[1] == crcLo)) { return((int)Errors.SUCCESS); } return((int)Errors.ERR_CRC); }
public int IsValid(byte[] data) { if (data == null || data.Length != 7) { return((int)Errors.ERR_INVALID_DATA); } if (data[IDX_CMD] != 0x03) { return((int)Errors.ERR_INVALID_DATA); } byte[] crc16 = { data[IDX_CRC16_Res], data[IDX_CRC16_Res + 1] }; byte crcHi; byte crcLo; ValueHelper.CheckCRC16(data, 0, 5, out crcLo, out crcHi); if ((crc16[0] != crcHi) || (crc16[1] != crcLo)) { return((int)Errors.ERR_INVALID_DATA); } return((int)Errors.SUCCESS); }