Python 使用pymodbus读取寄存器
我正在尝试使用pymodbus从PLC读取modbus寄存器。我遵循上面的例子。当我尝试打印.registers时,我得到以下错误:Python 使用pymodbus读取寄存器,python,modbus-tcp,Python,Modbus Tcp,我正在尝试使用pymodbus从PLC读取modbus寄存器。我遵循上面的例子。当我尝试打印.registers时,我得到以下错误:对象没有属性“registers” 该示例没有显示正在导入的模块,但似乎是公认的答案。我认为错误可能是我导入了错误的模块,或者我缺少了一个模块。我只是想读一个寄存器 这是我的密码: from pymodbus.client.sync import ModbusTcpClient c = ModbusTcpClient(host="192.168.1.20")
对象没有属性“registers”
该示例没有显示正在导入的模块,但似乎是公认的答案。我认为错误可能是我导入了错误的模块,或者我缺少了一个模块。我只是想读一个寄存器
这是我的密码:
from pymodbus.client.sync import ModbusTcpClient
c = ModbusTcpClient(host="192.168.1.20")
chk = c.read_holding_registers(257,10, unit = 1)
response = c.execute(chk)
print response.registers
从读取中,似乎read\u holding\u registers
对象的execute
方法将返回包含错误的响应对象或ExceptionResponse
对象。我猜你收到的是后者。您需要尝试以下方法:
from pymodbus.register_read_message import ReadHoldingRegistersResponse
#...
response = c.execute(chk)
if isinstance(response, ReadHoldingRegistersResponse):
print response.registers
else:
pass # handle error condition here
从读取中,似乎read\u holding\u registers
对象的execute
方法将返回包含错误的响应对象或ExceptionResponse
对象。我猜你收到的是后者。您需要尝试以下方法:
from pymodbus.register_read_message import ReadHoldingRegistersResponse
#...
response = c.execute(chk)
if isinstance(response, ReadHoldingRegistersResponse):
print response.registers
else:
pass # handle error condition here
@J Earls错误消失,但不读取寄存器。这本书读起来不错。您是否发现该代码还有其他错误?您是否尝试过先连接modbus实例?。c、 在读取之前连接()_registers@J厄尔斯错误消失了,但没有读取寄存器。这本书读起来不错。您是否发现该代码还有其他错误?您是否尝试过先连接modbus实例?。c、 在读取\保持\寄存器之前连接()