+++ /dev/null
-#!/usr/bin/env python3
-
-#import serial
-import fileinput
-import matplotlib.pyplot as plt
-import numpy as pl
-
-def readbyte(line):
- 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)
- 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)
- cl=cl&0xF
- le,line=readbyte(line)
- os=(le&0xC0)>>6
- le=le&0x3F
- apdu=line[0:3*le]
- 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):
- #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
-
-def process(line):
- global timezero
- global t
- global speed
- global master
-
- typ,ds,le,da,sa,line=process_head(line)
- print("Type:",typ,"Length:",le,"Destination:",da,"Source:",sa)
- if da==0xF7:
- 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
-t=[]
-master=[]
-speed=[]
-speed.append([])
-speed.append([])
-speed.append([])
-speed.append([])
-
-for line in fileinput.input():
- process(line)
- print("------------------------------------------------")
-
-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.legend('1234')
-plt.plot(t,master,'black')
-plt.title('multipump')
-plt.show()
-plt.close(fig)
-
--- /dev/null
+#!/usr/bin/env python3
+
+#import serial
+import fileinput
+import matplotlib.pyplot as plt
+import numpy as pl
+
+def readbyte(line):
+ byte=0x100 # invalid
+ while byte==0x100:
+ try:
+ byte=int(line[0:2],16)
+ line=line[1:]
+ except:
+ 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**********"
+ print("Type:",typ,"DS:",ds)
+ return typ,ds,0,0,0,line
+ le,line=readbyte(line)
+ da,line=readbyte(line)
+ sa,line=readbyte(line)
+ 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"
+ 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
+ le,line=readbyte(line)
+ os=(le&0xC0)>>6
+ le=le&0x3F
+ print("Class:",cl,"Operation:",os,"Length:",le)
+ return cl,os,le,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],"Value:",val[i])
+ return id,val,line
+
+def process_crc(line):
+ #FIXME: does not work in general
+ crc=line[2:7]
+ line=line[7:]
+ print("CRC:",crc)
+ return crc,line
+
+def process_isok(line):
+ isok=False
+ if "OK" in line.upper():
+ isok=True
+ print("IsCrcOk:",isok)
+ return isok
+
+def process_timestamp(line):
+ #FIXME: does not work in general
+ info=line.split(":")
+ try:
+ timestamp=int(info[1].strip())/1000
+ print(timestamp)
+ return timestamp
+ except:
+ return 0
+
+def process(line):
+ global timezero
+ global t
+ global speed
+ global master
+
+ # Some extra info from logging tool
+ isok=process_isok(line)
+ timestamp=process_timestamp(line)
+ if timezero==0:
+ timezero=timestamp
+ # Process received data
+ typ,ds,le,da,sa,line=process_head(line)
+ if da==0xF7:
+ numbytes=le-2
+ while numbytes>0:
+ cl,os,ale,line=process_apdu(line)
+ numbytes=numbytes-2-ale
+ if cl==5:
+ id,val,line=process_class5(line,int(ale/2))
+ t=t+[timestamp-timezero]
+ master=master+[1+(sa-31)/10]
+ for i in range(4):
+ speed[i]=speed[i]+[val[i]/254]
+ else:
+ line=line[ale*3:]
+ crc=process_crc(line)
+ return
+
+timezero=0
+t=[]
+master=[]
+speed=[]
+speed.append([])
+speed.append([])
+speed.append([])
+speed.append([])
+
+for line in fileinput.input():
+ print("====================================================================")
+ print(line)
+ print("--------------------------------------------------------------------")
+ process(line)
+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.legend('1234')
+plt.plot(t,master,'black')
+plt.title('multipump')
+plt.show()
+plt.close(fig)