Skip to main content

Slave diagnostics

Each slave provides its own diagnostic properties and sub-objects for port errors, redundancy, DC synchronization and more.

Use with events

Drive exception handling from events (such as on_slave_state_changed, on_slave_offline, on_dc_sync_lost) instead of polling.

Feature overview​

FeatureAccess pathDescription
WKC and health mirrorslave.wc_contributed() / al_status_mirror() / mailbox_healthy() etc.Per-slave WKC / AL / mailbox health read-only mirror (WKC and mailbox read as the current value; the AL mirror is a cache sampled while the master is in OP)
Communication diagnosticsslave.diagnostics().read_port_errors()ESC port error counters
Redundancy diagnosticsslave.diagnostics().*Redundancy activation, line break detection
DC synchronizationslave.diagnostics().dc().*Sync state, time difference

WKC and health mirror​

These methods are called directly on slave (not under the slave.diagnostics() sub-object). wc_contributed() / mailbox_healthy() / free_run_demoted() / health_degraded_count() return the current real-time value — every read truthfully reflects the bus state at that moment, with no refresh needed. al_status_mirror() is not in that category. This value is a cache refreshed by the master only while it is in OP; it is not the live truth at read time. When the master is not currently in OP (or no sample has been taken yet) the getter returns 0 = not sampled / unknown; only a value other than 0 is a trustworthy sample. Once the master leaves OP the sampler stops and the cache keeps its last sample, so this property no longer claims realtime behaviour. Together with the master-level master.diagnostics_info().wc_deficit() you can pinpoint the exact slave when one drops out.

CategoryMethodTypeR/WDescription
WKC contributionwc_contributed()WcContributionread-onlyContribution state of this slave to the Working Counter (WKC). NotContributed ≠ a master fault — it means this slave is not responding right now (possibly lost / recovering from a hot-plug exchange); when no data is available yet it truthfully returns Unknown and never invents one
AL state mirroral_status_mirror()SlaveAlMirrorread-onlyParsed AL state mirror struct for this slave (with al_state / al_status_code / raw + has_fault() / is_unknown()). It is a cache sampled while the master is in OP, not the live truth at read time
Mailbox healthmailbox_healthy()boolread-onlyWhether the mailbox is healthy (in OP and the mailbox is available)
free_run_demoted()boolread-onlyWhether this slave was demoted to FreeRun (it switches to FreeRun automatically after a DC/SM synchronization fault, reported faithfully)
health_degraded_count()u32read-onlyConsecutive cumulative count of mailbox half-failure (approximately seconds; 0 = healthy or not evaluated). A rising value means the half-failure has not recovered; it repairs automatically once the threshold is reached. It clears to zero when health recovers, and also when the slave drops out of OP
recover_mailbox_health()boolwrite-onlyRepairs mailbox half-failure (an explicit maintenance action; true = success). Usually already repaired automatically

Observability contract (not negotiable): wc_contributed() returning NotContributed is not a master fault — it means this slave is not responding right now (possibly lost / recovering from a hot-plug exchange); after the repair it returns to Contributed automatically. When no data is available yet it truthfully returns Unknown and never invents a contributed state.

WcContribution​

#[repr(u8)]
pub enum WcContribution {
NotContributed = 0, // this slave contributed no WKC this cycle (lost / not responding)
Contributed = 1, // this slave contributes WKC normally this cycle
Unknown = 0xFF, // no data for this slave yet (not mapped / not ready) — not invented
}

impl WcContribution {
pub fn from_raw(raw: u8) -> Self; // 1=>Contributed, 0=>NotContributed, anything else=>Unknown
pub fn is_contributed(self) -> bool; // only Contributed is true (Unknown does not count as a contribution)
}

SlaveAlMirror​

pub struct SlaveAlMirror {
pub al_state: EcSlaveStatus, // AL State (full named state including the Error bit)
pub al_status_code: u16, // low 8 bits of the AL Status Code (valid only with the Error bit; >=0x0100 truncated; 0 = no error code / Unknown)
pub raw: u16, // raw 16-bit value (for tracing)
}

impl SlaveAlMirror {
pub fn from_raw(raw: u16) -> Self; // parse from the raw 16-bit mirror
pub fn has_fault(self) -> bool; // whether the AL State carries the Error flag (bit4 = 0x10)
pub fn is_unknown(self) -> bool; // raw == 0 (no data yet, means Unknown)
}

Example:

use darra_ethercat::WcContribution;

for slave in master.slaves().into_iter() {
// WKC contribution: locate a lost slave
if slave.wc_contributed() == WcContribution::NotContributed {
let al = slave.al_status_mirror();
println!("从站 {} 未贡献 WKC, AL=0x{:04X}", slave.index(), al.al_status_code); // Slave {} is not contributing WKC, AL=0x{:04X}
}

// mailbox half-failure (in OP but CoE may block)
if !slave.mailbox_healthy() {
println!("从站 {} 邮箱半失效 {} 次{}", slave.index(), // Slave {} mailbox partially failing {} times{}
slave.health_degraded_count(),
if slave.free_run_demoted() { " (被迫 FreeRun)" } else { "" }); // (forced FreeRun)
if slave.health_degraded_count() >= 5 {
slave.recover_mailbox_health(); // usually already repaired automatically; this is manual intervention
}
}
}

Communication diagnostics​

read_port_errors()​

pub fn read_port_errors(&self) -> Option<EscPortErrors>

Reads the slave ESC port error counters.

