public static bool checkBuf(byte[] bb) { ModbusCRC16 crc16 = new ModbusCRC16(); crc16.update(bb, bb.Length - 2); int ri = crc16.getValue(); if (bb[bb.Length - 1] == (byte)(ri & 0xff) && bb[bb.Length - 2] == (byte)((0xff00 & ri) >> 8)) { return(true); } return(false); }
//将16进制的字符串转换为带有CRC校验的字节型数组的方法 public static byte[] getSendBuf(String toSend) { toSend = toSend.Replace(" ", ""); byte[] bb = HexString2Buf(toSend); ModbusCRC16 crc16 = new ModbusCRC16(); crc16.update(bb, bb.Length - 2); int ri = crc16.getValue(); bb[bb.Length - 1] = (byte)(0xff & ri); bb[bb.Length - 2] = (byte)((0xff00 & ri) >> 8); return(bb); }