summaryrefslogtreecommitdiff
path: root/libraries/OBD
diff options
context:
space:
mode:
authorStanley Huang <stanleyhuangyc@gmail.com>2014-04-17 20:48:44 +0800
committerStanley Huang <stanleyhuangyc@gmail.com>2014-04-17 20:48:44 +0800
commite72f6dd48477170354dc3da58cc95b13f1b8e2a5 (patch)
treecab5dc98e34dbd115b03e73c53a25be8e0334537 /libraries/OBD
parent9a0730a5d9422441d3cfb168fb35772232a4e385 (diff)
download2021-arduino-obd-e72f6dd48477170354dc3da58cc95b13f1b8e2a5.tar.gz
2021-arduino-obd-e72f6dd48477170354dc3da58cc95b13f1b8e2a5.tar.bz2
2021-arduino-obd-e72f6dd48477170354dc3da58cc95b13f1b8e2a5.zip
Update OBD library
Diffstat (limited to 'libraries/OBD')
-rw-r--r--libraries/OBD/OBD.cpp30
-rw-r--r--libraries/OBD/OBD.h10
2 files changed, 28 insertions, 12 deletions
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 <stanleyhuangyc@gmail.com>
*************************************************************************/
@@ -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 <stanleyhuangyc@gmail.com>
*************************************************************************/
@@ -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);