【问题标题】:Modbus Error: [Input/Output] No Response received from the remote unitModbus 错误:[输入/输出] 未收到来自远程单元的响应
【发布时间】:2018-05-28 19:05:41
【问题描述】:

我正在尝试使用串行连接从我的 Mac 笔记本电脑连接到 Modbus 设备 (MR-SI4),该设备使用“安装”到 /dev/cu.SLAB_USBtoUART 的 USB RS485 转换器。

这是我的代码:

import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)

from pymodbus.constants import Endian
from pymodbus.constants import Defaults
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
from pymodbus.transaction import ModbusRtuFramer

# settings for USB-RS485 adapter
SERIAL = '/dev/cu.SLAB_USBtoUART'
BAUD = 19200

# set Modbus defaults

Defaults.UnitId = 1
Defaults.Retries = 5

client = ModbusClient(method='rtu', port=SERIAL, stopbits=1, bytesize=8, timeout=3, baudrate=BAUD, parity='E')

connection = client.connect()
print "Readout started"

#result = client.read_discrete_inputs(0)
#result = client.read_holding_registers(12,19)
result = client.read_input_registers(0,1)
print(result)

控制台的输出是:

$ sudo python test.py 
Readout started
DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:send: 0x1 0x4 0x0 0x0 0x0 0x1 0x31 0xca
DEBUG:pymodbus.client.sync:will sleep to wait for 3.5 char
DEBUG:pymodbus.transaction:recv: 
DEBUG:pymodbus.transaction:getting transaction 1
Modbus Error: [Input/Output] No Response received from the remote unit

我很想在理解我遇到的错误方面得到一点帮助。我尝试了pymodbus 的配置以及read_discrete_inputs 或read_holding_registers 等不同的功能。有什么帮助吗?

谢谢

【问题讨论】:

  • 日志显示,远程单元没有为您的请求返回任何响应,如果您看到带有 DEBUG:pymodbus.transaction:recv: 的行,那么在 3 秒的读取时间内您什么也得不到。我会说检查你的连接(接地)并仔细检查奇偶校验和波特率。

标签: python modbus pymodbus pymodbus3


【解决方案1】:

在使用 modbus 时,即使您使用的是高级库,手头上的官方 modbus documentation 也总是有用的。 通过文档,您可以检查帧的每个字节的含义:

0x01 0x04 0x00 0x00 0x00 0x01 0x31 0xCA

第一个字节是设备地址(0x01)

第二个字节为功能码(0x04, Read Input Registers)

第三和第四个字节为起始位置(0x00, 0x00)

第五和第六字节为输出数量(0x00, 0x01)

最后2个字节是CRC控制(0x31, 0xCA)

这意味着您从地址为 1 的设备(0x01) 的内存中第一个位置(0x00, 0x00) 的输入寄存器(0x04) 中请求一个(0x00, 0x01) 寄存器。

帧本身是正确的,如果设备软件/固件遵循 modbus 标准,您应该有一个答案:您要求的寄存器或错误帧(0x01, 0x80, crc, crc)。

也就是说,我们可以检查您为什么没有从您的设备收到答复。为此,如果您不确定您的代码/您的要求/设备的行为方式,您可以使用外部工具来比较结果。我建议你像docklight 这样的想法,它可以帮助你设置连接和发送/接收 modbus 帧。

我首先要检查的是连接参数:

client = ModbusClient(method='rtu', port=SERIAL, stopbits=1, bytesize=8, timeout=3, baudrate=BAUD, parity='E')

方法是正确的,因为是您要求的协议。

端口正确,否则返回系统错误。

超时是引发错误的原因:在给定时间内,没有收到响应。无论如何,问题可能不在这里,因为您为超时设置了一个高值。

停止位不应干扰帧接收。

问题可能在于波特率和奇偶校验:这里的错误可能会导致您的代码引发错误。

如果您不知道波特率和/或奇偶校验的正确值,您可以尝试使用most commons baudrates 和奇偶校验值:'N', 'E', 'O', 'M', 'S'(代表:无、偶数、奇数、标记、空格。默认为无)。

如果我必须打赌,我会先用无 (parity = 'N') 替换偶校验。

如果仍有问题,可能是设备地址(0x01)有误。地址可以表示为从 0 (0x00) 到 255 (0xFF) 的值。

按照协议标准,即使起始地址(0x00, 0x00)、输出数量(0x00, 0x01)或crc(0x31, 0xCA)错误,设备也应该有响应,但并不总是这样:如果您认为自己处于这种情况,请研究设备的特定文档。

最后一种可能是使用低级库,例如PySerial,然后您定义自己的协议。

【讨论】:

  • 这有助于我调试。谢谢!
【解决方案2】:

检查这个Stack_post。

您可以处理错误:

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

    print(result.registers)  # Note.

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

[注意]:

  • 在许多情况下,RTU 奇偶校验为无:parity='N'
  • 确保您的串行端口 (/dev/cu.SLAB_USBtoUART) 具有 root 权限。

【讨论】:

    【解决方案3】:

    我可以通过将常量 RetryOnEmpty 设置为 True 来解决这个问题。

    from pymodbus.constants import Defaults
    Defaults.RetryOnEmpty = True
    

    配置超时和一些重试也可能很有用。

    Defaults.Timeout = 5
    Defaults.Retries = 5
    

    https://pymodbus.readthedocs.io/en/v1.3.2/library/constants.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-04
      • 1970-01-01
      • 2020-03-24
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      相关资源
      最近更新 更多