最小的 Modbus IO 错误

MinimalModbus IO error

我正在尝试通过我的 windows 机器 (windows 10) 的 Modbus RTU-半双工 RS-485 连接与自定义板通信。我正在使用 python MinimalModbus API 并安装了所有依赖库。我还有合适的 RS-422/485 适配器。我知道波特率、COM 端口、从地址和奇偶校验位都是正确的。我也知道我的代码正在从 O 范围跟踪输出比特流。由于某种原因,董事会根本不会回应。任何帮助将不胜感激。

import minimalmodbus
import serial 

#minimalmodbus.CLOSE_PORT_AFTER_EACH_CALL = True 
minimalmodbus.PARITY = serial.PARITY_EVEN
#minimalmodbus.handle_local_echo=True
minimalmodbus.BAUDRATE = 57600
minimalmodbus.TIMEOUT = .01
instrument = minimalmodbus.Instrument('COM4',0)
instrument.debug = True
print(instrument.read_register(11,1))

MinimalModbus debug mode. Writing to instrument (expecting 7 bytes back): '\x00\x03\x00\x0b\x00\x01ô\x19' (00 03 00 0B 00 01 F4 19)
MinimalModbus debug mode. No sleep required before write. Time since previous read: 1478637162864.7 ms, minimum silent period: 0.67 ms.
MinimalModbus debug mode. Response from instrument: '' () (0 bytes), roundtrip time: 10.9 ms. Timeout setting: 10.0 ms.

Traceback (most recent call last):
  File "RS485.py", line 11, in <module>
    print(instrument.read_register(11,1))
  File "C:\Users\mtangy\AppData\Local\Programs\Python\Python35-32\lib\site-packages\minimalmodbus.py", line 258, in read_register
    return self._genericCommand(functioncode, registeraddress, numberOfDecimals=numberOfDecimals, signed=signed)
  File "C:\Users\mtangy\AppData\Local\Programs\Python\Python35-32\lib\site-packages\minimalmodbus.py", line 697, in _genericCommand
    payloadFromSlave = self._performCommand(functioncode, payloadToSlave)
  File "C:\Users\mtangy\AppData\Local\Programs\Python\Python35-32\lib\site-packages\minimalmodbus.py", line 795, in _performCommand
    response = self._communicate(request, number_of_bytes_to_read)
  File "C:\Users\mtangy\AppData\Local\Programs\Python\Python35-32\lib\site-packages\minimalmodbus.py", line 930, in _communicate
    raise IOError('No communication with the instrument (no answer)')
OSError: No communication with the instrument (no answer)

问题出在以下行:

instrument = minimalmodbus.Instrument('COM4',0)

您正在指示 minimalmodbus 获取 modbus id 为零的仪器。这是一个无效的 ID。

检查您的仪器设置并更正代码中的 ID。

尝试设置slave id instrument = minimalmodbus.Instrument('COM4',1) ,其中 1 是您自定义仪器的从站 ID。

并尝试通过此行增加超时参数

minimalmodbus.TIMEOUT = 1 其中 1 等于 1000 毫秒。