python pymodbus读取保持寄存器

Sch*_*ack 3 modbus python-2.7

我是 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()
Run Code Online (Sandbox Code Playgroud)

结果:

============= RESTART: D:\Users\mxbruckn\Desktop\read_modbus.py =============
ReadRegisterResponse (38)
0
0
>>> 
Run Code Online (Sandbox Code Playgroud)

现在的问题:

  1. 我从寄存器 1000, 10 Words, with slave number 0 中读取。这是正确的,但是值 38 是什么意思

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

曹医生

wew*_*ewa 6

首先,我认为您的代码中有一些错误。使用 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
Run Code Online (Sandbox Code Playgroud)

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

ReadRegisterResponse (10)
[17, 17, 17, 17, 17, 17, 17, 17, 17, 17]
ReadRegisterResponse (2)
[17, 17]
Run Code Online (Sandbox Code Playgroud)

现在回答你的问题:

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

希望有帮助,wewa