Hi glv,
I am able to get your example working perfectly! Many thanks for this, as you saved me a lot of hours of frustration!
However, when I am trying to use it with my actual application, it is not working correctly.
I have discovered the issue has to do with converting the string in Arduino code to a char.
If I use this code in Arduino, it works fine:
void SSStr(String str) {
char msg[] = "Fader Presidential"; ////HARD CODING THE STRING TO A CHAR ARRAY
uint32_t crc32_res = crc32.calc((uint8_t *)msg, strlen(msg));
Serial.write(HEADER);
Serial.print(',');
Serial.print(str);
Serial.print(',');
Serial.print(crc32_res);
Serial.write(LF);
delay(ComDelay);
}
In this case, with the hard-coded character array, it is sending a correct crc and it matches what processing generates.
However, I need to be able to send various type of messages, so I don’t want to hard code the char array. I want to use the str parameter that is passed into the function. But when I try to convert a string to a char array, it is not calculating correctly for some reason.
Here is the code that generates the wrong CRC from Arduino:
void SSStr(String str) {
int str_len = str.substring(3).length() + 1;
char str_array[str_len];
str.toCharArray(str_array, str_len);
uint32_t crc32_res = crc32.calc((uint8_t *)str_array, strlen(str_array));
Serial.write(HEADER);
Serial.print(',');
Serial.print(str);
Serial.print(',');
Serial.print(crc32_res);
Serial.write(LF);
delay(ComDelay);
}
What comes across is the string value with the wrong CRC calculated.
Also, here is how I am calling it:
SSStr("ID,Fader Presidential");
Any idea what I’m doing wrong here? It’s obviously the conversion of the string to the char array that I’m getting wrong.
Thanks for any insights. I hope this is a minimal and clear enough example as you stated.
Mike