]> git.zndr.dk Git - dotfiles.git/commitdiff
Robust gf.py
authorJannik Zander <jannikz@gmail.com>
Fri, 10 Jun 2016 18:14:56 +0000 (20:14 +0200)
committerJannik Zander <jannikz@gmail.com>
Fri, 10 Jun 2016 18:14:56 +0000 (20:14 +0200)
.local/bin/genilog.py [changed mode: 0644->0755]
.local/bin/gf.py

old mode 100644 (file)
new mode 100755 (executable)
index 7b5b59574c8011d1f5474c9387f65eb9ea38551d..bf69c6b14b209225530c614368539c01d3ac1135 100755 (executable)
@@ -6,8 +6,9 @@ import matplotlib.pyplot as plt
 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:]
@@ -26,7 +27,7 @@ def process_head(line):
   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)
@@ -60,18 +61,19 @@ def process_class5(line,numid):
   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
@@ -88,34 +90,33 @@ def process(line):
   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