i am getting pymodbus 502 connection refused.



  • @Lazar-Demin
    can you please help me i am getting pymodbus port 502 connection refused?. what could be the issue?.

    from pymodbus.client.sync import ModbusTcpClient
    import time
    import logging
    logging.basicConfig()
    log = logging.getLogger()
    log.setLevel(logging.DEBUG)
    host = '192.168.0.200'
    port = 502

    client = ModbusTcpClient(host,502)
    client.connect()
    while True:
    rr = client.read_holding_registers(0x0001,2,unit=2)
    print(rr)
    #assert(rr.function_code < 0x80) # test that we are not an error
    #print rr.registers
    time.sleep(5)

    error:
    root@Omega-6834:~# python modbus_ip.py
    ERROR:pymodbus.client.sync:Connection to (192.168.0.200, 502) failed: timed out
    ERROR:pymodbus.client.sync:Connection to (192.168.0.200, 502) failed: timed out
    Traceback (most recent call last):
    File "modbus_ip.py", line 13, in <module>
    rr = client.read_holding_registers(0x0001,2,unit=2)
    File "/usr/lib/python2.7/site-packages/pymodbus/client/common.py", line 114, in read_holding_registers
    return self.execute(request)
    File "/usr/lib/python2.7/site-packages/pymodbus/client/sync.py", line 107, in execute
    raise ConnectionException("Failed to connect[%s]" % (self.str()))
    pymodbus.exceptions.ConnectionException: Modbus Error: [Connection] Failed to connect[ModbusTcpClient(192.168.0.200:502)]
    Note: i am able to pinging 192.168.0.200 this IPaddress.



  • @Nagarjuna-Reddy This suggests that port 502 is not open on IP 192.168.0.200. Is the service running on that device on that IP and port? Is there a firewall in place?

    A good test is to try to telnet to that IP and port, but. You'll need to provide more specifics of your setup if you need more help but check the above.



  • @crispyoz sir it was working previously, while working time suddenly I got this issue. but here i am able to ping the device IP. using telnet port forbidden error happening. I was done onion restart after also port was not opened. after done Modbus device restart port was opened. Is there any way to kill that 502 port issue from terminal side?



  • @Nagarjuna-Reddy I suspect the issue is that when you connect to the Modbus host then disconnect, the port on the host is not closed and the device can only accept a single connection. So you need to make sure that you close the port on the client side each time you or subsequent connections will fail.

    I'm making some assumptions here because I don't have details of the host device, but the logic is sound.



  • Hello, i am looking for a pymodbus that matches the distribution of my omega2+, can you please give me the reference of the library that you are using, it is exactly what i am looking for



  • @Ghofran-Daoues I assumed at the time @Nagarjuna-Reddy was using libmodbus which is available on OpenWrt 18 and later. OOTB Omega2(+) has a customised version of OpenWrt 18.

    From OpenWrt 22 PyLibModbus was introduced. The author's github is here:

    https://github.com/stephane/pylibmodbus/tree/main



  • @crispyoz thank you sir i found the library and now i'm getting the same error as @Nagarjuna-Reddy : "i am getting pymodbus 502 connection refused"



  • @Ghofran-Daoues this is not really a question for this forum, it's really for a libmodbus forum. However, your error indicates it cannot connect to port 502 of the device. There can be a few reasons for this, the first things I would look at are 1) perhaps the device is not accessible on your network; 2) the device is not listening on port 502.

    To check 1, see if you can ping the device (ping <ip address>)
    To check 2, (assuming check 1 passes) check your modbus device is listening on port 502 using nmap (nmap -p 502 <ip address>) nmap should report the port is "open" but if it shows as closed, there's your problem, if it reports as "filtered" there is probably a firewall blocking you.



Looks like your connection to Community was lost, please wait while we try to reconnect.