can bus project

hi,bought a can bus shield on ali to connect to can bus
and connecting via obd2 to db9 connector,
however i can`t get any information from the car,what kind of problem can it be?

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

long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];

MCP_CAN CAN0(10); // Set CS to pin 10

void setup()
{
Serial.begin(115200);
CAN0.begin(CAN_250KBPS); // init can bus : baudrate = 500k
pinMode(2, INPUT); // Setting pin 2 for /INT input
Serial.println("MCP2515 Library Receive Example...");
}

void loop()
{
if(!digitalRead(2)) // If pin 2 is low, read receive buffer
{
CAN0.readMsgBuf(&len, rxBuf); // Read data: len = data length, buf = data byte(s)
rxId = CAN0.getCanId(); // Get message ID
Serial.print("ID: ");
Serial.print(rxId, HEX);
Serial.print(" Data: ");
for(int i = 0; i<len; i++) // Print each byte of the data
{
if(rxBuf < 0x10) // If data byte is less than 0x10, add a leading zero

  • {*
  • Serial.print("0");*
  • }*
    _ Serial.print(rxBuf*, HEX);_
    _
    Serial.print(" ");_
    _
    }_
    _
    Serial.println();_
    _
    }_
    _
    }[/quote]_
    _
    can3.ino (1.2 KB)*_

Does the library you are using have a loopback test program?
If not, use this library

and run the loopback test.
Show the results.

mikb55:
Does the library you are using have a loopback test program?
If not, use this library
GitHub - coryjfowler/MCP_CAN_lib: MCP_CAN Library
and run the loopback test.
Show the results.


i got this,does it mean that i used wrong libraries?

hahabuka:
i got this,does it mean that i used wrong libraries?

Not necessarily. It's just that some libraries are more up to date than others and have better examples. Generally speaking they all work to some degree.

The loopback results you have posted are good so the next step is to run the CAN_receive program and post those results.

If you don't receive any data try changing the CAN bus speed from CAN_500KBPS to CAN_250KBPS.

If you still don't see any data then you need to search the web for car forums that deal with your specific make, model and year of car and include the phrase 'CAN bus hacking'. It's possible that you may need to send 'request' messages to the car before it responds with any data. Again this is all car specific so this information is probably out there on the web somewhere.