python pymodbus读取保持寄存器

时间:2015-12-10 10:05:18

标签: python-2.7 modbus

我是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. 如何从寄存器1007中读取2个字?我的代码不起作用:(0x3EF,2,unit = 0)异常响应(131,3,IllegalValue)

  3. 侨, 文件

1 个答案:

答案 0 :(得分:2)

首先我认为你的代码中有一些错误。使用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

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

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

现在回答你的问题:

  1. 该值显示您从服务器读取的寄存器数量。
  2. 见上面的代码。
  3. 希望有所帮助,wewa