繁体   English   中英

使用 Python(pymodbus) 通过 UDP/IP 读取数据 MODBUS

[英]Read Data MODBUS over UDP/IP using Python(pymodbus)

有一种设备使用 Modbus 协议发送信息。 我正在使用以下 python 代码来接收此设备的信息:

from pymodbus.client.sync import ModbusTcpClient,ModbusUdpClient
from pymodbus.transaction import ModbusRtuFramer as ModbusFramer

client = ModbusUdpClient("109.125.128.232", port=50450, framer=ModbusFramer)
success = client.connect()

print(success)

read = client.read_holding_registers(address=100, unit=2)

我收到以下错误:

Traceback (most recent call last):
  File "C:\Users\hossein\Desktop\modebuspool\modebus.py", line 9, in <module>
    read = client.read_holding_registers(address=100, unit=2)
  File "C:\Users\hossein\AppData\Local\Programs\Python\Python37\lib\site-packages\pymodbus\client\common.py", line 114, in read_holding_registers
    return self.execute(request)
  File "C:\Users\hossein\AppData\Local\Programs\Python\Python37\lib\site-packages\pymodbus\client\sync.py", line 109, in execute
    return self.transaction.execute(request)
  File "C:\Users\hossein\AppData\Local\Programs\Python\Python37\lib\site-packages\pymodbus\transaction.py", line 178, in execute
    broadcast=broadcast
  File "C:\Users\hossein\AppData\Local\Programs\Python\Python37\lib\site-packages\pymodbus\transaction.py", line 274, in _transact
    size = self._send(packet)
  File "C:\Users\hossein\AppData\Local\Programs\Python\Python37\lib\site-packages\pymodbus\transaction.py", line 309, in _send
    return self.client.framer.sendPacket(packet)
  File "C:\Users\hossein\AppData\Local\Programs\Python\Python37\lib\site-packages\pymodbus\framer\rtu_framer.py", line 263, in sendPacket
    timeout = start + self.client.timeout
TypeError: unsupported operand type(s) for +: 'float' and 'NoneType'

您是否 100% 确定需要使用ModbusRtuFramer

你应该考虑到这一点

...
It should be noted that although you are not limited to trying whatever
would like, the library makes no guarantees that all framers with
all transports will produce predictable or correct results (for example
tcp transport with an RTU framer). However, please let us know of any
success cases that are not documented!
"""

在某些情况下,您可能需要通过 TCP 发送 Modbus RTU 帧(例如一些串行到以太网的转发器),请参阅此线程以获取更多详细信息。

如果你真的想通过 TCP 使用 Modbus RTU 成帧器,你可能想尝试像这样实例化你的客户端:

client = ModbusTcpClient('x.y.z.w', port=yyy, timeout=10)

这应该可以帮助您摆脱TypeError: unsupported operand type(s) for +: 'float' and 'NoneType'错误。

我从未测试过这种组合,但据此它应该有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM