Skip to main content

Slave diagnostics

Every slave has its own diagnostics functions for port errors, redundancy, DC sync, link quality and related information.

Use alongside events

Drive exception handling from events (for example RegisterSlaveStateChangeCallbackSync / RegisterSlaveStateChangeCallbackAsync, RegisterSlaveDiscoveryCallbackSync / RegisterSlaveDiscoveryCallbackAsync, SetDCSyncLostCallback) instead of polling. Reading the slave diagnostic functions directly suits UI display and similar cases.

Feature overview​

CategoryFunctionAccessDescription
WKC and health mirrorGetSlaveWcState() / GetSlaveAlStatusMirror() / GetSlaveMailboxHealth() and othersread-onlyPer-slave WcState / AL / mailbox health mirror (WKC and mailbox are direct per-slave reads; the AL mirror is a cache sampled while the master is in OP)
Communication diagnosticsReadSlavePortErrorCounters()read-onlyESC port error counters (RX / frames / link)
Redundancy diagnosticsGetSlaveRedundancyActivated()read-onlyRedundancy active, primary / redundant line break detection
DC syncGetSlaveSyncWindowStatus()read-onlySync status, time difference
Link qualityGetSlaveLinkQuality()read-onlyLink quality percentage (0-100, -1 = unknown)
Slave detailsGetSlaveDetailedInfo()read-onlyFull snapshot pointer for one slave
Port link-down eventsRegisterSlavePortLinkChangedCallbackregister callbackP0-P3 link toggle events

WKC and health mirror​

/* per-slave WKC contribution: 1=contributed this cycle / 0=no contribution (slave likely dropped) / 0xFF=unknown (contract not ready) */
uint8_t GetSlaveWcState(uint16_t master_index, uint16_t slave_index);

/* per-slave AL mirror (16bit): low byte = AL State (bit4=Error bit),
* when the Error bit is set the high byte = low byte of the AL Status Code */
uint16_t GetSlaveAlStatusMirror(uint16_t master_index, uint16_t slave_index);

/* raw mailbox health byte: 0=unknown / 1=healthy / 2=degraded (in OP but mailbox half-failed) */
uint8_t GetSlaveMailboxHealth(uint16_t master_index, uint16_t slave_index);

/* whether the slave was forced down to FreeRun: 1=demoted / 0=normal */
uint8_t GetSlaveFreeRunDemoted(uint16_t master_index, uint16_t slave_index);

/* cumulative degraded count for mailbox half-failure (not reset after the slave recovers; reflects historic degradation) */
uint32_t GetSlaveHealthDegradedCount(uint16_t master_index, uint16_t slave_index);

/* targeted mailbox half-failure repair: TRUE=repair triggered / FALSE=nothing to repair or failed.
* rebuilds only the mailbox channel handler; does not change WKC and does not drop the slave out of OP */
BOOL RecoverSlaveMailboxHealth(uint16_t master_index, uint16_t slave_index);

The runtime maintains a per-slave diagnostics cache every cycle (immediately when the WKC is abnormal), and the C SDK reads it directly — one call returns one reading, and the reading is the real bus at that moment, with no refresh needed. GetSlaveAlStatusMirror() is the exception. 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. Combined with the master-level GetWcDeficit(mi), it locates the specific slave when one drops out.

Observability contract (non-negotiable): GetSlaveWcState() returning 0 (no contribution) is not a master fault — that slave is not responding at this moment (likely dropped / recovering from a hot-plug), and the value returns to 1 by itself once the slave recovers. When the contract is not ready or the index is out of range it returns 0xFF (unknown): exposed honestly, never invented. GetSlaveMailboxHealth() returning 2 (degraded) means the slave is still in OP but its mailbox is half-failed (CoE/SDO may block); that is reported as it is and takes part in no WKC tampering.

GetSlaveWcState return values:

ValueMeaning
0The slave is not contributing WKC at this moment (likely dropped / recovering from a hot-plug)
1The slave contributes WKC normally (responding on the bus)
0xFFMirror not populated (contract not ready / index out of range) — honest unknown, never invented

