microcontrollercrc

Decode serial data structure


I have a digital clock system with a master clock transmitting data packets and slave clocks receiving that and decoding to display on it. In data structure 1st byte is constant 0x7e, 2nd byte is hour, 3rd byte is minutes, 4th byte is seconds and 5th is always 0x00. Unknown to me are last two bytes. I hope those bytes represent CRC (I'm not sure) and have no idea how it is calculated. Communication method is serial RF signal. Here are some more transmitting data packets.

I tried to transmit the same structures with same values using Arduino and XBee transmitter, then slave clocks response and worked fine. If I change any value out of those 7 bytes, slave clocks do not response.

0x7e 0x00 0x00 0x00 0x00 0x0c 0x0a
0x7e 0x00 0x00 0x01 0x00 0x0d 0x9a
0x7e 0x00 0x00 0x02 0x00 0x0d 0x6a
0x7e 0x00 0x00 0x03 0x00 0x0c 0xfa
0x7e 0x00 0x00 0x04 0x00 0x0e 0xca
0x7e 0x00 0x00 0x05 0x00 0x0f 0x5a
0x7e 0x00 0x00 0x06 0x00 0x0f 0xaa
0x7e 0x00 0x00 0x07 0x00 0x0e 0x3a
0x7e 0x00 0x00 0x08 0x00 0x0b 0xca
0x7e 0x00 0x00 0x09 0x00 0x0a 0x5a
0x7e 0x00 0x00 0x10 0x00 0x01 0xca
0x7e 0x00 0x00 0x11 0x00 0x00 0x5a
0x7e 0x00 0x00 0x12 0x00 0x00 0xaa
0x7e 0x00 0x00 0x13 0x00 0x01 0x3a

I used online CRC calculators to find last two bytes values, but nothing worked for me.


Solution

  • A standard 16-bit CRC works for your data:

    width=16  poly=0x8005  init=0xffff  refin=true  refout=true  xorout=0x0000  check=0x4b37  residue=0x0000  name="CRC-16/MODBUS"
    

    when including the initial 0x7e in the CRC calculation.

    If the messages are always the same length, then that will work. However if there are other messages of different lengths, you would need to provide one of those with its CRC in order to be able to solve for both init and xorout. There are 65536 different combinations of init and xorout that will exactly match your examples, which are all the same length.