import numpy as pl
def readbyte(line):
- str=line[0:2].strip()
- byte=0
- if str!='':
- byte=int(str,16)
- tail=line[2:].strip()
- return byte,tail
+ byte=0x100 # invalid
+ while byte==0x100:
+ try:
+ byte=int(line[0:2],16)
+ line=line[1:]
+ except ValueError:
+ pass
+ finally:
+ line=line[2:]
+ return byte,line
def process_head(line):
ds,line=readbyte(line)
+ if ds==0x27:
+ typ="Data Request"
+ elif ds==0x26:
+ typ="Data Message"
+ elif ds==0x24:
+ typ="Data Reply"
+ else:
+ typ="**********Error**********"
+ return typ,ds,0,0,0,line
le,line=readbyte(line)
da,line=readbyte(line)
sa,line=readbyte(line)
- typ="**********Error**********"
- if ds==0x27: typ="Data Request"
- if ds==0x27 and da==0xFE: typ="Connection Request"
- if ds==0x27 and da==0xFF: typ="Broadcast Request"
- if ds==0x26: typ="Data Message"
- if ds==0x26 and da==0xF7: typ="Multicast Message"
- if ds==0x24: typ="Data Reply"
- return ds,le,da,sa,typ
+ if ds==0x27 and da==0xFE:
+ typ="Connection Request"
+ if ds==0x27 and da==0xFF:
+ typ="Broadcast Request"
+ if ds==0x26 and da==0xF7:
+ typ="Multicast Message"
+ return typ,ds,le,da,sa,line
def process_pdu(line):
cl,line=readbyte(line)
line=line[3*le:]
return cl,os,le,apdu,line
+def process_class5(line,numid):
+ id=[0]*numid
+ val=[0]*numid
+ for i in range(0,numid):
+ id[i],line=readbyte(line)
+ val[i],line=readbyte(line)
+ print("Id:",id[i],"Val:",val[i])
+ return id,val,line
+
def process_crc(line):
- crc=line
- return crc
+ #FIXME: does not work in general
+ crc=line[2:7]
+ line=line[7:]
+ return crc,line
def process_isok(line):
+ #FIXME: does not work in general
isok=line
return line
def process_timestamp(line):
+ #FIXME: does not work in general
timestamp=int(line)/1000
return timestamp
global t
global speed
global master
- line=line[2:]
- packet=line.split(" - ")
- n=len(packet)
- if n>0:
- print("Header:",packet[0])
- ds,le,da,sa,typ=process_head(packet[0])
- print("Type:",typ,"Length:",le,"Destination:",da,"Source:",sa)
+
+ typ,ds,le,da,sa,line=process_head(line)
+ print("Type:",typ,"Length:",le,"Destination:",da,"Source:",sa)
if da==0xF7:
- if n>1:
- print("Payload:",packet[1])
- pdu=packet[1]
- while len(pdu)>0:
- cl,os,ale,apdu,pdu=process_pdu(pdu)
- print("Class:",cl,"Operation:",os,"Length:",ale,"APDU:",apdu)
- if cl==5:
- id=[0]*4
- ref=[0]*4
- id[0],apdu=readbyte(apdu)
- ref[0],apdu=readbyte(apdu)
- id[1],apdu=readbyte(apdu)
- ref[1],apdu=readbyte(apdu)
- id[2],apdu=readbyte(apdu)
- ref[2],apdu=readbyte(apdu)
- id[3],apdu=readbyte(apdu)
- ref[3],apdu=readbyte(apdu)
- for i in range(0,4):
- print("Id:",id[i],"Ref:",ref[i])
- if n>2:
- crc=process_crc(packet[2])
- if n>3:
- info=packet[3].split(" : ")
- m=len(info)
- if m>0:
- isok=process_isok(info[0])
- if m>1:
- timestamp=process_timestamp(info[1].strip())
- if timezero==0:
- timezero=timestamp
- t=t+[timestamp-timezero]
- master=master+[1+(sa-31)/10]
- speed[0]=speed[0]+[ref[0]/254]
- speed[1]=speed[1]+[ref[1]/254]
- speed[2]=speed[2]+[ref[2]/254]
- speed[3]=speed[3]+[ref[3]/254]
+ numbytes=le-2
+ while numbytes>0:
+ cl,os,ale,apdu,line=process_pdu(line)
+ numbytes=numbytes-2-ale
+ print("Class:",cl,"Operation:",os,"Length:",ale,"APDU:",apdu)
+ if cl==5:
+ id,val,apdu=process_class5(apdu,int(ale/2))
+ crc,line=process_crc(line)
+ print(crc)
+ info=line.split(" : ")
+ m=len(info)
+ if m>0:
+ isok=process_isok(info[0])
+ if m>1:
+ timestamp=process_timestamp(info[1].strip())
+ if timezero==0:
+ timezero=timestamp
+ t=t+[timestamp-timezero]
+ master=master+[1+(sa-31)/10]
+ speed[0]=speed[0]+[val[0]/254]
+ speed[1]=speed[1]+[val[1]/254]
+ speed[2]=speed[2]+[val[2]/254]
+ speed[3]=speed[3]+[val[3]/254]
return
timezero=0