Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

started work on adding discrete inputs and coils working. #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions modbus4mqtt/modbus_interface.py
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I would suggest to stick with more explicit nomenclature:

di -> discrete_input
do -> coils

Also i think method read_do does not exist.

Please let me know if you are willing to improve it, otherwise I can submit another PR; since I am also interested in merging new feature.

Original file line number Diff line number Diff line change
Expand Up @@ -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()}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

improve table names


# 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': {}}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here.


self._planned_writes = Queue()
self._writing = False
Expand Down Expand Up @@ -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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read_do do not exists, you would like to call read_coils instead.
PyModBus docs

image

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):
Expand Down