changeset 2:1e65aca0f39c

- Refactor use of struct.unpack() for greater clarity. - Fix logging of checksum errors.
author darius@midget.dons.net.au
date Mon, 29 Oct 2007 10:44:26 +1030
parents 93b375e93fe1
children afd270964f5b
files __init__.py
diffstat 1 files changed, 8 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/__init__.py	Sun Oct 28 11:09:44 2007 +1030
+++ b/__init__.py	Mon Oct 29 10:44:26 2007 +1030
@@ -85,6 +85,7 @@
 
 
     def HandleChar(self, ch):
+        chint = struct.unpack('B', ch)[0]
 	if (self.state == 'init'):
 	    if (ch != '\x7e'):
                 return
@@ -93,30 +94,30 @@
             return
         
 	elif (self.state == 'sizemsb'):
-            self.bufszmsb = struct.unpack('B', ch)[0]
+            self.bufszmsb = chint
             self.state = 'sizelsb'
 	    return
 
 	elif (self.state == 'sizelsb'):
-	    self.bufszlsb = struct.unpack('B', ch)[0]
+	    self.bufszlsb = chint
 	    self.dataleft = self.bufszmsb << 8 | self.bufszlsb
 	    
 	    self.state = 'data'
             return
         
 	elif (self.state == 'data'):
-	    self.buffer.append(struct.unpack('B', ch)[0])
+	    self.buffer.append(chint)
             self.dataleft = self.dataleft - 1
             if (self.dataleft == 0):
                 self.state = 'cksum'
             return
          
         elif (self.state == 'cksum'):
-            cksum = reduce(lambda x, y: x + y, self.buffer) & 0xff
-            rxcksum = struct.unpack('B', ch)[0]
-            if (cksum + rxcksum != 0xff):
+            pktsum = reduce(lambda x, y: x + y, self.buffer) & 0xff
+            rxcksum = chint
+            if (pktsum + rxcksum != 0xff):
                 self.PrintError("Bad checksum, got 0x%02x, expected 0x%02x" %
-                                cksum, 0xff - cksum)
+                                rxcksum, 0xff - pktsum)
             else:
                 self.PrintError("Triggered event: " + str(self.buffer))
                 self.TriggerEvent(str(self.buffer))