Hi, i am developing module to filter one ID and replace some data of the message that goes to the cluster and sent it back, all the code is working and tested, but i have some type of issue.
My code: Using CoryJFowler MCP_Can libraries with arduino nano.
#include <mcp_can_dfs.h>
#include <mcp_can.h>
#include <SPI.h>
unsigned long rxId;
unsigned long rxId_Filter;
/////////////
unsigned char flagRecv = 0;
//unsigned char len = 0;
unsigned char buf[8];
char str[20];
byte len;
byte rxBuf[8];
MCP_CAN CAN0(10); // CAN0 interface usins CS on digital pin 10
MCP_CAN CAN1(9); // CAN1 interface using CS on digital pin 9
#define CAN0_INT 2 // Set INT to pin 2
#define CAN1_INT 3 // Set INT to pin 3
void setup()
{
Serial.begin(115200);
// iniciar CAN0 bus, baudrate: 100k@8MHz
if(CAN0.begin(MCP_ANY, CAN_100KBPS, MCP_8MHZ) == CAN_OK){ //MCP_EXT Vs MCP_ANY?
Serial.print("CAN0: Arranque OK!\r\n");
CAN0.setMode(MCP_NORMAL);
} else Serial.print("CAN0: Arranque Falhou!!!\r\n");
// iniciar CAN1 bus, baudrate: 100k@8MHz
if(CAN1.begin(MCP_ANY, CAN_100KBPS, MCP_8MHZ) == CAN_OK){
Serial.print("CAN1: Arranque OK!\r\n");
CAN1.setMode(MCP_NORMAL);
} else Serial.print("CAN1: Arranque Falhou!!!\r\n");
SPI.setClockDivider(SPI_CLOCK_DIV2); // Set SPI to run at 8MHz (16MHz / 2 = 8 MHz)
Serial.end();
rxId_Filter = 0x100;
}
void loop(){
if (!digitalRead(CAN1_INT)) { // If pin 2 is low, read CAN0 receive buffer
CAN1.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
if (rxId != rxId_Filter) {
CAN0.sendMsgBuf(rxId, 0, len, rxBuf); // Immediately send message out CAN1 interface
}
else
{
{
if (rxBuf[1] == 0xFF)
{
byte data[8] = { rxBuf[0], rxBuf[0], rxBuf[2], rxBuf[3], rxBuf[4], rxBuf[5], rxBuf[6], rxBuf[7] };
CAN0.sendMsgBuf(rxId, 0, len, data );
}
}
}
}
if(!digitalRead(CAN0_INT)){ // If pin 3 is low, read CAN1 receive buffer
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
CAN1.sendMsgBuf(rxId, 0, len, rxBuf); // Immediately send message out CAN1 interface
}
}
I am using this on my bmw, the module is conected between the cluster and factory can lines, and when i conect it to the can network, cluster work but i can not make diagonostics on my obd port, because it stop working. When i measure the can lines without the module it measures 0.2 to 0.25v and 4.8 to 4.75v, when conected it measures 0.35 to 0.4 and 4.6 to 4.65v. Only way i can make all work is if dont sent messages back from the cluster out of can 1, that way i have fully working obd diagonistics, but i can not see the cluster on modules list, because return messages are not been sent, and that way i can not comunicate with cluster on obd port, i can comunicate with all the other modules. Seems like can 1 is senting back messages that it is creating some type of problem like repeted data on can network. If anyone can help me will be good because i lost almost 2 weeks trying to understand the problem and i can not work out the problem. sry my english