private ushort GetCRC16_XMODEM(byte[] b)
{
//CRC-16/XMODEM x16 + x12 + x5 + 1
ushort crc = 0x0000;//初始值
ushort poly = 0x1021;//多项式
foreach (byte item in b)
{
crc ^= (ushort)(item << 8);
for (int i = 0; i < 8; i++)
{
if ((crc & 0x8000) == 0x8000)
{
crc = (ushort)((crc << 1) ^ poly);
}
else
{
crc <<= 1;
}
}
}
//crc = (ushort)((crc >> 8) + (crc << 8));//高低字节对调
//return BitConverter.GetBytes(crc);//以字节数组的形式返回指定的 16 位无符号整数值
return crc;
}
private ushort GetCRC16_MODBUS(byte[] b)
{
//CRC-16/MODBUS x16 + x15 + x2 + 1
ushort crc = 0xFFFF;//初始值
ushort poly = 0xA001;//多项式 0x8005 的高低位对调 得到 0xA001
foreach (byte item in b)
{
crc ^= item;
for (int i = 0; i < 8; i++)
{
if ((crc & 0x0001) == 1)
{
crc = (ushort)((crc >> 1) ^ poly);
}
else
{
crc >>= 1;
}
}
}
return crc;
}