Skip to content

Compatibility with non-standard Elektro Automatik equipment #82

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

Open
wants to merge 3 commits 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
15 changes: 11 additions & 4 deletions minimalmodbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def __init__(
mode: str = MODE_RTU,
close_port_after_each_call: bool = False,
debug: bool = False,
allow_broadcast_address: bool = False,
) -> None:
"""Initialize instrument and open corresponding serial port."""
self.address = slaveaddress
Expand Down Expand Up @@ -162,6 +163,11 @@ def __init__(

Changing this will not affect how other instruments use the same serial port.
"""

self.allow_broadcast_address = allow_broadcast_address
"""Set this to :const:`True` to allow coomunicating with an instrument
with address 0.
"""

self.clear_buffers_before_each_transaction = True
"""If this is :const:`True`, the serial port read and write buffers are
Expand Down Expand Up @@ -1070,7 +1076,8 @@ def _generic_command(

# Check combinations: Broadcast and functioncode
if (
self.address == _SLAVEADDRESS_BROADCAST
self.allow_broadcast_address == False
and self.address == _SLAVEADDRESS_BROADCAST
and functioncode not in ALLOWED_FUNCTIONCODES_BROADCAST
):
raise ValueError(
Expand Down Expand Up @@ -1245,7 +1252,7 @@ def _generic_command(
payload_from_slave = self._perform_command(functioncode, payload_to_slave)

# There is no response for broadcasts
if self.address == _SLAVEADDRESS_BROADCAST:
if self.allow_broadcast_address == False and self.address == _SLAVEADDRESS_BROADCAST:
return None

# Parse response payload
Expand Down Expand Up @@ -1300,9 +1307,9 @@ def _perform_command(self, functioncode: int, payload_to_slave: str) -> str:

# Calculate number of bytes to read
number_of_bytes_to_read = DEFAULT_NUMBER_OF_BYTES_TO_READ
if self.address == _SLAVEADDRESS_BROADCAST:
if self.allow_broadcast_address == False and self.address == _SLAVEADDRESS_BROADCAST:
number_of_bytes_to_read = 0
elif self.precalculate_read_size:
if self.precalculate_read_size:
try:
number_of_bytes_to_read = _predict_response_size(
self.mode, functioncode, payload_to_slave
Expand Down