AttributeError:“ExceptionResponse”对象在pymodbus中没有属性“registers”

2024-05-16 16:12:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从SMA STP6.0太阳能逆变器读取温度,使用基于pymodbus的代码,使用modbus TCP协议。在

首先,我从https://pymodbus.readthedocs.io/en/latest/source/example/sunspec_client.html运行Sunspec客户机示例。此代码失败,并显示以下错误消息:

AttributeError: 'ExceptionResponse' object has no attribute 'registers'

我在Ubuntu上使用Python3运行示例代码。在

DEBUG:__main__:reading device block[40000..40002]
Traceback (most recent call last):
  File "/home/pi/scripts/sma-modbus.py", line 301, in <module>
    client = create_sunspec_sync_client("192.168.178.110")
  File "/home/pi/scripts/sma-modbus.py", line 179, in create_sunspec_sync_client
    client.initialize()
  File "/home/pi/scripts/sma-modbus.py", line 225, in initialize
    decoder  = self.get_device_block(self.offset, 2)
  File "/home/pi/scripts/sma-modbus.py", line 266, in get_device_block
    return SunspecDecoder.fromRegisters(response.registers)
AttributeError: 'ExceptionResponse' object has no attribute 'registers'

我研究过这个问题,但没有找到解决办法。如有任何帮助/建议,我们将不胜感激。在


Tags: 代码inpyclienthomedevicelinescripts
1条回答
网友
1楼 · 发布于 2024-05-16 16:12:33

您使用的脚本似乎不支持您的设备。这就是你得到一个错误的原因,你试图读取一个还没有定义的Modbus地址(实际上看起来你和你的设备没有连接,如果你读错了地址,你会得到一个不同的错误,所以首先要确保你有一个与你的设备的连接,并且你的端口502打开且未过滤)。在

在任何情况下,Sunspec客户端脚本只从设备读取信息,您应该能够回收大部分信息。在

您可以尝试以下操作从设备读取内部温度:

#                                      - #
# import the various server implementations
#                                      - #
from pymodbus.client.sync import ModbusTcpClient as ModbusClient

#                                      - #
# configure the client logging
#                                      - #
import logging
FORMAT = ('%(asctime)-15s %(threadName)-15s '
          '%(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
logging.basicConfig(format=FORMAT)
log = logging.getLogger()
log.setLevel(logging.DEBUG)

#                                     #
# Define UNIT ID
#                                     #
UNIT = 126  # default according to the manual, see below

#                                     #
# Define client
#                                     #
client = ModbusClient('localhost', port=502)
client.connect()

#                                    - #
# Read temperature
#                                    - #
rr = client.read_holding_registers(219, 1, unit=UNIT) #see documentation linked below
print(rr.registers[0])

#                                    - #
# close the client
#                                    - #
client.close()

如果您有默认的设备单元ID,这个脚本应该可以工作

如果您得到一个error: Modbus address does not exist,则表示由于某种原因,您的设备具有不同的单元ID。您可以使用相同的脚本读取地址,更改以下行:

^{pr2}$

我从prodcut documentation site取了单元ID和寄存器号。这是我用的info package。还有一个software section,你可能想检查一下(我想你在里面找不到任何源代码)。在

如果您想读取int以外的值(例如Power drawing,它是unsigned32类型),那么您应该能够从Sunspec客户机回收{}。在

请注意,根据这些设备的Modbus映射,您必须使用read_input_registers作为制造商所称的SMA Modbus配置文件(默认设备ID 3),而{}用于Sunspec Modbus配置文件(默认设备ID 126)。在

作为最后的免责声明,我自己从来没有使用过这些逆变器,所以我的回答只是基于对文档的快速回顾。这意味着我不能保证我的答案100%准确,但我希望它会有用。在

编辑:我忘了提到,当您不确定单元id或寄存器时,最好使用QModMaster这样的工具。这将允许你快速检查设置在几次点击。一旦你确定什么是有效的,你可以继续你的代码。在

相关问题 更多 >