CAN-bus MCP2515 can not alter multi can message

My set up:

CAN A: has the diagnostic tool
CAN B: has the node ECU

I have installed a Arduino in between the CAN networks. Creating a physical CAN gateway. I started with simply transferring the signals from CAN A to CAN B and in the reverse. Essentially the traffic moved as if nothing was there. However, I have now tried receiving a multi frame signal and then altering some of the byte data and sending it back on bus. My observations are that 'VIN_reuest == true' seems to not allow it to go into the loop. And also without the 'VIN request' there still the if statements with count don't alter the CAN message. The original CAN message is just returned.

Has anyone done this before, am I going in the right direction?

I have done this in the past with single CAN messages.

#include <mcp2515.h>          //Library for CAN interfaces
#include <SPI.h>              //Library for using SPI Communication 

//Define the CS pin for each MCP2515 board
const int CS_PIN_1 = 9;
const int CS_PIN_2 = 10;

//create instances of the MCP2515 class for each board
MCP2515 mcp2515_1(CS_PIN_1);
MCP2515 mcp2515_2(CS_PIN_2);

struct can_frame canMsg; 

bool VIN_request = false;

int count = 0;
bool VIN_reuest = false;

void setup() {
  SPI.begin();                       //Begins SPI communication

  Serial.begin(9600);                //Begins Serial Communication at 9600 baudrate 

  mcp2515_1.reset();
  mcp2515_2.reset();                          

  mcp2515_1.setBitrate(CAN_500KBPS,MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz 
  mcp2515_2.setBitrate(CAN_500KBPS,MCP_8MHZ);

  mcp2515_1.setNormalMode();                  //Sets CAN at normal mode
  mcp2515_2.setNormalMode();                  //Sets CAN at normal mode

}

void loop() {

  // Reading diagnostic side and sending to ECU side
  if (mcp2515_2.readMessage(&canMsg) == MCP2515::ERROR_OK) {
    if (canMsg.can_id == 0x740) {
      // Set VIN flag to true when request 1A 90 is found
      if (canMsg.data[3] == 0x1A && canMsg.data[4] == 0x90) {
        VIN_request = true;
        count = 0;  // Reset count for new request
        //lastSendTime = millis();  // Reset timer
      }
    }
    mcp2515_1.sendMessage(&canMsg);
  }

  // Reading ECU side and sending to diagnostic side with VIN modifications
  if (mcp2515_1.readMessage(&canMsg) == MCP2515::ERROR_OK) {
    //if (VIN_reuest == true && canMsg.can_id == 0x300) {
    if (canMsg.can_id == 0x300) {
      //if (millis() - lastSendTime >= 20) {  // Ensure 20 ms interval
      Serial.println("Inside");
      Serial.println(count);

          // canMsg.data[3] = 0x00;
          // canMsg.data[4] = 0x00;
          // canMsg.data[5] = 0x00;
          // canMsg.data[6] = 0x00;
          // canMsg.data[7] = 0x00;

        if (count == 0) {
          // Adjust first message data
          canMsg.data[3] = 0x00;
          canMsg.data[4] = 0x00;
          canMsg.data[5] = 0x00;
          canMsg.data[6] = 0x00;
          canMsg.data[7] = 0x00;
          count++;
        }
        else if (count == 1) {
          // Adjust second message data
          canMsg.data[1] = 0x00;  
          canMsg.data[2] = 0x00;  
          canMsg.data[3] = 0x00;
          canMsg.data[4] = 0x00;
          canMsg.data[5] = 0x00;
          canMsg.data[6] = 0x00;
          canMsg.data[7] = 0x00;
          count++;
        }
        else if (count == 2) {
          // Adjust third message data
          canMsg.data[0] = 0x00;
          canMsg.data[1] = 0x00;  
          canMsg.data[2] = 0x00;  
          canMsg.data[3] = 0x00;
          canMsg.data[4] = 0x00;
          canMsg.data[5] = 0x00;

          // Reset flags for the next request
          count = 0;
          VIN_request = false;
        }

        // Send the modified message and update time
        mcp2515_2.sendMessage(&canMsg);
        //lastSendTime = millis();  // Update last sent time
      }
      }
    else {
      // Pass through other messages
      mcp2515_2.sendMessage(&canMsg);
    }
}



This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.