Protocol fails

Hello, I need some help. My canbus codes works well, But I think needs little modify. When receiving messages I want to see codes like this 201:00;FF;03;00;32;09;00;FF But I see 201:0:FF:3:0:32:9:FF
I think I must add some codes too. My Codes below and help me for modify please :slight_smile:

#include <CAN.h>

void setup()
{
Serial.begin(9600);
Serial.println(“Starting…”);

CAN.begin(500);

}

void loop()
{

if (CAN.CheckNew())
{

if ( CAN.ReadFromDevice(&CAN_RxMsg) )
{

Serial.print(CAN_RxMsg.id,HEX);
Serial.print(";");
Serial.print(“0x”);
Serial.print(CAN_RxMsg.data[0],HEX);
Serial.print(";");
Serial.print(“0x”);
Serial.print(CAN_RxMsg.data[1],HEX);
Serial.print(";");
Serial.print(“0x”);
Serial.print(CAN_RxMsg.data[2],HEX);
Serial.print(";");
Serial.print(“0x”);
Serial.print(CAN_RxMsg.data[3],HEX);
Serial.print(";");
Serial.print(“0x”);
Serial.print(CAN_RxMsg.data[4],HEX);
Serial.print(";");
Serial.print(“0x”);
Serial.print(CAN_RxMsg.data[5],HEX);
Serial.print(";");
Serial.print(“0x”);
Serial.print(CAN_RxMsg.data[6],HEX);
Serial.print(";");
Serial.print(“0x”);
Serial.print(CAN_RxMsg.data[7],HEX);
Serial.print(";");
Serial.println();
}
}

}

Why are you printing all those "0x" and ";" ?

Anyway the problem is that the Arduino print functions have no provision to print leading zeros. Have a look at using the sprintf() function, that will allow you to create a formatted string in an array and then you can print the array.

Do you need to display with the 0s? Because if not it's still valid for program use, just move on to the real purpose of your program.


Rob