Skip to content

Commit

Permalink
Add number of expected responses parameter to speed up Arduino <> Car…
Browse files Browse the repository at this point in the history
… communications
  • Loading branch information
Thom wijtenburg committed Mar 20, 2018
1 parent db7fdf2 commit 4f3e11b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
14 changes: 7 additions & 7 deletions libraries/OBD/OBD.cpp
Expand Up @@ -63,29 +63,29 @@ byte COBD::sendCommand(const char* cmd, char* buf, byte bufsize, int timeout)
return receive(buf, bufsize, timeout);
}

void COBD::sendQuery(byte pid)
void COBD::sendQuery(byte pid, byte num_results)
{
char cmd[8];
sprintf(cmd, "%02X%02X\r", dataMode, pid);
sprintf(cmd, "%02X%02X%01X\r", dataMode, pid, num_results);
#ifdef DEBUG
debugOutput(cmd);
#endif
write(cmd);
}

bool COBD::readPID(byte pid, int& result)
bool COBD::readPID(byte pid, int& result, byte num_results)
{
// send a query command
sendQuery(pid);
sendQuery(pid, num_results);
// receive and parse the response
return getResult(pid, result);
}

byte COBD::readPID(const byte pid[], byte count, int result[])
byte COBD::readPID(const byte pid[], byte count, int result[], byte num_results)
{
byte results = 0;
for (byte n = 0; n < count; n++) {
if (readPID(pid[n], result[n])) {
if (readPID(pid[n], result[n], num_results)) {
results++;
}
}
Expand Down Expand Up @@ -430,7 +430,7 @@ bool COBD::init(OBD_PROTOCOLS protocol)
bool success = false;
for (byte i = 0; i < 4; i++) {
byte pid = i * 0x20;
sendQuery(pid);
sendQuery(pid, 1);
if (receive(buffer, sizeof(buffer), OBD_TIMEOUT_LONG) > 0) {
char *p = buffer;
while ((p = strstr(p, "41"))) {
Expand Down
6 changes: 3 additions & 3 deletions libraries/OBD/OBD.h
Expand Up @@ -130,9 +130,9 @@ class COBD
// get connection state
virtual OBD_STATES getState() { return m_state; }
// read specified OBD-II PID value
virtual bool readPID(byte pid, int& result);
virtual bool readPID(byte pid, int& result, byte num_results);
// read multiple (up to 8) OBD-II PID values, return number of values obtained
virtual byte readPID(const byte pid[], byte count, int result[]);
virtual byte readPID(const byte pid[], byte count, int result[], byte num_results);
// set device into low power mode
virtual void enterLowPowerMode();
// wake up device from low power mode
Expand All @@ -152,7 +152,7 @@ class COBD
// read out MEMS data (acc for accelerometer, gyr for gyroscope, temp in 0.1 celcius degree)
virtual bool memsRead(int16_t* acc, int16_t* gyr = 0, int16_t* mag = 0, int16_t* temp = 0);
// send query for specified PID
virtual void sendQuery(byte pid);
virtual void sendQuery(byte pid, byte num_results);
// retrive and parse the response of specifie PID
virtual bool getResult(byte& pid, int& result);
// determine if the PID is supported
Expand Down

0 comments on commit 4f3e11b

Please sign in to comment.