Hello everyone,
i am trying to test Recieve and send signal using Seed-Arduino CAN Shield V2.0 using arduino Mega 2560 the sending signal working good actualy. but i have issue with recieve data:
The problem is that the code cannot get the data length and always get it zero so i cannot got the write signal as i need it to programm some digital stuff.
Aslo the Loop didnot want to work until i put (CAN_MSGAVAIL != CAN.checkReceive())
insteadt (CAN_MSGAVAIL = CAN.checkReceive()) . I have no idea why !
What i understand from the syntax example code that:
1- len=0 is a defined var will be filled as soon as the data length will delivered then gonna use this data to read it or control something else.
2- [[ CAN_MSGAVAIL == CAN.checkReceive() ]] this line is for checking if data is comming and will not be equal until data is comming, howerver if i send data throw CAN.
Actually (CAN_MSGAVAIL != CAN.checkReceive()) working and allow the loop to continiue. but the further Loop (for Loop) didn’t work as “len” always equal zero cause it didn’t get the new value.
I am testing the send and recieve using CANoe Software and i only can see the data that i send, but i cannot read this data back to my Arduino Program.
Questions:
Is there anyway to check/Solve that or should i add something in the code.
I hope my attached code lead u to find the issue.
Thanks
Ahmed
Blockzitat
#include <mcp2518fd_can_dfs.h>
#include <can-serial.h>
#include <mcp2518fd_can.h>
#include “mcp2518fd_can.h”
#include <mcp_can.h>
#include <SPI.h>
#include “mcp2515_can.h”
#include <mcp2515_can.h>
#include <mcp2515_can_dfs.h>
#define CAN_2515
const int SPI_CS_PIN = 9;
const int CAN_INT_PIN = 2;
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
// 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;
//-----------------------------------S E T U P--------------------------------------------
void setup() {
Serial.begin(115200);
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!”);
}
//-----------------------------------L O O P-----------------------------------------------
void loop() {
unsigned char len = 0;
unsigned char buf[8];
unsigned long canId = CAN.getCanId();
if (CAN_MSGAVAIL == CAN.checkReceive()) {
CAN.readMsgBufID(&canId, &len, buf);
Serial.print("data len = ");
Serial.print(len);
Serial.println();
Serial.print(“get data from ID: 0xA”);
Serial.print(canId, HEX);
Serial.println();
Serial.print("data buf = ");
Serial.print(buf[7]);
Serial.println();
for (int i = 0; i < len; i++) { // print the data
Serial.print(buf[i]);
Serial.println("\t");
if (ledON && i == 0) {
digitalWrite(LED, buf[i]);
ledON = 0;
Serial.println("LED OFF!");
delay(100);
}
else if ((!(ledON)) && i == 4) {
digitalWrite(LED, buf[i]);
ledON = 1;
Serial.println("LED ON!");
delay(100);
}
}
Serial.println();
Serial.println("for Loop not running");
}
else Serial.println("CAN READ CODE Not Working");
delay(100);
// send data
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();
Serial.println(”-----------------------------”);
//Create data packet for CAN message
unsigned char canMsg[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, cantxValue};
// send data: id = 0xA, standrad flame, data len = 8, stmp: data buf
CAN.sendMsgBuf(0xA, 0, 8, canMsg);
delay(100);
}