I'm struggling and I need help!
I've been working with the CoryJFowler MCP_Can libraries to build a pass through and filter 'box' to get some kit (nav screen) to function in a car it was never intended to go in.
In a nut shell I have an Arduino Pro Mini hooked up to two CAN transceivers (TJA1050) and I am running a vehicle log file into one transceiver (CAN0) using BUSMaster, and pushing the messages straight back on to CAN1 which is monitored in BUSMaster and connected to the screen.
The examples in the library are excellent, as is the mark up in the header files, a credit to Cory and the reason I've got this far.
As a simple bi directional pass through its working great (I get an occasional error frame, but I can live with that!)
So now I need to pluck out a single message, with a specific id (0x401) and specific content (01 yy xx xx xx xx xx xx), change the value in y and push it back out.
I've managed to mask out the ID but that doesn't really help.
I've tried 'IF' statements, but they seem to really screw with the CAN messages, typically filtering out way more than I intended.
More than likely my lack of programming skill is the issue, its something I pick up every now and again and forget pretty much everything in between.
My current code is below and features a few of the attempts i'm made to get what I want, currently commented out
// Demo: Dual CAN-BUS Shields, Data Pass-through
// Written by: Cory J. Fowler
// January 31st 2014
// This examples the ability of this library to support more than one MCP2515 based CAN interface.
#include <mcp_can_dfs.h>
#include <mcp_can.h>
#include <SPI.h>
unsigned long rxId;
unsigned long rxId_Filter;
byte len;
byte rxBuf[8];
//byte txBuf0[] = {AA,55,AA,55,AA,55,AA,55};
//byte txBuf1[] = {55,AA,55,AA,55,AA,55,AA};
MCP_CAN CAN0(9); // CAN0 interface usins CS on digital pin 10
MCP_CAN CAN1(8); // 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 2
void setup()
{
Serial.begin(115200);
// init CAN0 bus, baudrate: 250k@16MHz
if(CAN0.begin(MCP_ANY, CAN_125KBPS, MCP_8MHZ) == CAN_OK){ //MCP_EXT Vs MCP_ANY?
Serial.print("CAN0: Init OK!\r\n");
CAN0.setMode(MCP_NORMAL);
} else Serial.print("CAN0: Init Fail!!!\r\n");
// init CAN1 bus, baudrate: 250k@16MHz
if(CAN1.begin(MCP_ANY, CAN_125KBPS, MCP_8MHZ) == CAN_OK){
Serial.print("CAN1: Init OK!\r\n");
CAN1.setMode(MCP_NORMAL);
} else Serial.print("CAN1: Init Fail!!!\r\n");
Serial.end();
SPI.setClockDivider(SPI_CLOCK_DIV2); // Set SPI to run at 8MHz (16MHz / 2 = 8 MHz)
//CAN0.sendMsgBuf(0x1000000, 1, 8, txBuf0);
//CAN1.sendMsgBuf(0x1000001, 1, 8, txBuf0);
rxId_Filter = 401;
}
void loop(){
if (!digitalRead(CAN0_INT)) { // If pin 2 is low, read CAN0 receive buffer
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
//if (rxId == rxId_Filter) {
CAN1.sendMsgBuf(rxId, 0, len, rxBuf); // Immediately send message out CAN1 interface
//}
}
if(!digitalRead(CAN1_INT)){ // If pin 3 is low, read CAN1 receive buffer
CAN1.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
CAN0.sendMsgBuf(rxId, 0, len, rxBuf); // Immediately send message out CAN0 interface
}
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/
I guess I'm asking for some pointers for basic programming that might make identifying, modifying and sending a single message possible, without messing up the CAN message.