簡體   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