diff --git a/keywords.txt b/keywords.txt index 3a3ec6d..235d70d 100644 --- a/keywords.txt +++ b/keywords.txt @@ -24,6 +24,7 @@ pidReadRaw KEYWORD2 vinRead KEYWORD2 ecuNameRead KEYWORD2 setTimeout KEYWORD2 +clearAllStoredDTC KEYWORD2 ####################################### # Constants (LITERAL1) diff --git a/src/OBD2.cpp b/src/OBD2.cpp index bece249..8fd329c 100644 --- a/src/OBD2.cpp +++ b/src/OBD2.cpp @@ -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 diff --git a/src/OBD2.h b/src/OBD2.h index 52312e0..0605763 100644 --- a/src/OBD2.h +++ b/src/OBD2.h @@ -131,6 +131,8 @@ class OBD2Class { void setTimeout(unsigned long timeout); + int clearAllStoredDTC(); + private: int supportedPidsRead();