Hello!
I'm trying to write a sketch that tests the CAN hardware filters on the Arduino R4 Wifi, but they don't seem to work. I want to filter messages from a single specific standard ID, but I get all the messages from the network. Does it work for anyone? I'm sharing my sketch:
#include <Arduino_CAN.h>
void setup() {
Serial.begin(115200);
while (!Serial)
;
if (!CAN.begin(CanBitRate::BR_250k)) {
Serial.println("CAN initialization error!");
while (1)
;
}
Serial.println("CAN successfully initialized.");
// Set hardware filters
CAN.setFilterMask_Standard(0x7FF); // standard mask
CAN.setFilterId_Standard(0, 0x123); // mailbox 0
}
void loop() {
if (CAN.available()) {
// Read a frame directly
arduino::CanMsg frame = CAN.read();
Serial.print("Received ID: 0x");
Serial.print(frame.id, HEX);
Serial.print(" LEN: ");
Serial.println(frame.data_length);
for (int i = 0; i < frame.data_length; i++) {
Serial.print("Data[");
Serial.print(i);
Serial.print("] = ");
Serial.println(frame.data[i], HEX);
}
}
}
Best regards!!