import numpy as pl
def readbyte(line):
- byte=0x100 # invalid
- while byte==0x100:
+ # read next byte in line
+ byte=256
+ while byte==256 and len(line)>0:
try:
byte=int(line[0:2],16)
line=line[1:]
elif ds==0x24:
typ="Data Reply"
else:
- typ="**********Error**********"
+ typ="Data Error"
print("Type:",typ,"DS:",ds)
return typ,ds,0,0,0,line
le,line=readbyte(line)
return id,val,line
def process_crc(line):
- #FIXME: does not work in general
- crc=line[2:7]
- line=line[7:]
+ crc1,line=readbyte(line)
+ crc2,line=readbyte(line)
+ crc1=format(crc1,'02X')
+ crc2=format(crc2,'02X')
+ crc=crc1+' '+crc2
print("CRC:",crc)
- return crc,line
+ return crc
-def process_isok(line):
- isok=False
- if "OK" in line.upper():
- isok=True
- print("IsCrcOk:",isok)
- return isok
+def process_islineok(line):
+ ok=True
+ if "ERR" in line.upper() or len(line) < 12:
+ ok=False
+ return ok
def process_timestamp(line):
#FIXME: does not work in general
global t
global speed
global master
+
# Some extra info from logging tool
- isok=process_isok(line)
+ ok=process_islineok(line)
+ if not ok: return
+
timestamp=process_timestamp(line)
if timezero==0:
timezero=timestamp
+
# Process received data
typ,ds,le,da,sa,line=process_head(line)
numbytes=le-2
- print("head",numbytes)
- if da==0xF7:
- while numbytes>0:
- cl,os,ale,line=process_apdu(line)
- numbytes=numbytes-2
- print("apdu",numbytes)
- if cl==5:
- id,val,line=process_class5(line,int(ale/2))
- numbytes=numbytes-ale
- print("cl5",numbytes)
- # Debugging info....
- t=t+[(timestamp-timezero)/1000]
- master=master+[1+(sa-31)/10]
- for i in range(4):
- speed[i]=speed[i]+[val[i]/254]
- else:
- line=line[ale*3:]
- numbytes=numbytes-ale
- print("clxxx",numbytes)
- crc=process_crc(line)
+ while numbytes>0:
+ cl,os,ale,line=process_apdu(line)
+ numbytes=numbytes-2
+ if cl==5:
+ id,val,line=process_class5(line,int(ale/2))
+ numbytes=numbytes-ale
+ # Debugging info....
+ master=master+[1+(sa-31)/10]
+ t=t+[(timestamp-timezero)/1000]
+ for i in range(4):
+ speed[i]=speed[i]+[val[i]/254]
+ else:
+ line=line[ale*3:]
+ numbytes=numbytes-ale
+ crc=process_crc(line)
return
timezero=0