串口CRC CCITT-FALSE 校验
public static bool CRC16_CCITT_FALSE(byte[] byteData) //C# crc-16/CCITT-FALSE,带判断校验的
{
bool flag = false;
ushort crc = 0xFFFF;
int size = byteData.Length-2; //计算待计算的数据长度
int i = 0;
if (size > 0)
{
while (size-- > 0)
{
crc = (ushort)((crc >> 8) | (crc << 8));
crc ^= byteData[i++];
crc ^= (ushort)(((byte)crc) >> 4);
crc ^= (ushort)(crc << 12);
crc ^= (ushort)((crc & 0xff) << 5);
}
}
if (byteData[byteData.Length-1] == (byte)((crc >> 8) & 0xff)
&& byteData[byteData.Length - 2] == (byte)(crc & 0xff))
{
flag = true;
}
return flag;
}
调用
byte[] QueryInfoByte
= new byte[] { 0xE1, 0xAB, 0xE1, 0x01, 0x02, 0x00, 0x00, 0x04, 0x2E, 0x0B, 0x00, 0xFF, 0x99, 0x31 };
if (CRC16_CCITT_FALSE(QueryInfoByte))
{
//你的代码
}