Search code examples
c#checksumcrc16

C# Check Sum CRC16 CCITT


Although I am following the method to calculate Check Sum CRC16. I'm cant get the result as user manual show. Can anyone help on this?

private static UInt16 CRC16_Update(UInt16 crc, byte c)
{
    crc = (((ushort)(((crc >> 4) & 0x0FFF) ^ crc_tab[((crc ^ c) & 0x000F)])));
    crc = (((ushort)(((crc >> 4) & 0x0FFF) ^ crc_tab[((crc ^ (c >> 4)) & 0x000F)])));
    return crc;
}

Command Format:

Command Format

CRC Manual Example:

CRC Manual Example

Manual Command example:

Manual Command example


Solution

  • Just initialize crc result to 0xFFFF not zero as I expect. such:

    byte[] data = new byte[] { 0x3F, 0x01, 0x00, 0x01, 0x06, 0x8C, 0x00, 0x01, 0x00, 0xCD, 0x13 };
        
    UInt16 crc = 0xFFFF;
    for (int i = 0; i < data.Length-2; i++)
    {
      crc = CRC16_Update(crc, data[i]);
    }