shmerlard
hey
i have this simple script to write 1 to QX6.7
❯ cat read_modbus.py
import minimalmodbus
import time

# Configure instrument
instrument = minimalmodbus.Instrument('/dev/ttyUSB0', 2)  # slave ID 2
instrument.serial.baudrate = 19200
instrument.serial.bytesize = 8
instrument.serial.parity = 'N'
instrument.serial.stopbits = 1
instrument.serial.timeout = 0.5
instrument.serial.write_timeout = 0.5
instrument.close_port_after_each_call = True  # helps if ESP resets occasionally
instrument.debug = False  # True for raw Modbus bytes debugging

RETRIES = 3  # retry count for each operation
DELAY = 0.05  # delay between reads/writes in seconds

def safe_write_bit(address, value):
    for attempt in range(RETRIES):
        try:
            instrument.write_bit(address, value)
            return True
        except (minimalmodbus.NoResponseError, minimalmodbus.InvalidResponseError):
            time.sleep(DELAY)
    return False

def safe_read_bit(address):
    for attempt in range(RETRIES):
        try:
            return instrument.read_bit(address, 1)
        except (minimalmodbus.NoResponseError, minimalmodbus.InvalidResponseError):
            time.sleep(DELAY)
    return None  # failed after retries

try:
    print("Turning ON Manual Override...")
    if not safe_write_bit(55, 1):  # QX6.7
        print("Failed to write Manual Bit after retries")
    else:
        print("Manual Bit set, monitoring states for 5 seconds...")

    start = time.time()
    while time.time() - start < 5:
        manual_bit = safe_read_bit(55)
        relay_state = safe_read_bit(0)

        print(f"Manual Bit: {manual_bit} | Relay: {relay_state}")

        if manual_bit == 0:
            print("!!! The ESP32 just reset the Manual Bit to 0.")
            break

        time.sleep(DELAY)

except Exception as e:
    print(f"Unexpected error: {e}")
 
and i have this
[203128%2F9714938%2Fmceclip0]
MANUAL_ENABLE is the bool at QX6.7, and RELAY_CTRL_1 is the led
when running the script the led does turn on for a few seconds but then it reset back to zero
any way to force it without rewriting 1?
Reply 0 0
thiagoralves
If it is getting back to zero then most likely you (in your PLC program) or another external Modbus program is writing zero to it.
Reply 0 0
shmerlard
If it is getting back to zero then most likely you (in your PLC program) or another external Modbus program is writing zero to it.
so i've found the problem and i dont realy understand why it caused it
i had a block called mqtt_handling that tried to connect to mqtt and send some data
and i've disabled the tcp rtu in the configuration, but didn't remove the mqtt_handling
so even when trying to debug using openplc i saw that nothing was updating, i removed the block and suddenly everything works
i understand why i can debug now using openplc, because the esp isnt stuck anymore trying to connect, but im not sure why the QX6.7 was reset to 0 all the time
Reply 0 0
thiagoralves
Without your full project it is hard to guess, but perhaps your device could be resetting with timeouts making the register go back to zero
Reply 0 0
Reply