如何从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)
我的从设备ID是1,我想读取的寄存器也是1,但是我不知道为什么它仍然不起作用。
答案 0 :(得分: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
答案 1 :(得分:0)
此代码也适用于我:
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
client = ModbusClient(method='rtu', port='COM5', baudrate=9600, timeout=0.5)
client.connect()
read=modb.read_holding_registers(address = 0x00,
count =65,
unit=1)
data=read.registers[int(64)] #read register id 64
print(data) #print register data
也许您应该尝试以下方法:
print(coil.registers)