Skip to content

Commit

Permalink
Added function to clear diagnostic trouble codes (DTC) (#7)
Browse files Browse the repository at this point in the history
* Added function to clear diagnostic trouble codes (DTC) using Service 04 functionality.
* Added clearAllStoredDTC function to keywords.txt file
* Corrected return value of clearAllStoredDTC function to return 1 upon success
  • Loading branch information
JasonJoyner01 authored Jan 23, 2021
1 parent 28be93a commit 20ef945
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
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

2 comments on commit 20ef945

@omarbadar
Copy link

Choose a reason for hiding this comment

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

Only worked for me when line 714 changed to below:

CAN.write(0x01); // number of additional bytes

@SYMEPETM
Copy link

Choose a reason for hiding this comment

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

how can i clear the DTC based of that?

"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(0x01); // 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;

}"

thats my .ino code in my arduino sketch |

"#include <CAN.h> // the OBD2 library depends on the CAN library
#include <OBD2.h>

bool running = false; // Variable to control data printing

void setup() {
Serial.begin(9600); // Initializes serial communication with a baud rate of 9600
while (!Serial); // Waits until the serial connection is established

Serial.println(F("OBD2 data printer"));
Serial.println(F("Type 'start' to begin or 'stop' to end printing data."));

// Attempt to connect to the OBD-II CAN bus
while (true) {
Serial.print(F("Attempting to connect to OBD2 CAN bus ... "));

if (!OBD2.begin()) { // Checks connection
  Serial.println(F("failed!"));
  delay(1000); // Waits before retrying
} else {
  Serial.println(F("success"));
  break;
}

}

Serial.println();
Serial.print("VIN = ");
Serial.println(OBD2.vinRead());
Serial.print("ECU Name = ");
Serial.println(OBD2.ecuNameRead());
Serial.println();
}

void loop() {
// Check if any data is available in the Serial buffer
if (Serial.available()) {
String command = Serial.readStringUntil('\n'); // Read the command until a newline
command.trim(); // Remove any leading/trailing whitespace

// Check for start/stop commands
if (command.equalsIgnoreCase("start")) {
  running = true; // Set running to true
  Serial.println(F("Started printing data."));
} else if (command.equalsIgnoreCase("stop")) {
  running = false; // Set running to false
  Serial.println(F("Stopped printing data."));
}

}

// Only process PIDs if running is true
if (running) {
for (int pid = 0; pid < 96; pid++) {
processPid(pid);
}
Serial.println(); // Empty line for readability
delay(1000); // Reduced delay for more frequent updates
}
}

void processPid(int pid) {
if (!OBD2.pidSupported(pid)) { // Check if PID is supported
return; // Skip unsupported PIDs
}

Serial.print(OBD2.pidName(pid));
Serial.print(F(" = "));

if (OBD2.pidValueRaw(pid)) {
unsigned long pidRawValue = OBD2.pidReadRaw(pid);
Serial.print(F("0x"));
Serial.print(pidRawValue, HEX);
} else {
float pidValue = OBD2.pidRead(pid);
if (isnan(pidValue)) {
Serial.print("error");
} else {
Serial.print(pidValue);
Serial.print(F(" "));
Serial.print(OBD2.pidUnits(pid));
}
}
delay(500);
Serial.println(); // New line after each PID output
}
"

Please sign in to comment.