Skip to content Skip to sidebar Skip to footer

Modbus Rtu Crc16 Calculation

I'm coding a MODBUS CRC16 calculator in C. What I have before is a python that do this, I wanted to convert it to C. I found some codes online but it's not giving me the correct an

Solution 1:

  • WORD should be unsigned short (2 bytes), not unsigned long (4 or 8 bytes depended on platform)

    typedefunsignedshort WORD;
    
  • As @paddy said, loop from 0 to 6 in the for loop:

    for (i=0; i<6; i++) {
        ...
    }
    
  • crc_data should be of type WORD, not char

    WORD crc_data;
    
  • use %04x in last printf()

    printf("this is CRC: %04x\n", crc_data);
    

Post a Comment for "Modbus Rtu Crc16 Calculation"