python - Modbus 错误 : [Input/Output] No Response received from the remote unit

标签 python modbus pymodbus pymodbus3

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

这是我的代码:

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_inputsread_holding_registers。有什么帮助吗?

谢谢

最佳答案

在使用 modbus 时,即使您使用的是高级库,将官方 modbus documentation 放在手边总是很有用的. 通过文档,您可以检查帧的每个字节的含义:

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

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

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

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

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

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

这意味着您要从内存中的第一个位置 (0x00, 0x00) 来自地址为 1 (0x01) 的设备。

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

这就是说,我们可以检查为什么您没有从您的设备收到答复。为此,如果您不确定您的代码/您所询问的内容/您的设备的行为方式,您可以使用外部工具来比较结果。我建议你考虑一下 docklight ,这有助于您设置连接和发送/接收 modbus 帧。

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

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

Method is correct, since is the protocol you're asking for.

The port is correct, otherwise it gives back a system error.

Timeout is what raise the error: in a given time, no response was received. Anyway the problem is probably not here, since you set a high value for timeout.

Stopbits should not interfere with the frame reception.

The problem could be in baudrate and parity: an error here can cause the error raised by your code.

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

如果我要打赌,我会先用 None (parity = 'N') 替换 Even parity。

如果问题仍然存在,则设备地址 (0x01) 可能有误。地址可以表示为 0 (0x00) 到 255 (0xFF) 之间的值。

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

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

关于python - Modbus 错误 : [Input/Output] No Response received from the remote unit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47817634/

相关文章:

python - Pandas groupby 累计总和

c - Arduino的MODBUS协议(protocol)

python - 如何从 Modbus Slave 读取所有保持寄存器?

python - 读/写时 modbus 错误无法连接到 ModbusTcpClient

python - 如何将多处理与多线程一起使用?

python - 构建 LXML。预编译的 .c Cython 文件丢失

python - 如何将数据保存到 csv Cantera 和错误 <cantera.composite.SolutionArray object at 0x7f4badca0fd0>

python - pymodbus 异常响应(131、3、非法地址)

python - 如何通过 TCP 使用 pymodbus 读取/写入连接到网关的设备寄存器

docker - pymodbus + docker 连接问题