Storing and handling data types to use with CRC8 library

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

Please DO NOT start a new thread for every question on this topic.

You are indeed confused about data types. In the following line, the character array str[] contains binary data, and is initialized by using the human readable HEX representation of binary numbers.

char str[4] = {0x03,0x02,0x69,0x47};

If you want to store an additional byte with the binary CRC value, declare str[] to be of length 5 and write

char str[5] = {0x03,0x02,0x69,0x47,0};
//...
str[4] = crc8((uint8_t *)str, 4, 0xD5);

how can I convert that such as it is stored as hexadecimal in the format of character str

Do you have a really good reason to do so, or are you still confused?

I am sorry for creating a new thread for the same topic, thought had to create a new one when I accepted an answer.

I ran the code you provided str[4] = crc8((uint8_t *)str, 4, 0xD5); with the declared str[] of length 5. When you put str[4], is it because the stored value goes from 0 to 4 right?

I tried serial printing the character array at position 4 using Serial.println(str[4]); to check whether I would see the binary CRC value but a strange character appear instead (⸮), tried serial printing the whole character array Serial.println(str); and I get:

how can I convert that such as it is stored as hexadecimal in the format of character str
The reason is that I am obtaining sensor data from another program and I exported it as a text file, those values have decimal places say 58.88888, so I multiplied it by of 100000 until they had no decimal places and their values are then stored as decimal base 10.

So I thought to use the crc8 library I would then store that base 10 decimal as a hexadecimal and once the data reaches the receiver I recover the original data by dividing it by 100000.

You need to learn more about arrays. If you declare str[5], then array indices 0 to 4 address the five binary values stored in str[0..4].

To print the binary value stored in str[4] in human readable hexadecimal format, use

Serial.println(str[4], HEX);

how can I convert that such as it is stored as hexadecimal in the format of character str

There is almost never a reason to do that. Please explain what you actually need to do, not what you think you should do.

Do not confuse binary data, stored in a computer, with various human readable representations of data (text strings).

Please explain what you actually need to do, not what you think you should do.

Transmit a message, which is an integer, say 50489671 with its CRC value to the receiver. Storing this as a value.

Serial.println(str[4], HEX);

Why are there F's before 8C?

Thanks a lot

Bug in Serial.print(). Get around it with
Serial.println( (0xFF & str[4]), HEX);

Transmit a message

How, exactly, will this message be transmitted, and why do you describe it as an integer, if it is an array of byte values?

How, exactly, will this message be transmitted, and why do you describe it as an integer, if it is an array of byte values?

Using two MCP2515 CAN Bus Interface Modules, and the <SPI.h> and <mcp2515.h> libraries.

This is my current set-up:

The context is I want to transmit a GPS coordinate value from one CAN bus module to the other one, this coordinate is in the format of a number, for example: 22.2222222. I cannot send numbers with decimal places using the CAN bus modules so I converted them into an integer.

image

Data is sent through the module with this function from the mcp2515.h library, each one of canMsgLIDAR.data[] can store up to one byte of data.

So canMsgLIDAR_1.data[0] would equal char str[0], canMsgLIDAR_1.data[1] = char str[1] and so on. I want to be able to take that integer, describe it as an array of byte values plus its CRC value at the end, and transmit it using the above function.

Edit: forget my question about integer to char array, I will just input the data directly as binary values. With that being said, is it possible to input data from a .txt file to Arduino?

To copy binary data arrays, use memcpy()

memcpy(canMsgLIDAR_1.data,str,5);

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