首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用pymodbus读取寄存器值

无法使用pymodbus读取寄存器值
EN

Stack Overflow用户
提问于 2020-12-18 00:37:31
回答 1查看 58关注 0票数 0

当我尝试以下代码时,我可以连接到智能电表,但我无法读取注册内容。

代码语言:javascript
复制
import traceback
from pymodbus.client.sync import ModbusTcpClient
client = ModbusTcpClient('192.168.2.1', port=502)
client.connect()

try:
    res = client.read_holding_registers(0x0063, 1, unit=1)
    print(res.registers)

except:
    traceback.print_exc()
    
client.close()

给出了一个异常

代码语言:javascript
复制
  Traceback <Most recent call last>
    File "m2.py", Line 8 ,in <module>
    print<res.registers>
AttributeErro: 'ModbusIOException' objecthas noattribute 'registers'    

请帮我解决这个问题

EN

回答 1

Stack Overflow用户

发布于 2020-12-19 13:20:00

您可能在此处错过了client.connect

代码语言:javascript
复制
from pymodbus.client.sync import ModbusTcpClient
client = ModbusTcpClient(host='192.168.2.1', port=502)
client.connect() ### Added here

try:
   result = client.read_holding_registers(0x0063,1, unit=1)
   print (result.registers)

except Exception as e:
   print (e)

client.close()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65344754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档