# HG changeset patch # User darius@midget.dons.net.au # Date 1193616866 -37800 # Node ID 1e65aca0f39ccf684ce9412383ab6e2e83611e31 # Parent 93b375e93fe1a22536c65562e5c7b1a1315d5055 - Refactor use of struct.unpack() for greater clarity. - Fix logging of checksum errors. diff -r 93b375e93fe1 -r 1e65aca0f39c __init__.py --- 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))