Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Modbus从站不响应_Python_Ubuntu_Raspberry Pi_Serial Port_Modbus - Fatal编程技术网

Python Modbus从站不响应

Python Modbus从站不响应,python,ubuntu,raspberry-pi,serial-port,modbus,Python,Ubuntu,Raspberry Pi,Serial Port,Modbus,我尝试使用Raspberry Pi 3B(运行Ubuntu Mate 16.04操作系统)作为主控设备,从支持Modbus RTU协议的电能表读取值 我使用了一个RS232/USB适配器和一个RS485/RS232适配器来连接仪表和Raspberry Pi上的USB端口。我已经尝试了modbus_tk 0.5.7和最小modbus来实现modbus RTU协议下的通信 当我使用modbus_tk 0.5.7并运行以下代码时: import sys import serial #add log

我尝试使用Raspberry Pi 3B(运行Ubuntu Mate 16.04操作系统)作为主控设备,从支持Modbus RTU协议的电能表读取值

我使用了一个RS232/USB适配器和一个RS485/RS232适配器来连接仪表和Raspberry Pi上的USB端口。我已经尝试了modbus_tk 0.5.7最小modbus来实现modbus RTU协议下的通信

当我使用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()))
#!/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
端口、波特率、字节大小、奇偶校验和停止位等参数设置正确,但它始终返回以下信息:

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
它总是返回以下内容:

    raise IOError('No communication with the instrument (no answer)')
IOError: No communication with the instrument (no answer)
然后,我使用相同的串行传输线将电表和笔记本电脑连接起来,并使用运行在Windows XP上的调试工具,该工具是由电表制造商开发的。调试工具发送与以前相同的请求(1-3-0-0-0-49-132-30),但调试工具可以得到正确的响应。(可能是因为它忽略了一些不正确的响应,并定期发送请求),它可以表示请求消息是正确的,串行传输的连接没有问题。

我还使用了CuteCom(图形串行终端)和RS232/USB适配器来确认USB端口可以正确发送和接收。在两条RS485线之间添加一个电阻器也是无用的

我尝试过很多次,但是Raspberry Pi从未得到响应,总是返回相同的错误信息。我还尝试在Ubuntu虚拟机上运行相同的代码,它返回与上面相同的消息,但从未得到响应


我不熟悉Modbus和串行通信,因此如果您有任何帮助,我将不胜感激。

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

所以我建议你买一个更贵的连接器,这样可以节省你很多时间和精力


就这些,谢谢你

我也有同样的问题。使用mbpoll工具后,我意识到奇偶校验是错误的。刚刚更新到parity.EVEN,现在一切正常。

  • 尝试将电缆的RS232端连接到带有串行端口(若你们有ofc)的PC上,以确保usb/RS232工作正常
  • 如果你有示波器(比如fluke),你可以观察RS485线路(事实上并不困难),了解问题出在哪里
  • 我碰巧发现串行参数(如波特率)可以在Windows的两个位置进行配置—当您打开端口时,在驱动程序配置中
  • 尝试平衡所有设备的电位。我知道rs485应该是免疫的,但rs232不是

    • 如果Pi的超时时间太短,请尝试将其增加到100毫秒,它应该可以工作


      编辑:根据下面评论中的要求,我将提供我建议的更多细节。根据问题的详细信息以及随后的回答和评论,硬件设置似乎是正确的。我经常看到这种情况:当你在一侧设置一个非常短的超时时,链接突然停止工作(这也是有道理的,当你在你用timeout参数确定的时间范围内没有收到答复时,就会发生超时错误),当你再次增加它时,它就会恢复活动状态。我还注意到,这种效果取决于硬件,有些设备的响应速度比其他设备快

      您能否为您的回答添加一些解释,以便其他人可以从中学习?OP已经回答说他又买了一条电缆,那么是什么让你认为“暂停时间太短”是个问题呢?