diff --git a/minimalmodbus.py b/minimalmodbus.py index 0d13224..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 @@ -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( @@ -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 @@ -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