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
28 changes: 28 additions & 0 deletions src/OBD2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,34 @@ int OBD2Class::supportedPidsRead()
return 1;
}

int OBD2Class::clearAllStoredDTC() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add an entry for clearAllStoredDTC in both keywords.txt and API.md.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I'll add the function to the keywords.txt file. How do you feel about this for the API.md?

Clear Stored Diagnostic Trouble Codes (DTC)

Clears any stored DTCs in the vehicle ECU, and turns off Malfunction Indicator Light (MIL, more commonly known as the Check Engine light).

int success = OBD2.clearAllStoredDTC();

Returns 1 if successful, or 0 on failure

sandeepmistry marked this conversation as resolved.
Show resolved Hide resolved
//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 {
sandeepmistry marked this conversation as resolved.
Show resolved Hide resolved
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) {
sandeepmistry marked this conversation as resolved.
Show resolved Hide resolved
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