【问题标题】:Modbus RTU using pymodbus - unable to read holding register from modbus data使用 pymodbus 的 Modbus RTU - 无法从 modbus 数据中读取保持寄存器
【发布时间】:2018-12-15 17:57:34
【问题描述】:

大家好,我需要帮助

代码:

from pymodbus.client.sync import ModbusSerialClient as ModbusClient
import logging

def readModbusData():
    logging.basicConfig()
    log = logging.getLogger()
    log.setLevel(logging.DEBUG)

    client=ModbusClient(method='rtu',port='COM3',
    baudrate=9600,timeout=1,parity='N',stopbits=2)
    print(client.connect())
    client.debug_enabled()

    log.debug("Read holding registers")
    response=client.read_holding_registers(0,1)

    print(response) #This returns the response for whole length of registers

    # print(response.getRegister(0));  #This returns value of only one 

    client.close()
readModbusData()

【问题讨论】:

  • 控制台日志:DEBUG:pymodbus.transaction:SEND: 0x0 0x3 0x0 0x0 0x0 0x1 0x85 0xdb DEBUG:pymodbus.client.sync:New Transaction state 'SENDING' DEBUG:pymodbus.transaction:Changing transaction state从“发送”到“等待回复” Modbus 错误:[输入/输出] Modbus 错误:[无效消息] 收到不完整的消息,预计至少 2 个字节(收到 0 个)
  • 我建议您使用(而不是您的 python 代码)CAS Modbus Scanner(免费)或 Modbus Poll(免费试用)来测试 Modbus 设备。两者都是我知道工作的 Modbus 主/客户端工具。如果您的它可以与这些工具一起使用,那么您的问题出在您的代码中;如果它不能与它们一起工作,则问题出在 Modbus 从设备上。
  • 需要传递设备地址response=client.read_holding_registers(0,1, unit=<your slave address here>)
  • 它与 minimummodbus 一起工作。感谢大家的回复。
  • @AshishBatra 我发布了一个答案,希望对您有所帮助

标签: python python-3.x modbus pymodbus


【解决方案1】:

检查这个stack_post

您忘记了unit 参数并使用print(response.registers) 而不是print(response)

你应该有如下的 sn-p 代码:

response = client.read_holding_registers(0, 1, unit=<set-the-unit-ID>)  # Note.

if not response.isError():
    '''isError() method implemented in pymodbus 1.4.0 and above'''

    print(response.registers)  # Note.

else:
    # Handle Error.
    print('Unable to read or there is the connection problem.')

[注意]:

  • 在许多情况下,unit 在从属端默认为1

【讨论】:

    猜你喜欢
    • 2015-03-08
    • 2019-05-14
    • 1970-01-01
    • 2023-02-08
    • 2021-12-07
    • 2021-04-20
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    相关资源
    最近更新 更多