ModbusFB ClientRequest: never recovers after a single ReplyTimeout (requires full download)

winki
2026-06-19
7 days ago
  • winki - 2026-06-19

    Library: ModbusFB (CODESYS Modbus package)
    IDE / Runtime: CODESYS V3.5 SP___ / Control runtime ___
    Target: ___ (e.g. gateway, Linux ARM64 / Debian)
    Slave: Modbus TCP inverter

    Summary

    I poll a Modbus TCP slave with a permanent connection: one ClientTCP
    (kept connected, xConnect:=TRUE) and one
    ClientRequestReadHoldingRegisters, triggered cyclically.

    Everything works fine after a fresh application download. But as soon as
    one single ReplyTimeout occurs (slave temporarily not answering), the
    request never recovers. It stays stuck and no further messages are sent,
    even though the TCP connection is still reported as alive. Only a full
    application download brings it back β€” an online change or a runtime
    reset is not enough / not what I want in production.

    State observed when stuck

    So the connection is alive, the request finished in ReplyTimeout, the FB
    is back to _state = None, but udiNumMsgSent no longer increases β€” as if a
    new rising edge on xExecute is no longer accepted / no longer produces a
    new message.

    My trigger logic (simplified)

    Each PLC cycle, in this order:

    If the request is fully at rest (NOT xExecute AND NOT xBusy AND NOT xDone AND NOT xError) and a read is pending, I set xExecute := TRUE.
    I call clientTcp() then clientRequest(rClient := clientTcp) once
    each, per cycle.
    On xDone or xError, I set xExecute := FALSE.

    I made sure to insert at least one full cycle with xExecute = FALSE
    (seen by the FB call) before re-triggering, so the falling edge is processed.

    Questions

    After a ReplyTimeout (xError = TRUE), what is the exact, correct
    sequence to re-trigger the same ClientRequest so a new message is
    actually sent again? Is a full cycle with xExecute = FALSE between two
    executions mandatory?
    Is there a known condition where, after ReplyTimeout, the
    ClientTCP keeps xConnected = TRUE but silently stops sending new
    requests (e.g. an internal request queue / _udiRequestId that gets out
    of sync)? udiNumMsgSent freezing at 23 while the FB shows _state = None is what puzzles me.
    Is the recommended pattern to drop the connection (ClientTCP.xConnect := FALSE for one cycle, then TRUE) on a request error, rather than
    keeping it alive? The docs state a request error does not close the
    connection, so I kept it open β€” but maybe that is the issue here.
    Could the large uiStartItem (50514) be relevant? It works right after
    download, so addressing seems correct, but I want to rule it out.

    Any guidance on the canonical recovery pattern after a request timeout would
    be greatly appreciated.

    Thanks!

     
    πŸ‘
    1

    Last edit: winki 2026-06-19
  • YIN - 7 days ago

    That frozen udiNumMsgSent with _state back at None is the interesting part, because it tells you the request block itself thinks it is idle while nothing is going out on the wire. I have seen this shape of problem with ModbusFB before, and in my case it wasn't the trigger edge at all, it was the connection block holding on to the timed-out request.

    Your trigger logic sounds right to me, one full cycle with xExecute FALSE between triggers is exactly what these blocks want, and the fact that it works fine after a fresh download rules out the addressing, so I would stop chasing uiStartItem 50514. What I would do next is watch the two blocks separately on the cycle where it gets stuck. If the request block's _state stays None and its xBusy never comes up, the edge is not reaching it, but if xBusy comes up and udiNumMsgSent still doesn't move, the request is being handed to the TCP block and the TCP block is the one not sending. That distinction matters because the fix is different for each.

    In my experience the practical recovery that actually works here is to drop and re-establish the connection after a ReplyTimeout instead of keeping it open. The documentation says a request error doesn't close the connection, and that is true, but the TCP block can still be left waiting on the response that never arrived, and then every new request just queues behind it. So on xError, after you have cleared xExecute, set ClientTCP.xConnect FALSE for a cycle or two until xConnected drops, then set it TRUE again and only re-trigger the request once xConnected is back. It is a bit ugly and I know it feels wrong to tear down a healthy-looking connection, but it resets the internal queue and transaction state, and it is a lot cheaper than a full download in production.

    One more thing worth adding is a small watchdog of your own, something like: if you have been triggering reads and udiNumMsgSent hasn't changed for a few seconds, run the same disconnect and reconnect sequence automatically. That way a single silent slave doesn't leave the polling dead until someone notices. And a practical note before you try this on the live inverter: test the recovery logic first in simulation or on a bench setup, and keep safety functions separate from anything like this, so a comms test can't touch a running drive.

     

Log in to post a comment.