Repository navigation
Expand file tree
/
Copy pathobd.py
More file actions
196 lines (149 loc) · 7.07 KB
/
Copy pathobd.py
File metadata and controls
196 lines (149 loc) · 7.07 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
########################################################################
# #
# python-OBD: A python OBD-II serial module derived from pyobd #
# #
# Copyright 2004 Donour Sizemore (donour@uchicago.edu) #
# Copyright 2009 Secons Ltd. (www.obdtester.com) #
# Copyright 2009 Peter J. Creath #
# Copyright 2015 Brendan Whitfield (bcw7044@rit.edu) #
# #
########################################################################
# #
# obd.py #
# #
# This file is part of python-OBD (a derivative of pyOBD) #
# #
# python-OBD is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 2 of the License, or #
# (at your option) any later version. #
# #
# python-OBD is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with python-OBD. If not, see <http://www.gnu.org/licenses/>. #
# #
########################################################################
import time
from .__version__ import __version__
from .elm327 import ELM327
from .commands import commands
from .utils import scanSerial, Response
from .debug import debug
class OBD(object):
"""
Class representing an OBD-II connection with it's assorted commands/sensors
"""
def __init__(self, portstr=None, baudrate=38400):
self.port = None
self.supported_commands = []
debug("========================== python-OBD (v%s) ==========================" % __version__)
self.__connect(portstr, baudrate) # initialize by connecting and loading sensors
debug("=========================================================================")
def __connect(self, portstr, baudrate):
"""
Attempts to instantiate an ELM327 connection object.
Upon success, __load_commands() is called
"""
if portstr is None:
debug("Using scanSerial to select port")
portnames = scanSerial()
debug("Available ports: " + str(portnames))
for port in portnames:
debug("Attempting to use port: " + str(port))
self.port = ELM327(port, baudrate)
if self.port.is_connected():
# success! stop searching for serial
break
else:
debug("Explicit port defined")
self.port = ELM327(portstr, baudrate)
# if a connection was made, query for commands
if self.is_connected():
self.__load_commands()
else:
debug("Failed to connect")
def close(self):
""" Closes the connection """
if self.is_connected():
debug("Closing connection")
self.port.close()
self.port = None
self.supported_commands = []
def is_connected(self):
""" Returns a boolean for whether a successful serial connection was made """
return (self.port is not None) and self.port.is_connected()
def get_port_name(self):
""" Returns the name of the currently connected port """
if self.is_connected():
return self.port.get_port_name()
else:
return "Not connected to any port"
def __load_commands(self):
"""
Queries for available PIDs, sets their support status,
and compiles a list of command objects.
"""
debug("querying for supported PIDs (commands)...")
self.supported_commands = []
pid_getters = commands.pid_getters()
for get in pid_getters:
# PID listing commands should sequentialy become supported
# Mode 1 PID 0 is assumed to always be supported
if not self.supports(get):
continue
response = self.__send(get) # ask nicely
if response.is_null():
continue
supported = response.value # string of binary 01010101010101
# loop through PIDs binary
for i in range(len(supported)):
if supported[i] == "1":
mode = get.get_mode_int()
pid = get.get_pid_int() + i + 1
if commands.has_pid(mode, pid):
c = commands[mode][pid]
c.supported = True
# don't add PID getters to the command list
if c not in pid_getters:
self.supported_commands.append(c)
debug("finished querying with %d commands supported" % len(self.supported_commands))
def print_commands(self):
"""
Utility function meant for working in interactive mode.
Prints all commands supported by the car.
"""
for c in self.supported_commands:
print(str(c))
def supports(self, c):
""" Returns a boolean for whether the car supports the given command """
return commands.has_command(c) and c.supported
def __send(self, c):
"""
Back-end implementation of query()
sends the given command, retrieves and parses the response
"""
if not self.is_connected():
debug("Query failed, no connection available", True)
return Response() # return empty response
debug("Sending command: %s" % str(c))
# send command and retrieve message
m = self.port.send_and_parse(c.get_command())
if m is None:
return Response() # return empty response
else:
return c(m) # compute a response object
def query(self, c, force=False):
"""
primary API function. Sends commands to the car, and
protects against sending unsupported commands.
"""
# check that the command is supported
if self.supports(c) or force:
return self.__send(c)
else:
debug("'%s' is not supported" % str(c), True)
return Response() # return empty response