【问题标题】:After read holding registers with Pymodbus connection is broken in Raspberry Pi在 Raspberry Pi 中读取带有 Pymodbus 连接的保持寄存器后断开
【发布时间】: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


【解决方案1】:

经过几次测试,我可以在 windows 机器上复制问题,这与 pymodbus 版本有关。使用 pymodbus 版本 2.5.0 关闭了连接,因此只需降级到 2.4.0,直到他们解决问题。

要解决您的问题,请使用pip3 show pymodbus检查两台机器上的版本

使用pip3 uninstall pymodbus从树莓派中卸载pymodbus

pip3 install pymodbus==2.4.0安装旧版本


我在github there is an existing issue reporting this error上发现了这个


创建者已发布针对 this 的修复程序

安装更新版本2.5.1rc1

pip3 install --upgrade pymodbus

【讨论】:

猜你喜欢
  • 2022-10-26
  • 1970-01-01
  • 2022-01-23
  • 2019-07-30
  • 2022-07-18
  • 2020-05-07
  • 2021-12-21
  • 2018-12-15
  • 1970-01-01
相关资源
最近更新 更多