GetSlaveMailboxHealth return values:

ValueMeaning
0Unknown / no mailbox / not in OP — mailbox health is meaningless here
1Healthy — in OP and the mailbox is usable
2Degraded — in OP but the mailbox is half-failed (handler lost / DC degraded accompanied by AL Status Code 0x001F EC_AL_INVALID_WATCHDOG_CONFIG)

Example:

for (uint16_t s = 1; s <= slave_count; s++) {
/* WKC contribution: locate a dropped slave */
if (GetSlaveWcState(master, s) == 0) {
uint16_t al = GetSlaveAlStatusMirror(master, s);
printf("从站 %d 未贡献 WKC, AL 镜像=0x%04X\n", s, al); /* Slave %d contributes no WKC, AL mirror=0x%04X */
}

/* mailbox half-failed (in OP but CoE may block) */
if (GetSlaveMailboxHealth(master, s) == 2) {
printf("从站 %d 邮箱半失效 %u 次%s\n", s, /* Slave %d mailbox partially failing %u times%s */
GetSlaveHealthDegradedCount(master, s),
GetSlaveFreeRunDemoted(master, s) ? " (被迫 FreeRun)" : ""); /* (forced to FreeRun) */
RecoverSlaveMailboxHealth(master, s); /* usually repaired automatically already; this is manual intervention */
}
}

ESC port error structure:

typedef struct {
uint8_t RxErrorCount[4]; /* RX error count per port [Port0-3], physical-layer CRC or coding errors */
uint8_t InvalidFrameCount[4]; /* invalid frame count per port [Port0-3], malformed frame */
uint8_t LostLinkCount[4]; /* link loss count per port [Port0-3], number of physical disconnects */
uint8_t FwdRxErrorCount[4]; /* forwarded RX error count per port [Port0-3] */
} ec_esc_port_error_stats_t;

Communication diagnostics​

BOOL ReadSlavePortErrorCounters(uint16_t master_index, uint16_t slave_index,
uint8_t* rx_error, uint8_t* invalid_frame, uint8_t* lost_link);
BOOL ResetSlavePortErrorCounters(uint16_t master_index, uint16_t slave_index);
int ReadAllSlavePortErrorCounters(uint16_t master_index);
void* GetSlavePortErrorStats(uint16_t master_index, uint16_t slave_index);
void UpdateDiagnosticsWithESCErrors(uint16_t master_index);

ReadSlavePortErrorCounters() reads three counters from every slave port (each output is a 4-byte array indexed P0-P3): RX errors, invalid frames and lost links. ResetSlavePortErrorCounters() resets one slave, and ReadAllSlavePortErrorCounters() reads all slaves in one pass and returns how many succeeded (-1 on failure). GetSlavePortErrorStats() returns a pointer to the statistics block owned by the SDK (including the forwarded RX error count FwdRxErrorCount); the caller must not free it.

note

ReadSlavePortErrorCounters() needs a live read of the slave, so do not call it at high frequency (allow at least 100 ms between calls). The redundancy and DC status queries are "always live" and need no diagnostics module enabled.

Example:

uint8_t rx[4], inv[4], lost[4];
if (ReadSlavePortErrorCounters(master, 1, rx, inv, lost)) {
for (int p = 0; p < 4; p++) {
if (rx[p] || inv[p] || lost[p])
printf("从站1 P%d: RX=%u Inv=%u Lost=%u\n", p, rx[p], inv[p], lost[p]); /* Slave 1 P%d: RX=%u Inv=%u Lost=%u */
}
}

Redundancy diagnostics​

BOOL GetSlaveRedundancyActivated(uint16_t master_index, uint16_t slave_index);
BOOL GetSlavePrimaryLinkBroken(uint16_t master_index, uint16_t slave_index);
BOOL GetSlaveSecondaryLinkBroken(uint16_t master_index, uint16_t slave_index);

Meaningful only when redundancy is enabled on the master; it detects physical link breaks only and does not cover CRC failures.

CategoryFunctionTypeAccessDescription
Redundancy diagnosticsGetSlaveRedundancyActivatedboolread-onlyRedundancy protection is running = master-level GetRingMode(mi) >= 1 (Dual / Degraded); always FALSE on line / end-fold topologies, it is not a per-slave verdict
GetSlavePrimaryLinkBrokenboolread-onlyPrimary line broken (a break exists on the path from the primary port Port0 to this slave)
GetSlaveSecondaryLinkBrokenboolread-onlyRedundant line broken (a break exists on the path from the secondary port Port1 back to this slave)

Example:

for (uint16_t s = 1; s <= slave_count; s++) {
if (!GetSlaveRedundancyActivated(master, s)) continue;
printf("从站 %d: 冗余激活", s); /* Slave %d: redundancy active */
if (GetSlavePrimaryLinkBroken(master, s)) printf(", 主线路断路"); /* , primary line broken */
if (GetSlaveSecondaryLinkBroken(master, s)) printf(", 冗余线路断路"); /* , redundancy line broken */
printf("\n");
}

Topology foldback (P1)​

Reachability of the foldback window from the ring tail to a branch, in a dual-NIC ring plus branch topology. All of these are read-only queries and change no state.

CategoryFunctionReturn typeAccessDescription
P1 foldbackGetTopoP1Mode(mi)intread-onlyFoldback requirement mode: 0=NONE (no foldback needed, or not calibrated in this run) / 1=SINGLE_NIC / 2=DUAL_RING_BRANCH; -1=invalid master index
P1 foldbackGetTopoP1Measured(mi)intread-onlyWhether on-site topology measurement completed in this run: 1=measured / 0=not measured / -1=invalid master index
P1 foldbackGetTopoP1FullWkc(mi)intread-onlyFull working counter measured at the foldback position; -1=unavailable (not calibrated / not a dual-NIC ring plus branch / invalid index)
P1 foldbackGetTopoMeasuredRingCount(mi)intread-onlyMeasured slave count on the ring; 0 is a valid measurement; -1=unavailable
P1 foldbackGetSlaveP1FoldbackOnly(mi, si)intread-only1=this slave is reachable only through the foldback window / 0=not a branch slave or not calibrated / -1=invalid master index or slave index
Port topologyRefreshTopology(mi)voidwrite-onlyAfter a hot-plug, rescan the ports, rebuild the topology graph and recompute the expected WKC of every path; the bus is not restarted and the PDO cycle keeps running. The entry point returns void — read the rebuilt topology values for the result

-1 means only unavailable / not calibrated / invalid argument: treat it as "no reading", not as a measured value. 0 is a real value, not a failure — for GetTopoP1Mode, 0 is NONE; for GetTopoP1Measured, 0 means no measurement in this run; for GetTopoMeasuredRingCount, 0 means 0 slaves were measured.

GetSlaveP1FoldbackOnly() == 1 means the slave is reachable only through the foldback window, so position addressing to it requires opening the foldback window first. A return of 0 means it is not a branch slave or the attribute is not calibrated; that does not make the slave "normally reachable".

Example:

int mode = GetTopoP1Mode(master);
int ring = GetTopoMeasuredRingCount(master);
int full = GetTopoP1FullWkc(master);

if (ring >= 0 && full >= 0)
printf("环 %d 站, 折返完整 WKC %d (mode=%d)\n", ring, full, mode); /* Ring %d slaves, full WKC via loopback %d (mode=%d) */

for (uint16_t s = 1; s <= slave_count; s++) {
int foldback = GetSlaveP1FoldbackOnly(master, s);
if (foldback == 1)
printf("从站 %u 只能经折返窗口可达, 位置寻址前先打开折返窗口\n", s); /* Slave %u is reachable only through the loopback window; open the loopback window before position addressing */
}

Per-slave diagnostics verdict​

