#!/usr/bin/env python3 # -*- coding: utf-8 -*- # wiki.binefa.cat # # https://pymodbustcp.readthedocs.io from pyModbusTCP.client import ModbusClient # sudo pip3 install pyModbusTCP from time import sleep from sys import argv,exit SENSOR_IREG = 100 #LDR at ESP8266's code LED_COIL = 100 #small blue led at ESP8266's code LED_VERMELL_COIL = 101 #Red RGB led at ESP8266's code if len(argv) != 2: print("\nHow to use:\n\npython3 %s 174.105.0.63\n\nChanging 174.105.0.63 by ModBusTCP device IP."%argv[0]) exit(1) SERVER_HOST = argv[1] c = ModbusClient(host=SERVER_HOST, auto_open=True, auto_close=True) c.port(502) if not c.open(): print("Unable to connect to "+SERVER_HOST) exit(1) toggle = True while True: try: addr = LED_VERMELL_COIL is_ok = c.write_single_coil(addr, toggle) if is_ok: print("bit at #" + str(addr) + " coil address: write to " + str(toggle)) else: print("bit at #" + str(addr) + ": coil unable to write " + str(toggle)) sleep(0.5) bits = c.read_coils(addr, 1) if bits: print("bit at #" + str(addr) + " coil address: read to " + str(bits)) else: print("coil unable to read at #" + str(addr),end = ' coil') sleep(0.5) toggle = not toggle except KeyboardInterrupt: print("\n%s has been stopped."%argv[0]) break exit(0)