From c09ad9a88036f9f4c658abe795cf2ee1eb8d3e1c Mon Sep 17 00:00:00 2001 From: Stanley Huang Date: Mon, 4 May 2015 11:23:46 +1000 Subject: Fix setBaudRate() --- libraries/OBD/OBD.cpp | 21 +++++++++------------ libraries/OBD/OBD.h | 8 ++++++-- 2 files changed, 15 insertions(+), 14 deletions(-) (limited to 'libraries') diff --git a/libraries/OBD/OBD.cpp b/libraries/OBD/OBD.cpp index 72955f0..8ee5ae9 100644 --- a/libraries/OBD/OBD.cpp +++ b/libraries/OBD/OBD.cpp @@ -2,7 +2,7 @@ * Arduino Library for OBD-II UART/I2C Adapter * Distributed under GPL v2.0 * Visit http://freematics.com for more information -* (C)2012-2014 Stanley Huang +* (C)2012-2015 Stanley Huang *************************************************************************/ #include @@ -377,17 +377,14 @@ void COBD::end() bool COBD::setBaudRate(unsigned long baudrate) { - char buf[OBD_RECV_BUF_SIZE]; OBDUART.print("ATBR1 "); OBDUART.print(baudrate); OBDUART.print('\r'); - if (receive(buf) && strstr(buf, "OK")) { - OBDUART.end(); - OBDUART.begin(baudrate); - return true; - } else { - return false; - } + delay(100); + OBDUART.end(); + OBDUART.begin(baudrate); + while (available()) read(); + return true; } bool COBD::initGPS(unsigned long baudrate) @@ -490,7 +487,7 @@ void COBDI2C::write(const char* s) Wire.endTransmission(); } -bool COBDI2C::sendCommand(byte cmd, uint8_t data, byte* payload, byte payloadBytes) +bool COBDI2C::sendCommandBlock(byte cmd, uint8_t data, byte* payload, byte payloadBytes) { COMMAND_BLOCK cmdblock = {millis(), cmd, data}; Wire.beginTransmission(I2C_ADDR); @@ -548,13 +545,13 @@ void COBDI2C::setPID(byte pid) void COBDI2C::applyPIDs() { - sendCommand(CMD_APPLY_OBD_PIDS, 0, (byte*)obdPid, sizeof(obdPid)); + sendCommandBlock(CMD_APPLY_OBD_PIDS, 0, (byte*)obdPid, sizeof(obdPid)); delay(200); } void COBDI2C::loadData() { - sendCommand(CMD_LOAD_OBD_DATA); + sendCommandBlock(CMD_LOAD_OBD_DATA); dataIdleLoop(); Wire.requestFrom((byte)I2C_ADDR, (byte)MAX_PAYLOAD_SIZE, (byte)0); Wire.readBytes((char*)obdInfo, MAX_PAYLOAD_SIZE); diff --git a/libraries/OBD/OBD.h b/libraries/OBD/OBD.h index d7e46cb..362c1bb 100644 --- a/libraries/OBD/OBD.h +++ b/libraries/OBD/OBD.h @@ -2,7 +2,7 @@ * Arduino Library for OBD-II UART/I2C Adapter * Distributed under GPL v2.0 * Visit http://freematics.com for more information -* (C)2012-2014 Stanley Huang +* (C)2012-2015 Stanley Huang *************************************************************************/ #include @@ -132,6 +132,7 @@ public: virtual void wakeup(); // set working protocol (default auto) virtual bool setProtocol(OBD_PROTOCOLS h = PROTO_AUTO); + // send AT command and receive response virtual byte sendCommand(const char* cmd, char* buf); // clear diagnostic trouble code virtual void clearDTC(); @@ -213,15 +214,18 @@ public: void end(); bool read(byte pid, int& result); void write(const char* s); + // API not applicable + bool setBaudRate(unsigned long baudrate) { return false; } // Asynchronized access API void setPID(byte pid); void applyPIDs(); void loadData(); uint16_t getData(byte pid, int& result); protected: - bool sendCommand(byte cmd, uint8_t data = 0, byte* payload = 0, byte payloadBytes = 0); byte receive(char* buffer, int timeout = OBD_TIMEOUT_LONG); byte m_addr; PID_INFO obdInfo[MAX_PIDS]; byte obdPid[MAX_PIDS]; +private: + bool sendCommandBlock(byte cmd, uint8_t data = 0, byte* payload = 0, byte payloadBytes = 0); }; -- cgit v1.2.3