I'm asking here because the forum for the ELM327 unit is dead and the creator rarely visits.
To start, here is the OBD DTC reading code
byte COBD::readDTC(uint16_t codes[], byte count)
{
byte codesRead = 0;
for (byte n = 0; n < 6; n++) {
char buffer[128];
sprintf(buffer, "03%02X\r", n);
write(buffer);
Serial.println(buffer);
if (receive(buffer, sizeof(buffer)) > 0) {
Serial.println(buffer);
if (!strstr(buffer, "NO DATA")) {
char *p = strstr(buffer, "43");
if (p) {
while (codesRead < count && *p) {
p += 6;
if (*p == '\r') {
p = strchr(p, ':');
if (!p) break;
p += 2;
}
uint16_t code = hex2uint16(p);
if (code == 0) break;
codes[codesRead++] = code;
}
}
break;
}
}
}
return codesRead;
}
uint16_t hex2uint16(const char *p)
{
char c = *p;
uint16_t i = 0;
for (char n = 0; c && n < 4; c = *(++p)) {
if (c >= 'A' && c <= 'F') {
c -= 7;
} else if (c>='a' && c<='f') {
c -= 39;
} else if (c == ' ') {
continue;
} else if (c < '0' || c > '9') {
break;
}
i = (i << 4) | (c & 0xF);
n++;
}
return i;
}
int COBD::getDTCCount()
{
char buffer[64];
char val3[3];
write("01 01\r"); // send OBD request for DTCs
int bytesReceived = receive(buffer, 1000);
buffer[bytesReceived + 1] = 0;
if (bytesReceived>0) {
// Response should be "41 01 xx yy yy yy yy"
// looking for the value in xx
int response = strncmp(buffer, "41", 2);
if (response == 0) {
// Extract the numver of codes from the string
memcpy(val3, &buffer[6], 2);
val3[2] = '\0';
int numCodes = strtol(val3, NULL, 16); // Convert hex string to decimal
free(val3);
if ((numCodes & 128)>0) { // MIL is on
numCodes = numCodes - 128;
}
return numCodes;
}
else {
return 0;
}
}
}
My code to use the DTC is:
unsigned int numberofcodes = obd.getDTCCount();
lcd.println(numberofcodes);
if (numberofcodes > 0)
{
uint16_t codes[8];
byte dtcCount = obd.readDTC(codes, numberofcodes);
if (dtcCount == 0) {
lcd.println("No DTC");
}
else
{
for (byte i = 0; i < numberofcodes; i++)
{
lcd.println(codes[i]);
}
}
}
Using this code, I pulled 2 DTCs. The code gave me 20484 and 16896. My dumb butt tested the clear DTCs on my car and it takes ~100 miles to reset the codes so reproduction of this is time consuming.
Anyway. Normal DTCs in cars are like P0401 or P1101 so this output is pretty off the wall. I'm not even sure why the code sample is doing a hex to int conversion. I thought the ELM327 gave the code back in integer format.
Anyone know what's going on or maybe what's not right?