Hello, I have the following code using the RobTillaart/CRC library.
#include "CRC8.h"
#include "CRC.h"
char str[4] = {0x03,0x02,0x69,0x47};
char str2[5] = {0x03,0x02,0x69,0x47,0x8c};
CRC8 crc;
void setup()
{
Serial.begin(115200);
generateCRC();
verifyCRC();
}
void loop()
{
}
void generateCRC()
{
Serial.println(crc8((uint8_t *)str, 4, 0xD5), HEX);
}
void verifyCRC()
{
Serial.println(crc8((uint8_t *)str2, 5, 0xD5), HEX);
}
This prints out 8C which is the correct CRC code for the giving message and polynomial, and zero which is verifying that there is no transmission error (str2 includes the CRC value after the message).
I want to be able to store these CRC values as variables instead of directly serial printing them.
I tried:
#include "CRC8.h"
#include "CRC.h"
//long decimalLat = 50489671;
char str[4] = {0x03,0x02,0x69,0x47};
//char str2[5] = {0x03,0x02,0x69,0x47,0x8c};
CRC8 crc;
void setup()
{
Serial.begin(115200);
test();
}
void loop()
{
}
void test()
{
//Serial.println(crc8((uint8_t *)str, 4, 0xD5), HEX);
byte crcCodeDec = crc8((uint8_t *)str, 4, 0xD5); //serial printing this gives 140 (decimal)
Serial.println(crcCodeDec);
String crcCodeHex = String(crcCodeDec, HEX); //convert 140 decimal to hexadecimal
Serial.println(crcCodeHex);
}
Which prints out 140 (decimal equivalent of 8c) and 8c.
I want to be able to append 'crcCodeHex' which is the CRC (8c) to the end of my char str which contains the message I want to transmit. So after appending it I want to end up with the commented str2.
I searched online and I tried using strlcpy to do this but when I print out the char it shows squares which means I am using the wrong data type for this?
char str[5] = {0x03,0x02,0x69,0x47};
void setup() {
Serial.begin(115200);
//Serial.println(sizeof str);
// print all the strings (some are empty)
for (byte i = 0; i < 5; i++) Serial.println(str[i]);
// you can use strlcpy() to initialize a string without overflow
strlcpy(str[5], 0x9c,4); //not sure if size 4 is correct?
}
void loop() {}
The other question I have is if the message I want to transmit is originally in decimal and stored as a long, how can I convert that such as it is stored as hexadecimal in the format of character str?
So
long decimalLat = 50489671; //decimal equivalent of 0x3026947.
From this long, obtain:
char str[4] = {0x03,0x02,0x69,0x47}; // 0x03026947, the hexadecimal equivalent of decimalLat.
Thanks