PyModbus 问题

问题描述 投票:0回答:1

我正在尝试使用 python 从服务器设备读取数据。我觉得我的代码应该做一些事情,但它给了我这个错误:

Modbus 错误:[输入/输出] Modbus 错误:[无效消息] 未收到响应,预计至少 4 个字节(收到 0 个)

我非常确定所有参数都是正确的。

这是我正在尝试的代码。

from pymodbus.client import ModbusSerialClient as ModbusClient


# Configure the client with a timeout
client = ModbusClient(
    method='rtu',
    port='COM4',
    stopbits=1,
    bytesize=8,
    parity='E',
    baudrate=115200,
    timeout= 1  # Timeout in seconds
)

# Connect to the client
connection = client.connect()
if connection:
    print("Port Opened...")
else:
    print("Failed to open port")
    client.close()
    exit(1)

# Define the address and quantity to read
address = 36
quantity = 1

# Read holding registers
response = client.read_holding_registers(address, quantity, unit= 1)  # Specify the correct unit

if not response.isError():
    print("Read values:", response.registers)
else:
    print("Failed to read:", response)

# Close the connection
client.close()
python pyserial modbus
1个回答
0
投票

我可以通过将 pyModbus 降级到 2.5.3 来修复它。我知道这不是最好的解决方案,但它能够发送和接收数据。我还将“.sync”添加到“pymodbus.client”。

© www.soinside.com 2019 - 2024. All rights reserved.