4

I have problem with pymodbus TcpClient timeout:

import logging
from pymodbus.client.sync import ModbusTcpClient

logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)

client = ModbusTcpClient('x.y.z.w', port=yyy)
client.connect()

result = client.read_holding_registers(10, 10)
print(result.registers)
client.close()

Error:

DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:Transaction failed. (timed out)
ERROR:pymodbus.client.sync:Connection to (x.y.z.w, yyy) failed: [Errno 10061] ╧юфъы■ўхэшх эх єёЄрэютыхэю,

Modpool with tiomeot=1 I have error:

modpoll.exe -c 5 -r 10 -o 1 -p yyy -m tcp x.y.z.w
-- Polling slave... (Ctrl-C to stop)
Reply time-out!

But with timeout=10 all goods:

modpoll.exe -c 5 -r 10 -o 10 -p yyy -m tcp x.y.z.w
-- Polling slave... (Ctrl-C to stop)
[10]: 2
[11]: 10
[12]: 10
[13]: 10

  • How change default timeout in pymodbus TcpClient?
srgi0
  • 3,319
  • 1
  • 23
  • 20

2 Answers2

4

Try

client = ModbusTcpClient('x.y.z.w', port=yyy, timeout=10)

it works for rtu in pymodbus.

Azat
  • 196
  • 1
  • 8
4
from pymodbus.constants import Defaults

Defaults.Timeout = 10
client = ModbusTcpClient('x.y.z.w', port=yyy)
client.connect()

ModbusTcpClient class doesn't have any argument in it's constructor or specific method to pass the timeout to the class. Instead, one can change the timeout of the class by globally changing the timeout variable using the Defaults which consequently affects the ModbusTcpClient connection timeout variable.

ahj
  • 745
  • 1
  • 6
  • 13
  • Please explain your answers! – Sterling Archer Nov 30 '15 at 14:21
  • Pymodbus ModbusTcpClient now supports setting a timeout value directly during instantiation, as of v 1.3.0. – GG2 May 04 '18 at 08:04
  • Specifically 1.3.0.rc2. Like so: `ModbusTcpClient( ..., timeout=5 )` Reference: https://pymodbus.readthedocs.io/en/latest/changelog.html#version-1-3-0-rc2 – GG2 May 04 '18 at 08:23