Python 2.7 python pymodbus读保持寄存器
我是Modbus python新手,现在我对我的第一步有一些问题 剧本:Python 2.7 python pymodbus读保持寄存器,python-2.7,modbus,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/
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
>>>
现在问题是:
Doc首先,我认为您的代码中有一些错误。对于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
现在回答你的问题:
希望对您有所帮助,wewa通常,当您试图访问无效的寄存器地址或偏移量时,会出现非法值错误,请查看您是否真的在0x3EF、0x3F0和0x3F1处有寄存器,ReadRegisterResponse中的38是从您的请求返回的寄存器的长度。
ReadRegisterResponse (10)
[17, 17, 17, 17, 17, 17, 17, 17, 17, 17]
ReadRegisterResponse (2)
[17, 17]