AttributeError: 'ModbusSerialClient' object has no attribute 'last_response'
时间: 2023-06-21 12:23:05
浏览: 115
很抱歉,我的回答中有误。实际上,`ModbusSerialClient` [对象](https://geek.csdn.net/educolumn/04c51611e4b730957464192e0307b82c?spm=1055.2569.3001.10083)没有 `last_response` 属性。如果要获取完整的 Modbus 报文,可以使用 `ModbusSerialClient` [对象](https://geek.csdn.net/educolumn/04c51611e4b730957464192e0307b82c?spm=1055.2569.3001.10083)的 `_last_frame` 属性。以下是修改后的示例[代码](https://geek.csdn.net/educolumn/1572ef9b473b4e00f6b2bf6d428b7c27?spm=1055.2569.3001.10083):
```python
from pymodbus.client.sync import ModbusSerialClient
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
# 创建Modbus串口客户端
client = ModbusSerialClient(method='rtu', port='/dev/ttyUSB0', baudrate=9600)
# 连接到Modbus设备
client.connect()
# 读取Modbus[信号](https://geek.csdn.net/educolumn/05057486f43155154a04d7d84a955d04?spm=1055.2569.3001.10083)
response = client.read_holding_registers(address=0, count=2, unit=1)
# 解码Modbus[信号](https://geek.csdn.net/educolumn/05057486f43155154a04d7d84a955d04?spm=1055.2569.3001.10083)
decoder = BinaryPayloadDecoder.fromRegisters(response.registers, Endian.Big)
data = decoder.decode_32bit_float()
# 获取完整的Modbus报文
full_message = client._last_frame
# 断开Modbus连接
client.
```