summaryrefslogtreecommitdiff
path: root/libraries/OBD/OBD.cpp
diff options
context:
space:
mode:
authorStanley Huang <stanleyhuangyc@live.com>2018-04-11 11:38:05 +1000
committerStanley Huang <stanleyhuangyc@live.com>2018-04-11 11:38:05 +1000
commit47b642a6e06e05d17b70c2369af5acbc2ae9daf2 (patch)
treeb39c3f994a28862463c0ac04f0fddb81cc6f18ba /libraries/OBD/OBD.cpp
parent26196c5808a0c1dd2fba7e357d9a7ba8aa7037d7 (diff)
parentda6126b1c37ed3aa8e095a1dcc8509d4537c4819 (diff)
download2021-arduino-obd-47b642a6e06e05d17b70c2369af5acbc2ae9daf2.tar.gz
2021-arduino-obd-47b642a6e06e05d17b70c2369af5acbc2ae9daf2.tar.bz2
2021-arduino-obd-47b642a6e06e05d17b70c2369af5acbc2ae9daf2.zip
Merge branch 'master' of https://github.com/stanleyhuangyc/ArduinoOBD
Diffstat (limited to 'libraries/OBD/OBD.cpp')
-rw-r--r--libraries/OBD/OBD.cpp40
1 files changed, 25 insertions, 15 deletions
diff --git a/libraries/OBD/OBD.cpp b/libraries/OBD/OBD.cpp
index 8dbdd29..92b9bdf 100644
--- a/libraries/OBD/OBD.cpp
+++ b/libraries/OBD/OBD.cpp
@@ -298,21 +298,31 @@ float COBD::getVoltage()
bool COBD::getVIN(char* buffer, byte bufsize)
{
- if (sendCommand("0902\r", buffer, bufsize)) {
- char *p = strstr(buffer, "0: 49 02");
- if (p) {
- char *q = buffer;
- p += 10;
- do {
- for (++p; *p == ' '; p += 3) {
- if (*q = hex2uint8(p + 1)) q++;
- }
- p = strchr(p, ':');
- } while(p);
- *q = 0;
- return true;
- }
- }
+ for (byte n = 0; n < 5; n++) {
+ if (sendCommand("0902\r", buffer, bufsize)) {
+ int len = hex2uint16(buffer);
+ char *p = strstr_P(buffer + 4, PSTR("0: 49 02 01"));
+ if (p) {
+ char *q = buffer;
+ p += 11; // skip the header
+ do {
+ while (*(++p) == ' ');
+ for (;;) {
+ *(q++) = hex2uint8(p);
+ while (*p && *p != ' ') p++;
+ while (*p == ' ') p++;
+ if (!*p || *p == '\r') break;
+ }
+ p = strchr(p, ':');
+ } while(p);
+ *q = 0;
+ if (q - buffer == len - 3) {
+ return true;
+ }
+ }
+ }
+ delay(100);
+ }
return false;
}