blob: acc67356858069a9d87640291fb03d80f61b0475 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
/*************************************************************************
* Arduino OBD-II UART Adapter Library
* http://www.arduinodev.com/hardware/obd-kit
* Distributed under GPL v2.0
* (C)2012~2013 Written by Stanley Huang <stanleyhuangyc@gmail.com>
* All rights reserved.
*************************************************************************/
#define OBD_TIMEOUT_SHORT 2000 /* ms */
#define OBD_TIMEOUT_LONG 7000 /* ms */
#define OBD_TIMEOUT_INIT 3000 /* ms */
#define OBD_SERIAL_BAUDRATE 38400
#define OBD_RECV_BUF_SIZE 64
#ifndef OBDUART
#ifdef __AVR_ATmega32U4__ /* for Leonardo */
#define OBDUART Serial1
#else
#define OBDUART Serial
#endif
#endif
// mode 0 pids
#define PID_RPM 0x0C
#define PID_SPEED 0x0D
#define PID_THROTTLE 0x11
#define PID_ENGINE_LOAD 0x04
#define PID_COOLANT_TEMP 0x05
#define PID_INTAKE_TEMP 0x0F
#define PID_MAF_FLOW 0x10
#define PID_ABS_ENGINE_LOAD 0x43
#define PID_AMBIENT_TEMP 0x46
#define PID_FUEL_PRESSURE 0x0A
#define PID_INTAKE_MAP 0x0B
#define PID_BAROMETRIC 0x33
#define PID_TIMING_ADVANCE 0x0E
#define PID_FUEL_LEVEL 0x2F
#define PID_RUNTIME 0x1F
#define PID_DISTANCE 0x31
unsigned int hex2uint16(const char *p);
unsigned char hex2uint8(const char *p);
class COBD
{
public:
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);
// 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;
protected:
static int GetPercentageValue(char* data)
{
return (int)hex2uint8(data) * 100 / 255;
}
static int GetLargeValue(char* data)
{
return hex2uint16(data);
}
static int GetSmallValue(char* data)
{
return hex2uint8(data);
}
static int GetTemperatureValue(char* data)
{
return (int)hex2uint8(data) - 40;
}
virtual void DataTimeout() {}
byte pidmap[4 * 4];
};
|