SparkFun_CAN-Bus_Arduino_Library icon indicating copy to clipboard operation
SparkFun_CAN-Bus_Arduino_Library copied to clipboard

CAN transmission is working, but not the listening part

Open Ipsitakoley opened this issue 1 year ago • 0 comments

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?

Ipsitakoley avatar Sep 08 '22 13:09 Ipsitakoley