相关文章推荐
性感的香蕉  ·  java执行linux命令 - ...·  1 年前    · 
多情的莴苣  ·  CenterOS(7.8)安装TypeScr ...·  1 年前    · 
朝气蓬勃的煎鸡蛋  ·  [1091]. ...·  1 年前    · 
越狱的人字拖  ·  Python try except ...·  1 年前    · 
喝醉的骆驼  ·  cmake 编译 ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have some trouble trying to get info from a device using the following code:

from pymodbus.client.sync import ModbusSerialClient as ModbusClient
# mbClient = ModbusClient(method = "rtu", port="COM4", stopbits = 1, bytesize = 8, parity = 'N', baudrate = 9600)
mbClient = ModbusClient(method = "rtu", port="COM4")
mbClient.connect()
totalEnergy = mbClient.read_holding_registers(0x0000, 2, unit=1)
print(totalEnergy.registers)
mbClient.close()

It shows the error I put in the title. The 0x0000 address should be the hour day etc according to the manual. Here you can see the hour address that appears in the manual

Thanks and I wish to have luck finding a solution to my problem.

Use the code below instead of print(totalEnergy.registers) + enable Modbus logging, to dealing with Modbus error throwing:

import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
# Your code body ...
if not totalEnergy.isError():
    '''isError() method implemented in pymodbus 1.4.0 and later'''
    print(totalEnergy.registers)
else:
    # Do stuff to error handling.
    print('Error message: {}'.format(totalEnergy))

And check mbClient.connect() return and its input arguments.

These are all of the things which you could manage it on client-side.

Thus, check your Modbus server/slave side also.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.