3030########################################################################
3131
3232import time
33+
34+ from obd import __version__
3335from .elm327 import ELM327
3436from .commands import commands
3537from .utils import scanSerial , Response
3840
3941
4042class 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