RobTillaart / CRC

CRC library for Arduino
MIT License
79 stars 17 forks source link

Hexa number as input #30

Closed Ktysai closed 1 year ago

Ktysai commented 1 year ago

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:

uint8_t strX = 0x01010000; //does not work

 uint8_t  strX [4] = {0x01, 0x01, 0x00, 0x00};  //  it works, but I really have to break all into bytes?

Working code:

//
//    FILE: CRC16_test.ino
//  AUTHOR: Rob Tillaart
// PURPOSE: demo
//    DATE: 2021-01-20
//    (c) : MIT

#include "CRC16.h"
#include "CRC.h"

uint8_t  strX[4] = {0x01, 0x01, 0x00, 0x00};  

CRC16 crc;

void setup()
{
  SerialUSB.begin(115200);
  delay(5000);
  SerialUSB.println(__FILE__);

  // SerialUSB.println("Verified with - http://zorc.breitbandkatze.de/crc.html \n");

  test();
}

void loop()
{
  test();
  delay(5000);
}

void test()
{
  uint16_t  strCRC = 0;
  SerialUSB.print("cred ca CRC calculat: ");

  strCRC = crc16((uint8_t *) strX, 4, 0x8005, 0, 0, false, false);
  SerialUSB.println(strCRC, HEX);

}

// -- END OF FILE --
RobTillaart commented 1 year ago

Thanks for the issue, I look into it asap.

RobTillaart commented 1 year ago

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 --
RobTillaart commented 1 year ago

Note that the LSB method is less complex.

Ktysai commented 1 year ago

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!

RobTillaart commented 1 year ago

Your welcome,

Maybe you can post a stripped version of your CRC + RFID application here so people can find it in the future!