Search code examples
pythoncrc

Calculating a CRC


I need to be able to calculate a CRC in order to communicate with a machine. I'm pretty new to calculating CRC's so I'm sorry if this exact question has already been answered as I'm not sure what I am looking for. Anyway, I need to compute a CCIT 16-bit CRC in Python. My message is a binary stream that looks like:

b'G18M1314D4417,2511165'

The message also begins with a STX and ends with a ETX character. I tried using this Python module:

import crc16
print(crc16.crc16xmodem(b'G18M1314D4417,2511165'))

Which returned:

61154

When the correct CRC was:

A855

What am I missing?

Edit: I'm using Python 3.3. I got the 'correct' CRC from a message sent from the machine. I'm not sure whether STX/ETX characters should be included as I have never worked with CRC's before.


Solution

  • I believe that your machine is using a parametrised CRC algorithm named CRC-16/MCRF4XX.

    You can use the crcmod module which can be installed with pip. The CRC can be calculated using the predefined algorithm crc-16-mcrf4xx. STX and ETX should be included in the CRC calculation.

    import crcmod
    import crcmod.predfined
    
    STX = b'\x02'
    ETX = b'\x03'
    data = b'G18M1314D4417,2511165'
    message = STX + data + ETX
    
    crc16 = crcmod.predefined.Crc('crc-16-mcrf4xx')
    crc16.update(message)
    crc = crc16.hexdigest()
    
    >>> crc
    'A855'