-
Notifications
You must be signed in to change notification settings - Fork 2
/
CANDecoder.py
executable file
·303 lines (260 loc) · 10.7 KB
/
CANDecoder.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"""
Title: Dodge CAN Decoder
Desc:
Version: 0.2
Release Notes:
Original Authors: http://tucrrc.utulsa.edu/DodgeCAN.html
Further Modifications by: Jean-Claude Thibault, Liam O'Brien
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
For a full copy of the license please vist this website: http://creativecommons.org/licenses/by-nc-sa/4.0/
"""
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
global pp
global basePath
pp=PdfPages('results/test.pdf')
basePath = 'results/'
fileName = 'CAN6/TestDrive.txt'
file=open(fileName,'r') #opens the data file
data=file.readlines() #Load the entire file into memory
file.close()
frame_id_filter = ['e','102','106','108','116','125','126','154','168','1f8','202','210','212','218','222','22c','2ac','2c8','2bc','238','312','3d8']
SHOW_ALL_IDs = True
#initialize variables
times=[]
IDs=[]
DLCs=[]
payloads=[]
hexpayloads=[]
IDLengths={}
for line in data[10:]: #Parse each line
#Split each line where the commas are. This may need to be tab characters if the data was tab delimited
entries=line.split(',')
#print entries #(only for debugging)
#convert timestamp (hours:minutes:seconds:millis:micros) into plain seconds
#timestamp=entries[0]
#timeParts=timestamp.split(':')
#time=float(timeParts[0])*3600 + float(timeParts[1])*60 + float(timeParts[2]) + float(timeParts[3])*0.001 + float(timeParts[4])*0.000001
time=float(entries[0])
#print time
times.append(time)
#build a list of IDs
identifier=entries[1]
id_dec = int(identifier,16)
identifier = hex(id_dec)[2:]
if (identifier in frame_id_filter) or SHOW_ALL_IDs:
IDs.append(identifier)
dataLengthCode=int(entries[2],16)
DLCs.append(dataLengthCode)
#Build the IDLength dictionary
IDLengths[identifier]=int(dataLengthCode)
#parse the message 3-10
#message=entries[6].split(' ')
data=[]
hexdata=[]
message=[]
#for d in message:
# data.append(int(d,16))
for i in range(0, dataLengthCode):
if entries[3+i]=='':
chunk=0
else:
chunk = int(entries[3+i])
data.append(chunk)
message.append(hex(chunk)[2:])
#print data
payloads.append(data)
hexpayloads.append(message)
else:
print 'Skipped 0x%s - ' %identifier
#Data Length Analysis for each ID
print 'CAN ID (Hex) -> Data Length'
IDList=IDLengths.keys()
IDList.sort()
for key in IDList:
print key+' -> %g' %IDLengths[key]
#print 'CAN ID (Dec) -> Data Length'
#for key in IDList:
# print `int(key,16)`+' -> %g' %IDLengths[key]
#Timing Analysis
print 'CAN ID (Hex) -> Counts Per time (Hz) or '
results=open(basePath + 'results.html','w')
results.write('''<table border="1" cellspacing="1" cellpadding="2">
<tr>
<th scope="col">CAN ID (Hex)</th>
<th scope="col">CAN ID (Dec)</th>
<th scope="col">Data Length</th>
<th scope="col">Frequency (Hz)</th>
<th scope="col">Period (sec.)</th>
<th scope="col">Notes</th>
</tr>
''')
totalTime=float(times[-1]-times[0])
for key in IDList:
occurances=IDs.count(key)
print key+' -> %g Hz or %g sec' %((occurances/totalTime),totalTime/float(occurances))
results.write(' <tr>\n <td>%s</td>\n <td>%g</td>\n <td>%g</td>\n <td>%g</td>\n <td>%g</td>\n <td> </td>\n </tr>\n' %(key,int(key,16),IDLengths[key],(occurances/totalTime),totalTime/float(occurances)))
results.write('</table>\n')
results.close()
#calculate number of Bytes
totalBytes=0
for key in IDList:
totalBytes+=int(IDLengths[key])
print 'There are %g unique IDs in the log file' %len(IDLengths)
print 'Total number of bytes to examine in the log: %g' %totalBytes
def plotDataCharvsTime(ID,times=times,IDs=IDs,data=payloads,IDLengths=IDLengths):
X=[]
Y_lsb=[]
Y_msb=[]
startTime=times[0]
endTime=times[-1]
for location in range(IDLengths[ID]):
Y_lsb.append([])
Y_msb.append([])
for i,t,d in zip(IDs,times,data):
if i==ID:
X.append(t)
for location in range(len(Y_lsb)):
value = "{0:08b}".format(d[location])[2:]
lsb = int(value[4:],2)
msb = int(value[0:3],2)
Y_lsb[location].append(lsb)
Y_msb[location].append(msb)
for location in range(IDLengths[ID]):
outputName='Time History of CAN ID %s for byte %g 4-LSB' %(ID,location)
plt.plot(X,Y_lsb[location],'-',rasterized=True)
plt.xlim( startTime, endTime )
plt.title(outputName)
plt.ylabel('Value')
plt.xlabel('Time [sec]')
#plt.savefig(basePath + outputName+'.png')
#plt.savefig(basePath + outputName+'.pdf')
pp.savefig()
plt.close()
print 'File %s was written.' %outputName
outputName='Time History of CAN ID %s for byte %g 4-MSB' %(ID,location)
plt.plot(X,Y_msb[location],'-',rasterized=True)
plt.xlim( startTime, endTime )
plt.title(outputName)
plt.ylabel('Value')
plt.xlabel('Time [sec]')
#plt.savefig(basePath + outputName+'.png')
#plt.savefig(basePath + outputName+'.pdf')
pp.savefig()
plt.close()
print 'File %s was written.' %outputName
def plotDatavsTime(ID,times=times,IDs=IDs,data=payloads,IDLengths=IDLengths):
X=[]
Y=[]
startTime=times[0]
endTime=times[-1]
for location in range(IDLengths[ID]):
Y.append([])
for i,t,d in zip(IDs,times,data):
if i==ID:
X.append(t)
for location in range(len(Y)):
Y[location].append(d[location])
for location in range(IDLengths[ID]):
outputName='Time History of CAN ID %s for byte %g' %(ID,location)
plt.plot(X,Y[location],'-',rasterized=True)
#plt.xlim( startTime, endTime )
plt.title(outputName)
plt.ylabel('Value')
plt.xlabel('Time [sec]')
#plt.savefig(basePath + outputName+'.png')
#plt.savefig(basePath + outputName+'.pdf')
pp.savefig()
plt.close()
print 'File %s was written.' %outputName
def plotEvenDatavsTime(ID,times=times,IDs=IDs,data=payloads,IDLengths=IDLengths):
bits=16
X=[]
Y=[]
startTime=times[0]
endTime=times[-1]
for location in range(0,IDLengths[ID]-1,2):
#print location
Y.append([])
for i,t,d in zip(IDs,times,data):
if i==ID:
X.append(t)
#print d
for location in range(len(Y)):
try:
#binaryString='%s' %bin(int(d[2*location]+d[2*location+1],16))[2:]
#THISONEbinaryString='%s' %bin( int( (d[2*location+1]*256) + d[2*location] ,16) )[2:]
#Y[location].append( int(binaryString[-(bits-1):],2) )
#THISONEY[location].append( float(int(binaryString[0:bits],2)/100) )
value = float( (d[2*location+1]*256.0+d[2*location])/1.0 )
Y[location].append(value)
#if location==1 and value>390 and value<395:
# print '%3.2f' %value
except IndexError:
print 'Something is screwy in plotEvenDatavsTime! location=%s' %location
#print location
#print d
Y.pop(location)
pass
for location in range(len(Y)):
outputName='Time History of CAN ID %s for (b%gx256 + b%g) using %g bits' %(ID,2*location+1,2*location,bits)
#outputName='Time History of CAN ID %s for bytes %g and %g' %(ID,2*location,2*location+1)
plt.figure(1)
#plt.subplot(111)
plt.plot(X,Y[location],'-',rasterized=True)
#plt.xlim( startTime, endTime )
plt.title(outputName)
plt.ylabel('Value')
plt.xlabel('Time [sec]')
#plt.savefig(basePath + outputName+'.png')
#plt.savefig(basePath + outputName+'.pdf')
pp.savefig()
plt.close()
print 'File %s was written.' %outputName
def plotOddDatavsTime(ID,times=times,IDs=IDs,data=payloads,IDLengths=IDLengths):
bits=16
X=[]
Y=[]
startTime=times[0]
endTime=times[-1]
for location in range(1,IDLengths[ID]-1,2):
#print location
Y.append([])
for i,t,d in zip(IDs,times,data):
if i==ID:
X.append(t)
for location in range(len(Y)):
try:
#ORIGINAL binaryString='%s' %bin(int(d[2*location]+d[2*location+1],16))[2:]
#ORIGINALY[location].append(int(binaryString[-(bits-1):],2))
value = float( (d[2*location+2]*256.0+d[2*location+1])/100.0 )
Y[location].append(value)
except IndexError:
Y.pop(location)
print 'Something is wrong.'
pass
for location in range(len(Y)):
outputName='Time History of CAN ID %s for bytes %g and %g using %g bits' %(ID,2*location+2,2*location+1,bits)
outputName='Time History of CAN ID %s for bytes %g and %g' %(ID,2*location,2*location+1)
plt.plot(X,Y[location],'-',rasterized=True)
plt.xlim( startTime, endTime )
plt.title(outputName)
plt.ylabel('Value')
plt.xlabel('Time [sec]')
#plt.savefig(basePath + outputName+'.png')
#plt.savefig(basePath + outputName+'.pdf')
pp.savefig()
plt.close()
print 'File %s was written.' %outputName
print IDList
for key in IDList:
#for key in [ '222','22c','2ac','412','6f2','71c','79c']:
#for key in [ '222']:
if key in IDList:
plotDatavsTime(key)
#plotDataCharvsTime(key)
plotEvenDatavsTime(key) # This one
#plotOddDatavsTime(key)
print 'Done Plotting Data for '+key
pp.close()
print 'All done!'