summaryrefslogtreecommitdiff
path: root/libraries
diff options
context:
space:
mode:
authorStanley Huang <stanleyhuangyc@gmail.com>2013-04-20 17:20:37 +0800
committerStanley Huang <stanleyhuangyc@gmail.com>2013-04-20 17:20:37 +0800
commit8dcb1c2acdbd6a80e4aaa1b9b7f8833d6547e40c (patch)
tree846d9e8c1aaef2e041b5ef74139dec546e1705ef /libraries
parent1d62e07da40ba9225dfe4f7e93129d4d7da03ccb (diff)
download2021-arduino-obd-8dcb1c2acdbd6a80e4aaa1b9b7f8833d6547e40c.tar.gz
2021-arduino-obd-8dcb1c2acdbd6a80e4aaa1b9b7f8833d6547e40c.tar.bz2
2021-arduino-obd-8dcb1c2acdbd6a80e4aaa1b9b7f8833d6547e40c.zip
improving OBD-II library, adding IsValidPID API
Diffstat (limited to 'libraries')
-rw-r--r--libraries/OBD/OBD.cpp33
-rw-r--r--libraries/OBD/OBD.h28
2 files changed, 42 insertions, 19 deletions
diff --git a/libraries/OBD/OBD.cpp b/libraries/OBD/OBD.cpp
index 938523b..5ea4c47 100644
--- a/libraries/OBD/OBD.cpp
+++ b/libraries/OBD/OBD.cpp
@@ -9,13 +9,14 @@
#include <avr/pgmspace.h>
#include "OBD.h"
-#define INIT_CMD_COUNT 7
+#define INIT_CMD_COUNT 4
#define MAX_CMD_LEN 6
-const char PROGMEM s_initcmd[INIT_CMD_COUNT][MAX_CMD_LEN] = {"ATZ\r","ATE0\r","ATL1\r","ATI\r","0100\r","0120\r","0140\r"};
+const char PROGMEM s_initcmd[INIT_CMD_COUNT][MAX_CMD_LEN] = {"ATZ\r","ATE0\r","ATL1\r","ATI\r"};
const char PROGMEM s_searching[] = "SEARCHING";
const char PROGMEM s_cmd_fmt[] = "%02X%02X 1\r";
-const char PROGMEM s_cmd_sleep[MAX_CMD_LEN] = "atlp\r";
+const char PROGMEM s_cmd_sleep[] = "atlp\r";
+const char PROGMEM s_cmd_vin[] = "0902\r";
const char PROGMEM s_response_begin[] = "41 ";
unsigned int hex2uint16(const char *p)
@@ -176,7 +177,7 @@ bool COBD::GetParsedData(byte pid, char* data, int& result)
break;
case PID_SPEED:
case PID_BAROMETRIC:
- case PID_INTAKE_PRESSURE:
+ case PID_INTAKE_MAP:
result = GetSmallValue(data);
break;
case PID_TIMING_ADVANCE:
@@ -227,6 +228,16 @@ void COBD::Sleep(int seconds)
}
}
+bool COBD::IsValidPID(byte pid)
+{
+ if (pid >= 0x7f)
+ return false;
+ pid--;
+ byte i = pid >> 3;
+ byte b = 0x80 >> (pid & 0x7);
+ return pidmap[i] & b;
+}
+
bool COBD::Init(bool passive)
{
unsigned long currentMillis;
@@ -265,6 +276,20 @@ bool COBD::Init(bool passive)
}
}
}
+
+ // load pid map
+ memset(pidmap, 0, sizeof(pidmap));
+ for (byte i = 0; i < 4; i++) {
+ Query(i * 0x20);
+ char* data = GetResponse(i * 0x20, buffer);
+ if (!data) break;
+ data--;
+ for (byte n = 0; n < 4; n++) {
+ if (data[n * 3] != ' ')
+ break;
+ pidmap[i * 4 + n] = hex2uint8(data + n * 3 + 1);
+ }
+ }
errors = 0;
return true;
}
diff --git a/libraries/OBD/OBD.h b/libraries/OBD/OBD.h
index e608fee..acc6735 100644
--- a/libraries/OBD/OBD.h
+++ b/libraries/OBD/OBD.h
@@ -1,7 +1,8 @@
/*************************************************************************
-* OBD-II (ELM327) data accessing library for Arduino
+* Arduino OBD-II UART Adapter Library
+* http://www.arduinodev.com/hardware/obd-kit
* Distributed under GPL v2.0
-* Copyright (c) 2012 Stanley Huang <stanleyhuangyc@gmail.com>
+* (C)2012~2013 Written by Stanley Huang <stanleyhuangyc@gmail.com>
* All rights reserved.
*************************************************************************/
@@ -9,7 +10,7 @@
#define OBD_TIMEOUT_LONG 7000 /* ms */
#define OBD_TIMEOUT_INIT 3000 /* ms */
#define OBD_SERIAL_BAUDRATE 38400
-#define OBD_RECV_BUF_SIZE 48
+#define OBD_RECV_BUF_SIZE 64
#ifndef OBDUART
#ifdef __AVR_ATmega32U4__ /* for Leonardo */
@@ -30,7 +31,7 @@
#define PID_ABS_ENGINE_LOAD 0x43
#define PID_AMBIENT_TEMP 0x46
#define PID_FUEL_PRESSURE 0x0A
-#define PID_INTAKE_PRESSURE 0x0B
+#define PID_INTAKE_MAP 0x0B
#define PID_BAROMETRIC 0x33
#define PID_TIMING_ADVANCE 0x0E
#define PID_FUEL_LEVEL 0x2F
@@ -43,25 +44,24 @@ unsigned char hex2uint8(const char *p);
class COBD
{
public:
- COBD()
- {
- dataMode = 1;
- errors = 0;
- }
+ COBD():dataMode(1),errors(0) {}
bool Init(bool passive = false);
bool ReadSensor(byte pid, int& result, bool passive = false);
+ bool IsValidPID(byte pid);
void Sleep(int seconds);
- // Query and GetResponse for advanced usage only
+ // following APIs are for advanced usages only
void Query(byte pid);
+ bool GetParsedData(byte pid, char* data, int& result);
virtual char* GetResponse(byte pid, char* buffer);
virtual bool GetResponse(byte pid, int& result);
virtual bool GetResponsePassive(byte& pid, int& result);
virtual bool DataAvailable();
+ virtual char ReadData();
+ virtual void WriteData(const char* s);
+ virtual void WriteData(const char c);
byte dataMode;
byte errors;
- //char recvBuf[OBD_RECV_BUF_SIZE];
protected:
- static bool GetParsedData(byte pid, char* data, int& result);
static int GetPercentageValue(char* data)
{
return (int)hex2uint8(data) * 100 / 255;
@@ -78,8 +78,6 @@ protected:
{
return (int)hex2uint8(data) - 40;
}
- virtual char ReadData();
- virtual void WriteData(const char* s);
- virtual void WriteData(const char c);
virtual void DataTimeout() {}
+ byte pidmap[4 * 4];
};