arduino-OBD2
arduino-OBD2 copied to clipboard
How to read the dtc codes
Hello, How to read the dtc codes?
int OBD2Class::ReadDtcs(uint8_t mode, void* data, int length) { // make sure at least 60 ms have passed since the last response unsigned long lastResponseDelta = millis() - _lastPidResponseMillis; if (lastResponseDelta < 60) { delay(60 - lastResponseDelta); } for (int retries = 10; retries > 0; retries--) { if (_useExtendedAddressing) { CAN.beginExtendedPacket(0x18db33f1, 8); } else { CAN.beginPacket(0x7df, 8); } CAN.write(0x02); // number of additional bytes CAN.write(mode); //CAN.write(pid); if (CAN.endPacket()) { // send success break; } else if (retries <= 1) { return 0; } } bool splitResponse = (length > 5); for (unsigned long start = millis(); (millis() - start) < _responseTimeout;) { if (CAN.parsePacket() != 0 && (splitResponse ? (CAN.read() == 0x10 && CAN.read()) : CAN.read()) && (CAN.read() == (mode | 0x40))) {
_lastPidResponseMillis = millis();
// got a response
if (!splitResponse) {
return CAN.readBytes((uint8_t*)data, length);
}
int read = CAN.readBytes((uint8_t*)data, 3);
for (int i = 0; read < length; i++) {
delay(60);
// send the request for the next chunk
if (_useExtendedAddressing) {
CAN.beginExtendedPacket(0x18db33f1, 8);
} else {
CAN.beginPacket(0x7df, 8);
}
CAN.write(0x30);
CAN.endPacket();
// wait for response
while (CAN.parsePacket() == 0 ||
CAN.read() != (0x21 + i)); // correct sequence number
while (CAN.available()) {
((uint8_t*)data)[read++] = CAN.read();
}
}
_lastPidResponseMillis = millis();
return read;
}
}
return 0; }
I have added a function like this. While running this function, there is no output. could you please suggest any changes.
Thanks
Hello, Is the above code is correct to read the stored dtc codes?
hi, you removed the pids byte, try the following: CAN.write(0x01); // number of additional bytes
hi, you removed the pids byte, try the following: CAN.write(0x01); // number of additional bytes
Thanks for the response, I tried but the above function returned zero (0). I used this PID to get the status of the DTC codes.
It returned "516352" value. As per below table.
How should I encode this value.
Hi, I tried on my car, it returns 0 but actually I don't have DTC code, like in your case: 516352 = binary 00000000 00000111 11100001 00000000 A = 00000000 B = 00000111 C = 11100001 D = 00000000 A7 is 0 (it means that the warning light on the dashboard is off) A6-A0 is 0 (no DTCs)
Hi, Could you please provide an example as to how this function can be used to read DTCs? I don't understand the arguments I have to pass, besides 0x03 to select mode 3.
Thanks !
That function is incomplete to read DTCs you have to do more work than what is done there
Thank you for the reply!
Yes this is what I suspected, sadly I don't have the skill to complete the function, I tried but I'm too far off :)
Too bad Sandeep's great library doesn't provide this feature though.
Cheers!