Skip to content

ModbusUdpClient does not bind to specified source_address #2859

@dignakov

Description

@dignakov

I'm trying to communicate with a waveshare configured to receive and respond via UDP on port 4196.

TCP works great, however with UDP the client doesn't seem to listen on the port I specify in source_address. I tried setting it to both an actual IP, or '', same result. I'm able to do send command, but not get responses:

client.write_coil(address=0, value=False, device_id=SLAVE_ID)

I would hear the relay toggle, but get the error below, unless I remake the socket.

No response received after 3 retries, continue with next request
---------------------------------------------------------------------------
ModbusIOException                         Traceback (most recent call last)
Cell In[28], line 1
----> 1 client.write_coil(address=0, value=False, device_id=SLAVE_ID)
      2 # result = client.read_coils(address=0, count=8, device_id=SLAVE_ID)
      3 # print(result.bits)

File ~/devel/python/modbus/.venv/lib/python3.12/site-packages/pymodbus/client/mixin.py:152, in ModbusClientMixin.write_coil(self, address, value, device_id, no_response_expected)
    139 """Write single coil (code 0x05).
    140 
    141 :param address: Address to write to
   (...)    149 Coils are addressed as 0-N (Note some device manuals uses 1-N, assuming 1==0).
    150 """
    151 pdu = pdu_bit.WriteSingleCoilRequest(address=address, bits=[value], dev_id=device_id)
--> 152 return self.execute(no_response_expected, pdu)

File ~/devel/python/modbus/.venv/lib/python3.12/site-packages/pymodbus/client/base.py:202, in ModbusBaseSyncClient.execute(self, no_response_expected, request)
    200 if not self.connect():
    201     raise ConnectionException(f"Failed to connect[{self!s}]")
--> 202 return self.transaction.sync_execute(no_response_expected, request)

File ~/devel/python/modbus/.venv/lib/python3.12/site-packages/pymodbus/transaction/transaction.py:153, in TransactionManager.sync_execute(self, no_response_expected, request)
    151 txt = f"No response received after {self.retries} retries, continue with next request"
    152 Log.error(txt)
--> 153 raise ModbusIOException(txt)

ModbusIOException: Modbus Error: [Input/Output] No response received after 3 retries, continue with next request

This is what I did to fix it:

client = ModbusUdpClient(
    '192.168.1.200',
    port=4196,
    framer=FramerType.RTU,
    source_address=('', 4196),  # Bind to port 4196 locally
    timeout=2.0
)
client.connect()
# I had to do this to make it work
client.socket.close()
client.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client.socket.bind(('', 4196))
client.socket.settimeout(2.0)
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions