From 72357dd188605ed2f9e21e5f9b387afde747ed42 Mon Sep 17 00:00:00 2001 From: Jens Eichler Date: Fri, 10 May 2024 14:33:47 +0200 Subject: [PATCH] started work on adding discrete inputs and coils working. - added additional tables - added relevant pymodbus code to _scan_value_range - modified return statement to differentiate between analogue and digital result-objects --- modbus4mqtt/modbus_interface.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/modbus4mqtt/modbus_interface.py b/modbus4mqtt/modbus_interface.py index c301c9b..75a11ba 100644 --- a/modbus4mqtt/modbus_interface.py +++ b/modbus4mqtt/modbus_interface.py @@ -50,10 +50,10 @@ def __init__(self, self._port = port # This is a dict of sets. Each key represents one table of modbus registers. # At the moment it has 'input' and 'holding' - self._tables = {'input': set(), 'holding': set()} + self._tables = {'input': set(), 'holding': set(), 'do': set(), 'di': set()} # This is a dicts of dicts. These hold the current values of the interesting registers - self._values = {'input': {}, 'holding': {}} + self._values = {'input': {}, 'holding': {}, 'do': {}, 'di': {}} self._planned_writes = Queue() self._writing = False @@ -226,11 +226,18 @@ def _scan_value_range(self, table, start, count): result = self._mb.read_input_registers(start, count, unit=self._unit) elif table == 'holding': result = self._mb.read_holding_registers(start, count, unit=self._unit) + elif table == 'do': + result = self._mb.read_do(start, count, unit=self._unit) + elif table == 'di': + result = self._mb.read_discrete_inputs(start, count, unit=self._unit) try: - return result.registers + if table == 'input' or table == 'holding': + return result.registers + elif table == 'do' or table == 'di': + return result.bits except: - # The result doesn't have a registers attribute, something has gone wrong! - raise ValueError("Failed to read {} {} table registers starting from {}: {}".format(count, table, start, result)) + # The result doesn't have either registers or bits attribute, something has gone wrong! + raise ValueError("Failed to read {} {} table data starting from {}: {}".format(count, table, start, result)) def type_length(type):