import fileinput
import matplotlib.pyplot as plt
import numpy as pl
+from datetime import datetime,time
+from math import modf
+######################################################
def readbyte(line):
# read next byte in line
byte=256
line=line[2:]
return byte,line
+######################################################
def process_head(line):
ds,line=readbyte(line)
if ds==0x27:
print("Type:",typ,"Length:",le,"Destination:",da,"Source:",sa)
return typ,ds,le,da,sa,line
+######################################################
def process_apdu(line):
cl,line=readbyte(line)
cl=cl&0xF
print("Class:",cl,"Operation:",os,"Length:",le)
return cl,os,le,line
+######################################################
def process_class5(line,numid):
id=[0]*numid
val=[0]*numid
print("Id:",id[i],"Value:",val[i])
return id,val,line
+######################################################
def process_crc(line):
crc1,line=readbyte(line)
crc2,line=readbyte(line)
print("CRC:",crc)
return crc
+######################################################
def process_islineok(line):
- ok=True
- if "ERR" in line.upper() or len(line) < 12:
- ok=False
+ ok=False
+ if not "ERR" in line.upper() and len(line)>10:
+ ok=True
return ok
+######################################################
def process_timestamp(line):
- #FIXME: does not work in general
info=line.split(":")
try:
- timestamp=int(info[1].strip())
- print(timestamp)
- return timestamp
+ timestamp=int(info[1].strip())/1e3
except:
- return 0
+ timestamp=0
+ finally:
+ ts=datetime.fromtimestamp(timestamp)
+ if timestamp<1e9: # 2001-09-09
+ try:
+ h=int(info[0].split(' ')[1])
+ m=int(info[1])
+ u,s=modf(float(info[2].split(' ')[0]))
+ ts=time(h,m,int(s),int(u*1e6))
+ except:
+ pass
+ print("timestamp",ts)
+ return ts
+######################################################
def process(line):
- global timezero
- global t
- global speed
+ global td
global master
+ global speed
- # Some extra info from logging tool
- ok=process_islineok(line)
- if not ok: return
+ # Check if timestamp was added to line
+ ts=process_timestamp(line)
- timestamp=process_timestamp(line)
- if timezero==0:
- timezero=timestamp
+ # Sanity check line
+ ok=process_islineok(line)
+ if not ok:
+ return
# Process received data
typ,ds,le,da,sa,line=process_head(line)
numbytes=le-2
- while numbytes>0:
+ while numbytes>0 and len(line)>=2*numbytes:
cl,os,ale,line=process_apdu(line)
numbytes=numbytes-2
if cl==5:
- id,val,line=process_class5(line,int(ale/2))
+ id5,val5,line=process_class5(line,int(ale/2))
numbytes=numbytes-ale
- # Debugging info....
+ # Debugging ..........
+ td=td+[ts.timestamp()]
master=master+[1+(sa-31)/10]
- t=t+[(timestamp-timezero)/1000]
for i in range(4):
- speed[i]=speed[i]+[val[i]/254]
+ speed[i]=speed[i]+[val5[i]/254]
else:
line=line[ale*3:]
numbytes=numbytes-ale
crc=process_crc(line)
return
-timezero=0
-t=[]
+
+
+######################################################
+
+td=[]
master=[]
speed=[]
speed.append([])
for line in fileinput.input():
print("====================================================================")
print(line)
- print("--------------------------------------------------------------------")
process(line)
+
+# Calculate timedelta
+if len(td)==0:
+ exit()
+t0=td[0]
+td[:]=[x-t0 for x in td]
+# Make a plot
fig=plt.figure()
plt.xlabel('time/s')
plt.ylabel('speed/upper')
-plt.plot(t,speed[0],'green')
-plt.plot(t,speed[1],'red')
-plt.plot(t,speed[2],'blue')
-plt.plot(t,speed[3],'magenta')
+plt.plot(td,speed[0],'green')
+plt.plot(td,speed[1],'red')
+plt.plot(td,speed[2],'blue')
+plt.plot(td,speed[3],'magenta')
plt.legend('1234')
-plt.plot(t,master,'black')
+plt.plot(td,master,'black')
plt.title('multipump')
plt.show()
plt.close(fig)