Hello
I am trying to read my vehicoes CanB with the Leonardo Can Bus Shield Leonardo CAN BUS board.
I have it configured via the 4 pin inputs not the DB9 conector. I have checked the vehices OBD port to confirm the correct readings at CanH and CanL and can see on the meter there is activity on the pins. I have checked at the CanH and CanL inputs on the board when plugged into OBD and im getting signal to the board.
When I try to send data I get message send confirmation however there is no data being read by the board.
I have tried many different librarys and edited my code to suit each but to no avail. I just cant get it to read data.
I have searched here but all similar topics are around code not compiling, my issue is getting it to read data once running.
Can anyone help please?
Excuse the code its a bit messy atm as its been edited 50 times for different librarys, but I can tell why its not working.
#include <Arduino.h>
#include <mcp_can.h>
#include <SPI.h>
#define CANint 2
unsigned char len = 0;
unsigned char buf[8];
unsigned long ID = 0;
unsigned long line = 0;
unsigned long time;
byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
MCP_CAN CAN0(17); // Set CS to pin 17
void setup() {
Serial.begin(115200);
while (!Serial) {
Serial.print("I will wait here forever...");
delay(1000);
};
pinMode(23, OUTPUT);
digitalWrite(23, HIGH);
pinMode(CANint, INPUT);
Serial.println("CAN init:");
if (CAN0.begin(CAN_500KBPS) == CAN_OK) {
Serial.println("Can Init Success");
} else {
Serial.println("Can Init Failed");
while (1) {
Serial.print("I will wait here forever...");
delay(1000);
}
}
Serial.println("Good to go!");
}
void loop() {
time = millis();
if (CAN_MSGAVAIL == CAN0.checkReceive() && line < 10000) { // Check to see whether data is read
digitalWrite(23, LOW);
if (CAN0.readMsgBuf(&len, buf)) { // Read data
ID = CAN0.getCanId();
line = line + 1;
Serial.print("ID:");
Serial.print(ID, HEX); // Output HEX Header
Serial.print("\t");
Serial.print("Len:");
Serial.print(len);
Serial.print("\t");
Serial.print("DATA:");
for (int i = 0; i < len; i++) { // Output 8 Bytes of data in Dec
Serial.print(buf[i]);
Serial.print("\t");
}
Serial.print("\t");
Serial.print("time:");
Serial.print(time); // Timestamp
Serial.print("\t");
Serial.print("line:");
Serial.println(line); // Line Number
}
digitalWrite(23, HIGH);
} else {
// send data: ID = 0x100, Standard CAN Frame, Data length = 8 bytes, 'data' = array of data bytes to send
byte sndStat = CAN0.sendMsgBuf(0x100, 0, 8, data);
if (sndStat == CAN_OK) {
Serial.println("Message Sent Successfully!");
} else {
Serial.println("Error Sending Message...");
}
delay(100); // send data per 100ms
}
delay(10);
}