Skip to content

Commit

Permalink
fix: Validate that the queried number of registers matches the number…
Browse files Browse the repository at this point in the history
… of registers received

Sometimes we get a response with almost all values being 0, usually also multiple registers are missing. In that case we just return an empty dictionary. This validation is not perfect but it should be good enough for now.
  • Loading branch information
ahinko committed Jul 4, 2024
1 parent f5b2036 commit e9579a8
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,15 @@ def query_modbus(self):
registers = {}
current_register = self.register_span_start
chunk_size = self.config["datalogger"]["register_chunks"]
queried_registers_counter = 0

# Loop while we still have registers to query
while current_register < self.register_span_end:
try:
logging.info(
f"Querying register {current_register} to {current_register + chunk_size}"
)
queried_registers_counter += chunk_size

message = client.read_input_registers(
slave=self.config["datalogger"]["slave_id"],
Expand Down Expand Up @@ -368,6 +370,13 @@ def query_modbus(self):

client.close()

# Sometimes we get a response with almost all values being 0, usually also multiple registers
# are missing. In that case we just return an empty dictionary. This validation is not perfect
# but it should be good enough for now.
if len(registers) != queried_registers_counter:
logging.info(f"Validation of number of queried registers failed. Queried: {len(registers)}, received: {queried_registers_counter}")
return {}

return registers

def pick_from_registers(self, registers, start, count):
Expand Down

0 comments on commit e9579a8

Please sign in to comment.