Skip to content

Commit 294907b

Browse files
authored
Merge pull request #221 from jimwhitelaw/add-mode-22
Fix for Custom PIDs #218
2 parents 3576084 + 77ad96e commit 294907b

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/ELMduino.cpp

+19-7
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ bool ELM327::initializeELM(const char &protocol, const byte &dataTimeout)
117117
// the timeout value to 30 seconds, then restore the previous value.
118118
uint16_t prevTimeout = timeout_ms;
119119
timeout_ms = 30000;
120-
120+
121121
int8_t state = sendCommand_Blocking("0100");
122122

123123
if (state == ELM_SUCCESS)
@@ -203,6 +203,8 @@ void ELM327::formatQueryArray(uint8_t service, uint16_t pid, uint8_t num_respons
203203
Serial.println(pid);
204204
}
205205

206+
isMode0x22Query = (service == 0x22 && pid <= 0xFF); // mode 0x22 responses always zero-pad the pid to 4 chars, even for a 2-char pid
207+
206208
query[0] = ((service >> 4) & 0xF) + '0';
207209
query[1] = (service & 0xF) + '0';
208210

@@ -616,7 +618,7 @@ double ELM327::processPID(const uint8_t &service, const uint16_t &pid, const uin
616618
{
617619
nb_query_state = SEND_COMMAND; // Reset the query state machine for next command
618620

619-
findResponse();
621+
findResponse(service, pid);
620622

621623
return conditionResponse(numExpectedBytes, scaleFactor, bias);
622624
}
@@ -2312,7 +2314,7 @@ int8_t ELM327::get_response(void)
23122314
}
23132315

23142316
/*
2315-
uint64_t ELM327::findResponse()
2317+
uint64_t ELM327::findResponse(uint8_t &service)
23162318
23172319
Description:
23182320
------------
@@ -2326,7 +2328,7 @@ int8_t ELM327::get_response(void)
23262328
-------
23272329
* uint64_t - Query response value
23282330
*/
2329-
uint64_t ELM327::findResponse()
2331+
uint64_t ELM327::findResponse(const uint8_t &service, const uint8_t &pid)
23302332
{
23312333
uint8_t firstDatum = 0;
23322334
char header[7] = {'\0'};
@@ -2344,8 +2346,18 @@ uint64_t ELM327::findResponse()
23442346
{
23452347
header[0] = query[0] + 4;
23462348
header[1] = query[1];
2347-
header[2] = query[2];
2348-
header[3] = query[3];
2349+
if (isMode0x22Query) // mode 0x22 responses always zero-pad the pid to 4 chars, even for a 2-char pid
2350+
{
2351+
header[2] = '0';
2352+
header[3] = '0';
2353+
header[4] = query[2];
2354+
header[5] = query[3];
2355+
}
2356+
else
2357+
{
2358+
header[2] = query[2];
2359+
header[3] = query[3];
2360+
}
23492361
}
23502362

23512363
if (debugMode)
@@ -2359,7 +2371,7 @@ uint64_t ELM327::findResponse()
23592371

23602372
if (firstHeadIndex >= 0)
23612373
{
2362-
if (longQuery)
2374+
if (longQuery | isMode0x22Query)
23632375
firstDatum = firstHeadIndex + 6;
23642376
else
23652377
firstDatum = firstHeadIndex + 4;

src/ELMduino.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ class ELM327
315315
bool begin(Stream& stream, const bool& debug = false, const uint16_t& timeout = 1000, const char& protocol = '0', const uint16_t& payloadLen = 40, const byte& dataTimeout = 0);
316316
bool initializeELM(const char& protocol = '0', const byte& dataTimeout = 0);
317317
void flushInputBuff();
318-
uint64_t findResponse();
318+
uint64_t findResponse(const uint8_t &service, const uint8_t &pid);
319319
bool queryPID(const uint8_t& service, const uint16_t& pid, const uint8_t& num_responses = 1);
320320
bool queryPID(char queryStr[]);
321321
double processPID(const uint8_t& service, const uint16_t& pid, const uint8_t& num_responses, const uint8_t& numExpectedBytes, const double& scaleFactor = 1, const float& bias = 0);
@@ -421,6 +421,7 @@ class ELM327
421421
private:
422422
char query[QUERY_LEN] = { '\0' };
423423
bool longQuery = false;
424+
bool isMode0x22Query = false;
424425
uint32_t currentTime;
425426
uint32_t previousTime;
426427

0 commit comments

Comments
 (0)