forked from brendan-w/python-OBD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
214 lines (173 loc) · 6.55 KB
/
utils.py
File metadata and controls
214 lines (173 loc) · 6.55 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
########################################################################
# #
# 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) #
# #
########################################################################
# #
# utils.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 serial
import errno
import string
import time
import glob
import sys
from .debug import debug
class Unit:
NONE = None
RATIO = "Ratio"
COUNT = "Count"
PERCENT = "%"
RPM = "RPM"
VOLT = "Volt"
F = "F"
C = "C"
SEC = "Second"
MIN = "Minute"
PA = "Pa"
KPA = "kPa"
PSI = "psi"
KPH = "kph"
MPH = "mph"
DEGREES = "Degrees"
GPS = "Grams per Second"
MA = "mA"
KM = "km"
LPH = "Liters per Hour"
class Response():
def __init__(self, command=None, message=None):
self.command = command
self.message = message
self.value = None
self.unit = Unit.NONE
self.time = time.time()
def is_null(self):
return (self.message == None) or (self.value == None)
def __str__(self):
if self.unit != Unit.NONE:
return "%s %s" % (str(self.value), str(self.unit))
else:
return str(self.value)
class Status():
def __init__(self):
self.MIL = False
self.DTC_count = 0
self.ignition_type = ""
self.tests = []
class Test():
def __init__(self, name, available, incomplete):
self.name = name
self.available = available
self.incomplete = incomplete
def __str__(self):
a = "Available" if self.available else "Unavailable"
c = "Incomplete" if self.incomplete else "Complete"
return "Test %s: %s, %s" % (self.name, a, c)
def ascii_to_bytes(a):
b = []
for i in range(0, len(a), 2):
b.append(int(a[i:i+2], 16))
return b
def numBitsSet(n):
# TODO: there must be a better way to do this...
total = 0
ref = 1
for b in range(8):
total += int(bool(n & ref))
ref = ref << 1
return total
def unhex(_hex):
_hex = "0" if _hex == "" else _hex
return int(_hex, 16)
def unbin(_bin):
return int(_bin, 2)
def bitstring(_hex, bits=None):
b = bin(unhex(_hex))[2:]
if bits is not None:
b = ('0' * (bits - len(b))) + b
return b
def bitToBool(_bit):
return (_bit == '1')
def twos_comp(val, num_bits):
"""compute the 2's compliment of int value val"""
if( (val&(1<<(num_bits-1))) != 0 ):
val = val - (1<<num_bits)
return val
def isHex(_hex):
return all(c in string.hexdigits for c in _hex)
def constrainHex(_hex, b):
"""pads or chops hex to the requested number of bytes"""
diff = (b * 2) - len(_hex) # length discrepency in number of hex digits
if diff > 0:
debug("Receieved less data than expected, trying to parse anyways...")
_hex += ('0' * diff) # pad the right side with zeros
elif diff < 0:
debug("Receieved more data than expected, trying to parse anyways...")
_hex = _hex[:diff] # chop off the right side to fit
return _hex
# checks that a list of integers are consequtive
def contiguous(l, start, end):
if not l:
return False
if l[0] != start:
return False
if l[-1] != end:
return False
# for consequtiveness, look at the integers in pairs
pairs = zip(l, l[1:])
if not all([p[0]+1 == p[1] for p in pairs]):
return False
return True
def try_port(portStr):
"""returns boolean for port availability"""
try:
s = serial.Serial(portStr)
s.close() # explicit close 'cause of delayed GC in java
return True
except serial.SerialException:
pass
except OSError as e:
if e.errno != errno.ENOENT: # permit "no such file or directory" errors
raise e
return False
def scanSerial():
"""scan for available ports. return a list of serial names"""
available = []
possible_ports = []
if sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
possible_ports += glob.glob("/dev/rfcomm[0-9]*")
possible_ports += glob.glob("/dev/ttyUSB[0-9]*")
elif sys.platform.startswith('win'):
possible_ports += ["\\.\COM%d" % i for i in range(256)]
elif sys.platform.startswith('darwin'):
exclude = [
'/dev/tty.Bluetooth-Incoming-Port',
'/dev/tty.Bluetooth-Modem'
]
possible_ports += [port for port in glob.glob('/dev/tty.*') if port not in exclude]
# possible_ports += glob.glob('/dev/pts/[0-9]*') # for obdsim
for port in possible_ports:
if try_port(port):
available.append(port)
return available