From 0115dae1666fdc77fb261b6446fc7a81d03f7d63 Mon Sep 17 00:00:00 2001 From: TrystanLea Date: Wed, 11 Aug 2021 17:56:59 +0100 Subject: [PATCH] improved timing --- src/interfacers/EmonHubMBUSInterfacer.py | 92 ++++++++++++------------ 1 file changed, 48 insertions(+), 44 deletions(-) diff --git a/src/interfacers/EmonHubMBUSInterfacer.py b/src/interfacers/EmonHubMBUSInterfacer.py index 047bf8ef..ec0a7efb 100644 --- a/src/interfacers/EmonHubMBUSInterfacer.py +++ b/src/interfacers/EmonHubMBUSInterfacer.py @@ -377,7 +377,7 @@ def parse_frame_meterbus_lib(self,data,records): def request_data(self, address, records): for i in range(0,2): self.mbus_short_frame(address, 0x5b) - time.sleep(1.0) + # time.sleep(1.0) result = self.read_data_frame(records) if result!=None: return result @@ -387,7 +387,7 @@ def request_data(self, address, records): def request_data_sdm120(self, address, records): for i in range(0,2): self.mbus_request_sdm120(address) - time.sleep(1.0) + # time.sleep(1.0) result = self.read_data_frame(records) if result!=None: return result @@ -404,53 +404,57 @@ def read_data_frame(self,records): valid = False start_time = time.time() + val = 0 - while self.ser.in_waiting: - # Read in byte - val = ord(self.ser.read(1)) - data.append(val) - # print(str(bid)+" "+str(val)+" "+str(hex(val))) - # Long frame start, reset checksum - if bid == 0 and val == 0x68: - # print("MBUS start") - valid = True - checksum = 0 - - # 2nd byte is the frame length - if valid and bid == 1: - length = val - bid_end = length + 4 + 2 - 1 - bid_checksum = bid_end - 1 - # print("MBUS length "+str(length)) - # print("MBUS bid_end "+str(bid_end)) - # print("MBUS bid_checksum "+str(bid_checksum)) - - if valid and bid == 2 and val != length: - valid = False # 3rd byte is also length, check that its the same as 2nd byte - if valid and bid == 3 and val != 0x68: - valid = False # 4th byte is the start byte again - if valid and bid > 3 and bid < bid_checksum: - checksum += val # Increment checksum during data portion of frame - - if valid and bid == bid_checksum and val != checksum % 256: - if self._settings['validate_checksum']: - valid = False # Validate checksum - - if bid == bid_end and val == 0x16: - time_elapsed = time.time()-start_time - self._log.debug("MBUS data received "+str(bid)+" bytes "+str(time_elapsed)+"s") + + while (time.time()-start_time)<2.0: + while self.ser.in_waiting: + # Read in byte + val = ord(self.ser.read(1)) + data.append(val) + # print(str(bid)+" "+str(val)+" "+str(hex(val))) + # Long frame start, reset checksum + if bid == 0 and val == 0x68: + # print("MBUS start") + valid = True + checksum = 0 + + # 2nd byte is the frame length + if valid and bid == 1: + length = val + bid_end = length + 4 + 2 - 1 + bid_checksum = bid_end - 1 + # print("MBUS length "+str(length)) + # print("MBUS bid_end "+str(bid_end)) + # print("MBUS bid_checksum "+str(bid_checksum)) + + if valid and bid == 2 and val != length: + valid = False # 3rd byte is also length, check that its the same as 2nd byte + if valid and bid == 3 and val != 0x68: + valid = False # 4th byte is the start byte again + if valid and bid > 3 and bid < bid_checksum: + checksum += val # Increment checksum during data portion of frame + + if valid and bid == bid_checksum and val != checksum % 256: + if self._settings['validate_checksum']: + valid = False # Validate checksum - if valid: # Parse frame if still valid - if self.use_meterbus_lib: - return self.parse_frame_meterbus_lib(data,records) - else: - return self.parse_frame(data,records) - - bid += 1 + if bid == bid_end and val == 0x16: + time_elapsed = time.time()-start_time + self._log.debug("Invalid MBUS data received %d bytes %0.1f ms" % (bid,time_elapsed*1000)) + + if valid: # Parse frame if still valid + if self.use_meterbus_lib: + return self.parse_frame_meterbus_lib(data,records) + else: + return self.parse_frame(data,records) + + bid += 1 + time.sleep(0.1) # If we are here data response is corrupt time_elapsed = time.time()-start_time - self._log.debug("Invalid MBUS data received "+str(bid)+" bytes "+str(time_elapsed)+"s") + self._log.debug("Invalid MBUS data received %d bytes %0.1f ms" % (bid,time_elapsed*1000)) # end of read_data_frame def add_result_to_cargo(self,meter,c,result):