54 lines
1.1 KiB
Python
Executable File
54 lines
1.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import time
|
|
from pymodbus.client import ModbusTcpClient
|
|
|
|
WISE_IP = "10.43.54.249"
|
|
UNIT = 1
|
|
|
|
|
|
def u32_lo_hi(lo, hi):
|
|
return (lo & 0xFFFF) + ((hi & 0xFFFF) << 16)
|
|
|
|
|
|
client = ModbusTcpClient(WISE_IP, port=502, timeout=2)
|
|
|
|
if not client.connect():
|
|
raise RuntimeError("No conecta con WISE")
|
|
|
|
last_di1_counter = None
|
|
|
|
try:
|
|
print("Leyendo contador DI1 por Modbus/TCP. Pulsa DI1 y observa cambios.")
|
|
while True:
|
|
rr = client.read_holding_registers(
|
|
address=0,
|
|
count=4,
|
|
device_id=UNIT,
|
|
)
|
|
|
|
if rr.isError():
|
|
print("ERROR:", rr)
|
|
time.sleep(1)
|
|
continue
|
|
|
|
regs = rr.registers
|
|
|
|
di0_counter = u32_lo_hi(regs[0], regs[1])
|
|
di1_counter = u32_lo_hi(regs[2], regs[3])
|
|
|
|
if di1_counter != last_di1_counter:
|
|
print(
|
|
f"DI0_counter={di0_counter} "
|
|
f"DI1_counter={di1_counter} "
|
|
f"raw={regs}"
|
|
)
|
|
last_di1_counter = di1_counter
|
|
|
|
time.sleep(0.2)
|
|
|
|
except KeyboardInterrupt:
|
|
print("Parado")
|
|
|
|
finally:
|
|
client.close()
|