Skip to content

added function to read trouble codes DTC #20

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/OBD2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,4 +795,79 @@ int OBD2Class::pidRead(uint8_t mode, uint8_t pid, void* data, int length)
return 0;
}

int OBD2Class::pidReadCustom(uint8_t mode, uint8_t pid, 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) && CAN.read() == pid)) {

_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;
}

OBD2Class::getDTC(void* data,int length){
pidReadCustom(0x03,0x01,data,length);
}

OBD2Class OBD2;
3 changes: 3 additions & 0 deletions src/OBD2.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ class OBD2Class {

int clearAllStoredDTC();

int pidReadCustom(uint8_t mode, uint8_t pid, void* data, int length);
void getDTC(void* data,int length);

private:
int supportedPidsRead();

Expand Down