简体   繁体   中英

After read holding registers with Pymodbus connection is broken in Raspberry Pi

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)

This code is working on Windows. But i run it in raspberry pi "client.is_socket_open" is return false after read holding registers. How can i solve this? Or what is the problem in here. I dont force the client to close.

After a few test I can replicate the problem on the windows machine, and it's related to the pymodbus version. The connection is closed with the pymodbus version 2.5.0, so just downgrade to 2.4.0 until they solve the problem.

To solve your problem check the version on both the machines with pip3 show pymodbus

Uninstall pymodbus from the raspberry with pip3 uninstall pymodbus

Install the old version with pip3 install pymodbus==2.4.0


I have found that on github there is an existing issue reporting this error


The creator has released a fix for this

Install the updated version 2.5.1rc1

pip3 install --upgrade pymodbus

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM