#!Python
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
global pp
pp=PdfPages('Graphic CAN Output-12.pdf')
file=open('20320012.xls','r') #opens the data file produced be the Vector
data=file.readlines() #Load the entire file into memory
file.close()
#initialize variables
times=[]
IDs=[]
DLCs=[]
payloads=[]
hexpayloads=[]
IDLengths={}
PGNs={}
PFs={}
DAs={}
SAs={}
for line in data[7:]: #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(',')
if entries[3][:7]=='CAN Bus':
print entries
continue
#print entries #(only for debugging)
#convert timestamp (hours:minutes:seconds:millis:micros) into plain seconds
time=entries[0]
#build a list of IDs
#Build the IDLength dictionary
identifier=entries[7]
dataLengthCodetemp=entries[5].split(':')
dataLengthCode=dataLengthCodetemp[-1]
IDLengths[identifier]=int(dataLengthCode)
DLCs.append(dataLengthCode)
IDs.append(identifier)
times.append(float(time))
#parse the message
message=entries[9:17]
#print message
data=[]
for d in message:
if d!='\n':
data.append(int(d,16))
#print data
payloads.append(data)
hexpayloads.append(message)
SA=identifier[-2:]
PF=identifier[-6:-4]
DA=identifier[-4:-2]
PGN=identifier[-6:-2]
try:
SAs[identifier]=int(SA,16)
PFs[identifier]=int(PF,16)
DAs[identifier]=int(DA,16)
PGNs[identifier]=int(PGN,16)
except ValueError:
SAs[identifier]=9999
PFs[identifier]=9999
DAs[identifier]=9999
PGNs[identifier]=9999
pass
# if identifier=='18fef100':
# print message
# except IndexError:
# pass
# except ValueError:
# pass
#print hexpayloads
#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]
#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
#Timing Analysis
print 'CAN ID (Hex) -> Counts Per time (Hz) or '
results=open('results.html','w')
results.write('''
CAN ID (Hex) |
PGN (Dec) |
PF (Dec) |
DA (Dec) |
SA (Dec) |
Data Length |
Frequency (Hz) |
Period (sec.) |
Notes |
''')
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(' \n %s | \n %g | \n %g | \n %g | \n %g | \n %g | \n %g | \n %g | \n | \n
\n' %(key,PGNs[key],PFs[key],DAs[key],SAs[key],IDLengths[key],(occurances/totalTime),totalTime/float(occurances)))
results.write('
\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 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],'.')
plt.xlim( startTime, endTime )
plt.title(outputName)
plt.ylabel('Value')
plt.xlabel('Time [sec]')
f=plt.savefig(outputName+'.png')
#plt.savefig(outputName+'.pdf')
pp.savefig()
plt.close()
print 'File %s was written.' %outputName
def plotEvenDatavsTime(ID,times=times,IDs=IDs,data=hexpayloads,IDLengths=IDLengths):
X=[]
Y=[]
startTime=times[0]
endTime=times[-1]
for location in range(0,IDLengths[ID],2):
#print location
Y.append([])
for i,t,d in zip(IDs,times,data):
if i==ID:
X.append(t-startTime)
#print i
#print d
for location in range(len(Y)):
try:
Y[location].append(int(d[2*location+0]+d[2*location+1],16))
except IndexError:
print 'Something is screwy!'
print location
print d
Y.pop(location)
pass
for location in range(len(Y)):
outputName='Time History of CAN ID %s for bytes %g and %g' %(ID,2*location+0,2*location+1)
plt.plot(X,Y[location],'.')
plt.xlim( 0,endTime-startTime )
plt.title(outputName)
plt.ylabel('Value')
plt.xlabel('Time [sec]')
f=plt.savefig(outputName+'.png')
datafile=open(outputName+'.csv','w')
datafile.write(outputName+'\n')
for x,y in zip(X,Y[location]):
datafile.write('%g,%g\n' %(x,y))
datafile.close()
#plt.savefig(outputName+'.pdf')
pp.savefig()
plt.close()
print 'File %s was written.' %outputName
def plotOddDatavsTime(ID,times=times,IDs=IDs,data=hexpayloads,IDLengths=IDLengths):
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-startTime)
for location in range(len(Y)):
try:
Y[location].append(int(d[2*location+1]+d[2*location+2],16))
except IndexError:
Y.pop(location)
pass
for location in range(len(Y)):
outputName='Time History of CAN ID %s for bytes %g and %g' %(ID,2*location+1,2*location+2)
plt.plot(X,Y[location],'.')
plt.xlim( 0,endTime-startTime )
plt.title(outputName)
plt.ylabel('Value')
plt.xlabel('Time [sec]')
f=plt.savefig(outputName+'.png')
datafile=open(outputName+'.csv','w')
datafile.write(outputName+'\n')
for x,y in zip(X,Y[location]):
datafile.write('%0.6f,%g\n' %(x,y))
datafile.close()
#plt.savefig(outputName+'.pdf')
pp.savefig()
plt.close()
print 'File %s was written.' %outputName
#for key in IDList:
#for key in ['18fef100', '18fef117', '18febf0b','302']:
#for key in ['302']:
#plotDatavsTime(key)
#plotEvenDatavsTime(key)
#plotOddDatavsTime(key)
#~ print 'Done Plotting Data for '+key
pp.close()