Unable to receive all can frames at the receiving end of my arduino

i've been experimenting with sending variables over multiple frames and receiving said frames on another Arduino

The hardware setup consists of 2 ESP32s and MCP2515s wired up over SPI

here are the sender and receiver code respectively. The problem here is that the sender properly sends 5 frames but the reciever recieves only the 1st one

the library used is GitHub - autowp/arduino-mcp2515: Arduino MCP2515 CAN interface library

Sender

#include <SPI.h>
#include <mcp2515.h>

int cyc1;
int cyc2;
int cyc3;
int cyc4;
int cyc5;
int cyc6;
int cyc7;
int cyc8;

int volt1;
int volt2;
int volt3;
int volt4;

int curr1;
int curr2;
int curr3;
int curr4;


struct can_frame canMsg1;
struct can_frame canMsg2;
struct can_frame canMsg3;
struct can_frame canMsg4;
struct can_frame canMsg5;
MCP2515 mcp2515(5);



int chunkSize = 255;


void setup() {

  
  canMsg3.can_id  = 0xCC;
  canMsg3.can_dlc = 8;
    canMsg4.can_id  = 0xDD;
  canMsg4.can_dlc = 8;
    canMsg5.can_id  = 0xEE;
  canMsg5.can_dlc = 1;
  Serial.begin(115200);
  
    SPI.begin();

  mcp2515.reset();
  mcp2515.setBitrate(CAN_1000KBPS, MCP_8MHZ);
  mcp2515.setNormalMode();
  
  Serial.println("Example: Write to CAN");
}

void loop() {

    
      canMsg1.can_id  = 0xAA;
  canMsg1.can_dlc = 8;
  canMsg1.data[0] = 'S';
  canMsg1.data[1] = 'E';
  canMsg1.data[2] = 'R';
  canMsg1.data[3] = 'I';
  canMsg1.data[4] = 'A';
  canMsg1.data[5] = 'L';
  canMsg1.data[6] = 'N';
  canMsg1.data[7] = '0';

  canMsg2.can_id  = 0xBB;
  canMsg2.can_dlc = 8;
  canMsg2.data[0] = 'Y';
  canMsg2.data[1] = 'Y';
  canMsg2.data[2] = 'Y';
  canMsg2.data[3] = 'Y';
  canMsg2.data[4] = 'M';
  canMsg2.data[5] = 'M';
  canMsg2.data[6] = 'D';
  canMsg2.data[7] = 'D';
    mcp2515.sendMessage(&canMsg1);



int bms_voltage_can=693;
int bms_current_can=121;
int bms_soc_can=50;
int cycles_can=256;



int numSplits_volt = bms_voltage_can / 255;
int remainder_volt = bms_voltage_can % 255;

// Create an array to store the split variables
int splitVars_volt[numSplits_volt + 1];

// Initialize all the split variables to 255
for (int i = 0; i < numSplits_volt; i++) {
  splitVars_volt[i] = 255;
}

// Set the last split variable to the remainder
splitVars_volt[numSplits_volt] = remainder_volt;

 Serial.println("Voltage");
// Print the split variables
for (int i = 0; i <= numSplits_volt; i++) {
  Serial.println(splitVars_volt[i]);
}

int numSplits_curr = bms_current_can / 255;
int remainder_curr = bms_current_can % 255;

// Create an array to store the split variables
int splitVars_curr[numSplits_curr + 1];

// Initialize all the split variables to 255
for (int i = 0; i < numSplits_curr; i++) {
  splitVars_curr[i] = 255;
}

// Set the last split variable to the remainder
splitVars_curr[numSplits_curr] = remainder_curr;

 Serial.println("Current");
// Print the split variables
for (int i = 0; i <= numSplits_curr; i++) {
  Serial.println(splitVars_curr[i]);
}

int numSplits_cycles = cycles_can / 255;
int remainder_cycles = cycles_can % 255;

// Create an array to store the split variables
int splitVars_cycles[numSplits_cycles + 1];

// Initialize all the split variables to 255
for (int i = 0; i < numSplits_cycles; i++) {
  splitVars_cycles[i] = 255;
}

// Set the last split variable to the remainder
splitVars_cycles[numSplits_cycles] = remainder_cycles;

 Serial.println("Cycles");
// Print the split variables
for (int i = 0; i <= numSplits_cycles; i++) {
  Serial.println(splitVars_cycles[i]);
}

canMsg3.data[0] = splitVars_volt[0];
canMsg3.data[1] = splitVars_volt[1];
canMsg3.data[2] = splitVars_volt[2];
canMsg3.data[3] = splitVars_volt[3];
canMsg3.data[4] = splitVars_curr[0];
canMsg3.data[5] = splitVars_curr[1];
canMsg3.data[6] = splitVars_curr[2];
canMsg3.data[7] = splitVars_curr[3];

  mcp2515.sendMessage(&canMsg2);

  mcp2515.sendMessage(&canMsg3);

canMsg4.data[0] = splitVars_cycles[0];
canMsg4.data[1] = splitVars_cycles[1];
canMsg4.data[2] = splitVars_cycles[2];
canMsg4.data[3] = splitVars_cycles[3];
canMsg4.data[4] = splitVars_cycles[4];
canMsg4.data[5] = splitVars_cycles[5];
canMsg4.data[6] = splitVars_cycles[6];
canMsg4.data[7] = splitVars_cycles[7];

  mcp2515.sendMessage(&canMsg4);

canMsg5.data[0]= bms_soc_can;

  mcp2515.sendMessage(&canMsg5);

  Serial.println("Messages sent");


  
  delay(250);
}

here is the receiver code

#include <SPI.h>
#include <mcp2515.h>
float bms_voltage;
float bms_current;
int bms_cycles;
int bms_soc;


struct can_frame canMsg1;
struct can_frame canMsg2;
struct can_frame canMsg3;
struct can_frame canMsg4;
struct can_frame canMsg5;

MCP2515 mcp2515(5);



char s1;
char s2;
char s3;
char s4;
char s5;
char s6;
char s7;
char s8;
char s9;
char s10;
char s11;
char s12;
char s13;
char s14;
char s15;
char s16;



int msgcheck1;
int msgcheck2;
int msgcheck3;
int msgcheck4;
int msgcheck5;
int receivedData_check;
void setup() {
  Serial.begin(115200);

  SPI.begin();

  mcp2515.reset();
  mcp2515.setBitrate(CAN_1000KBPS, MCP_8MHZ);
  mcp2515.setNormalMode();



  Serial.println("------- CAN Read ----------");
  Serial.println("ID  DLC   DATA");
}

void loop() {

  if (mcp2515.readMessage(&canMsg1) == MCP2515::ERROR_OK) {
    if (canMsg1.can_id == 0xAA) {  //printout only message frame 0x18A

Serial.println("Data 1 Recieved");

      s1 = canMsg1.data[0];
      s2 = canMsg1.data[1];
      s3 = canMsg1.data[2];
      s4 = canMsg1.data[3];
      s5 = canMsg1.data[4];
      s6 = canMsg1.data[5];
      s7 = canMsg1.data[6];
      s8 = canMsg1.data[7];
      delay(200);
      Serial.print(" ");
    }

    Serial.println();
  }
  if (mcp2515.readMessage(&canMsg2) == MCP2515::ERROR_OK) {
    if (canMsg2.can_id == 0xBB) {  //printout only message frame 0x18A

    Serial.println("Data 2 Recieved");


      s9 = canMsg2.data[0];
      s10 = canMsg2.data[1];
      s11 = canMsg2.data[2];
      s12 = canMsg2.data[3];
      s13 = canMsg2.data[4];
      s14 = canMsg2.data[5];
      s15 = canMsg2.data[6];
      s16 = canMsg2.data[7];
      Serial.print(" ");
delay(200);

      Serial.println();
    }
  }
  if (mcp2515.readMessage(&canMsg3) == MCP2515::ERROR_OK) {
    if (canMsg3.can_id == 0xCC) {  //printout only message frame 0x18A

    Serial.println("Data 3 Recieved");


      int vol1 = canMsg3.data[0];
      int vol2 = canMsg3.data[1];
      int vol3 = canMsg3.data[2];
      int vol4 = canMsg3.data[3];
      int curr1 = canMsg3.data[4];
      int curr2 = canMsg3.data[5];
      int curr3 = canMsg3.data[6];
      int curr4 = canMsg3.data[7];
      Serial.print(" ");
      delay(200);
  bms_voltage = (vol1 + vol2 + vol3 + vol4) / 10;
  bms_current = (curr1 + curr2 + curr3 + curr4) / 10;
    Serial.println(bms_voltage);
  Serial.println(bms_current);
    }

    Serial.println();
  }

  if (mcp2515.readMessage(&canMsg4) == MCP2515::ERROR_OK) {

    if (canMsg4.can_id == 0xDD) {  //printout only message frame 0x18A

Serial.println("Data 4 Recieved");

      int cyc1 = canMsg4.data[0];
      int cyc2 = canMsg4.data[1];
      int cyc3 = canMsg4.data[2];
      int cyc4 = canMsg4.data[3];
      int cyc5 = canMsg4.data[4];
      int cyc6 = canMsg4.data[5];
      int cyc7 = canMsg4.data[6];
      int cyc8 = canMsg4.data[7];
delay(200);
        bms_cycles = cyc1 + cyc2 + cyc3 + cyc4 + cyc5 + cyc6 + cyc7 + cyc8;
          Serial.println(bms_cycles);
    }

    Serial.println();
  }

  if (mcp2515.readMessage(&canMsg5) == MCP2515::ERROR_OK) {
    if (canMsg5.can_id == 0xEE) {  //printout only message frame 0x18A

Serial.println("Data 5 Recieved");

      canMsg5.data[0] = bms_soc;
      delay(200);
        Serial.println(bms_soc);

      Serial.print(" ");
    }

    Serial.println();
  }






  delay(250);


  char SerialNo[] = { s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, '\0' };



  Serial.println(SerialNo);

  Serial.println(bms_soc);



  Serial.println(" ");
}

image
This is the serial monitor output, ideally it should receive all 5messages, but it only gets one

mcp2515.setBitrate(CAN_1000KBPS, MCP_8MHZ);

8MHz Xtal for 1Mbps CAN baud rate does not mix well.

IMHO you need at least a MCP2515 board which has at least a 16MHz Xtal on it.

hope that helps....

Hello there
I tested the same on 125k, 250k and 500k baud rates as well

The problem remains the same across all the baud rates. And the baud rate you see was the last value I was experimenting with.

Regards,
Snek

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