首页 文章

Modbus Slave没有回应

提问于
浏览
3

我正在尝试使用Raspberry Pi 3B(运行Ubuntu Mate 16.04操作系统)作为Master来读取支持Modbus-RTU协议的电能表的值 .

我使用RS232 / USB适配器和RS485 / RS232适配器连接仪表和Raspberry Pi上的USB端口 . 我已经尝试了 modbus_tk 0.5.7MinimalModbus 来实现 Modbus-RTU 协议下的通信 .

The meter reading system based on the Modbus-RTU protocol

当我使用 modbus_tk 0.5.7 并运行以下代码时:

import sys import serial

#add logging capability import logging import modbus_tk import modbus_tk.defines as cst import modbus_tk.modbus_rtu as modbus_rtu

logger = modbus_tk.utils.create_logger("console")
if __name__ == "__main__":
    try:
        #Connect to the slave
        master = modbus_rtu.RtuMaster(serial.Serial(port="/dev/ttyUSB0", baudrate=9600, bytesize=8, parity='N', stopbits=1, xonxoff=0))
        master.set_timeout(5.0)    #Change the timeout value/Defines a timeout on the MAC layer
        master.set_verbose(True)   #print some more log prints for debug purpose
        logger.info("connected")

        logger.info(master.execute(1, cst.READ_HOLDING_REGISTERS, 0, 49))

    except modbus_tk.modbus.ModbusError, e:
        logger.error("%s- Code=%d" % (e, e.get_exception_code()))

The parameters such as port, baudrate, bytesize,parity,and stopbits were set correctly, but it always returns this:

2017-08-10 19:24:34,282 INFO    modbus_rtu.__init__ MainThread  RtuMaster /dev/ttyUSB0 is opened
2017-08-10 19:24:34,283 INFO    rtumaster_example.<module>  MainThread  connected
2017-08-10 19:24:34,284 DEBUG   modbus.execute  MainThread  -> 1-3-0-0-0-49-132-30
2017-08-10 19:24:39,291 DEBUG   modbus.execute  MainThread  <-
Traceback (most recent call last):
  File "rtumaster_example.py", line 34, in <module>
    logger.info(master.execute(1, cst.READ_HOLDING_REGISTERS, 0, 49))
  File "build/bdist.linux-x86_64/egg/modbus_tk/utils.py", line 39, in new
modbus_tk.exceptions.ModbusInvalidResponseError: Response length is invalid 0

当我使用 MinimalModbus 并运行以下代码时:

#!/usr/bin/env python
import minimalmodbus

instrument.serial.port='/dev/ttyUSB0'          # this is the serial port name
instrument.serial.baudrate = 9600   # Baud
instrument.serial.bytesize = 8
instrument.serial.parity   = serial.PARITY_NONE
instrument.serial.stopbits = 1
instrument.serial.timeout  = 0.05   # seconds

#instrument.address     # this is the slave address number
instrument.mode = minimalmodbus.MODE_RTU   # rtu or ascii mode
instrument = minimalmodbus.Instrument('/dev/ttyUSB0', 1) # port name, slave address (in decimal)
energy = instrument.read_register(10, 1) # Registernumber, number of decimals
print energy

它总是返回:

raise IOError('No communication with the instrument (no answer)')
IOError: No communication with the instrument (no answer)

然后我使用相同的串行传输线连接仪表和笔记本电脑,并使用在Windows XP上运行的调试工具,该工具由仪表制造商开发 . 调试工具发送与以前相同的Request (1-3-0-0-0-49-132-30)but the debugging tool can get correct Responses. (Maybe it's because it ignored some incorrect Responses and keep on send Requests regularly) And it can represent that the Request message is correct and the connection of serial transmission has no problem.

我还使用CuteCom(一个图形串行终端)和RS232 / USB适配器来确认 the USB port can send and receive correctly. 在两条RS485线路之间添加电阻也没用

我已经尝试了很多次,但Raspberry Pi从未得到响应,并且总是返回相同的错误信息 . 我也尝试在Ubuntu虚拟机上运行相同的代码,它返回与上面相同的消息,并且永远不会得到响应 .

我是Modbus和串口通信的新手,所以任何帮助都将不胜感激 .

3 回答

  • 1

    我通过使用更昂贵的USBtoRS485连接器解决了我的问题 . 这个问题花了我很多时间来尝试不同的库和不同的代码 . 事实证明我买的适配器“QinHeng Electronics HL-340 usb-serial”在Windows上运行良好但不适用于Linux . 它可以在Linux上实现发送和接收消息,但它不能支持Modbus通信 .

    所以我建议你买一个更昂贵的连接器,它可以节省你的大量时间和精力 .

    这就是全部,谢谢!

  • 0

    我遇到了同样的问题 . 使用mbpoll工具后我意识到奇偶校验是错误的 . 刚更新到parity.EVEN,现在还可以 .

  • 1
    • 尝试将电缆的RS232端连接到带串口的PC(如果你有的话),以确保usb / rs232正常工作

    • 如果您有示波器(如fluke),您可以观察RS485线(实际上并不困难)了解问题所在 .

    • 在我看来,当你打开端口和驱动程序配置时,可以为Windows配置2个位置的串行参数(如波特率) .

    • 尝试均衡所有设备的潜力 . 我知道rs485应该对它免疫,但rs232不是

相关问题