Hi all! I'm in the process of building an OBD reader using FlexCan and I've got most of it working, but I'm really struggling with reading fault codes. I think my reader may be working, but when I try and read what's coming in in each of the CAN message buffers I'm just getting random symbols or unknown character squares in the Serial reader.
What I'm doing is using this code to request a MODE 3 (fault code) message from the CAN simulator I have:
CAN_Request(MODE3,0); //Read Trouble Codes
Which runs this function:
void CAN_Request(byte CAN_MODE, byte PID_CALL) {
can_MsgTx.buf[0] = 0x01;
can_MsgTx.buf[1] = CAN_MODE; //03 for Mode 3 request
can_MsgTx.buf[2] = PID_CALL; //0 for Mode 3 request
can_MsgTx.buf[3] = 0;
can_MsgTx.buf[4] = 0;
can_MsgTx.buf[5] = 0;
can_MsgTx.buf[6] = 0;
can_MsgTx.buf[7] = 0;
can_MsgTx.len = 8;
//can_MsgTx.ext = 0;
can_MsgTx.flags.extended = 0;
can_MsgTx.flags.remote = 0;
can_MsgTx.id = PID_REQUEST; //PID_REQUEST = 0x7DF
// can_MsgTx.timeout = 500;
Can0.write(can_MsgTx);
}
So the message constructed should read:
7df 8 01 03 00 00 00 00 00 00
I then should get the following response from the simulator:
7e8 8 06 43 02 01 00 02 00 00
This is received using this call:
int CAN_Receive(byte PID_RESPONSE) { //PID_REPLY = 0x7E8
while (Can0.available())
{
Can0.read(can_MsgRx);
if (can_MsgRx.buf[1] == MODE3_RESPONSE) { //MODE3_RESPONSE = 0x43
DTC = can_MsgRx.buf[3];
Serial.println(can_MsgRx.buf[1]);
Serial.println(DTC);
return can_MsgRx.buf[3];// & can_MsgRx.buf[4] & can_MsgRx.buf[5];
}
}
}
I am seeing two repeating characters on the Serial print, the first says 67, which when converted to Hex is 43, which is the correct response as per what I'm expecting above. The second character is a square "unknown character" symbol in the serial print, and when shown on my OLED display it comes up as a sad face icon - how apt!
I think then I might be getting the right response but I'm not reading it correctly. I need to get it into hex format as the response code should read as follows:
43 = MODE 3 response
02 = 2 fault codes flagged
01 added to
00 = P0100 fault code (Mass airflow sensor fault)
02 added to
00 = P0200 fault code (fuel injector fault)
So does anyone see what I'm doing wrong here?