今天在鼓搗一個手持操作器的時候,遇到一點問題,記錄一下今天的經驗包
由於之前公司產品在校驗時基本上都是和校驗,今天在准備用C#模擬一個古董操作器的時候,卻遇到一個問題,模擬器發出的數據,主板一律不回復,對比通訊協議也沒發現什么問題。由於文檔有些不全,只是知道通訊格式,對比之后覺得應該是校驗出了問題。由於CRC校驗是數據通信領域最常用的校驗方式,問了幾個老家伙之后才知道這個四字節ASCII碼校驗和應該是CRC16-CCITT生成的,然后就去仔細看大學時候煞筆了很久也沒明白的CRC校驗的細節。
具體CRC如何生成我不闡述了,關鍵點在於“生成多項式”和初始值。
CRC16-CCITT的生成多項式是 0x1021;
不多說了,提供代碼CRC16-CCITT類
public class Crc16Ccitt { public enum InitialCrcValue { Zeros, NonZero1 = 0xffff, NonZero2 = 0x1D0F } const ushort poly = 4129; ushort[] table = new ushort[256]; ushort initialValue = 0; public Crc16Ccitt(InitialCrcValue initialValue) { this.initialValue = (ushort)initialValue; ushort temp, a; for (int i = 0; i < table.Length; ++i) { temp = 0; a = (ushort)(i << 8); for (int j = 0; j < 8; ++j) { if (((temp ^ a) & 0x8000) != 0) { temp = (ushort)((temp << 1) ^ poly); } else { temp <<= 1; } a <<= 1; } table[i] = temp; } } public ushort ComputeChecksum(byte[] bytes) { ushort crc = this.initialValue; for (int i = 0; i < bytes.Length; ++i) { crc = (ushort)((crc << 8) ^ table[((crc >> 8) ^ (0xff & bytes[i]))]); } return crc; } public ushort ComputeChecksum(List<byte> listTemp) { byte[] bytes = listToBytes(listTemp); ushort crc = this.initialValue; for (int i = 0; i < bytes.Length; ++i) { crc = (ushort)((crc << 8) ^ table[((crc >> 8) ^ (0xff & bytes[i]))]); } return crc; } public byte[] ComputeChecksumBytes(byte[] bytes) { ushort crc = ComputeChecksum(bytes); return BitConverter.GetBytes(crc); } public byte[] listToBytes(List<byte> listTemp) { int length = listTemp.Count(); byte[] bytes = new byte[length]; for (int i = 0; i < length; i++) { bytes[i] = listTemp[i]; } return bytes; } }
最后,請叫我紅領巾