First off, I am very familiar with CAN Bus communication. I have some higher end equipment that I use to read CAN Bus on a regular basis. So I understand the communication method, what it should look like, and all the other details related to CAN Bus communication.
That said, I recently bought a Longan Labs CANBed V1 as I have a project I want to use a CAN Bus controller on. I have been fighting this for a few weeks and I have not been able to get the car to respond to the controller. I am more used to programming in Python and still need to learn the Arduino coding. Getting this thing to communicate would make that process a lot easier...
I will say I cannot find a lot of documentation on the CANBed input pin. I am using pin 2, as that is what is in the code but I am not 100% sure that is correct. I have tried changing the pin to about every other pin number with no change. But...pin 2 is what was in the code example, so I am thinking it should be right.
Anyway, this particular car works best if you send a message and wait for a response. Just listening on a "CAN Receive" code is unlikely to work well (it does not work for me either). With this in mind, I have the following code I am working with. It is sending a request, and that appears to be working correctly. However, I do not get a Rx response written to the serial monitor. The Rx and Tx lights do blink, but I am not seeing a Rx...
#include <SPI.h>
#include "mcp_can.h"
/* Please modify SPI_CS_PIN to adapt to different baords.
CANBed V1 - 17
CANBed M0 - 3
CAN Bus Shield - 9
CANBed 2040 - 9
CANBed Dual - 9
OBD-2G Dev Kit - 9
Hud Dev Kit - 9
*/
#define SPI_CS_PIN 17
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
#define PID_ENGIN_PRM 0x0C
#define PID_VEHICLE_SPEED 0x0D
#define PID_COOLANT_TEMP 0x05
#define CAN_ID_PID 0x7DF
void setup() {
Serial.begin(115200);
while(!Serial);
// below code need for OBD-II GPS Dev Kit
// pinMode(A3, OUTPUT);
// digitalWrite(A3, HIGH);
while (CAN_OK != CAN.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ)){ // init can bus : baudrate = 500k
Serial.println("CAN init fail, retry...");
delay(100);
}
Serial.println("CAN init ok!");
// set mask, set both the mask to 0x3ff
CAN.init_Mask(0, 0x7FF);
CAN.init_Filt(0, 0x7DF);
CAN.init_Filt(1, 0x7E8);
// set filter, we can receive id from 0x04 ~ 0x09
CAN.init_Mask(1, 0x7FF);
CAN.init_Filt(2, 0x7DF);
CAN.init_Filt(3, 0x7E8);
CAN.init_Filt(4, 0x7DF);
CAN.init_Filt(5, 0x7E8);
CAN.setMode(MCP_NORMAL);
//CAN.setMode(MCP_LOOPBACK);
pinMode(2, INPUT);
Serial.println("And then?....");
}
void sendPid(unsigned char __pid){
unsigned char tmp[8] = {0x02, 0x01, __pid, 0x55, 0x55, 0x55, 0x55, 0x55};
CAN.sendMsgBuf(CAN_ID_PID, 8, tmp);
}
bool getSpeed(int *s)
{
sendPid(PID_VEHICLE_SPEED);
unsigned long __timeout = millis();
Serial.println("Message Sent Successfully!");
while(millis()-__timeout < 1000) // 1s time out
{
unsigned long rxID;
unsigned char len = 0;
unsigned char buf[8];
if (CAN_MSGAVAIL == CAN.checkReceive()){ // check if get data
CAN.readMsgBuf(&rxID, &len, buf); // read data, rxID: Recieve ID, len: data length, buf: data buf
if(buf[1] == 0x41)
{
*s = buf[3];
return 1;
}
}
}
return 0;
}
void loop() {
int __speed = 0;
int ret = getSpeed(&__speed);
if(ret)
{
Serial.print("Vehicle Speed: ");
Serial.print(__speed);
Serial.println(" kmh");
}
delay(500);
}
// END FILE
And here is what is on the serial monitor....
18:25:08.754 -> Entering Configuration Mode Successful!
18:25:08.754 -> Setting Baudrate Successful!
18:25:08.754 -> CAN init ok!
18:25:08.754 -> Starting to Set Mask!
18:25:08.754 -> Setting Mask Successful!
18:25:08.754 -> Starting to Set Filter!
18:25:08.754 -> Setting Filter Successful!
18:25:08.754 -> Starting to Set Filter!
18:25:08.754 -> Setting Filter Successful!
18:25:08.754 -> Starting to Set Mask!
18:25:08.754 -> Setting Mask Successful!
18:25:08.754 -> Starting to Set Filter!
18:25:08.754 -> Setting Filter Successful!
18:25:08.754 -> Starting to Set Filter!
18:25:08.754 -> Setting Filter Successful!
18:25:08.754 -> Starting to Set Filter!
18:25:08.754 -> Setting Filter Successful!
18:25:08.754 -> Starting to Set Filter!
18:25:08.754 -> Setting Filter Successful!
18:25:08.754 -> And then?....
18:25:08.754 -> Message Sent Successfully!
18:25:10.235 -> Message Sent Successfully!
18:25:11.730 -> Message Sent Successfully!
18:25:13.227 -> Message Sent Successfully!
I appreciate any help I can get on this. I reached out to Longan Labs a few weeks back, and they have been completely silent. I thought this would be a simple little project, but it certainly has not been to date. Starting to get a bit frustrating. Thanks in advance!
ghost