今天在鼓搗一個手持操做器的時候,遇到一點問題,記錄一下今天的經驗包this
因爲以前公司產品在校驗時基本上都是和校驗,今天在準備用C#模擬一個古董操做器的時候,卻遇到一個問題,模擬器發出的數據,主板一概不回覆,對比通信協議也沒發現什麼問題。因爲文檔有些不全,只是知道通信格式,對比以後以爲應該是校驗出了問題。因爲CRC校驗是數據通訊領域最經常使用的校驗方式,問了幾個老傢伙以後才知道這個四字節ASCII碼校驗和應該是CRC16-CCITT生成的,而後就去仔細看大學時候煞筆了好久也沒明白的CRC校驗的細節。spa
具體CRC如何生成我不闡述了,關鍵點在於「生成多項式」和初始值。code
CRC16-CCITT的生成多項式是 0x1021;blog
很少說了,提供代碼CRC16-CCITT類ci
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; } }
最後,請叫我紅領巾文檔