Slave diagnostics
Each slave provides its own diagnostic properties and sub-objects for port errors, redundancy, Distributed Clocks (DC) synchronization and other information.
Drive exception handling from events (such as slave_state_changed, slave_offline, dc_sync_lost) instead of polling.
Feature overview
| Feature | Access path | Description |
|---|---|---|
| WKC and health mirror | slave.diagnostics.* | Per-slave WcState / AL / mailbox health mirrors (thin read, zero frames; the AL mirror is a cache sampled while the master is in OP, not real-time) |
| Communication diagnostics | slave.diagnostics.read_port_errors() | ESC port error counters |
| Redundancy diagnostics | slave.diagnostics.* | Redundancy activation, primary / redundant line break detection |
| DC synchronization | slave.diagnostics.dc.* | Sync state, time difference |
WKC and health mirror
These properties live on slave.diagnostics and most of them are thin reads with zero frames of the per-slave diagnostic cache — the runtime maintains the cache every cycle (immediately when the WKC is abnormal), so a read reflects the real bus reality at that moment and no refresh call is needed. The exception is al_status_mirror: it is a cache sampled while the master is in OP, not the live truth at read time (see SlaveAlStatusMirror). Together with the master-level master.diagnostics_info.wc_deficit they let you pinpoint the specific slave when one drops out.
| Category | Property | Type | R/W | Description |
|---|---|---|---|---|
| WKC contribution | wc_contributed | WcContribution | Read-only | Contribution state of this slave to the Working Counter (WKC). NOT_CONTRIBUTED ≠ a master fault: this slave is not responding at this moment (possibly lost / recovering from a hot-plug); when the runtime has not filled the value in, it honestly returns UNKNOWN and does not invent one |
| AL state mirror | al_status_mirror | SlaveAlStatusMirror | Read-only | Parsed object of this slave AL Status mirror (contains al_state / has_error / al_status_code). It is a cache sampled while the master is in OP, not the live truth at read time; when not in OP / not sampled it returns an object with raw=0 (unknown) |
| Mailbox health | mailbox_health | EcMailboxHealth | Read-only | Mailbox health of this slave. DEGRADED means it is in OP but the mailbox is half-failed (CoE / SDO may block); reported faithfully and not involved in the WKC judgement |
| is_free_run_demoted | bool | Read-only | Whether this slave was forced down to FreeRun synchronization mode. True only when "the configuration requires DC but the run time was forced back to FreeRun" — the observation point for the root cause of the half-failure | |
| health_degraded_count | Optional[int] | Read-only | Consecutive count of mailbox half-failure (approximately seconds). 0 = healthy or not evaluated; a rising value means the half-failure has not recovered, and the runtime tries to repair it automatically. Returns None when the read fails | |
| recover_mailbox_health() | bool | Write-only | Manually trigger mailbox half-failure repair; usually you do not need to call it, the runtime repairs automatically. Returns False when no repair is needed / the argument is invalid / a communication error occurred; it does not raise | |
| Per-slave true WKC probe | master.diagnostics_info.slave_wkc_probe_actual(si) | Optional<int> | Read-only | Per-slave true WKC (the true WKC of the probe datagram itself, input side): a non-negative integer = true value (0 = there was an echo but this slave did not contribute; 0 is a valid reading) / -2 = sent but no echo (not disguised as 0) / -1 = no sample (not enabled / not this slave's turn / this slave is not probeable / out of range / no baseline built for the table). Unreadable = unknown, never invented; read failed = None (unknown, never invented). si is a 1-based slave index |
| master.diagnostics_info.slave_wkc_probe_expected(si) | Optional<int> | Read-only | Expected WKC: 1 = expected (this slave has a read FMMU) / -1 = not probeable (no read FMMU / the true frame contribution is 0 / out of range). Unreadable = unknown, never invented; read failed = None (unknown, never invented) | |
| master.diagnostics_info.slave_wkc_probe_sample_age_ms(si) | Optional<int> | Read-only | Sample freshness: a non-negative integer = milliseconds since the most recent sample / -1 = never sampled; read failed = None (unknown, never invented) | |
| master.diagnostics_info.per_slave_wkc_probe_mode | Optional<int> | Read-write | Probe mode: 0 = OFF (default) / 1 = PROBE_READ; a failed read returns None (unknown, not folded into 0 = OFF). While it is OFF it has no behaviour; assigning a value sets it (there is no separate set_slave_wkc_probe_mode method), and an invalid value is rejected and the previous value is kept. The read-only probe sends zero frames, does not change the WKC contract (wkc_expected_mirror / wkc_actual_mirror semantics are unchanged), does not take a slave out of OP, and does not drive fault judgement / recovery |
The per-slave true WKC probe is a member of master.diagnostics_info (its methods take an explicit 1-based slave index) and is not on slave.diagnostics; slave.diagnostics.wc_contributed is a different WKC contribution mirror.
WcContribution
class WcContribution(IntEnum):
NOT_CONTRIBUTED = 0 # this slave is not contributing WKC at this moment (possibly lost / recovering from a hot-plug)
CONTRIBUTED = 1 # this slave contributes WKC normally (it responds on the bus)
UNKNOWN = 0xFF # runtime mirror not filled in / not mapped / mirror unavailable (honest unknown, never invented)
SlaveAlStatusMirror
class SlaveAlStatusMirror:
# raw is the runtime 16bit AL mirror: low byte = AL State (bit4 = Error), high byte = AL Status Code
raw: int # raw 16bit mirror value
al_state: EcState # AL Status state (low byte without the Error bit; an unrecognized raw value falls back to returning the raw integer)
has_error: bool # whether the slave is in an error state (bit4)
al_status_code: int # high byte of the AL Status Code (0-255); meaningful for diagnostics only when has_error is set
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 non-0 value 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.
EcMailboxHealth
class EcMailboxHealth(IntEnum):
UNKNOWN = 0 # unknown / no mailbox / not in OP — mailbox health is meaningless here
HEALTHY = 1 # healthy — in OP and the mailbox is available
DEGRADED = 2 # degraded — in OP but the mailbox is half-failed (handler lost / DC demoted with 0x001F)
Example:
from darra_ethercat.data.types import WcContribution, EcMailboxHealth
for slave in master.slaves:
d = slave.diagnostics
# WKC contribution: locate a lost slave
if d.wc_contributed == WcContribution.NOT_CONTRIBUTED:
print(f"从站 {slave.slave_num} 未贡献 WKC, AL=0x{d.al_status_mirror.al_status_code:02X}") # Slave {slave.slave_num} is not contributing WKC, AL=0x{d.al_status_mirror.al_status_code:02X}
# mailbox half-failure (in OP but CoE may block)
if d.mailbox_health == EcMailboxHealth.DEGRADED:
print(f"从站 {slave.slave_num} 邮箱半失效 {d.health_degraded_count}s" # Slave {slave.slave_num} mailbox partially failing for {d.health_degraded_count}s
+ (" (被迫 FreeRun)" if d.is_free_run_demoted else "")) # (forced FreeRun)
d.recover_mailbox_health()
Communication diagnostics
read_port_errors()
def read_port_errors(self) -> Optional[EscPortErrors]
Reads the ESC port error counters of the slave.
EscPortErrors data class:
@dataclass
class EscPortErrors:
rx_error: List[int] # RX error count per port [Port0-3]
invalid_frame: List[int] # invalid frame count per port [Port0-3]
lost_link: List[int] # link loss count per port [Port0-3]
@property
def has_errors(self) -> bool # whether any error is present (derived from the three lists, read-only)
Example:
for slave in master.slaves:
errors = slave.diagnostics.read_port_errors()
if errors is not None and errors.has_errors:
print(f"从站 {slave.slave_num}: " # Slave {slave.slave_num}:
f"RX错误={errors.rx_error}, " # RX errors={errors.rx_error},
f"无效帧={errors.invalid_frame}, " # Invalid frames={errors.invalid_frame},
f"链路丢失={errors.lost_link}") # Lost link={errors.lost_link}
The port error counters can be cleared through slave.reset_port_error_counters() (returns bool). The same counters can also be read as a dict through slave.read_port_error_counters() (rx_error / invalid_frame / lost_link; returns None when the read fails); use slave.diagnostics.read_port_errors() above when you need a structured object.
Redundancy diagnostics
Access the redundancy state of a slave through slave.diagnostics.
| Category | Property | Type | R/W | Description |
|---|---|---|---|---|
| Redundancy diagnostics | redundancy_activated | Optional[bool] | Read-only | Redundancy protection is running = master-level redundancy_ring_mode >= RingMode.DUAL (Dual / Degraded); always False in linear / end fold-back topologies, and not an independent per-slave conclusion. Returns None when the read fails |
| primary_link_broken | Optional[bool] | Read-only | Primary line break. Returns None when the read fails | |
| secondary_link_broken | Optional[bool] | Read-only | Redundant line break. Returns None when the read fails |
Example:
for slave in master.slaves:
activated = slave.diagnostics.redundancy_activated
if activated is None: # read failed = unknown; do not fold it into "not activated"
print(f"从站 {slave.slave_num}: 冗余状态未知") # Slave {slave.slave_num}: redundancy status unknown
continue
if not activated:
continue
msg = f"从站 {slave.slave_num}: 冗余激活" # Slave {slave.slave_num}: redundancy active
if slave.diagnostics.primary_link_broken is True:
msg += ", 主线路断路" # , primary link broken
if slave.diagnostics.secondary_link_broken is True:
msg += ", 冗余线路断路" # , redundancy link broken
print(msg)
DC synchronization
Access the DC synchronization state of a slave through slave.diagnostics.dc.
The reference clock itself: 0x092C (time difference) is N/A for it by definition, and the runtime no longer synthesizes an in-sync value. When this slave is the reference clock and SYNC0 is armed,
is_in_syncreturnsTrue— the reference clock cannot be out of step with itself, and that is the only conclusion that can be drawn (slaves that are not the reference clock still go through the runtime sync-window criterion). ThatTrueonly means "may be treated as synchronized"; it does not mean a measured deviation exists, nor that the whole network clock has converged. For the reference clock the tri-state diagnostic surfacedc_config.sync_windowstill faithfully reportsapplicable=False/ReferenceClockItself.
| Category | Property | Type | R/W | Description |
|---|---|---|---|---|
| DC synchronization | is_in_sync | Optional[bool] | Read-only | Whether the slave is inside the sync window. Tri-state: True = inside the window (including "reference clock with SYNC0 armed"), False = definitely outside (including a known-not-armed case), None = the slave has no DC, the SYNC0 armed conclusion is unknown, or the read failed |
| sync_time_difference | Optional[int] | Read-only | Current time difference from the reference clock (nanoseconds). Returns None when there is no DC / the read fails (not folded into 0) |
SyncWindowStatus fields (the only definition in the package; DcConfig.get_sync_window_status() returns this type):
class SyncWindowStatus:
diff_ns: int = 0 # current time difference (nanoseconds)
max_diff_ns: int = 0 # maximum time difference (nanoseconds)
min_diff_ns: int = 0 # minimum time difference (nanoseconds)
in_sync: bool = False # whether the slave is inside the sync window
out_of_sync_count: int = 0 # number of times the slave left the sync window
Get a one-shot snapshot through DcConfig.get_sync_window_status().
Example:
for slave in master.slaves:
# has_dc tri-state: None = read failed (unknown); do not skip it as "definitely no DC"
if slave.has_dc is False:
continue
dc = slave.diagnostics.dc
print(f"从站 {slave.slave_num}: " # Slave {slave.slave_num}:
f"时间差={dc.sync_time_difference}ns, " # Time difference={dc.sync_time_difference}ns,
f"同步={dc.is_in_sync}") # In sync={dc.is_in_sync}
Full example
for slave in master.slaves:
print(f"--- 从站 {slave.slave_num}: {slave.name} ---") # --- Slave {slave.slave_num}: {slave.name} ---
# port errors
errors = slave.diagnostics.read_port_errors()
if errors is not None and errors.has_errors:
print(f" 端口错误: RX={errors.rx_error}, " # Port errors: RX={errors.rx_error},
f"无效帧={errors.invalid_frame}, " # Invalid frames={errors.invalid_frame},
f"链路丢失={errors.lost_link}") # Lost link={errors.lost_link}
# redundancy (meaningful only in redundancy mode; None = read failed, not treated as not activated)
if slave.diagnostics.redundancy_activated is True:
print(f" 冗余激活: " # Redundancy active:
f"主线路断={slave.diagnostics.primary_link_broken is True}, " # Primary link broken={slave.diagnostics.primary_link_broken is True},
f"冗余线路断={slave.diagnostics.secondary_link_broken is True}") # Redundancy link broken={slave.diagnostics.secondary_link_broken is True}
# DC synchronization (has_dc tri-state: None = unknown; still read the diagnostics)
if slave.has_dc is not False:
dc = slave.diagnostics.dc
print(f" DC: 时间差={dc.sync_time_difference}ns, " # DC: time difference={dc.sync_time_difference}ns,
f"同步={dc.is_in_sync}") # In sync={dc.is_in_sync}
# topology leg model: ring / branch / closure point in one query
info = master.slave_topology.compute_ring_branch()
if info.model_known:
print(f"mode={info.mode_text} "
f"环成员={info.ring_count} 支线数={info.branch_count} " # Ring members={info.ring_count} branches={info.branch_count}
f"闭合点={info.closure_slave_num} " # Closure point={info.closure_slave_num}
f"环成员站号={info.ring_slave_nums}") # Ring member slave indices={info.ring_slave_nums}
else:
print(f"腿模型不可用: {info.unavailable_reason}") # not ready != broken
# DL Status (0x0110): read the port state
data = master[1].read_register(0x0110, 2) # little-endian
if data:
# two raw bits per port P0..P3 2 = communication established; 1 = internal loopback only, not counted as connected
print("通信建立(P0):", (data[0] & 0x03) == 2) # Communication established (P0):
print("端口活动掩码:", f"0x{master[1].active_ports:04X}") # Active port mask:
- When the leg-model query returns <0 the runtime writes no output at all, and the caller must not treat a pre-seeded initial value as this call's conclusion.
- A failed DL Status parse likewise writes no output and does not carry over the old value.
- When
model_known = Falsethe ring / branch / closure-point conclusions are all unavailable — this is not "linear / no ring"; only whenmodel_known = Truedoesclosure_slave_num = -1mean the definite conclusion "no closure point".