CAN transmission is working, but not the listening part in Arduin+SparkfunCANShield setup

We are using Sparkfun CAN shield and Arduino Uno to build a CAN network. We are observing the data in CAN network using KVaser Canking. When we send a data to the CAN bus, we can see the data in KVaser window. But, when we try to read something from the CAN bus, it is showing garbage values. Note that 120 ohm is also there. Here is the code:


#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>

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

void setup() {
  Serial.begin(115200);
  Serial.println("CAN Write - Testing transmission of CAN Bus messages");
  delay(1000);
  
  if(Canbus.init(CANSPEED_250))  //Initialise MCP2515 CAN controller at the specified speed
    Serial.println("CAN Init ok");
  else
    Serial.println("Can't init CAN");
    
  delay(1000);
}
  
unsigned long now=0, prev=0; 
 
void loop() {
  mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0);
  tCAN message;  // message read from the bus
  tCAN amessage;

  // Attack message details
  amessage.id = 0x631; //formatted in HEX
  amessage.header.rtr = 0;
  amessage.header.length = 8; //formatted in DEC
  amessage.data[0] = 0x00;
  amessage.data[1] = 0x00;
  amessage.data[2] = 0x00;
  amessage.data[3] = 0x00; //formatted in HEX
  amessage.data[4] = 0x00;
  amessage.data[5] = 0x00;
  amessage.data[6] = 0x00;
  amessage.data[7] = 0x00;
  

   while(! mcp2515_check_message()){
    
   }
  
    if(mcp2515_get_message(&message) and message.id==0x630){ //wait for target message on the bus
      amessage.id = 0x630;
      mcp2515_send_message(&amessage);//this is not working
      Serial.println("Sending attack message");//this is not working
   }
  
       auto error_tec = mcp2515_read_register(TEC);
       Serial.print("TEC Count of adversary : "); //this is working
       Serial.println(error_tec);  //this is working
   
       
}

Can some one help how to resolve this?

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

I would have written your code like this instead:

#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>

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

void setup() {
  Serial.begin(115200);
  Serial.println("CAN Write - Testing transmission of CAN Bus messages");
  delay(1000);

  if (Canbus.init(CANSPEED_250)) //Initialise MCP2515 CAN controller at the specified speed
    Serial.println("CAN Init ok");
  else
    Serial.println("Can't init CAN");

  delay(1000);
}

unsigned long now = 0, prev = 0;

void loop() {
  tCAN message;  // message read from the bus
  tCAN amessage;

  // Attack message details
  amessage.id = 0x631; //formatted in HEX
  amessage.header.rtr = 0;
  amessage.header.length = 8; //formatted in DEC
  amessage.data[0] = 0x00;
  amessage.data[1] = 0x00;
  amessage.data[2] = 0x00;
  amessage.data[3] = 0x00; //formatted in HEX
  amessage.data[4] = 0x00;
  amessage.data[5] = 0x00;
  amessage.data[6] = 0x00;
  amessage.data[7] = 0x00;


  while (! mcp2515_check_message()) {

  }

  if (mcp2515_get_message(&message)) { //wait for target message on the bus
    if (message.id == 0x630) {
      amessage.id = 0x630;
      mcp2515_bit_modify(CANCTRL, (1 << REQOP2) | (1 << REQOP1) | (1 << REQOP0), 0);
      mcp2515_send_message(&amessage);
      Serial.println("Sending attack message");
    }
  }

  auto error_tec = mcp2515_read_register(TEC);
  Serial.print("TEC Count of adversary : "); //this is working
  Serial.println(error_tec);  //this is working
}

hope that helps...

It really sounds like a library issue.
Try this library I maintain: GitHub - coryjfowler/MCP_CAN_lib: MCP_CAN Library
I also have access to a Kvaser adapter and thus CANKing, so if you still have an issue when using my library, I can see if there is something more to it.

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