I am having a bit of a problem isolating a single byte in one frame so I can modify it and send it on with the rest. If I run this code, I only get all of the incoming frames passing through the modified frame.
I tried stuffing it into another variable called speed and resending that as well but it wouldn't modify the value at all and only sent what was coming in.
Is there a way with this library that you can filter either side of the required frame so everything else can gateway through except for that one frame?
//Reads all traffic on CAN0 and forwards it to CAN1 but modifies 0x461 byte 6 when detected.
// Required libraries
#include
#include
//Leave defined if you use native port, comment if using programming port
#define Serial SerialUSB
void setup()
{
Serial.begin(115200);
// Initialize CAN0 and CAN1, Set the proper baud rates here
Can0.begin(CAN_BPS_500K);
Can1.begin(CAN_BPS_500K);
// Setup up output pin for gear position and speed zero output
pinMode(2,OUTPUT); //gearpos
pinMode(3,OUTPUT); //speedzero
//By default there are 7 mailboxes for each device that are RX boxes
//This sets each mailbox to have an open filter that will accept extended
//or standard frames
int filter;
//extended
//for (filter = 0; filter < 3; filter++) {
//Can0.setRXFilter(filter, 0, 0, true);
//Can1.setRXFilter(filter, 0, 0, true);
//}
//standard
for (int filter = 3; filter < 7; filter++) {
Can0.setRXFilter(filter, 0, 0, false);
Can1.setRXFilter(filter, 0, 0, false);
}
}
void printFrame(CAN_FRAME &frame) {
Serial.print("ID: 0x");
Serial.print(frame.id, HEX);
Serial.print(" Len: ");
Serial.print(frame.length);
Serial.print(" Data: 0x");
for (int count = 0; count < frame.length; count++) {
Serial.print(frame.data.bytes[count], HEX);
Serial.print(" ");
}
Serial.print("\r\n");
}
void loop(){
CAN_FRAME incoming0;
CAN_FRAME incoming1;
//CAN_FRAME speed;
//CAN_FRAME speedzero;
//CAN_FRAME gearpos;
if (Can0.available() > 0) {
Can0.read(incoming0);
//Can0.read(speed);
//incoming0.id = 0x461;
//incoming0.data.bytes[0] *= 2;
//speed.data.bytes[1] *= 2;
//speed.data.bytes[2] *= 2;
//speed.data.bytes[3] *= 2;
//speed.data.bytes[4] *= 2;
//speed.data.bytes[5] *= 2;
//speed.data.bytes[6] *= 2;
//speed.data.bytes[7] *= 2;
//Can0.read(gearpos);
//gearpos.id = 0x4BC;
//if (gearpos.data.bytes[1] = 8) {
// digitalWrite(2, HIGH);
//Can0.read(speedzero);
//speedzero.id = 0x4D0;
//if (speedzero.data.bytes[1] = 0) {
// digitalWrite(3, HIGH);
Can1.sendFrame(incoming0);
//Can1.sendFrame(speed);
//}
}
if (Can1.available() > 0) {
Can1.read(incoming1);
Can0.sendFrame(incoming1);
printFrame(incoming1); //uncomment line to print frames that are going out
}
//}
}