https://github.com/riptideio/pymodbuspymodbusの例を使用する
と、正常に動作し、PLC に接続して保持レジスタを読み取っています。しかし、私には1つの問題があります。PLC の電源がオフになると、コードは切断エラーをトラップできません。
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
UNIT = 0x1
def run_sync_client():
client = ModbusClient('192.168.1.190', port=502)
client.connect()
rr = client.read_holding_registers(1, 4, unit=UNIT)
# follwoing will write value 10 or 20 to address 1
rq = client.write_register(4, 20, unit=UNIT)
client.close()
print (rr)
print (rr.registers) ## This reads from input registers of the Modbus Slave / Server
if __name__ == "__main__":
run_sync_client()
試してみました..次に、client.connect()の場合。誰かがこれを行う方法を提案できますか。ありがとう