forked from AustinMurphy/OBD2-Scantool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo-scan.py
executable file
·235 lines (172 loc) · 5.88 KB
/
demo-scan.py
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env python
############################################################################
#
# demo-scan.py
#
# Copyright 2011 Austin Murphy ([email protected])
#
# This file is part of OBD2 Scantool.
#
# OBD2 Scantool 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.
#
# OBD2 Scantool 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 OBD2 Scantool; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
############################################################################
#
# This is a demo script to show basic usage of obd2.py
#
#
# With vehicle ON (Engine can be either OFF or RUNNING)
# Attach OBD2 interface to computer and vehicle
# Run this demo script
#
import sys, os, time
# req'd by obd2_reader
import serial
# req'd by obd2
import obd2_reader
# main OBD2 object
import obd2
# debug
import pprint
def main():
print "=================================================================="
print ""
print "OBD2 vehicle demo scan"
print "----------------------"
print ""
print "Scan date: ", time.ctime()
print ""
print "=================================================================="
print ""
print "Serial port attributes"
print "----------------------"
#ser_device = '/dev/pts/7'
ser_device = '/dev/ttyUSB0'
ser_settings = {
'baudrate': 38400,
'bytesize': serial.EIGHTBITS,
'parity' : serial.PARITY_NONE,
'stopbits': serial.STOPBITS_ONE,
'xonxoff' : False,
'rtscts' : False,
'dsrdtr' : False,
'timeout' : None,
'interCharTimeout': None,
'writeTimeout' : None
}
# create serial port (closed)
port = serial.Serial(None)
port.port = ser_device
port.applySettingsDict(ser_settings)
print "Device".rjust(16), ": ", port.name
#pprint.pprint( port.getSettingsDict() )
#print ""
settings = port.getSettingsDict()
for k in sorted(settings.keys()):
print k.rjust(16), ": ", settings[k]
print ""
print "=================================================================="
print ""
print "OBD2 reader device"
print "------------------"
TYPE = 'SERIAL'
READER = 'ELM327'
# create reader object (disconnected)
reader = obd2_reader.OBD2reader( TYPE, READER )
reader.Port = port
reader.Headers = 1
# we want a record of what we pulled
reader.record_trace()
reader.connect() # this also opens the serial port
print "Device".rjust(16), ": ", str(reader.Device)
print "State".rjust(16), ": ", str(reader.State)
print "Style".rjust(16), ": ", str(reader.Style)
print "Headers".rjust(16), ": ", str(reader.Headers)
#pprint.pprint( reader.attr )
for k in sorted(reader.attr.keys()):
print k.rjust(16), ": ", reader.attr[k]
# TODO
# notice that ELM327 is not connected and throw exception
print ""
print "=================================================================="
print ""
print "OBD2 initialization"
print "-------------------"
vehicle = obd2.OBD2( reader )
print ""
print "Loading default PID definitions from CSV file..."
obd2.load_pids_from_csv( 'obd2_std_PIDs.csv' )
print ""
print "Loading default DTC definitions from CSV file..."
obd2.load_dtcs_from_csv( 'obd2_std_DTCs.csv' )
# debug
#reader.RTRV_record()
print ""
print "Scanning vehicle for supported features..."
vehicle.scan_features()
#print ""
#print "Supported PIDs - DEBUG"
#print "----------------------"
#pprint.pprint(vehicle.suppPIDs)
print ""
print "=================================================================="
print " "
print "General vehicle info"
print "--------------------"
vehicle.scan_basic_info()
vehicle.show_basic_info()
print " "
# I should fix the inconsistent use of "scan"
print ""
print "=================================================================="
print " "
print "Scan for Diagnostic and Emmissions Monitor info"
print "-----------------------------------------------"
print " "
#print "OBD2 Status BEFORE scan:"
#pprint.pprint(vehicle.obd2status)
#print " "
#print "Scanning for OBD2 status... "
vehicle.scan_obd2_status()
print " "
#print "OBD2 Status AFTER scan:"
pprint.pprint(vehicle.obd2status)
print " "
print ""
print "=================================================================="
print " "
print "Scan for Current Sensor Readings"
print "--------------------------------"
print " "
#vehicle.scan_curr_sensors()
sensors = vehicle.curr_sensors()
for pid in sensors:
vehicle.scan_pid( pid )
vehicle.show_last_reading( pid )
print " "
for pid in sensors:
vehicle.scan_pid( pid )
vehicle.show_last_reading( pid )
print " "
print " RAW Data structure:"
pprint.pprint( vehicle.sensor_readings )
print " "
print " "
print " DTCs:"
vehicle.scan_pid( '03' )
vehicle.show_last_reading( '03' )
print ""
print "=================================================================="
print "=================================================================="
if __name__ == "__main__":
sys.exit(main())