From e72f6dd48477170354dc3da58cc95b13f1b8e2a5 Mon Sep 17 00:00:00 2001 From: Stanley Huang Date: Thu, 17 Apr 2014 20:48:44 +0800 Subject: Update OBD library --- libraries/OBD/OBD.cpp | 30 +++++++++++++++++++++--------- libraries/OBD/OBD.h | 10 +++++++--- 2 files changed, 28 insertions(+), 12 deletions(-) (limited to 'libraries') diff --git a/libraries/OBD/OBD.cpp b/libraries/OBD/OBD.cpp index cd5290e..65c424c 100644 --- a/libraries/OBD/OBD.cpp +++ b/libraries/OBD/OBD.cpp @@ -1,7 +1,7 @@ /************************************************************************* * Arduino Library for OBD-II UART/I2C Adapter * Distributed under GPL v2.0 -* Visit http://freematics.com for more information +* Visit http://arduinodev.com for more information * (C)2012-2014 Stanley Huang *************************************************************************/ @@ -214,9 +214,17 @@ bool COBD::isValidPID(byte pid) return pidmap[i] & b; } -void COBD::begin() +void COBD::begin(unsigned long baudrate) { OBDUART.begin(OBD_SERIAL_BAUDRATE); + if (baudrate) { + OBDUART.print("ATBR1 "); + OBDUART.print(baudrate); + OBDUART.print('\r'); + OBDUART.end(); + delay(100); + OBDUART.begin(baudrate); + } } byte COBD::receive(char* buffer, int timeout) @@ -225,20 +233,23 @@ byte COBD::receive(char* buffer, int timeout) unsigned char n = 0; bool prompted = false; - if (buffer) buffer[0] = 0; for (;;) { if (available()) { char c = read(); if (n > 2 && c == '>') { // prompt char received prompted = true; - } else if (n < OBD_RECV_BUF_SIZE - 1 && buffer) { - buffer[n++] = c; - if (c == '.') { - n = 0; - timeout = OBD_TIMEOUT_LONG; + } else if (n < OBD_RECV_BUF_SIZE - 1) { + if (buffer) { + if (c == '.' && n > 2 && buffer[n - 1] == '.' && buffer[n - 2] == '.') { + n = 0; + timeout = OBD_TIMEOUT_LONG; + } else { + buffer[n++] = c; + } + } else { + n++; } - buffer[n] = 0; } } else if (prompted) { break; @@ -250,6 +261,7 @@ byte COBD::receive(char* buffer, int timeout) dataIdleLoop(); } } + if (buffer) buffer[n] = 0; return n; } diff --git a/libraries/OBD/OBD.h b/libraries/OBD/OBD.h index 2718eb2..dcf4890 100644 --- a/libraries/OBD/OBD.h +++ b/libraries/OBD/OBD.h @@ -1,7 +1,7 @@ /************************************************************************* * Arduino Library for OBD-II UART/I2C Adapter * Distributed under GPL v2.0 -* Visit http://freematics.com for more information +* Visit http://arduinodev.com for more information * (C)2012-2014 Stanley Huang *************************************************************************/ @@ -58,7 +58,11 @@ class COBD { public: COBD():dataMode(1),errors(0),m_state(OBD_DISCONNECTED) {} - virtual void begin(); + /* + Serial baudrate is only adjustable for Arduino OBD-II Adapters V2 + Check out http://arduinodev.com/hardware/obd-kit/ + */ + virtual void begin(unsigned long baudrate = 0); virtual bool init(byte protocol = 0); virtual bool read(byte pid, int& result); virtual void sleep(); @@ -75,7 +79,7 @@ public: byte vin[17]; protected: virtual char* getResponse(byte& pid, char* buffer); - virtual byte receive(char* buffer, int timeout = OBD_TIMEOUT_SHORT); + virtual byte receive(char* buffer = 0, int timeout = OBD_TIMEOUT_SHORT); virtual bool available(); virtual char read(); virtual void write(const char* s); -- cgit v1.2.3