Search code examples
serial-portdecode

serial protocol decode and CRC calulation formula


maybe somebody can help to guess how byte 9 CRC (f1, 83, 97) is caclutaed here:

55 0с 00 ff 00 0a 01 05 f1 0a 82 01 80 09 0c
55 0c 00 03 05 81 01 05 83 01 82 01 64 e6 0c
55 0c 00 03 00 90 01 05 97 01 82 01 64 e6 0c
55 0c 00 03 01 90 01 05 96 01 82 01 64 e6 0c
55 0c 01 03 01 90 01 05 97 01 82 01 64 e6 0c
55 0c 01 04 01 90 01 05 90 01 82 01 64 e6 0c
55 0c 02 04 02 90 01 05 90 01 82 01 64 e6 0c
55 0c 00 01 00 81 01 05 84 01 82 01 64 e6 0c
55 0c 00 02 00 81 01 05 87 01 82 01 64 e6 0c
55 0c 00 03 00 81 01 05 86 01 82 01 64 e6 0c
55 0c 00 04 00 81 01 05 81 01 82 01 64 e6 0c
55 0c 00 05 00 81 01 05 80 01 82 01 64 e6 0c
55 0c 00 06 00 81 01 05 83 01 82 01 64 e6 0c

each line does the same command if I send to device, just need to understand how byte 9 calulated here.


Solution

  • def get_chksum(packet):
        chksum = 0
        for i in packet:
            chksum ^= ord(i)
        return hex(chksum)
        
    print(get_chksum('\x00\xff\x00\x0a\x01\x05')) # 0xF1
    print(get_chksum('\x00\x03\x05\x81\x01\x05')) # 0x83
    print(get_chksum('\x00\x03\x00\x90\x01\x05')) # 0x97