Hello everyone!
I hope someone can give me an advice.
I'm making a simple CAN-Bridge between a can-bus of my car and one of the modules. I'm using a Arduino Uno and a Dual_CAN hat with two mcp2515 can-controllers. First controller (CAN0) is connected to digital pins 10 and 2, second (CAN1) - to pins 9 and 3. Second has the 120 Ohm resistor connected as required.
Everything seems to work fine: if I connect a car's can-bus to any interface - I can see messages no problem.
But, the problem is: I'm not getting any messages from the connected module and no messages are being passed from one interface to another.
I'm trying to make it work for a week now to no avail, banging my head against the wall already - I just don't know what is wrong.
The sketch that I'm using is based on an example from MCP_CAN library. Temporarily, I've added code to forward messages from each interface to console to see what's going on:
#include <mcp_can.h>
#include <SPI.h>
unsigned long rxId;
byte len;
byte rxBuf[8];
char msgString[128];
MCP_CAN CAN0(10); // CAN0 interface usins CS on digital pin 10
MCP_CAN CAN1(9); // CAN1 interface using CS on digital pin 9
void setup()
{
Serial.begin(115200);
// init CAN0 bus, baudrate: 100k@16MHz
if(CAN0.begin(MCP_NORMAL, CAN_100KBPS, MCP_16MHZ) == CAN_OK){
Serial.print("CAN0: Init OK!\r\n");
CAN0.setMode(MCP_NORMAL);
} else Serial.print("CAN0: Init Fail!!!\r\n");
// init CAN1 bus, baudrate: 100k@16MHz
if(CAN1.begin(MCP_NORMAL, CAN_100KBPS, MCP_16MHZ) == CAN_OK){
Serial.print("CAN1: Init OK!\r\n");
CAN1.setMode(MCP_NORMAL);
} else Serial.print("CAN1: Init Fail!!!\r\n");
SPI.setClockDivider(SPI_CLOCK_DIV2); // Set SPI to run at 8MHz (16MHz / 2 = 8 MHz)
}
void loop(){
if(!digitalRead(2)){ // If CAN0 pin 2 is low, read CAN0 receive buffer
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
if((rxId & 0x80000000) == 0x80000000) // Determine if ID is standard (11 bits) or extended (29 bits)
sprintf(msgString, "CAN0 Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len);
else
sprintf(msgString, "CAN0 Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len);
Serial.print(msgString);
if((rxId & 0x40000000) == 0x40000000){ // Determine if message is a remote request frame.
sprintf(msgString, " REMOTE REQUEST FRAME");
Serial.print(msgString);
} else {
for(byte i = 0; i<len; i++){
sprintf(msgString, " 0x%.2X", rxBuf[i]);
Serial.print(msgString);
}
}
Serial.println();
CAN1.sendMsgBuf(rxId, 0, len, rxBuf); // Immediately send message out CAN1 interface
}
if(!digitalRead(3)){ // If CAN1 pin 3 is low, read CAN1 receive buffer
CAN1.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
if((rxId & 0x80000000) == 0x80000000) // Determine if ID is standard (11 bits) or extended (29 bits)
sprintf(msgString, "CAN1 Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len);
else
sprintf(msgString, "CAN1 Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len);
Serial.print(msgString);
if((rxId & 0x40000000) == 0x40000000){ // Determine if message is a remote request frame.
sprintf(msgString, " REMOTE REQUEST FRAME");
Serial.print(msgString);
} else {
for(byte i = 0; i<len; i++){
sprintf(msgString, " 0x%.2X", rxBuf[i]);
Serial.print(msgString);
}
}
Serial.println();
CAN0.sendMsgBuf(rxId, 0, len, rxBuf); // Immediately send message out CAN0 interface
}
}
So maybe someone have any knowledge about this topic, so I could rule out at-least a sw part. I would greatly appreciate any input!