Watchdogs and heartbeats
An open TCP socket does not prove a system is alive. Rolling counters, realistic timeouts, and deciding what happens after detection.
One of the most unsettling bugs in industrial automation is a frozen HMI screen that still looks reassuringly calm. The temperature reads 68.2 °C, the pump symbol is green, no alarm banner is flashing, and the operator assumes the line is running smoothly—much like a stopped wall clock that still manages to look confident twice a day. Meanwhile, the OPC gateway thread hung twelve minutes ago while keeping its TCP socket politely open.
A link LED or an open socket only tells you that network plumbing exists. It does not prove that the PLC is still executing its control scan or that the SCADA server is still processing fresh data. That is the job of an application-level watchdog.
Why a rolling counter beats a toggle bit
The simplest heartbeat is a single boolean that flips every second. It works, until a polling interval happens to line up with a multiple of two seconds (aliasing), or a SCADA client disconnects and reconnects just in time to catch the opposite state.
A small rolling integer (for example a UINT incrementing from 1 to 65535, skipping 0 so a reset or uninitialised memory block is immediately obvious) avoids aliasing and makes troubleshooting in a trend chart far easier: a healthy heartbeat looks like a clean sawtooth; a stall shows up instantly as a flat line.
// PLC side (Structured Text) - called inside a cyclic task (e.g. 500 ms)
IF HeartbeatCounter >= 65535 THEN
HeartbeatCounter := 1;
ELSE
HeartbeatCounter := HeartbeatCounter + 1;
END_IF;
Place the increment inside the actual control task whose health you care about, not in an independent background interrupt. If the main sequence stops running because of a loop fault or mode switch, you want the heartbeat to stop with it.
Monitoring the heartbeat on the other side
On the receiving side (whether another PLC, a SCADA gateway or an MES interface), compare the incoming value to the last seen value. Whenever it changes, reset a timeout timer. If the timer expires, raise a communication-loss fault.
// Receiver side (Structured Text)
IF RxHeartbeat <> LastRxHeartbeat AND RxHeartbeat <> 0 THEN
LastRxHeartbeat := RxHeartbeat;
tWatchdog(IN := FALSE, PT := T#5s);
ELSE
tWatchdog(IN := TRUE, PT := T#5s);
END_IF;
CommFault := tWatchdog.Q;
If two systems both depend on each other (for instance, a line controller coordinating with an upstream filler), run the heartbeat in both directions—or echo the sender’s counter back so the sender can measure round-trip liveness without trusting the peer’s clock.
Choosing a timeout without creating alarm fatigue
The most common mistake with watchdogs is picking a timeout by gut feel. Set it too tight (say 1.2 seconds on a 1-second poll), and every minor switch hiccup or Windows garbage-collection pause floods the alarm list until operators start ignoring it. Set it too loose (two minutes on a coordinated transfer conveyor), and equipment keeps running blind long after the peer stopped.
Measure the actual end-to-end update jitter under normal plant load first, then pick a timeout that leaves clear headroom (often 3 to 5 update cycles for PLC-to-SCADA supervision, tighter for direct machine-to-machine handshakes) while still sitting safely inside the process’s fault tolerance.
Detection is only half the job
What happens the second CommFault turns true? That answer belongs in the functional specification, not in an improvised patch during SAT. A few decisions have to be explicit:
- What should the machine do? An advisory SCADA connection going down should normally raise a local alarm and let the PLC finish its cycle safely. A lost peer handshake between two mechanically linked machines may require a controlled stop.
- What should the operator see? Grey out or badge stale values on the HMI instead of leaving the last known number looking live.
- How does recovery work? Should the fault clear automatically once three consecutive valid heartbeats arrive, or does an operator have to acknowledge and re-synchronise the line state first?
A watchdog never fixes a broken network cable or a crashed service. What it fixes is silence. In an industrial plant, knowing within five seconds that two systems stopped talking is the difference between a minor recovery and a shift full of bad data.