Filtering CAN bus data

Hello all,

I have a question regarding programming when using a CAN bus shield. I would like to filter the data that is sent and only display certain data frames. I use the following library: GitHub - autowp/arduino-mcp2515: Arduino MCP2515 CAN interface library.

For example I would like to get only the following data frame: "18A 63C C0 0 6 D0 0".

For reading I use the following code:

#include <SPI.h>
#include <mcp2515.h>

struct can_frame canMsg;
MCP2515 mcp2515(10);


void setup() {
  Serial.begin(115200);
  
  mcp2515.reset();
  mcp2515.setBitrate(CAN_125KBPS);
  mcp2515.setNormalMode();
  
  Serial.println("------- CAN Read ----------");
  Serial.println("ID  DLC   DATA");
}

void loop() {
  if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
    Serial.print(canMsg.can_id, HEX); // print ID
    Serial.print(" "); 
    Serial.print(canMsg.can_dlc, HEX); // print DLC
    Serial.print(" ");
    
    for (int i = 0; i<canMsg.can_dlc; i++)  {  // print the data
      Serial.print(canMsg.data[i],HEX);
      Serial.print(" ");
    }

    Serial.println();      
  }
}

Could you maybe help me with this?

What are you currently getting?

Currently I get about 30 different data frames. Now I would like to filter this output data to get an overview.

Please list preferably several examples of what you are getting.

Hint: the data you want always shows up in the same place.

with your main code you can simple do this! :wink:
(compiles, NOT tested!)

#include <SPI.h>
#include <mcp2515.h>

struct can_frame canMsg;
MCP2515 mcp2515(10);


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

  mcp2515.reset();
  mcp2515.setBitrate(CAN_125KBPS);
  mcp2515.setNormalMode();

  Serial.println("------- CAN Read ----------");
  Serial.println("ID  DLC   DATA");
}

void loop() {
  if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
    if (canMsg.can_id == 0x18A) { //printout only message frame 0x18A
      Serial.print(canMsg.can_id, HEX); // print ID
      Serial.print(" ");
      Serial.print(canMsg.can_dlc, HEX); // print DLC
      Serial.print(" ");

      for (int i = 0; i < canMsg.can_dlc; i++)  { // print the data
        Serial.print(canMsg.data[i], HEX);
        Serial.print(" ");
      }

      Serial.println();
    }
  }
}

hope that helps...

Are you using Standard (11-bit) addressing or Extended (18-bit) addressing?

I think you can filter on either the Extended Address or the Standard Address plus the first two data bytes.