首先要先安裝模組:pip install pyModbusTCP,程式碼如下,記錄一下,以免日後遺忘。
pypi:https://pypi.org/project/pyModbusTCP/
設備IP:192.168.1.191,port:702
連線Client有兩種方式,一個是持續開啟:
c = ModbusClient(host="192.168.1.191", port=702, auto_open=True)
另一個是TCP open/close for each reques
c = ModbusClient(host="192.168.1.191", port=702, auto_open=True, auto_close=True)
溫度的資料位於Address:31000,read 1x16bit資料,取出之後就可以加以利用
from pyModbusTCP.client import ModbusClient # TCP auto connect on first modbus request c = ModbusClient(host="192.168.1.191", port=702, auto_open=True, auto_close=True) # Address: 31000 , bit: 1x16 bit s = c.read_holding_registers(31000, 1) print(str(s[0]))