Clear CANbus buffer

Hi All,

I am using the Arduino Mega 2560 in combination with the Seeedstudio CAN-BUS Shield V2.0.
Library v1.0.0 (older version)
https://www.arduinolibraries.info/libraries/can-bus-shield

I am trying to create a test device which I connect to a CAN module and request some data.
When the tested CAN module is decoupled and a new one is attached which is defective or not connected at all, then the old CAN messages are still present when reading the CAN message with
CAN.readMsgBufID(&id, &RecMessageLen, RecMessage.U8);

Is there a way to clean the buffer?
Or should I set them all to Zero before reading the MsgBufID?

Is anyone with the change to library v2.3.0, would it improve this part as well?
Edit: tried the new library version, normal operations, that version is working with the Raspberry version as well..

Thanks.

This is for now done with the default demo "receive_check":

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


// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;

MCP_CAN CAN(SPI_CS_PIN);                                    // Set CS pin

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

    while (CAN_OK != CAN.begin(CAN_500KBPS))              // init can bus : baudrate = 500k
    {
        Serial.println("CAN BUS Shield init fail");
        Serial.println(" Init CAN BUS Shield again");
        delay(100);
    }
    Serial.println("CAN BUS Shield init ok!");
}


void loop()
{
    unsigned char len = 0;
    unsigned char buf[8];

    if(CAN_MSGAVAIL == CAN.checkReceive())            // check if data coming
    {
        CAN.readMsgBuf(&len, buf);    // read data,  len: data length, buf: data buf

        unsigned long canId = CAN.getCanId();
        
        Serial.println("-----------------------------");
        Serial.print("Get data from ID: 0x");
        Serial.println(canId, HEX);

        for(int i = 0; i<len; i++)    // print the data
        {
            Serial.print(buf[i], HEX);
            Serial.print("\t");
        }
        Serial.println();
    }
}

Please post your current code

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

If you get errors when compiling please copy them from the IDE using the "Copy error messages" button and paste the clipboard here in code tags

Added code to first post

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