Skip to content

Commit eddeb3c

Browse files
author
Brendan Whitfield
committed
Merge branch 'master' into dtc
2 parents 5bbf2a2 + 9f2df62 commit eddeb3c

24 files changed

Lines changed: 1270 additions & 529 deletions

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
python-OBD
2+
==========
3+
4+
A python module for handling realtime sensor data from OBD-II vehicle
5+
ports. Works with ELM327 OBD-II adapters, and is fit for the Raspberry
6+
Pi.
7+
8+
Installation
9+
------------
10+
11+
```Shell
12+
$ pip install obd
13+
```
14+
15+
Basic Usage
16+
-----------
17+
18+
```Python
19+
import obd
20+
21+
connection = obd.OBD() # auto-connects to USB or RF port
22+
23+
cmd = obd.commands.RPM # select an OBD command (sensor)
24+
25+
response = connection.query(cmd) # send the command, and parse the response
26+
27+
print(response.value)
28+
print(response.unit)
29+
```
30+
31+
Documentation
32+
-------------
33+
34+
[Visit the GitHub Wiki!](http://github.com/brendanwhitfield/python-OBD/wiki)
35+
36+
Commands
37+
--------
38+
39+
Here are a handful of the supported commands (sensors). For a full list, see [the wiki](https://github.com/brendanwhitfield/python-OBD/wiki/Command-Tables)
40+
41+
*note: support for these commands will vary from car to car*
42+
43+
- Calculated Engine Load
44+
- Engine Coolant Temperature
45+
- Fuel Pressure
46+
- Intake Manifold Pressure
47+
- Engine RPM
48+
- Vehicle Speed
49+
- Timing Advance
50+
- Intake Air Temp
51+
- Air Flow Rate (MAF)
52+
- Throttle Position
53+
- Engine Run Time
54+
- Fuel Level Input
55+
- Number of warm-ups since codes cleared
56+
- Barometric Pressure
57+
- Ambient air temperature
58+
- Commanded throttle actuator
59+
- Time run with MIL on
60+
- Time since trouble codes cleared
61+
- Hybrid battery pack remaining life
62+
- Engine fuel rate
63+
64+
License
65+
-------
66+
67+
GNU GPL v2
68+
69+
This library is forked from:
70+
71+
- <https://github.com/peterh/pyobd>
72+
- <https://github.com/Pbartek/pyobd-pi>
73+
74+
Enjoy and drive safe!

README.rst

Lines changed: 0 additions & 72 deletions
This file was deleted.

obd/OBDCommand.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
2+
########################################################################
3+
# #
4+
# python-OBD: A python OBD-II serial module derived from pyobd #
5+
# #
6+
# Copyright 2004 Donour Sizemore (donour@uchicago.edu) #
7+
# Copyright 2009 Secons Ltd. (www.obdtester.com) #
8+
# Copyright 2009 Peter J. Creath #
9+
# Copyright 2015 Brendan Whitfield (bcw7044@rit.edu) #
10+
# #
11+
########################################################################
12+
# #
13+
# OBDCommand.py #
14+
# #
15+
# This file is part of python-OBD (a derivative of pyOBD) #
16+
# #
17+
# python-OBD is free software: you can redistribute it and/or modify #
18+
# it under the terms of the GNU General Public License as published by #
19+
# the Free Software Foundation, either version 2 of the License, or #
20+
# (at your option) any later version. #
21+
# #
22+
# python-OBD is distributed in the hope that it will be useful, #
23+
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
24+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
25+
# GNU General Public License for more details. #
26+
# #
27+
# You should have received a copy of the GNU General Public License #
28+
# along with python-OBD. If not, see <http://www.gnu.org/licenses/>. #
29+
# #
30+
########################################################################
31+
32+
import re
33+
from .utils import *
34+
from .debug import debug
35+
36+
37+
class OBDCommand():
38+
def __init__(self, name, desc, mode, pid, returnBytes, decoder, supported=False):
39+
self.name = name
40+
self.desc = desc
41+
self.mode = mode
42+
self.pid = pid
43+
self.bytes = returnBytes # number of bytes expected in return
44+
self.decode = decoder
45+
self.supported = supported
46+
47+
def clone(self):
48+
return OBDCommand(self.name,
49+
self.desc,
50+
self.mode,
51+
self.pid,
52+
self.bytes,
53+
self.decode)
54+
55+
def get_command(self):
56+
return self.mode + self.pid # the actual command transmitted to the port
57+
58+
def get_mode_int(self):
59+
return unhex(self.mode)
60+
61+
def get_pid_int(self):
62+
return unhex(self.pid)
63+
64+
def __call__(self, message):
65+
66+
# create the response object with the raw data recieved
67+
# and reference to original command
68+
r = Response(self, message)
69+
70+
# combine the bytes back into a hex string
71+
# TODO: rewrite decoders to handle raw byte arrays
72+
_data = ""
73+
for b in message.data_bytes[2:]:
74+
h = hex(b)[2:].upper()
75+
h = "0" + h if len(h) < 2 else h
76+
_data += h
77+
78+
# constrain number of bytes in response
79+
if (self.bytes > 0): # zero bytes means flexible response
80+
_data = constrainHex(_data, self.bytes)
81+
82+
# decoded value into the response object
83+
d = self.decode(_data)
84+
r.value = d[0]
85+
r.unit = d[1]
86+
87+
return r
88+
89+
def __str__(self):
90+
return "%s%s: %s" % (self.mode, self.pid, self.desc)
91+
92+
def __hash__(self):
93+
# needed for using commands as keys in a dict (see async.py)
94+
return hash((self.mode, self.pid))
95+
96+
def __eq__(self, other):
97+
if isinstance(other, OBDCommand):
98+
return (self.mode, self.pid) == (other.mode, other.pid)
99+
else:
100+
return False

obd/__init__.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
# #
66
# Copyright 2004 Donour Sizemore (donour@uchicago.edu) #
77
# Copyright 2009 Secons Ltd. (www.obdtester.com) #
8-
# Copyright 2014 Brendan Whitfield (bcw7044@rit.edu) #
8+
# Copyright 2009 Peter J. Creath #
9+
# Copyright 2015 Brendan Whitfield (bcw7044@rit.edu) #
910
# #
1011
########################################################################
1112
# #
@@ -28,10 +29,11 @@
2829
# #
2930
########################################################################
3031

31-
__version__ = '0.3b0.0'
32+
__version__ = '0.3.0'
3233

33-
from obd import OBD
34-
from commands import commands, OBDCommand
35-
from utils import scanSerial, Unit
36-
from debug import debug
37-
from async import Async
34+
from .obd import OBD
35+
from .OBDCommand import OBDCommand
36+
from .commands import commands
37+
from .utils import scanSerial, Unit
38+
from .debug import debug
39+
from .async import Async

obd/async.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
# #
66
# Copyright 2004 Donour Sizemore (donour@uchicago.edu) #
77
# Copyright 2009 Secons Ltd. (www.obdtester.com) #
8-
# Copyright 2014 Brendan Whitfield (bcw7044@rit.edu) #
8+
# Copyright 2009 Peter J. Creath #
9+
# Copyright 2015 Brendan Whitfield (bcw7044@rit.edu) #
910
# #
1011
########################################################################
1112
# #
@@ -28,20 +29,17 @@
2829
# #
2930
########################################################################
3031

31-
import obd
3232
import time
3333
import threading
34-
from utils import Response
35-
from commands import OBDCommand
36-
from debug import debug
34+
from .utils import Response
35+
from .debug import debug
36+
from . import OBD
3737

38-
39-
40-
class Async(obd.OBD):
38+
class Async(OBD):
4139
""" subclass representing an OBD-II connection """
4240

43-
def __init__(self, portstr=None):
44-
super(Async, self).__init__(portstr)
41+
def __init__(self, portstr=None, baudrate=38400):
42+
super(Async, self).__init__(portstr, baudrate)
4543
self.commands = {} # key = OBDCommand, value = Response
4644
self.callbacks = {} # key = OBDCommand, value = list of Functions
4745
self.thread = None
@@ -85,7 +83,7 @@ def watch(self, c, callback=None, force=False):
8583
return
8684

8785
# new command being watched, store the command
88-
if not self.commands.has_key(c):
86+
if c not in self.commands:
8987
debug("Watching command: %s" % str(c))
9088
self.commands[c] = Response() # give it an initial value
9189
self.callbacks[c] = [] # create an empty list
@@ -130,7 +128,7 @@ def unwatch_all(self):
130128

131129

132130
def query(self, c):
133-
if self.commands.has_key(c):
131+
if c in self.commands:
134132
return self.commands[c]
135133
else:
136134
return Response()

obd/codes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
# #
66
# Copyright 2004 Donour Sizemore (donour@uchicago.edu) #
77
# Copyright 2009 Secons Ltd. (www.obdtester.com) #
8-
# Copyright 2014 Brendan Whitfield (bcw7044@rit.edu) #
8+
# Copyright 2009 Peter J. Creath #
9+
# Copyright 2015 Brendan Whitfield (bcw7044@rit.edu) #
910
# #
1011
########################################################################
1112
# #

0 commit comments

Comments
 (0)