Hey everyone! I'm working on CAN bus project but unfortunately I'm stuck on something. I have a sketch that receives the CAN data and stores it as a 'const' which is then sent out to the serial monitor. A received message, for example, looks like this:
[091] (8) : 00008500FFFFFFFF
The code that does this looks like this:
if (CAN.available())
{
CanMsg const msg = CAN.read();
Serial.println(msg);
}
How do I separate the message into the various parts so that it looks like this?
A = 91
B = 8
C = 00 (or just 0)
D = 00
E = 85
F = 00
G = FF
H = FF
I = FF
J = FF
To be clear, I'm using the Arduino_CAN library for this and an UNO R4 minima.
Interesting those are hex numbers but no clue as to what they represent. You are getting 8 bytes and trying to decode them to 10 bytes. More information is needed before we can help. What is the message source, what is the data protocol it is using? Are there symbols involved in the message? Is it a multi part message, etc.?
I don't fully understand it myself but I'm pretty sure that the first number, the [091] is the node ID number, the (8) might just be a message saying how many bytes there are - in this case 8 and the rest of the message is the actual data, which in this case consists of 8 bytes.
what these 8 bytes are depends from the register 91. 0xFF is for sure a filler. 0x85 is 133 or 34048, and 34048 can means value 2048 over middle point of 32000.
The default print behavior is set by the printTo method.
Use that as starting point for your own version that prints the data in whatever format you desire.
I thought I'd share my findings here in case someone in the future has the same problem as me.
If you want to extract the 8 bytes of data from the CAN message you can do so with this bit of code:
if (CAN.available())
{
CanMsg const msg = CAN.read();
Serial.print(msg.data[0],HEX); Serial.print(" ");
Serial.print(msg.data[1],HEX); Serial.print(" ");
Serial.print(msg.data[2],HEX); Serial.print(" ");
Serial.print(msg.data[3],HEX); Serial.print(" ");
Serial.print(msg.data[4],HEX); Serial.print(" ");
Serial.print(msg.data[5],HEX); Serial.print(" ");
Serial.print(msg.data[6],HEX); Serial.print(" ");
Serial.println(msg.data[7],HEX);
int C = msg.data[0];
int D = msg.data[1];
}