So i am just trying to read a specific message ID, change a few bytes and resend. I did that, but its way too slow responding since the lower IDs flood the system. So i need to filter the messages, and I did that. But I have no idea on how to combine the code so I am only seeing the filtered messages to change the bytes.
#include <mcp_can.h>
#include <SPI.h>
const uint32_t Ex_canID_rx = 0x02F83203;
const uint32_t Ex_canID_tx = 0x02F83204; //can id 0x0XXXXXXX
byte newValue[2] = {0x70, 0x17};
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128]; // Array to store serial string
#define CAN0_INT 2 // Set INT to pin 2
MCP_CAN CAN0(10); // Set CS to pin 10
void setup()
{
Serial.begin(115200);
// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
if (CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK)
Serial.println("MCP2515 Initialized Successfully!");
else
Serial.println("Error Initializing MCP2515...");
CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data.
pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input
Serial.println("MCP2515 Library Receive Example...");
}
void loop()
{
if (!digitalRead(CAN0_INT)) // If CAN0_INT pin is low, read 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, "Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len);
else
sprintf(msgString, "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();
if (rxId == Ex_canID_rx) {
rxBuf[0] = newValue[0];
rxBuf[1] = newValue[1];
// send data: ID = 0x0XXXXXXX, Extended CAN Frame, Data length = len, 'rxBuf' = array of data bytes to send
byte sndStat = CAN0.sendMsgBuf(Ex_canID_tx, 1, len, rxBuf);
if (sndStat == CAN_OK) {
Serial.println("Message Sent Successfully!");
} else {
Serial.println("Error Sending Message...");
}
}
for reading and sending, but too slow
Do you mean put that code in the first code? Im not sure where I would put it on the first. They both work fine independently, I just need to filter, then receive that code...and mody and resend. The first code modifies and resends, the 2nd filters...but i need them to work together so it executes faster instead of waiting for that one ID to finally get through.
#include <mcp_can.h>
#include <SPI.h>
const uint32_t Ex_canID_rx = 0x02F83203;
const uint32_t Ex_canID_tx = 0x02F83204; //can id 0x0XXXXXXX
byte newValue[2] = {0x70, 0x17};
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128]; // Array to store serial string
#define CAN0_INT 2 // Set INT to pin 2
MCP_CAN CAN0(10); // Set CS to pin 10
void setup() {
Serial.begin(115200);
// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
if (CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.println(" Initialized !");
else Serial.println("Error MCP2515...");
CAN0.init_Mask(0, 1, 0xFFFFFFFF);
CAN0.init_Mask(1, 1, 0xFFFFFFFF);
for (int i = 0; i < 6; i++) CAN0.init_Filt(i, 1, 0x02F83203);
CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data.
Serial.println("MCP2515 Library Receive Example...");
}
void loop() {
if (!digitalRead(CAN0_INT)) // If CAN0_INT pin is low, read receive buffer
{
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
if ((rxId & 0x80000000)) // Determine if ID is standard (11 bits) or extended (29 bits)
sprintf(msgString, "Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len);
else sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len);
Serial.print(msgString);
if ((rxId & 0x40000000)) { // Determine if message is a remote request frame.
Serial.print(" REMOTE REQUEST FRAME");
} else {
for (byte i = 0; i < len; i++) {
sprintf(msgString, " 0x%.2X", rxBuf[i]);
Serial.print(msgString);
}
}
Serial.println();
if (rxId == Ex_canID_rx) {
rxBuf[0] = newValue[0];
rxBuf[1] = newValue[1];
// send data: ID = 0x0XXXXXXX, Extended CAN Frame, Data length = len, 'rxBuf' = array of data bytes to send
byte sndStat = CAN0.sendMsgBuf(Ex_canID_tx, 1, len, rxBuf);
if (sndStat == CAN_OK)Serial.println("Sent");
else Serial.println("Error...");
}
}
}