Arduino CANBus shield output raw data

I picked up an Arduino Uno and the Sparkfun CANBus shield to do some tinkering, however, while I've gotten it working and outputting data, and I've gotten it mostly where I want it, there's one bit that I haven't been able to figure out. I used the info here to get it going, and initially, it spit out data like this: 'ID: 1BC, Data: 0 0 0 0 0 0 0 '. I don't need the 'ID' or 'Data' added, just want the raw output, so I got it to where it now outputs like this: 'F9 0 23 40 0 0 0 0 FF'. That's where what I can't figure out resides. It's dropping leading zeros, which I don't want it to do. That last string should actually be '0F9 00 23 40 00 00 00 00 FF'. How would I go about getting it to spit out the raw CAN messages, without dropping zeros and whatnot? For reference, below is the exact code that's running in it as of now (yes, I set the serial baud rate really high so as to not have that be a bottleneck to the 500kbps CAN traffic...).

While I'm at it, there is one other thing I haven't figured out... I can filter by ID using 'if(message.id == 0x100)' for example, but that is exact, so only ID 100 would be displayed - how would I go about masking? So if I want to see every ID that is '1**' or '1', etc?

#include <Canbus.h>  // don't forget to include these
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>

void setup()
{
Serial.begin(921600);
//Initialise MCP2515 CAN controller at the specified speed
if(Canbus.init(CANSPEED_500))
	Serial.println("CAN Init ok");
else
	Serial.println("Can't Init CAN");

delay(1000);
}

void loop()
{ 
tCAN message;

if (mcp2515_check_message()) 
	{
    if (mcp2515_get_message(&message)) 
	//{
    //if(message.id == 0x100)	  //filtering based on CAN bus message ID.
          {
              //Serial.print("ID: ");
               Serial.print(message.id,HEX);
               Serial.print(" ");
               //Serial.print("Data: ");
               for(int i=0;i<message.header.length;i++)
                {
                  Serial.print(message.data[i],HEX);
                  Serial.print(" ");
                }
               Serial.println(" ");
             }}//}
}

That is normal behaviour when using Serial.print(). Consider using sprintf() to format the output as you require


void setup()
{
    Serial.begin(115200);
    byte value = 5;
    Serial.println(value);
    char buffer[5];
    sprintf(buffer, "%02X", value);
    Serial.println(buffer);
}

void loop()
{
}

1 Like

I've tried to make this work and so far I haven't been able to one thing of possible note - you have 'byte value = 5;'.. It's usually a ID, followed by 8 bytes, however, that length can vary. Some packets are the ID plus 8 bytes, some are less, and I think some could be more... so the main thing there is we just don't want to be dropping the leading zeros - We don't really care about the length, we really just want to receive the packet from the CAN bus and spit it out to the serial port unaltered...

I generally print the data received as HEX bytes, that solves a lot of 'print' problems. Serial.print() interprets the data and make changes.

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