【发布时间】:2021-03-08 08:33:21
【问题描述】:
import pymodbus
from pymodbus.client.sync import ModbusTcpClient
from time import sleep
connection = False
data = {}
while True:
if not connection:
client = ModbusTcpClient(host="176.219.185.16", port="502")
connection = client.connect()
if connection:
print("connected")
if connection == True:
if not client.is_socket_open():
print("connection is broken")
connection = False
data = client.read_holding_registers(address=150,count=10,unit=1).registers
sleep(2)
print(data)
此代码在 Windows 上运行。但是我在树莓派中运行它“client.is_socket_open”在读取保持寄存器后返回false。我该如何解决这个问题?或者这里有什么问题。我不强制客户端关闭。
【问题讨论】:
-
最简单的解决方法是在事务之间引入一个小睡眠(比如 50-100 毫秒),这应该可以解决 pymodbus 2.5.0 的问题。请参阅评论 github.com/riptideio/pymodbus/issues/612#issuecomment-796758646。
标签: python raspberry-pi pymodbus