private byte[] GetWriteValueBytes(byte[] buffer) { // 更改标志位即可 buffer[1] = 0x10; buffer = CRCTool.AddCRC(buffer.Take(6).ToArray()); return(buffer); }
// 获取PLC实时值方法 private byte[] GetPLCBuffer(string dtuID) { byte[] head = GetPLCHeadData(dtuID); byte[] initBody = GetInitPLCBodyData(); ReplaceRealValue(ref initBody); byte[] body = CRCTool.AddCRC(initBody); return(head.Concat(body).ToArray()); }
//获取随机PLC数据方法 private byte[] GetPLCAutoBuffer(string dtuID) { byte[] head = GetPLCHeadData(dtuID); byte[] initBody = GetInitPLCBodyData(); this.Invoke(new Action(() => { ReplaceAutoRealValue(ref initBody); })); byte[] aa = initBody; byte[] body = CRCTool.AddCRC(initBody); return(head.Concat(body).ToArray()); }
private byte[] GetReadValueBytes(byte[] buffer) { //拿到地址值作为结果值返回去 byte[] addr = buffer.Skip(2).Take(2).ToArray(); buffer[2] = 0x02; ////// 使用随机数应答 1-10 //Random rd = new Random(); //ushort r= Convert.ToUInt16( rd.Next(1, 1000)); //buffer = buffer.Take(3).Concat(BitConverter.GetBytes(r).Reverse()).ToArray(); buffer = buffer.Take(3).Concat(addr).ToArray(); buffer = CRCTool.AddCRC(buffer); return(buffer); }
// 检验最后两个字节是CRC的数组 public static bool CheckCRC16Data(byte[] data) { if (data.Length < 2) { return(false); } byte[] buffer = new byte[data.Length - 2]; Buffer.BlockCopy(data, 0, buffer, 0, data.Length - 2); byte[] bufferAddCRC = CRCTool.AddCRC(buffer); if (bufferAddCRC.Length == data.Length) { if (data[data.Length - 2] == bufferAddCRC[bufferAddCRC.Length - 2] && data[data.Length - 1] == bufferAddCRC[bufferAddCRC.Length - 1]) { return(true); } } return(false); }