From eb4b45659858c8eee243e933aa5ae19467b57c26 Mon Sep 17 00:00:00 2001 From: Stanley Huang Date: Fri, 8 May 2015 23:15:54 +1000 Subject: Add getVIN() for retrieving VIN as string --- libraries/OBD/OBD.cpp | 22 +++++++++++++++++++++- libraries/OBD/OBD.h | 2 ++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/libraries/OBD/OBD.cpp b/libraries/OBD/OBD.cpp index 8ee5ae9..b431297 100644 --- a/libraries/OBD/OBD.cpp +++ b/libraries/OBD/OBD.cpp @@ -261,6 +261,26 @@ float COBD::getVoltage() return 0; } +bool COBD::getVIN(char* buffer) +{ + if (sendCommand("0902\r", buffer)) { + 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; + } + } + return false; +} + bool COBD::isValidPID(byte pid) { if (pid >= 0x7f) @@ -380,7 +400,7 @@ bool COBD::setBaudRate(unsigned long baudrate) OBDUART.print("ATBR1 "); OBDUART.print(baudrate); OBDUART.print('\r'); - delay(100); + delay(50); OBDUART.end(); OBDUART.begin(baudrate); while (available()) read(); diff --git a/libraries/OBD/OBD.h b/libraries/OBD/OBD.h index 362c1bb..dcd30fb 100644 --- a/libraries/OBD/OBD.h +++ b/libraries/OBD/OBD.h @@ -138,6 +138,8 @@ public: virtual void clearDTC(); // get battery voltage (in 0.1V, e.g. 125 for 12.5V, works without ECU) virtual float getVoltage(); + // get VIN as a string, buffer length should be >= OBD_RECV_BUF_SIZE + virtual bool getVIN(char* buffer); // send query for specified PID virtual void sendQuery(byte pid); // retrive and parse the response of specifie PID -- cgit v1.2.3