import fileinput
import matplotlib.pyplot as plt
import numpy as pl
-from datetime import datetime,time
+from datetime import datetime,date,time
from math import modf
######################################################
+def process_timestamp(line):
+ info=line.split(":")
+ try:
+ timestamp=int(info[1].strip())/1000
+ except:
+ timestamp=0
+ finally:
+ ts=datetime.fromtimestamp(timestamp)
+
+ if timestamp>1e9: # 2001-09-09
+ line=info[0]
+ else:
+ try:
+ h=int(info[0].split(' ')[1])
+ m=int(info[1])
+ s=info[2].split('.')
+ u=s[1].split(' ')
+ ts=datetime.combine(date.today(),time(h,m,int(s[0]),int(u[0])*1000))
+ line=s[1][4:]
+ except:
+ pass
+ print("Timestamp:",ts)
+ print(line)
+ return ts,line
+
+######################################################
+def checkline(line):
+ ok=False
+ if len(line)>16 and not "ERR" in line.upper():
+ ok=True
+ print("Ok:",ok)
+ return ok
+
+######################################################
+# read next byte from line
def readbyte(line):
- # read next byte in line
byte=256
while byte==256 and len(line)>0:
try:
print("CRC:",crc)
return crc
-######################################################
-def process_islineok(line):
- ok=False
- if not "ERR" in line.upper() and len(line)>10:
- ok=True
- return ok
-
-######################################################
-def process_timestamp(line):
- info=line.split(":")
- try:
- timestamp=int(info[1].strip())/1e3
- except:
- 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 td
global master
global speed
- # Check if timestamp was added to line
- ts=process_timestamp(line)
+ # Check if timestamp exists
+ ts,line=process_timestamp(line)
- # Sanity check line
- ok=process_islineok(line)
- if not ok:
- return
+ # Check if line is ok
+ ok=checkline(line)
+ if not ok: return
- # Process received data
+ # Process telegram
typ,ds,le,da,sa,line=process_head(line)
numbytes=le-2
while numbytes>0 and len(line)>=2*numbytes:
cl,os,ale,line=process_apdu(line)
- numbytes=numbytes-2
+ numbytes=numbytes-2-ale
if cl==5:
id5,val5,line=process_class5(line,int(ale/2))
- numbytes=numbytes-ale
- # Debugging ..........
+ # Debugging
td=td+[ts.timestamp()]
master=master+[1+(sa-31)/10]
for i in range(4):
speed[i]=speed[i]+[val5[i]/254]
else:
line=line[ale*3:]
- numbytes=numbytes-ale
crc=process_crc(line)
return
-
-
######################################################
-
+# Debugging
td=[]
master=[]
speed=[]
process(line)
# Calculate timedelta
-if len(td)==0:
- exit()
+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')