diff options
author | Stanley Huang <stanleyhuangyc@gmail.com> | 2017-04-11 13:19:42 +1000 |
---|---|---|
committer | Stanley Huang <stanleyhuangyc@gmail.com> | 2017-04-11 13:19:42 +1000 |
commit | dd16d9e320a1088b6437d2dd41384d4c45431980 (patch) | |
tree | 164dd8154f33e6dc114ee9c580c15c67ecc10a58 /libraries/OBD/OBD.cpp | |
parent | 7562899738e6d06d13f5c76a9b83574468fc396c (diff) | |
download | 2021-arduino-obd-dd16d9e320a1088b6437d2dd41384d4c45431980.tar.gz 2021-arduino-obd-dd16d9e320a1088b6437d2dd41384d4c45431980.tar.bz2 2021-arduino-obd-dd16d9e320a1088b6437d2dd41384d4c45431980.zip |
Fixed connection failure due to too short timeout on slower K-line protocols
Diffstat (limited to 'libraries/OBD/OBD.cpp')
-rw-r--r-- | libraries/OBD/OBD.cpp | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/libraries/OBD/OBD.cpp b/libraries/OBD/OBD.cpp index f23da93..116a68d 100644 --- a/libraries/OBD/OBD.cpp +++ b/libraries/OBD/OBD.cpp @@ -401,17 +401,17 @@ bool COBD::init(OBD_PROTOCOLS protocol) const char *initcmd[] = {"ATZ\r", "ATE0\r", "ATH0\r"}; char buffer[64]; + m_state = OBD_DISCONNECTED; for (unsigned char i = 0; i < sizeof(initcmd) / sizeof(initcmd[0]); i++) { write(initcmd[i]); if (receive(buffer, sizeof(buffer), OBD_TIMEOUT_LONG) == 0) { - m_state = OBD_DISCONNECTED; return false; } } if (protocol != PROTO_AUTO) { - sprintf_P(buffer, PSTR("ATSP%u\r"), protocol); + sprintf_P(buffer, PSTR("ATSP %u\r"), protocol); + write(buffer); if (receive(buffer, sizeof(buffer), OBD_TIMEOUT_LONG) == 0 && !strstr(buffer, "OK")) { - m_state = OBD_DISCONNECTED; return false; } } @@ -422,15 +422,19 @@ bool COBD::init(OBD_PROTOCOLS protocol) for (byte i = 0; i < 4; i++) { byte pid = i * 0x20; sendQuery(pid); - char* data = getResponse(pid, buffer, sizeof(buffer)); - if (!data) break; - data--; - for (byte n = 0; n < 4; n++) { - if (data[n * 3] != ' ') - break; - pidmap[i * 4 + n] = hex2uint8(data + n * 3 + 1); + if (receive(buffer, sizeof(buffer), OBD_TIMEOUT_LONG) > 0) { + char *p = buffer; + while ((p = strstr(p, "41 "))) { + p += 3; + if (hex2uint8(p) == pid) { + p += 2; + for (byte n = 0; n < 4 && *(p + n * 3) == ' '; n++) { + pidmap[i * 4 + n] = hex2uint8(p + n * 3 + 1); + } + success = true; + } + } } - success = true; } if (success) { |