Untitled
unknown
plain_text
a year ago
1.1 kB
1
Indexable
Never
from pymodbus.client.sync import ModbusTcpClient from time import sleep import socket # Set up Modbus TCP client client = ModbusTcpClient('localhost', 502) # Define function to send TCP notification def send_notification(message): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect(('192.168.1.100', 5000)) # set IP address and port of the device to receive notification s.sendall(message.encode()) data = s.recv(1024) print(data.decode()) # Initialize coil values previous_coils = client.read_coils(0, 100) # Continuously monitor coils for changes while True: current_coils = client.read_coils(0, 100) if current_coils.bits != previous_coils.bits: for i in range(100): if current_coils.bits[i] != previous_coils.bits[i]: coil_address = i coil_value = current_coils.bits[i] message = f"Coil {coil_address} has been set to {coil_value}" send_notification(message) previous_coils = current_coils sleep(0.1) # Close Modbus TCP client client.close()