CRC Library Problem with CRC16

I am trying to understand how the CRC library in Arduino IDE works but I think I have encountered a possible error or possibly it is on my end so I am asking this to figure out which is which.
This is the slightly modified example code called CRC_test.ino from the library itself:

//
//    FILE: CRC_test.ino
//  AUTHOR: Rob Tillaart
// PURPOSE: demo
//     URL: https://github.com/RobTillaart/CRC


#include "CRC.h"
#include "printHelpers.h"  //  for the 64 bit...

char str[24] = "123456789";


void setup() {
  Serial.begin(115200);
  //Serial.println(__FILE__);

  //Serial.println("Verified with - https://crccalc.com/ \n");

}

void loop() {
  uint8_t *data = (uint8_t *)&str[0];
  //  use default parameters for calcCRCxx()
  Serial.print("TEST:\t");
  Serial.println(str);
  Serial.print("CRC8:\t");
  Serial.println(calcCRC8(data, 9), HEX);
  Serial.print("CRC16:\t");
  Serial.println(calcCRC16(data, 9), HEX);
  Serial.print("CRC32:\t");
  Serial.println(calcCRC32(data, 9), HEX);
  //  Serial.print("*CRC64:\t");
  //  uint64_t t = calcCRC64(data, 9);
  //  Serial.println(print64(t, HEX));

  Serial.println("\n\nDone...");
  delay(5000);
}


//  -- END OF FILE --

Serial monitor output is this:

17:01:52.121 -> TEST: 123456789

17:01:52.121 -> CRC8: F4

17:01:52.121 -> CRC16: A829

17:01:52.121 -> CRC32: CBF43926

17:01:52.121 ->

17:01:52.121 ->

17:01:52.121 -> Done...

I don't know if the forum will let me post a picture since i am still a new user but using the link provided in the code I checked for CRC8, CRC16 and CRC32 and as I can see CRC8 checks out with CRC-8/SMBUS and CRC32 checks out with CRC-32/ISO-HDLC but I haven't able to verify CRC16 result printed on the serial monitor with any of the CRC results provided in the calculator.

I have checked with a few other calculators and they seem to be verifying each other so I don't know what went wrong with the code so if anyone can tell me what am I doing wrong, that will be greatly appreciated.

Have you looked at the code for CRC16 in the Arduino library to see if uses the same polynomial as the online calculators you checked?

EDIT:
See CrcParameters.h:

//  CRC 16
#define CRC16_POLYNOME              0x8001 // x15 + 1 =  1000 0000 0000 0001 = 0x8001

There are other CRC16 options in that library that do have matching polynomials to your those in your online calculator. Try one of those.

No, I haven't yet delved into the source code since this was the calculator the author provided in the code itself to verify the results, i assumed it would have the correct output format to match the one used in the code.

Bad assumption. There are multiple, equally valid polynomials that can be used for CRC16. The library supports this. If you want a comparison between two CRC calculations to match, you must use the same polynomial for each.

1 Like

thanks for help, Thank you so much for guiding, we want to configure it in our touch screens pos

In CRC algorithms, the polynomial isn't the only option. There are also starting and ending XOR values.

The reveng program library has lots of useful background info, and is a great tool for figuring out what options were used in a given CRC calculation.

Yup. But given that CRC8 and CRC32 "worked" for @thunguskha, I'd be willing to bet that the library's default parameters for those are the same as what the cited online calculator uses. So, first thing I'd try is:

 Serial.println(calcCRC16(data, 9, CRC16_CCITT_POLYNOME), HEX);

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.