gpt4 book ai didi

python - 如何使用 pymodbus 读取寄存器

转载 作者:行者123 更新时间:2023-11-28 17:05:56 33 4
gpt4 key购买 nike

如何从 Register with pymodbus 中读取数据?

我正在尝试这段代码:

import pymodbus
from pymodbus.pdu import ModbusRequest
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
#initialize a serial RTU client instance
from pymodbus.transaction import ModbusRtuFramer

#count= the number of registers to read
#unit= the slave unit this request is targeting
#address= the starting address to read from

client = ModbusClient(method = 'rtu', port='/dev/extcomm/0/0', stopbits = 1, bytesize = 8, parity = 'N' , baudrate= 38400)

#Connect to the serial modbus server
connection = client.connect()
print(connection)

#Starting add, num of reg to read, slave unit.
coil = client.read_holding_registers(0x01,1,unit=1)# address, count,
slave address
print(coil)

#Closes the underlying socket connection
client.close()

这给了我这个错误:

    Modbus Error: [Input/Output] Modbus Error: [Invalid Message] Incomplete message received, expected at least 2 bytes (0 received)

我的slave id是1号,我要读的寄存器也是1号,但不知道为什么还是不行。

最佳答案

from pymodbus.client.sync import ModbusSerialClient as ModbusClient

client = ModbusClient(method='rtu', port='COM4', baudrate=2400, timeout=1)

client.connect()
read=client.read_holding_registers(address = 222 ,count =10,unit=1)
//Address is register address e.g 30222,
//and count is number of registers to read,
//so it will read values of register 30222 to 30232
//unit is slave address, for 1 device leave it 1

data=read.registers[int(2)] #reading register 30223
print(data) #printing value read in above line

关于python - 如何使用 pymodbus 读取寄存器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51056141/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com