Read and write on CAN bus

Hi all,
I am working with Arduino Uno and mcp2515 CAN shield v 1.5 . First i am trying to write on can bus then trying to read, so i am getting successful message after writing but i am not able to read from can bus


#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 4                             // Set INT to pin 4
MCP_CAN CAN0(9);                               // Set CS to pin 9


void setup()
{
  Serial.begin(115200);
  
  // 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_8MHZ) == CAN_OK)
    Serial.println("MCP2515 Initialized Successfully!");
  else
    Serial.println("Error Initializing MCP2515...");
  
  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...");
}
byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
void loop()
{
  byte sndStat = CAN0.sendMsgBuf(0x100, 0, 8, data);
  if(sndStat == CAN_OK){
    Serial.println("Message Sent Successfully!");
  } else {
    Serial.println("Error Sending Message...");
  }
  
  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);
      }
    }
    delay(1000);
        
    Serial.println();
  }
}

Hi
When you reading - do you receive any diagnostic output in Serial?
What is a device used as transmitter when you try to read something from the bus?

Currently I am trying to communicate through mcp2515 can controller, is this possible to write and read through one can bus only which is connected to can shield and Arduino uno board?

Post a simple schematic showing how this bus is interconnected. Show any external parts such as resistors etc. CAN is an in frame response protocol forcing the external hardware to be reasonably correct.

I don't have schematics, but I have posted hardware which I am using down board is Arduino uno board upper one is CAN shield v1.5 in between CANH and CANL I have placed 120ohm resistor.


How do you know this?

I am checking with return value of sendMsgBuf so it is printing Message sent successfully.

Is the termination on or off on the shield? Are any filters set, Is the baud the same?

you do understand that for you to RECIEVE a message, there needs to be ANOTHER device in the CAN netowrk that's sending that message ?

1 Like

baud rate is same for both devices there are not any filter set. I don't have any idea about termination it should be on or off?

The is required on both ends of the bus but nothing in the middle or elsewhere. I think those switches turn the termination on or off for that shield. Are you using the demo code that came with the CAN library, if not load that and give it a try. Also set the baud to a low value as that will help in getting it operating

I do not have one, post a link to it.

// CAN Send Example
//

#include <mcp_can.h>
#include <SPI.h>
MCP_CAN CAN0(10);     // Set CS to pin 10
void setup()
{
  Serial.begin(115200);

  // 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_8MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!");
  else Serial.println("Error Initializing MCP2515...");

  CAN0.setMode(MCP_NORMAL);   // Change to normal mode to allow messages to be transmitted
}

byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};

void loop()
{

  // send data:  ID = 0x100, Standard CAN Frame, Data length = 8 bytes, 'data' = array of data bytes to send
  byte sndStat = CAN0.sendMsgBuf(0x100, 0, 8, data);
  if(sndStat == CAN_OK){
    Serial.println("Message Sent Successfully!");
  } else {
    Serial.println("Error Sending Message...");
  }
  delay(100);   // send data per 100ms
}


This is code I am trying to flash through Arduino but not able send message getting prints like Error Sending Message...

@revajd123
Please answer the question in #9
What is the "another device" in your tests?

1 Like

Since you want us to play guessing games by not answering questions I am out. Good Luck!

I am using teensy board with TJA1050 CAN trans receiver one side and Arduino with can shield another side.