Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added function to clear diagnostic trouble codes (DTC) #7

Merged
merged 10 commits into from
Jan 23, 2021
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pidReadRaw KEYWORD2
vinRead KEYWORD2
ecuNameRead KEYWORD2
setTimeout KEYWORD2
clearAllStoredDTC KEYWORD2

#######################################
# Constants (LITERAL1)
Expand Down
29 changes: 29 additions & 0 deletions src/OBD2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,35 @@ int OBD2Class::supportedPidsRead()
return 1;
}

int OBD2Class::clearAllStoredDTC()
{
//Function clears stored Diagnostic Trouble Codes (DTC)

// 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(0x00); // number of additional bytes
CAN.write(0x04); // Mode / Service 4, for clearing DTC
if (CAN.endPacket()) {
// send success
break;
} else if (retries <= 1) {
return 0;
}
}

return 1;
}

int OBD2Class::pidRead(uint8_t mode, uint8_t pid, void* data, int length)
{
// make sure at least 60 ms have passed since the last response
Expand Down
2 changes: 2 additions & 0 deletions src/OBD2.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ class OBD2Class {

void setTimeout(unsigned long timeout);

int clearAllStoredDTC();

private:
int supportedPidsRead();

Expand Down