Skip to content

Commit 8251755

Browse files
author
Brendan Whitfield
committed
added better docstrings, made some functions private
1 parent 856b5de commit 8251755

3 files changed

Lines changed: 44 additions & 17 deletions

File tree

obd/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11

2+
"""
3+
A serial module for accessing data from a vehicles OBD-II port
4+
5+
For more documentation, visit:
6+
https://github.com/brendanwhitfield/python-OBD/wiki
7+
"""
8+
29
########################################################################
310
# #
411
# python-OBD: A python OBD-II serial module derived from pyobd #

obd/async.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ def run(self):
143143
if len(self.commands) > 0:
144144
# loop over the requested commands, send, and collect the response
145145
for c in self.commands:
146-
r = self.send(c)
146+
147+
# force, since commands are checked for support in watch()
148+
r = super(Async, self).query(c, force=True)
147149

148150
# store the response
149151
self.commands[c] = r

obd/obd.py

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
########################################################################
3131

3232
import time
33+
34+
from obd import __version__
3335
from .elm327 import ELM327
3436
from .commands import commands
3537
from .utils import scanSerial, Response
@@ -38,19 +40,24 @@
3840

3941

4042
class OBD(object):
41-
""" class representing an OBD-II connection with it's assorted sensors """
43+
"""
44+
Class representing an OBD-II connection with it's assorted commands/sensors
45+
"""
4246

4347
def __init__(self, portstr=None, baudrate=38400):
4448
self.port = None
4549
self.supported_commands = []
4650

47-
debug("========================== Starting python-OBD ==========================")
48-
self.connect(portstr, baudrate) # initialize by connecting and loading sensors
51+
debug("========================== python-OBD (v%s) ==========================" % __version__)
52+
self.__connect(portstr, baudrate) # initialize by connecting and loading sensors
4953
debug("=========================================================================")
5054

5155

52-
def connect(self, portstr=None, baudrate=38400):
53-
""" attempts to instantiate an ELM327 object. Loads commands on success"""
56+
def __connect(self, portstr=None, baudrate=38400):
57+
"""
58+
Attempts to instantiate an ELM327 connection object.
59+
Upon success, __load_commands() is called
60+
"""
5461

5562
if portstr is None:
5663
debug("Using scanSerial to select port")
@@ -70,34 +77,37 @@ def connect(self, portstr=None, baudrate=38400):
7077

7178
# if a connection was made, query for commands
7279
if self.is_connected():
73-
self.load_commands()
80+
self.__load_commands()
7481
else:
7582
debug("Failed to connect")
7683

7784

7885
def close(self):
86+
""" Closes the connection """
7987
if self.is_connected():
8088
debug("Closing connection")
8189
self.port.close()
8290
self.port = None
91+
self.supported_commands = []
8392

8493

8594
def is_connected(self):
95+
""" Returns a boolean for whether a successful serial connection was made """
8696
return (self.port is not None) and self.port.is_connected()
8797

8898

8999
def get_port_name(self):
100+
""" Returns the name of the currently connected port """
90101
if self.is_connected():
91102
return self.port.get_port_name()
92103
else:
93104
return "Not connected to any port"
94105

95106

96-
def load_commands(self):
107+
def __load_commands(self):
97108
"""
98-
queries for available PIDs,
99-
sets their support status,
100-
and compiles a list of command objects
109+
Queries for available PIDs, sets their support status,
110+
and compiles a list of command objects.
101111
"""
102112

103113
debug("querying for supported PIDs (commands)...")
@@ -112,7 +122,7 @@ def load_commands(self):
112122
if not self.supports(get):
113123
continue
114124

115-
response = self.send(get) # ask nicely
125+
response = self.__send(get) # ask nicely
116126

117127
if response.is_null():
118128
continue
@@ -138,16 +148,24 @@ def load_commands(self):
138148

139149

140150
def print_commands(self):
151+
"""
152+
Utility function meant for working in interactive mode.
153+
Prints all commands supported by the car.
154+
"""
141155
for c in self.supported_commands:
142156
print(str(c))
143157

144158

145159
def supports(self, c):
160+
""" Returns a boolean for whether the car supports the given command """
146161
return commands.has_command(c) and c.supported
147162

148163

149-
def send(self, c):
150-
""" send the given command, retrieve and parse response """
164+
def __send(self, c):
165+
"""
166+
Back-end implementation of query()
167+
sends the given command, retrieves and parses the response
168+
"""
151169

152170
if not self.is_connected():
153171
debug("Query failed, no connection available", True)
@@ -162,17 +180,17 @@ def send(self, c):
162180
return Response() # return empty response
163181
else:
164182
return c(m) # compute a response object
165-
183+
166184

167185
def query(self, c, force=False):
168186
"""
169-
facade 'send' command function
187+
primary API function. Sends commands to the car, and
170188
protects against sending unsupported commands.
171189
"""
172190

173191
# check that the command is supported
174192
if self.supports(c) or force:
175-
return self.send(c)
193+
return self.__send(c)
176194
else:
177195
debug("'%s' is not supported" % str(c), True)
178196
return Response() # return empty response

0 commit comments

Comments
 (0)