So first I needed it bo send to Serial as a hex string, so solution I've found and used was
String frame = "CAN:";
char dataString[1] = {0};
sprintf(dataString, "%03X", can1Msg.can_id);
frame += dataString;
for (int i = 0; i < can1Msg.can_dlc; i++) { // print the data
char dataString[1] = {0};
sprintf(dataString, "%02X", can1Msg.data[i]);
frame += " " + String(dataString);
}
Doesn't seem elegant byt it did the job.
But then I needed to do some checking of ID and specific bytes
So I've saved all String ins String bytes[8]
So now I could something like
if (id == "2A0" && bytes[0] == "A0") {
And that was almost over, but then I realized that I need to do some checking of Integer number that is saved on 3 bytes.
E.G byte[2] + byte[3] + byte[4] which would something like 0020A0 whih will be 8352 as an Int
But converting things around gave all the problems such us String to const char* etc.
I wanted to convert __u8 to byte array so I can do checks and conversions and eventually convert it to string, but I've failed.
Can anyone suggest best approach for this ?
That’s already a bug - you have only 1 Byte in your char buffer and try to store 4 (3 digits and trailing null).
Same goes in the for loop
Don’t go to string representation, just compare the bytes from the array with && and print items separately to form the ASCII output, you likely don’t need in your code the full representation
Usually the __uN (like __u8) are Linux-specific typedefs predating the standard uintN_t (Like uint8_t) that were added later in C and C++ specification but made their ways in many libraries such as the CAN libraries that can be cross platforms
So I think it’s a fair assumption to treat them as unsigned char (bytes).
That's the bit I am not sure about and yeah I got a warning on that, as you can shift it with << 16.
What it should be is speed multiplied by 2 I believe. I will have to check that on the car.
But even then max value of the int would be something like 600 (?) meaning it's 300km/h, so I think byte[3] would always be 00.
But what I don't understand, what if I need such an integer ? what should I use, unsinged long ? That still doesn't silence warning, so am I right to assume that library would have to be adjusted with types ?
or something like int speed = (uint32_t)msg.data[5] | ((uint32_t)msg.data[4] << 8) | ((uint32_t)msg.data[3] << 16);
You've missed it, or I have - you're still assigning a 32 bit value to the int (left side of the expression), which will depend upon what the native int size is for your particular platform.
C