I'm trying to sniff some CAN messages but without luck. The RxLed is blinking but there is nothing in the terminal.
I've tried to change the CAN speed but it didn't help.
I'm stuck with it. Please give me a hint what to do.
// demo: CAN-BUS Shield, receive data with check mode
// send data coming to fast, such as less than 10ms, you can use this way
// loovee, 2014-6-13
#include <SPI.h>
#define CAN_2515
// #define CAN_2518FD
// Set SPI CS Pin according to your hardware
#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD)
// For Wio Terminal w/ MCP2518FD RPi Hat:
// Channel 0 SPI_CS Pin: BCM 8
// Channel 1 SPI_CS Pin: BCM 7
// Interupt Pin: BCM25
const int SPI_CS_PIN = BCM8;
const int CAN_INT_PIN = BCM25;
#else
// For Arduino MCP2515 Hat:
// 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 = 10;
const int CAN_INT_PIN = 2;
#endif
#ifdef CAN_2518FD
#include "mcp2518fd_can.h"
mcp2518fd CAN(SPI_CS_PIN); // Set CS pin
#endif
#ifdef CAN_2515
#include "mcp2515_can.h"
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
#endif
void setup() {
SERIAL_PORT_MONITOR.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
SERIAL_PORT_MONITOR.println("CAN init fail, retry...");
delay(100);
}
SERIAL_PORT_MONITOR.println("CAN 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_PORT_MONITOR.println("-----------------------------");
SERIAL_PORT_MONITOR.print("Get data from ID: 0x");
SERIAL_PORT_MONITOR.println(canId, HEX);
for (int i = 0; i < len; i++) { // print the data
SERIAL_PORT_MONITOR.print(buf[i], HEX);
SERIAL_PORT_MONITOR.print("\t");
}
SERIAL_PORT_MONITOR.println();
}
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/
Do you see "CAN init" in the serial monitor?
If not then check that the serial port baud rate matches the speed set in the sketch.
Another thing could be the clock.
My CAN Module stated 8MHz, which I had to set porperly:
uint8_t CanSpeed = 13; //500KBPS
bool CAN_Active = CAN0.begin(MCP_ANY, CanSpeed, MCP_8MHZ) == CAN_OK;
CAN0.setMode(MCP_LISTENONLY);
Yes, I see "CAN init". I can even sniff other devices, but not this one. And I don't know why, since the LED is blinking, then the CAN board is detecting all messages.
Tried all 3 modes (classic, listen only and normal). Same result if I can call it "result".
The clock is definitely 16MHZ.
Could it be that the pinout is wrong? I changed SPI_CS_PIN but I have no clue what CAN_INT_PIN does.
const int SPI_CS_PIN = 10;
const int CAN_INT_PIN = 2;
Your code isn't using interrupts so the CAN_INT_PIN doesn't matter.
Is the problematic CAN bus from a relatively modern car or device?
It's possible that it's using the newer CAN-FD standard which cannot be read by the relatively old MCP2515 device.
In some libraries when a reference to 'len' is passed, it is expected to contain the size of the buffer. Try adding this line: "len = sizeof buf;" just above:
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
doesn't work. I believe there is no data at all.
Bosch ECU 2011
I don't think it uses CAN-FD, and even if it does, why is the LED blinking?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.