Skip to content

Commit

Permalink
improved timing
Browse files Browse the repository at this point in the history
  • Loading branch information
TrystanLea committed Aug 11, 2021
1 parent bf3d1e7 commit 0115dae
Showing 1 changed file with 48 additions and 44 deletions.
92 changes: 48 additions & 44 deletions src/interfacers/EmonHubMBUSInterfacer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit 0115dae

Please sign in to comment.