python pymodbus 读取保持寄存器

python pymodbus read holding registers

我是 Modbus 新手 python,现在我对第一步有一些疑问

脚本:

from pymodbus.client.sync import ModbusTcpClient

host = '10.8.3.10'
port = 502   

client = ModbusTcpClient(host, port)
client.connect()

#Register address 0x102A (4138dec) with a word count of 1
#Value - MODBUS/TCP Connections
#Access - Read
#Description - Number of TCP connections

request = client.read_holding_registers(0x3E8,10,unit=0) 
response = client.execute(request)

print response
#print response.registers
print response.getRegister(12)
print response.registers[8]
client.close()

结果:

============= RESTART: D:\Users\mxbruckn\Desktop\read_modbus.py =============
ReadRegisterResponse (38)
0
0
>>> 

现在问题:

  1. 我从寄存器 1000 读取,10 个字,从站编号为 0。这是正确的吗,但是值 38

  2. 是什么意思
  3. 如何从寄存器 1007 中读取 2 个字?我的代码不起作用:(0x3EF,2, unit=0) Exception Response(131, 3, IllegalValue)

再见, 博士

首先我认为你的代码有一些错误。使用 pymodbus 1.2.0,代码应如下所示:

from pymodbus.client.sync import ModbusTcpClient

host = 'localhost'
port = 502 

client = ModbusTcpClient(host, port)
client.connect()

rr = client.read_holding_registers(0x3E8,10,unit=0)
assert(rr.function_code < 0x80)     # test that we are not an error
print rr
print rr.registers


# read 2 registers starting with address 1007
rr = client.read_holding_registers(0x3EF,2,unit=0)
assert(rr.function_code < 0x80)     # test that we are not an error
print rr
print rr.registers

这是输出(请注意,我用 17 在 modbusserver 上实例化了数据存储):

ReadRegisterResponse (10)
[17, 17, 17, 17, 17, 17, 17, 17, 17, 17]
ReadRegisterResponse (2)
[17, 17]

现在回答您的问题:

  1. 该值显示您从服务器读取的寄存器数量。
  2. 见上面的代码。

希望对您有所帮助,wewa