I'm trying to read CAN messages in the Serial Monitor of the Arduino IDE, but it's not working. I'm using the MKR WiFi 1010 together with the MKR CAN Shield. The CAN bus baud rate is 666.666 kbit/s — I verified this with another tool and confirmed that messages are being sent. I've installed the CAN library and connected the CAN Shield to the CAN network (CANL, CANH, and GND). Despite this setup, I don't see any messages in the Serial Monitor. Has anyone experienced this or knows what might be going wrong?
My code:
#include <CAN.h>
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("CAN Receiver");
// Start de CAN-bus op 666666 kbps
if (!CAN.begin(666666)) {
Serial.println("Starting CAN failed!");
while (1);
} else {
Serial.println("CAN started at 666666 kbps");
}
// Voeg deze regel toe om alle CAN-ID's te accepteren
CAN.filter(0, 0); // accepteer alle ID's
}
void loop() {
// try to parse packet
int packetSize = CAN.parsePacket();
if (packetSize) {
// received a packet
Serial.print("Received ");
if (CAN.packetExtended()) {
Serial.print("extended ");
}
if (CAN.packetRtr()) {
// Remote transmission request, packet contains no data
Serial.print("RTR ");
}
Serial.print("packet with id 0x");
Serial.print(CAN.packetId(), HEX);
if (CAN.packetRtr()) {
Serial.print(" and requested length ");
Serial.println(CAN.packetDlc());
} else {
Serial.print(" and length ");
Serial.println(packetSize);
// only print packet data for non-RTR packets
while (CAN.available()) {
Serial.print((char)CAN.read());
}
Serial.println();
}
Serial.println();
}
}