pub struct EscPortErrors {
pub rx_error: [u8; 4], // RX error count per port [Port0-3]
pub invalid_frame: [u8; 4], // invalid frame count per port [Port0-3]
pub lost_link: [u8; 4], // link loss count per port [Port0-3]
pub fwd_rx_error: [u8; 4], // forwarded RX error count per port [Port0-3]
}

impl EscPortErrors {
pub fn has_errors(&self) -> bool // whether any error is present
}
note

read_port_errors() reads the slave in real time; do not call it at a high rate (an interval of >=100ms is recommended). The redundancy and DC properties are "always active"; there is no need to enable the diagnostics module.

Example:

for slave in master.slaves().into_iter() {
if let Some(errors) = slave.diagnostics().read_port_errors() {
if errors.has_errors() {
println!("从站 {}: RX错误={:?}, 无效帧={:?}, 链路丢失={:?}", // Slave {}: RX errors={:?}, invalid frames={:?}, lost link={:?}
slave.index(), errors.rx_error,
errors.invalid_frame, errors.lost_link);
}
}
}

Redundancy diagnostics​

Access the redundancy state of a slave through slave.diagnostics(). It is meaningful only when redundancy is enabled on the master.

CategoryMethodTypeR/WDescription
Redundancy diagnosticsredundancy_activated()boolread-onlyRedundancy protection is running = the master-level redundancy_ring_mode() is Dual or Degraded (raw RingMode >= 1); always false in line / end fold-back topologies. It is not a per-slave independent conclusion, and CRC faults do not trigger it
primary_link_broken()Option<bool>read-onlyPrimary line break. A break exists on the path from the primary port (Port0) to this slave. Detects physical breaks only. None = the read failed; it does not mean the link is healthy
secondary_link_broken()Option<bool>read-onlyRedundant line break. A break exists on the path from the secondary port (Port1) back to this slave. Detects physical breaks only. None = the read failed; it does not mean the link is healthy

Example:

for slave in master.slaves().into_iter() {
if !slave.diagnostics().redundancy_activated() { continue; }

print!("从站 {}: 冗余激活", slave.index()); // Slave {}: redundancy active
// A failed read defaults to false: do not report a break falsely
if slave.diagnostics().primary_link_broken().unwrap_or(false) {
print!(", 主线路断路"); // , primary link broken
}
if slave.diagnostics().secondary_link_broken().unwrap_or(false) {
print!(", 冗余线路断路"); // , redundancy link broken
}
println!();
}

DC synchronization​

Access it through slave.diagnostics().dc().

The reference clock itself: 0x092C (time difference) is by definition N/A for it and produces no sync value. When this slave is the reference clock and SYNC0 is armed, is_in_sync() returns Some(true) — the reference clock cannot be out of step with itself, and that is the only conclusion that can be drawn (non-reference slaves are still judged against the sync window). That true only means "can be treated as synchronized"; it does not mean a measured deviation exists, and it does not mean the whole network clock has converged; for the reference clock the tri-state diagnostics surface dc_sync_window() still reports ReferenceClockItself faithfully.

CategoryMethodTypeR/WDescription
DC synchronizationis_in_sync()Option<bool>read-onlyWhether it is inside the sync window. Some(true) includes "reference clock and SYNC0 armed"; None = the read failed
sync_time_difference()Option<i32>read-onlyCurrent time difference from the reference clock (nanoseconds). None = the read failed

Example:

for slave in master.slaves().into_iter() {
if slave.has_dc() != Some(true) { continue; } // None = the capability cannot be read, do not treat it as having DC

let dc = slave.diagnostics().dc();
// A failed read falls back to defaults: time difference -1 / sync false; label it truthfully as "cannot read" when displaying
println!("从站 {}: 时间差={}ns, 同步={}", // Slave {}: time difference={}ns, in sync={}
slave.index(), dc.sync_time_difference().unwrap_or(-1), dc.is_in_sync().unwrap_or(false));
}

Slave error counters​

Access it through master.diagnostics_info().read_slave_error_counters().

pub fn read_slave_error_counters(&self, slave_index: i32) -> SlaveErrorCounters
pub struct SlaveErrorCounters {
pub slave_index: i32,
pub port0_crc_errors: u32,
pub port1_crc_errors: u32,
pub port2_crc_errors: u32,
pub port3_crc_errors: u32,
pub frame_errors: u32,
pub lost_frames: u32,
}

Full example​

for slave in master.slaves().into_iter() {
println!("--- 从站 {}: {} ---", slave.index(), slave.name()); // --- Slave {}: {} ---

// port errors
if let Some(errors) = slave.diagnostics().read_port_errors() {
if errors.has_errors() {
println!(" 端口错误: RX={:?}, 无效帧={:?}, 链路丢失={:?}", // Port errors: RX={:?}, invalid frames={:?}, lost link={:?}
errors.rx_error, errors.invalid_frame, errors.lost_link);
}
}

// redundancy (a failed read defaults to false, do not report a break falsely)
if slave.diagnostics().redundancy_activated() {
println!(" 冗余激活: 主线路断={}, 冗余线路断={}", // Redundancy active: primary link broken={}, redundancy link broken={}
slave.diagnostics().primary_link_broken().unwrap_or(false),
slave.diagnostics().secondary_link_broken().unwrap_or(false));
}

// DC synchronization (a failed read defaults to -1 / false)
if slave.has_dc() == Some(true) {
let dc = slave.diagnostics().dc();
println!(" DC: 时间差={}ns, 同步={}", dc.sync_time_difference().unwrap_or(-1), dc.is_in_sync().unwrap_or(false)); // DC: time difference={}ns, in sync={}
}
}