forked from AustinMurphy/OBD2-Scantool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode-trace.py
executable file
·214 lines (164 loc) · 5.34 KB
/
decode-trace.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
#!/usr/bin/env python
############################################################################
#
# decode-trace.py
#
# Copyright 2011-2012 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 development script to test the decoding of PIDs from a trace
#
# by "trace" I mean a log of the raw hex & ascii passed over the serial connection.
# a cut an paste of a serial session should work
import time
import sys, os
# main OBD2 object
import obd2
import obd2_reader
import pprint
# headers on(1)/off(0)
headers = 0
#headers = 1
#style = can/old
#style = 'old'
style = 'can'
# 0=off,
# 1+ - show raw & decoded records
# 2+ - show record as triaged
debug = 2
def main():
""" Decode a tracefile """
if len(sys.argv) < 2:
sys.exit('Usage: %s tracefile' % sys.argv[0])
if not os.path.exists(sys.argv[1]):
sys.exit('ERROR: Tracefile %s was not found!' % sys.argv[1])
tracefile = sys.argv[1]
print "=================================================================="
print ""
print "OBD2 trace decode"
print "----------------------"
print ""
print "Date: ", time.ctime()
print ""
print "Trace: ", tracefile
print ""
print "Loading default PID definitions from CSV file..."
obd2.load_pids_from_csv( 'obd2_std_PIDs.csv' )
# Global: obd2.PIDs[]
print ""
print "Loading default DTC definitions from CSV file..."
obd2.load_dtcs_from_csv( 'obd2_std_DTCs.csv' )
# Global: obd2.DTCs[]
# create OBD2_READER object
TYPE = 'FILE'
READER = 'ELM327'
# create reader object (disconnected)
reader = obd2_reader.OBD2reader( TYPE, READER )
reader.debug = debug
# open tracefile
reader.open_trace(tracefile)
# manually set since we can't query the tracefile
#reader.Style = 'can'
#reader.Headers = 1
#reader.Style = 'old'
#reader.Headers = 0
reader.Style = style
reader.Headers = headers
vehicle = obd2.OBD2( reader )
print ""
print "Reading from tracefile..."
print ""
while 1:
record = reader.RTRV_record()
if debug > 0:
print "-----------------------------------"
print "Raw Record: ",
pprint.pprint(record)
obd2_record = reader.triage_record( record )
if obd2_record == []:
# then this must have been something other than an obd2_record, nothing to do
pass
else:
if debug > 1:
print "--"
print "Triaged Record: ",
pprint.pprint(obd2_record)
print "--"
dec_rec = obd2.decode_obd2_record( obd2_record )
if debug > 0 :
print "--"
print "Decoded Record: ",
pprint.pprint( dec_rec )
print " "
print "=================================="
print " "
# do something with the decoded obd2_record
vehicle.store_info( dec_rec )
if reader.eof == 1:
break
print "-----------------------------------"
print ""
print ""
print "Reader Attributes:"
print "------------------"
pprint.pprint( reader.attr )
print ""
#
print "Reader State:"
print "-------------"
print " Type:", reader.Type
print " Device:", reader.Device
print " debug:", reader.debug
print " State:", reader.State
#print "recwaiting", reader.recwaiting
print " Style:", reader.Style
print " Headers:", reader.Headers
#reader.attr
#print reader.attr_cmds
print " Trace?:", reader.RecordTrace
print "Trace file:", reader.tf_out
#
print ""
print "Vehicle Basic Info:"
print "-------------------"
pprint.pprint( vehicle.info )
print ""
vehicle.show_basic_info()
print ""
print ""
print "Vehicle OBD2 Status:"
print "--------------------"
pprint.pprint( vehicle.obd2status )
print ""
print "-----------------------------------"
print ""
print "Vehicle supported PIDs:"
print "-----------------------"
pprint.pprint( vehicle.suppPIDs )
print ""
print "-----------------------------------"
print ""
print "Vehicle Sensor readings:"
print "--------------------"
pprint.pprint( vehicle.sensor_readings )
print ""
print "-----------------------------------"
print "END"
if __name__ == "__main__":
sys.exit(main())