From 8421e49b96cf4f7ad9e4e03488d53128a8fe8d96 Mon Sep 17 00:00:00 2001 From: Matias Senger Date: Sat, 25 Dec 2021 13:12:54 +0100 Subject: [PATCH 1/2] I can retrieve information from the power supply --- minimalmodbus.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/minimalmodbus.py b/minimalmodbus.py index 0d13224..5af31cb 100644 --- a/minimalmodbus.py +++ b/minimalmodbus.py @@ -1069,13 +1069,13 @@ def _generic_command( ) # Check combinations: Broadcast and functioncode - if ( - self.address == _SLAVEADDRESS_BROADCAST - and functioncode not in ALLOWED_FUNCTIONCODES_BROADCAST - ): - raise ValueError( - f"Wrong functioncode for broadcast. Given: {functioncode!r}" - ) + # ~ if ( + # ~ self.address == _SLAVEADDRESS_BROADCAST + # ~ and functioncode not in ALLOWED_FUNCTIONCODES_BROADCAST + # ~ ): + # ~ raise ValueError( + # ~ f"Wrong functioncode for broadcast. Given: {functioncode!r}" + # ~ ) # Check combinations: signed if signed: @@ -1245,8 +1245,8 @@ def _generic_command( payload_from_slave = self._perform_command(functioncode, payload_to_slave) # There is no response for broadcasts - if self.address == _SLAVEADDRESS_BROADCAST: - return None + # ~ if self.address == _SLAVEADDRESS_BROADCAST: + # ~ return None # Parse response payload return _parse_payload( @@ -1300,9 +1300,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: - number_of_bytes_to_read = 0 - elif self.precalculate_read_size: + # ~ if self.address == _SLAVEADDRESS_BROADCAST: + # ~ number_of_bytes_to_read = 0 + if self.precalculate_read_size: try: number_of_bytes_to_read = _predict_response_size( self.mode, functioncode, payload_to_slave From 934fe6b6bee82cfeae9ec1e196b1dd6d1e1fdc10 Mon Sep 17 00:00:00 2001 From: Matias Senger Date: Sat, 25 Dec 2021 13:40:08 +0100 Subject: [PATCH 2/2] Added argument to switch between the two modes --- minimalmodbus.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/minimalmodbus.py b/minimalmodbus.py index 5af31cb..2b7a3ab 100644 --- a/minimalmodbus.py +++ b/minimalmodbus.py @@ -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 @@ -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 @@ -1069,13 +1075,14 @@ def _generic_command( ) # Check combinations: Broadcast and functioncode - # ~ if ( - # ~ self.address == _SLAVEADDRESS_BROADCAST - # ~ and functioncode not in ALLOWED_FUNCTIONCODES_BROADCAST - # ~ ): - # ~ raise ValueError( - # ~ f"Wrong functioncode for broadcast. Given: {functioncode!r}" - # ~ ) + if ( + self.allow_broadcast_address == False + and self.address == _SLAVEADDRESS_BROADCAST + and functioncode not in ALLOWED_FUNCTIONCODES_BROADCAST + ): + raise ValueError( + f"Wrong functioncode for broadcast. Given: {functioncode!r}" + ) # Check combinations: signed if signed: @@ -1245,8 +1252,8 @@ def _generic_command( payload_from_slave = self._perform_command(functioncode, payload_to_slave) # There is no response for broadcasts - # ~ if self.address == _SLAVEADDRESS_BROADCAST: - # ~ return None + if self.allow_broadcast_address == False and self.address == _SLAVEADDRESS_BROADCAST: + return None # Parse response payload return _parse_payload( @@ -1300,8 +1307,8 @@ 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: - # ~ number_of_bytes_to_read = 0 + if self.allow_broadcast_address == False and self.address == _SLAVEADDRESS_BROADCAST: + number_of_bytes_to_read = 0 if self.precalculate_read_size: try: number_of_bytes_to_read = _predict_response_size(