Need help Reading specific Canbus ID, change the value and send it to new ID

Hi all,

I'm having trouble to get the code working. i have been succesfully using an arduino nano and an MCP2515 module reading all canbus messages and writing canbus using the examples from @coryjfowler library GitHub - coryjfowler/MCP_CAN_lib: MCP_CAN Library. But now i want to combine those 2. So i want to read the "default" setting from can id 0x02F83203 change the first 2 bytes(?) and send/write these settings to another can id 0x0XXXXXXX

Here is an example of the value i was able to read using the example from the library:

21:33:34.166 -> Extended ID: 0x02F83203 DLC: 6 Data: 0xC4 0x09 0xC0 0x2B 0xC4 0x08

Here is the code I have so far:


#include <mcp_can.h>
#include <SPI.h>

long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128]; // Array to store serial string

const uint32_t readId = 0x02F83203;
const uint32_t writeId = 0x0XXXXXXX;
bool foundDefault = false;

// newValue setting
byte newValue[2] = {0x70, 0x17};

#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_250KBPS, MCP_8MHZ) == 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
  {
    while (foundDefault == false)
    {
      CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
      Serial.println("Searching default value"):
      if (rxId == readId)
      { 
        foundDefault = true;
        sprintf(msgString, "Extended ID: 0x%.8lX  DLC: %1d  Data:", (rxId & 0x1FFFFFFF), len);
        for (byte i = 0; i < len; i++)
        {
          sprintf(msgString, " 0x%.2X", rxBuf[i]);
          Serial.print(msgString);
        }
        rxBuf[0] = newValue[0];
        rxBuf[1] = newValue[1];
      }
      Serial.println();
    }
  }

  // write new settings
  for (int i = 0; i < 4; i++)
  {
    if (CAN0.sendMsgBuf(writeId, 1, 6, rxBuf) == CAN_OK)
    {
      Serial.println("Change succesfull");
    }
    else
    {
      Serial.println("Error changing settings");
    }
    delay(200);
  }
  delay(999999);
}

If you want to edit the message with defined ID only, your sending code below the comment:

should be inside the condition

Cool I will try that. But my main problem is that somehow it won't find/read the wanted id..

This part doesn't seem to do its job:

Sorry
I thought you wrote, that already successfully read incoming messages?
if not, you first need to solve this problem before you start editing.
I have no experience with CAN on arduino, but I heard that people had difficulties in reading the extended ID by the standard library

Ah maybe i wasn't clear enough in my first post. I was able to read all can messages, but now I want to read a specific ID which seems to fail for some reason.

did this just for the sake of writing the code.

Give it a go if you want...

(compiles, NOT tested!)

#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...");
      }
    }
    
  }
  
}

hope that helps...

I will test it as soon as I get home from work in about 4 hrs. Thanks for now!!

Any use of delay() should be heavily scrutinized in a communications system. The microcontroller does mostly nothing in that time. You should check out the blink without delay example. I am also not sure you need the while loop as that is also blocking the microcontroller from doing other things.

I tested it, but the if statement doesn't seem to work. So the message doesn't get send.

20:06:21.178 -> Extended ID: 0x02F83203 DLC: 6 Data: 0x70 0x17 0x04 0x06 0x7F 0x05
20:06:21.178 -> Extended ID: 0x02F83201 DLC: 8 Data: 0x00 0x00 0x00 0x00 0x70 0x0D 0x3E 0xFF
20:06:21.178 -> Extended ID: 0x02F83200 DLC: 8 Data: 0x14 0x00 0x00 0x00 0x7C 0x04 0x20 0x03

Thanks for your reply, as you might have noted im kinda new to this arduino stuff. i will have a look at the delay and try to get rid of it.

changing this:

const uint32_t Ex_canID_rx = 0x02F83203;

To this:

const uint32_t Ex_canID_rx = 0x82F83203;

Did the trick!

Thanks so much guys!