From 864705656403becde36823cb4b937f1b43b25974 Mon Sep 17 00:00:00 2001 From: Pim van den Berg Date: Sun, 15 Nov 2020 14:30:41 +0100 Subject: [PATCH] refactor(itho-wpu): compose request + calculate checksum on the fly Messages have 6 header bytes, data bytes and 1 checksum byte. The 6 header bytes are: 1: i2c destination address (0x82) 2: i2c reply address (0x80) 3,4: message class 5: message type (0x04 = request) 6: data length (0x00 in case of a request) --- itho-wpu.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/itho-wpu.py b/itho-wpu.py index f16d524..1e6049b 100755 --- a/itho-wpu.py +++ b/itho-wpu.py @@ -125,17 +125,33 @@ class I2CMaster: self.i = i2c_raw.I2CRaw(address=address, bus=bus) self.queue = queue - def execute_action(self, action): + def compose_request(self, action): actions = { - "getnodeid": [0x80, 0x90, 0xE0, 0x04, 0x00, 0x8A], - "getserial": [0x80, 0x90, 0xE1, 0x04, 0x00, 0x89], - "getdatatype": [0x80, 0xA4, 0x00, 0x04, 0x00, 0x56], - "getdatalog": [0x80, 0xA4, 0x01, 0x04, 0x00, 0x55], + "getnodeid": [0x90, 0xE0], + "getserial": [0x90, 0xE1], + "getdatatype": [0xA4, 0x00], + "getdatalog": [0xA4, 0x01], } + # 0x80 = source, 0x04 = msg_type, 0x00 = length + request = [0x80] + actions[action] + [0x04, 0x00] + request.append(self.calculate_checksum(request)) + return request + + def calculate_checksum(self, request): + s = 0x82 + for i in request: + s += i + checksum = 256 - (s % 256) + if checksum == 256: + checksum = 0 + return checksum + + def execute_action(self, action): + request = self.compose_request(action) result = None for i in range(0, 20): logger.debug(f"Executing action: {action}") - self.i.write_i2c_block_data(actions[action]) + self.i.write_i2c_block_data(request) time.sleep(0.21) logger.debug("Queue size: {}".format(self.queue.qsize())) if self.queue.qsize() > 0: