pub struct Heartbeat {
pub ok: bool,
/* private fields */
}Expand description
Heartbeat Monitor (FB_Heartbeat)
Monitors a counter that is incremented by a remote source (typically a server or PLC). If the counter stops changing for longer than the configured timeout, the connection is considered lost.
§Behavior
okisfalseuntil the first change inbeat_countis observed- Each time
beat_countdiffers from the previous call, the internal watchdog timer resets andokbecomestrue - If
beat_countremains unchanged for longer thanperiod,okbecomesfalse
§Example
use autocore_std::fb::Heartbeat;
use std::time::Duration;
let mut hb = Heartbeat::new();
let timeout = Duration::from_millis(100);
// First call — no previous value, ok is false
hb.call(0, timeout);
assert_eq!(hb.ok, false);
// Beat count changes — connection confirmed
hb.call(1, timeout);
assert_eq!(hb.ok, true);
// Beat count keeps changing — still ok
hb.call(2, timeout);
assert_eq!(hb.ok, true);
// Beat count stalls...
hb.call(2, timeout);
assert_eq!(hb.ok, true); // Within timeout
std::thread::sleep(Duration::from_millis(110));
hb.call(2, timeout);
assert_eq!(hb.ok, false); // Timed out — connection lost
// Beat count resumes — connection restored
hb.call(3, timeout);
assert_eq!(hb.ok, true);§Timing Diagram
beat_count: 0 1 2 3 3 3 3 3 3 4 5
↑ timed out
ok: F T T T T T T F F T T§Use Cases
- Monitoring a PLC ↔ server communication link
- Detecting a stalled remote process
- Watchdog supervision of a periodic counter
Fields§
§ok: boolOutput: true when the heartbeat is alive (counter changing within
the timeout period). false on first scan or after a timeout.
Implementations§
Source§impl Heartbeat
impl Heartbeat
Sourcepub const DEFAULT_PERIOD: Duration = DEFAULT_PERIOD
pub const DEFAULT_PERIOD: Duration = DEFAULT_PERIOD
The default timeout period (7 seconds).
Provided as a constant for convenience so callers don’t have to hard-code the value.
§Example
use autocore_std::fb::Heartbeat;
let mut hb = Heartbeat::new();
hb.call(1, Heartbeat::DEFAULT_PERIOD);Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new heartbeat monitor.
The monitor starts on its first scan — ok will be false until
the beat count changes for the first time.
§Example
use autocore_std::fb::Heartbeat;
let hb = Heartbeat::new();
assert_eq!(hb.ok, false);Sourcepub fn call(&mut self, beat_count: i64, period: Duration)
pub fn call(&mut self, beat_count: i64, period: Duration)
Executes one scan cycle of the heartbeat monitor.
Call this once per control cycle with the current beat count from the remote source.
§Arguments
beat_count- The latest heartbeat counter value from the remote source. Anyi64value; only changes matter.period- Maximum allowed time between counter changes. If the counter remains static for longer than this,okbecomesfalse. A typical default is 7 seconds.
§Example
use autocore_std::fb::Heartbeat;
use std::time::Duration;
let mut hb = Heartbeat::new();
// Call cyclically in your control loop
hb.call(remote_counter, Duration::from_secs(7));
if !hb.ok {
// Handle connection loss
}Sourcepub fn with_defaults() -> Self
pub fn with_defaults() -> Self
Creates a new heartbeat monitor with the default 7-second period.
This is a convenience alias; the period is still passed per-call
via call(), but this documents the standard default.
§Example
use autocore_std::fb::Heartbeat;
use std::time::Duration;
let mut hb = Heartbeat::with_defaults();
// Equivalent to Heartbeat::new(), period is passed to call()
hb.call(0, Duration::from_secs(7));