Error getting CAN frames

Hello everyone.

I’m CAN protocol worker and fan :slight_smile:

Currently I’m working to read CAN frames in diagnostic CAN from a car (500 kBit/sec). I’m using Arduino UNO and CAN bus shield from sparkfun developer. As you know this CAN shield is achieving this task using MCP2515 to get communication with Arduino through SPI protocol and afterwards MCP2551 as transceiver between CAN protocol and physical bus.

I’m able to read CAN frames from the car using the example code provided from sparkfun:


/*
This is for use with Sparkfun’s CAN Bus Shield: https://www.sparkfun.com/products/10039
*/

#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>

void setup() {
Serial.begin(9600); // For debug use
Serial.println(“CAN Read - Testing receival of CAN Bus message”);
delay(1000);

if(Canbus.init(CANSPEED_500)) //Initialise MCP2515 CAN controller at the specified speed
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 == 0x620 and message.data[2] == 0xFF) //uncomment when you want to filter
//{

Serial.print("ID: “);
Serial.print(message.id,HEX);
Serial.print(”, ");
Serial.print("Data: ");
Serial.print(message.header.length,DEC);
for(int i=0;i<message.header.length;i++)
{
Serial.print(message.data*,HEX);*

  • Serial.print(" ");*
  • }*
  • Serial.println("");*
  • //}*
  • }}*
    }
    ---------------------------------------------------------------------------------------------------------------------
    I’m getting 26 CAN frames per second using serial monitor:
    15:16:47.042 → ID: A1, Data: 873 A4 0 0 0 0 0 0
    15:16:47.089 → ID: 2CF, Data: 80 0 B6 FF 0 4F A 5C
    15:16:47.089 → ID: 96, Data: 856 2D 80 FA 7F 2 0 20
    15:16:47.135 → ID: 2B9, Data: 8FE 0 0 4F 5D 67 7 60
    15:16:47.182 → ID: 96, Data: 8AF 25 80 F5 7F 2 0 20
    15:16:47.229 → ID: 96, Data: 843 29 80 FA 7F 2 0 20
    15:16:47.276 → ID: 2F, Data: 813 2D 38 1F 0 20 FF 3F
    15:16:47.323 → ID: 122, Data: 876 74 3 A 7E 0 0 C0
    15:16:47.370 → ID: 377, Data: 8C0 3 FF FF FF F3 3F 0
    15:16:47.370 → ID: 34B, Data: 20 0
    15:16:47.417 → ID: A1, Data: 838 A8 0 0 0 0 0 0
    15:16:47.463 → ID: A3, Data: 879 A9 0 0 0 0 0 0
    15:16:47.463 → ID: 307, Data: 81F 0 0 0 F 2 0 0
    15:16:47.510 → ID: 96, Data: 820 27 80 F5 7F 2 0 20
    15:16:47.557 → ID: 96, Data: 838 2B 80 FA 7F 2 0 20
    15:16:47.604 → ID: 96, Data: 835 2F 80 FA 7F 2 0 20
    15:16:47.651 → ID: 2B9, Data: 8FE 0 0 4F 5D 67 7 60
    15:16:47.698 → ID: 96, Data: 85D 27 80 FA 7F 2 0 20
    15:16:47.745 → ID: 96, Data: 838 2B 80 FA 7F 2 0 20
    15:16:47.745 → ID: 96, Data: 835 2F 80 FA 7F 2 0 20
    15:16:47.792 → ID: B1, Data: 8D2 2 66 0 0 CF 47 28
    15:16:47.838 → ID: 2F, Data: 8CD 27 38 1F 0 20 FF 3F
    15:16:47.885 → ID: 2F, Data: 861 2B 38 1F 0 20 FF 3F
    15:16:47.932 → ID: A1, Data: 8BA A1 0 0 0 0 0 0
    15:16:47.979 → ID: 3BC, Data: 830 0 0 0 0 0 0 0
    15:16:47.979 → ID: B1, Data: 856 6 66 0 0 CF 47 28
    If I use other kind of external sniffing tool such as CANOE CANalyzer, I’m getting around 1100 CAN frames per second.
    Does anyone know why I am getting this problem with Arduino and CAN Bus shield?
    Do you think it is because of not enough processing time?
    Do you think it is because of SPI protocol is in the middle?
    Or something wrong in the code? Because I just took as a first moment sparkfun example
    Any kind of support is welcome and of course will allow the progress of the project: to graph signals shared into CAN bus such as gas pedal, brake pedal, shift position in OLED display :slight_smile:
    Regards.
    Carlos Esparza.

have you tested higher speeds than...

Serial.begin(9600); // For debug use

A higher baud rate will help.

However, I would be surprised if the MCP2515/Uno combination is capable of matching the capture rate of a commercial CAN analyser which typically uses a 32 bit ARM Cortex microcontroller with integrated CAN controller.

For a MCP2515/Uno combination you'll probably need to setup the filters inside the MCP2515 to only capture the particular messages you are interested in, rather than trying to capture everything.

Really thanks friends,

I’ve changed serial baud rate up to 2000000 bit/sec and now I’m getting around 700 CAN frames per second. Really soon I will try through Microchip test board and MPLAB X to configure 32 bits MCU :slight_smile:

Meanwhile I’m really excited for the results.

Thanks guys for the support. Let us continue with the project and soon I’ll share at https://forum.arduino.cc/.

Best regards :slight_smile: