Missing Signals CAN/I2C

Hello everyone,

I am using two Arduino Uno Boards and I am recieving signals from external Hardware over CAN Bus to one Arduino board and these signals are then send to other Arduino Board unsing I2C Communication.

But Sometimes CAN Signals are getting skipped and sometime they are not. Mostly Message id: 0x3 is getting skipped. And when wire transmission is not involved then it's working perfectly fine.
Can anyone please help me with this issue as I need to use wire transmission?

#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>
#include <stdlib.h>
#include <math.h>
#include <Wire.h>


byte Bot_1[8];
byte Bot_2[8];
byte Bot_3[8];
byte Bot_4[8];

//********************************Setup Loop*********************************//

void setup() {
   Wire.begin();
  Serial.begin(9600); // For debug use
  Serial.println("CAN Read - Testing receival of CAN Bus message");
  if (Canbus.init(CANSPEED_500)) //Initialise MCP2515 CAN controller at the specified speed
    Serial.println("CAN Init ok");
  else
    Serial.println("Can't init CAN");
}

//********************************Main Loop*********************************//

void loop()
{ 


  tCAN message;
  if (mcp2515_check_message())
  { 
    if (mcp2515_get_message(&message))
    {  

      if (message.id == 0x2) //Botschaft 1
      {
      Bot_1[0]=message.data[0];
      Bot_1[1]=message.data[1];
      Bot_1[2]=message.data[2];
      Bot_1[3]=message.data[3];
      Bot_1[4]=message.data[4];
      Bot_1[5]=message.data[5];
      Bot_1[6]=message.data[6];
      Bot_1[7]=message.data[7];
      Serial.println("Bot_1");
      }
  
      if (message.id == 0x1) //Botschaft 2
      {
      Bot_2[0]=message.data[0];
      Bot_2[1]=message.data[1];
      Bot_2[2]=message.data[2];
      Bot_2[3]=message.data[3];
      Bot_2[4]=message.data[4];
      Bot_2[5]=message.data[5];
      Bot_2[6]=message.data[6];
      Bot_2[7]=message.data[7];

      Serial.println("Bot_2");
      }

      if (message.id == 0x0) //Botschaft 3
      {
      Bot_3[0]=message.data[0];
      Bot_3[1]=message.data[1];
      Bot_3[2]=message.data[2];
      Bot_3[3]=message.data[3];
      Bot_3[4]=message.data[4];
      Bot_3[5]=message.data[5];
      Bot_3[6]=message.data[6];
      Bot_3[7]=message.data[7];
      Serial.println("Bot_3");
      }
    
      if (message.id == 0x3) //Botschaft 4
      {
      Bot_4[0]=message.data[0];
      Bot_4[1]=message.data[1];
      Bot_4[2]=message.data[2];
      Bot_4[3]=message.data[3];
      Bot_4[4]=message.data[4];
      Bot_4[5]=message.data[5];
      Bot_4[6]=message.data[6];
      Bot_4[7]=message.data[7];
      Serial.println("Bot_4");
      }

  Wire.beginTransmission(8); // transmit to device #8
  Wire.write(Bot_1,8);
  Wire.write(Bot_2,8);
  Wire.write(Bot_3,8);
  Wire.write(Bot_4,8);
  Wire.endTransmission();    // stop transmitting
    }
  }

}

when you receive a Can message, e.g. Bot_1 you transmit Bot_1 thru Bot_4 using Write.Wire()
why not just transmit the message received? e.g.

    if (mcp2515_get_message(&message))
    { 

      if (message.id == 0x2) //Botschaft 1
      {
      Bot_1[0]=message.data[0];
      Bot_1[1]=message.data[1];
      Bot_1[2]=message.data[2];
      Bot_1[3]=message.data[3];
      Bot_1[4]=message.data[4];
      Bot_1[5]=message.data[5];
      Bot_1[6]=message.data[6];
      Bot_1[7]=message.data[7];
      Serial.println("Bot_1");  
      Wire.write(Bot_1,8);   // << transmit
      }

also you serial baud rate is very low - if you are doing many prints this could cause you to loose Can messages

Hello,

thank you for the insight. I tried again with your suggestion but still I am facing the same issue. I have used prints only 4 times just for debugging purpose. It's skipping the message id: 0x3 and as soon as I remove the wire transmission commands. I can see that all the messages are fetched from CAN bus.

Regards

if you remove the calls to Wire.write() do you receive all the Canbus messages OK?
I find a USB-CAN dongle for a PC is useful for monitoring traffic on a Canbus system

Yes, as soon as Wire.write() calls are removed I receive all the CAN Bus messages

Regards

which Canbus module are you using?

I am using SparkFun CANBus-Shield with Arduino Uno Board.

the pins should not clash - the SparkFun CANBus-Shield is SPI using pins 9 (SD_CS) 10 (CAN_CS) 12 (MISO) and 13 (SCK)
whereas I2C uses SCA and SCL
should you disable SD_CS?
have you correctly terminated the canbus?
you probably need to look at the library code - could something be disabling interrupts at a critical point?

perhaps ask on the sparkfun forum

Thank you for the tips.

I have uploaded an Arduino based Canbus monitor to

a host program (in C# or Java) runs on a PC displaying Canbus settings and messages