I have successfully done filters that worked great in the past, but now struggling to make any headway. The library example gives odd results, data that i know is not on the bus. Below i was doing a test to see if I could only see ID 222 on the bus, but it showed only 258 and 259 (and those IDs are not even on the bus)
#include <SPI.h>
#include "mcp_can.h"
#define SPI_CS_PIN D7
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
unsigned char flagRecv = 0;
unsigned char len = 0;
unsigned char buf[8];
char str[20];
void setup()
{
Serial.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS FAIL!");
delay(100);
}
Serial.println("CAN BUS OK!");
/*
* set mask, set both the mask to 0x3ff
*/
/*
* set filter, we can receive id from 0x04 ~ 0x09
*/
CAN.init_Mask(0, 0, 0x3ff);
CAN.init_Filt(0, 0, 0x222); // there are 6 filter in mcp2515
CAN.init_Filt(1, 0, 0x222); // there are 6 filter in mcp2515
CAN.init_Mask(1, 0, 0x3ff);
CAN.init_Filt(2, 0, 0x222); // there are 6 filter in mcp2515
CAN.init_Filt(3, 0, 0x222); // there are 6 filter in mcp2515
CAN.init_Filt(4, 0, 0x222); // there are 6 filter in mcp2515
CAN.init_Filt(5, 0, 0x222); // there are 6 filter in mcp2515
}
void loop()
{
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if get data
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
Serial.println("\r\n------------------------------------------------------------------");
Serial.print("Get Data From id: ");
Serial.println(CAN.getCanId());
for(int i = 0; i<len; i++) // print the data
{
Serial.print("0x");
Serial.print(buf[i], HEX);
Serial.print("\t");
}
Serial.println();
}
}
// END FILE