Calculating CRC16 in Python for modbus

Viewed 2035

Firstly, sorry! I am a beginner...

I got the following byte sequence on a modbus: "01 04 08 00 00 00 09 00 00 00 00 f8 0c". The CRC on bold on this byte sequence is correct. However, to check/create the CRC I have to follow the device especs that states:

The error checking must be done using a 16 bit CRC implemented as two 8 bit bytes. The CRC is appended to the frame as the last field. The low order byte of the CRC is appended first, followed by the high order byte. Thus, the CRC high order byte is the last byte to be sent in the frame. The polynomial value used to generate the CRC must be 0xA001.

Now, how can I check the CRC using crcmod? My code is:

import crcmod
crc16 = crcmod.mkCrcFun(0x1A001, rev=True, initCrc=0xFFFF, xorOut=0x0000)
print crc16("0104080000000900000000".decode("hex"))

I tried everything but I can't get the "f8 0C" that is correct on the byte sequence...

1 Answers
Related