Getting garbage values while reading CAN frames

Hello everyone, I am new to CAN Bus. I used CANBed V1 Arduino board to read the CAN bus frames that are present on the CAN bus network. the below code was taken from internet and modified a bit to suit my car. The CAN bus initialisation is happening but I am getting garbage values i.e. single message frame is repeated every single time.

below is the code:

    ```
#include <mcp_can.h>
#include <SPI.h>

long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128];                        // Array to store serial string

#define CAN0_INT 2                              // Set INT to pin 2
MCP_CAN CAN0(17);                               // Set CS to pin 17

bool foundDefault = false;

void setup()
{
  Serial.begin(115200);
  while (!Serial);

  //Serial.println("CAN Receiver --");
  // Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
  if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK)
  {
    Serial.println("MCP2515 Initialized Successfully! for 500");
  }
  else
  {
    Serial.println("Error Initializing MCP2515... for 500");
    return;
  }
  
  CAN0.setMode(MCP_NORMAL);                     // Set operation mode to normal so the MCP2515 sends acks to received data.
  pinMode(CAN0_INT, INPUT);                            // Configuring pin for /INT input
  
  Serial.println("MCP2515 Library Receive Example...");
}

void loop()
{
  
  if(!digitalRead(CAN0_INT))                         // If CAN0_INT pin is low, read receive buffer
  {
    CAN0.readMsgBuf(&rxId, &len, rxBuf);      // Read data: len = data length, buf = data byte(s)
    
    if((rxId & 0x80000000) == 0x80000000)     // Determine if ID is standard (11 bits) or extended (29 bits)
      sprintf(msgString, "Extended ID: 0x%.8lX  DLC: %1d  Data:", (rxId & 0x1FFFFFFF), len);
      
    else
      sprintf(msgString, "Standard ID: 0x%.3lX       DLC: %1d  Data:", rxId, len);
      
  
    Serial.print(msgString);
  
    if((rxId & 0x40000000) == 0x40000000){    // Determine if message is a remote request frame.
      sprintf(msgString, " REMOTE REQUEST FRAME");
      Serial.print(msgString);
      
    } else {
      for(byte i = 0; i<len; i++){
        sprintf(msgString, " 0x%.2X", rxBuf[i]);
        Serial.print(msgString);
        
      }
    }
        
    Serial.println();
  }
}
   
I would also like to know what is the use of 'msgString' variable.

msgString is a text buffer; the first two and the last sprintf in your code create a message with some text and a text representation of (parts of) the received data. I'm not familiar with CAN bus but guess the data is binary.

I can't help you with the rest as I don't have experience with CAN.

Can you post the original code that your code was based on?

Thanks for the reply, this is the original code, I just changed the baud rate of Serial Communication and Chip Select(CS) pin to 17, rest everything is as such in the built in example.

for that board, pin 17 is CS and pin 7 is the INT pin; so try changing

#define CAN0_INT 2

to

#define CAN0_INT 7

and try again.

hope that helps....

will try and update

I get the same values after changing the interrupt pin, still thanks for suggestion.

what are you getting?

could you post a sample of the output please?

also how are you connecting to your vehicle's CAN BUS?

there is also an CAN library SPECIFICALLY made for this board. did you try it?

Screenshot 2023-07-06 102815

I have connected the CAN high, CAN low, GND and Power pin to the OBD2 port

will try this library and updated you

Thank you for sharing the output.

tbh that does not look like garbage data to me! what makes you think it is?

depending on the make and year of your vehicle the OBD port may well be restricted ie only limited data can be read/received through it.

yeah, I understand that, but all I get is only one data repeatedly when i run it, some times id is x124, other times it is x007, 0x318 only these Ids are repeated every time. and their data values are random. not to mention the DLC is 8 for most messages

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