When the application needs a per-slave verdict, use the public diagnostics and status interfaces; do not rely on unpublished query entry points:

  • Reachability and topology: query the topology with TopologyBuild() / TopologyGetChildren() and the redundancy status interfaces. When the evidence is insufficient, keep the current delivery policy and do not start dual sending on your own.
  • Liveness: decide with GetSlaveIsLost() / GetSlaveWcState() and slave discovery events; never write "unknown" as "lost".
  • Clean OP: rule on it through the public diagnostics entry point of the real OP verdict; a count of consecutive clean samples is not a public criterion.
  • SYNC0: use dc_sync0_armed() to decide whether the slave promises to generate SYNC0; armed=0 can also mean FreeRun / not yet configured / degraded, and is not an error. That verdict does not mean the network clock has converged — for sync status use GetSlaveSyncWindowStatus() / IsAllSlavesInSync().

DC synchronization​

BOOL GetSlaveSyncWindowStatus(uint16_t master_index, uint16_t slave_index,
int* diff_ns, int* max_diff_ns, int* min_diff_ns,
BOOL* in_sync, uint32_t* out_of_sync_count);
void ResetSlaveSyncWindowStats(uint16_t master_index, uint16_t slave_index);
int16_t GetSlaveLinkQuality(uint16_t master_index, uint16_t slave_index);

GetSlaveSyncWindowStatus() returns the current, maximum and minimum deviation, whether the slave is inside the sync window (the threshold is controlled by the master-level SetSyncWindowThreshold()), and the cumulative out-of-sync count. GetSlaveLinkQuality() returns the link quality percentage (0-100, -1 = unknown), computed from RxError + InvalidFrame + WKC anomalies.

The reference clock itself: 0x092C (time difference) is N/A for it by definition, and the SDK no longer synthesizes an in-sync value. When this slave is the reference clock and SYNC0 is armed, in_sync from GetSlaveSyncWindowStatus() / dc_get_sync_window_status() is TRUE (the C++ IsInSync() / TryIsInSync() follow the same rule) — a reference clock cannot be out of step with itself, and that is the only verdict that can be made (a slave that is not the reference clock still goes through the sync-window criterion). The value only means "can be treated as synchronized"; it does not mean a measured deviation exists, and it does not mean the network clock has converged. dc_get_sync_window_info() still reports applicable=FALSE + EC_DC_WINDOW_NA_REFERENCE_CLOCK_ITSELF for the reference clock, truthfully.

Full example​

#include "ethercat.h"
#include <stdio.h>

int main(void) {
uint16_t master = Initialize();
uint16_t slave_count = SetNetwork(master, "\\Device\\NPF_{GUID}", "");
SetStateSequence(master, EC_STATE_OPERATIONAL, 10000);
Start(master);

for (uint16_t s = 1; s <= slave_count; s++) {
printf("--- 从站 %d ---\n", s); /* --- Slave %d --- */

/* port errors */
uint8_t rx[4], inv[4], lost[4];
if (ReadSlavePortErrorCounters(master, s, rx, inv, lost)) {
for (int p = 0; p < 4; p++)
if (rx[p] || inv[p] || lost[p])
printf(" P%d: RX=%u Inv=%u Lost=%u\n", p, rx[p], inv[p], lost[p]);
}

/* link quality */
printf(" 链路质量: %d%%\n", GetSlaveLinkQuality(master, s)); /* Link quality: %d%% */

/* redundancy */
if (GetSlaveRedundancyActivated(master, s))
printf(" 冗余激活: 主断=%d, 副断=%d\n", /* Redundancy active: primary broken=%d, secondary broken=%d */
GetSlavePrimaryLinkBroken(master, s),
GetSlaveSecondaryLinkBroken(master, s));

/* DC sync */
int diff, max_d, min_d; BOOL sync; uint32_t oos;
if (GetSlaveSyncWindowStatus(master, s, &diff, &max_d, &min_d, &sync, &oos))
printf(" DC: 偏差=%dns, 同步=%s, 失步=%u\n", /* DC: deviation=%dns, in sync=%s, out-of-sync=%u */
diff, sync ? "是" : "否", oos);
}

Stop(master);
Dispose(master);
return 0;
}