Closed Ktysai closed 1 year ago
Thanks for the issue, I look into it asap.
updated your post to support syntax highlighting (readability)
CRC codes are typical byte stream oriented, a message comes in over the internet, token ring or from a disk or slow satellite phone, and the CRC is calculated while these bytes drop in.
You can have larger data objects like an uint32_t or a IEEE754 float (4 byte) an IEEE754 double (8 byte) and you need to split them into bytes before handing them over to the CRC algorithm. If you have a float and you have 4 bytes you have 24 ways to feed the CRC, although only two are used: MSB first or LSB first. It is important to use the CRC in the same way at the sending and receiving end, otherwise error!.
(MSB = Most Significant Byte, LSB = Least SIgnificant Byte)
Check this
//
// FILE: CRC16_test.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo
// DATE: 2021-01-20
// (c) : MIT
#include "CRC16.h"
#include "CRC.h"
CRC16 crc(0x8005, 0, 0, false, false);
void setup()
{
Serial.begin(115200);
delay(5000);
Serial.println(__FILE__);
Serial.println();
// LSB first
crc.add(0x00);
crc.add(0x00);
crc.add(0x01);
crc.add(0x01);
Serial.print("reference LSB: ");
Serial.println(crc.getCRC(), HEX);
delay(1000);
crc.restart();
// MSB first
crc.add(0x01);
crc.add(0x01);
crc.add(0x00);
crc.add(0x00);
Serial.print("reference MSB: ");
Serial.println(crc.getCRC(), HEX);
delay(1000);
crc.restart();
}
void loop()
{
uint32_t big = 0x01010000;
test_lsb(big);
test_msb(big);
delay(10000);
}
void test_lsb(uint32_t value)
{
crc.restart();
for (int i = 0; i < 4; i++)
{
uint8_t n = value & 0xFF; // split of 1 byte LSB first
crc.add(n);
value = value / 256; // or shift 8 bits
}
Serial.print("cred ca CRC calculat LSB: ");
Serial.println(crc.getCRC(), HEX);
}
void test_msb(uint32_t value)
{
crc.restart();
int shift = 24;
for (int i = 0; i < 4; i++)
{
uint8_t n = (value >> shift) & 0xFF; // split of MSB first
crc.add(n);
shift -= 8;
}
Serial.print("cred ca CRC calculat MSB: ");
Serial.println(crc.getCRC(), HEX);
}
// -- END OF FILE --
Note that the LSB method is less complex.
Thank you, Rob!
In my case the hexa string come from a RFID reader. It has a CRC checksum provided and I want to check it to be sure.
You've gave me a wonderful starting point!
Your welcome,
Maybe you can post a stripped version of your CRC + RFID application here so people can find it in the future!
I Rob!
It's a cool CRC library. Useful for a n00b like me!
For Hexa inputs like 0xFFFF443443 or 0xAA78DD56898EE55EE33, how can I define the input?
Example:
Working code: