From: Jannik Zander Date: Thu, 9 Jun 2016 22:05:32 +0000 (+0200) Subject: Rename genilog.py to gf.py X-Git-Url: https://git.zndr.dk/?a=commitdiff_plain;h=7cd495931a52ac6e9efe0593a40f638a994c6e39;p=dotfiles.git Rename genilog.py to gf.py --- diff --git a/.local/bin/genilog.py b/.local/bin/genilog.py deleted file mode 100755 index 31352c3..0000000 --- a/.local/bin/genilog.py +++ /dev/null @@ -1,136 +0,0 @@ -#!/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) - diff --git a/.local/bin/gf.py b/.local/bin/gf.py new file mode 100755 index 0000000..59a424d --- /dev/null +++ b/.local/bin/gf.py @@ -0,0 +1,140 @@ +#!/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)