Skip to content

Commit

Permalink
Fix for Custom PIDs PowerBroker2#218
Browse files Browse the repository at this point in the history
Mode 0x22 response headers always zero-pad the pid to 4 chars, even for a 2-char pid. That confused the header detection and response parsing. This change checks for the mode and pid in findResponse() and adjusts the expected header accordingly.
  • Loading branch information
jimwhitelaw committed Jan 31, 2024
1 parent 3576084 commit 77ad96e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
26 changes: 19 additions & 7 deletions src/ELMduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ bool ELM327::initializeELM(const char &protocol, const byte &dataTimeout)
// the timeout value to 30 seconds, then restore the previous value.
uint16_t prevTimeout = timeout_ms;
timeout_ms = 30000;

int8_t state = sendCommand_Blocking("0100");

if (state == ELM_SUCCESS)
Expand Down Expand Up @@ -203,6 +203,8 @@ void ELM327::formatQueryArray(uint8_t service, uint16_t pid, uint8_t num_respons
Serial.println(pid);
}

isMode0x22Query = (service == 0x22 && pid <= 0xFF); // mode 0x22 responses always zero-pad the pid to 4 chars, even for a 2-char pid

query[0] = ((service >> 4) & 0xF) + '0';
query[1] = (service & 0xF) + '0';

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

findResponse();
findResponse(service, pid);

return conditionResponse(numExpectedBytes, scaleFactor, bias);
}
Expand Down Expand Up @@ -2312,7 +2314,7 @@ int8_t ELM327::get_response(void)
}

/*
uint64_t ELM327::findResponse()
uint64_t ELM327::findResponse(uint8_t &service)
Description:
------------
Expand All @@ -2326,7 +2328,7 @@ int8_t ELM327::get_response(void)
-------
* uint64_t - Query response value
*/
uint64_t ELM327::findResponse()
uint64_t ELM327::findResponse(const uint8_t &service, const uint8_t &pid)
{
uint8_t firstDatum = 0;
char header[7] = {'\0'};
Expand All @@ -2344,8 +2346,18 @@ uint64_t ELM327::findResponse()
{
header[0] = query[0] + 4;
header[1] = query[1];
header[2] = query[2];
header[3] = query[3];
if (isMode0x22Query) // mode 0x22 responses always zero-pad the pid to 4 chars, even for a 2-char pid
{
header[2] = '0';
header[3] = '0';
header[4] = query[2];
header[5] = query[3];
}
else
{
header[2] = query[2];
header[3] = query[3];
}
}

if (debugMode)
Expand All @@ -2359,7 +2371,7 @@ uint64_t ELM327::findResponse()

if (firstHeadIndex >= 0)
{
if (longQuery)
if (longQuery | isMode0x22Query)
firstDatum = firstHeadIndex + 6;
else
firstDatum = firstHeadIndex + 4;
Expand Down
3 changes: 2 additions & 1 deletion src/ELMduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ class ELM327
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);
bool initializeELM(const char& protocol = '0', const byte& dataTimeout = 0);
void flushInputBuff();
uint64_t findResponse();
uint64_t findResponse(const uint8_t &service, const uint8_t &pid);
bool queryPID(const uint8_t& service, const uint16_t& pid, const uint8_t& num_responses = 1);
bool queryPID(char queryStr[]);
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);
Expand Down Expand Up @@ -421,6 +421,7 @@ class ELM327
private:
char query[QUERY_LEN] = { '\0' };
bool longQuery = false;
bool isMode0x22Query = false;
uint32_t currentTime;
uint32_t previousTime;

Expand Down

0 comments on commit 77ad96e

Please sign in to comment.