summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStanley Huang <stanleyhuangyc@gmail.com>2017-04-11 13:19:42 +1000
committerStanley Huang <stanleyhuangyc@gmail.com>2017-04-11 13:19:42 +1000
commitdd16d9e320a1088b6437d2dd41384d4c45431980 (patch)
tree164dd8154f33e6dc114ee9c580c15c67ecc10a58
parent7562899738e6d06d13f5c76a9b83574468fc396c (diff)
download2021-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
-rw-r--r--libraries/OBD/OBD.cpp26
-rw-r--r--libraries/OBD2UART/OBD2UART.cpp26
2 files changed, 30 insertions, 22 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) {
diff --git a/libraries/OBD2UART/OBD2UART.cpp b/libraries/OBD2UART/OBD2UART.cpp
index fd6b35d..4e9a8de 100644
--- a/libraries/OBD2UART/OBD2UART.cpp
+++ b/libraries/OBD2UART/OBD2UART.cpp
@@ -399,17 +399,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;
}
}
@@ -420,15 +420,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) {