Explain the Approach
i am trying to test Recieve and send DATA throw CAN bus between (Arduino Mega 2560 connected with Arduino CAN Shield V2.0) & (Software CANoe for detectinge the data) All the sent data comming from arduino will be displayed in CANoe.
I used the send and the recieve Example codes provided from the supplier: [Seeed_Arduino_CAN/examples at master · Seeed-Studio/Seeed_Arduino_CAN · GitHub]
and implement them together in Arduino to run a small program sothat i can test the send /Recieve commuication of the CAN BUS
The Data from arduino send succefuly to the CANoe and i can see it in the Monitor as it shown in the attachment.
The problem is:
I cannot read any data which comming from CANoe to the Arduino.
What i figure out:
if i print CAN_MSGAVAIL and CAN.checkReceive() always get:
CAN_MSGAVAIL =3
CAN.checkReceive()=4
Is this problem has omething to do with the library or i had issue in my connection?
Arduino CODE*
#include <mcp2518fd_can_dfs.h>
#include <can-serial.h>
#include "mcp2518fd_can.h"
#include "mcp2515_can.h"
#include <mcp2515_can_dfs.h>
#define CAN_2515
#include <mcp2518fd_can.h>
#include <mcp_can.h>
#include <SPI.h>
#include <mcp2515_can.h>
const int SPI_CS_PIN = 9;
const int CAN_INT_PIN = 2;
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
byte MCP_CAN::readMsgBuf(byte *len, byte buf[]);
// Define Digital Signal for recieving Signal fromm CAN
const int LED = 13; // LED Pin Number
boolean ledON = 1;
// Define Analoge for Send data to CAN
int sensorPin = A0;
int sensorValue = 0;
int cantxValue = 0;
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT); // Output Signal delaration for the LED
while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
SERIAL_PORT_MONITOR.println("CAN init fail, retry...");
delay(100);
}
SERIAL_PORT_MONITOR.println("CAN init ok!");
}
void loop() {
unsigned char len = 0;
unsigned char buf[8];
unsigned long canId = CAN.getCanId();
int giveout;
if (CAN_MSGAVAIL != CAN.checkReceive()) { // check if data coming CAN_MSGAVAIL:Indicates there are data in FIFO buffer to be read.
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
Serial.print("CAN.readMsgBuf(&len, buf) = ");
Serial.print(CAN.readMsgBuf(&len, buf), HEX);
Serial.println();
Serial.print("data len = ");
Serial.print(len);
Serial.println();
Serial.print("get data from ID: 0xA");
Serial.print(canId, HEX);
Serial.println();
for (int i = 0; i <= len; i++) { // print the data
Serial.println(i);
Serial.print("buf[i]= ");
Serial.print(buf[i]);
Serial.println("\t");
digitalWrite(LED, buf[i]);
}
}
else Serial.println("CAN READ Communication Failed!!!");
delay(100);
//Read the Voltage value Analog
sensorValue = analogRead(sensorPin); //Each CAN bus byte can store a value between 0-255.
cantxValue = sensorValue / 4; //Dividing sensorValue by 4 puts us in that range.
Serial.print("cantxValue: ");
Serial.print(cantxValue);
Serial.println();
unsigned char canMsg[8] = {cantxValue, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; //Create data packet for CAN message
Serial.print("CAN.sendMSg: ");
Serial.println (CAN.sendMsgBuf(0xA, 1, 8, canMsg), HEX); // send data: id = 0xA, standrad flame, data len = 8, stmp: data buf
CAN.checkReceive();
Serial.println("-----------------------------");
delay(100);
}