Redundancy
EtherCAT redundancy provides link-level fault tolerance with two NICs and a ring topology: the primary port carries primary frames, the secondary port carries secondary frames, and when any section breaks the SDK merges the per-slave state completed from the WKC of both sides, so the whole network keeps running.
Redundancy is enabled by binding the primary and secondary NICs together through SetNetwork
Redundancy is set up at initialization with SetNetwork(mi, ifPrimary, ifSecondary), which names the two ports; after that it is enabled automatically by topology detection plus RedProcessdata, so at runtime there is no manual switch and none is needed.
- Redundancy link state change event:
RedundancyModeChanged - Automatic detection of the redundancy pair during the port scan:
GetNetworkInfo - Per-slave redundancy diagnostics (primary / secondary link break flags): Slave diagnostics - Redundancy diagnostics
RedundancyState
ec_redundancy_state_t has four values: EC_REDUNDANCY_NONE(0) not enabled / EC_REDUNDANCY_PRIMARY(1) primary port only / EC_REDUNDANCY_SECONDARY(2) secondary port only / EC_REDUNDANCY_BOTH(3) both in use. ec_ring_mode_t has three values: EC_RING_INACTIVE(0) not enabled or a single NIC / EC_RING_DUAL(1) healthy in both directions / EC_RING_DEGRADED(2) there is a break in the ring and dual sending has been merged to recover.
RedundancyStatus
Detailed redundancy runtime status.
typedef struct {
ec_redundancy_state_t state;
BOOL primary_link_up;
BOOL secondary_link_up;
uint32_t primary_tx_frames;
uint32_t primary_rx_frames;
uint32_t secondary_tx_frames;
uint32_t secondary_rx_frames;
uint32_t failover_count; /* failover count */
uint32_t last_failover_time; /* time of the last failover (ms timestamp) */
} ec_redundancy_status_t;
Redundancy control functions:
BOOL ForceRedundancyFailover(uint16_t master_index);
BOOL CheckRedundancyHealth(uint16_t master_index);
void* GetRedundancyStatus(uint16_t master_index); /* returns ec_redundancy_status_t*, freed by the caller with FreeMemory */
int GetRingMode(uint16_t master_index); /* ring run mode: 0/1/2, not the P1 foldback requirement mode */
BOOL GetSecondaryLinkStatus(uint16_t master_index); /* TRUE=explicitly up; FALSE=down or unknown */
void SetRedProcessdata(int mode); /* 0=standard ETG redundancy (default), 1=primary port only, 2=dynamic bidirectional redundancy (dual send + merge) */
int GetRedProcessdata(void);
- Enabling redundancy: construct the master with
SetNetworknaming both the primary and the secondary NIC (ifSecondarynot an empty string); topology detection plusRedProcessdatathen enable it automatically (the real driver is the topology layer'sset_red_processdata(mode), written automatically from the topology). ForceRedundancyFailover()always returnsFALSE: under bidirectional redundancy (dual send + merge) frames run on both sides at once and merge handles the send and receive paths automatically, so there is no manual switchover path. AFALSEreturn is not a "failed switchover" — this entry point simply does nothing. Do not call it as a debugging or planned-maintenance tool.CheckRedundancyHealth()returnsFALSEwhen either path is abnormal.GetRedundancyStatus()returns a snapshotec_redundancy_status_t*allocated by the SDK; the caller must free it withFreeMemory()(exactly once). For long-running monitoring, fetch it again periodically rather than keeping an old pointer for a long time.GetRedProcessdata()returns the current process-data send/receive mode (0= standard ETG redundancy /1= primary port only /2= dynamic bidirectional redundancy).SetRedProcessdata(mode)switches the process-data send/receive mode:0= standard ETG redundancy (default),1= primary port only (primary-only, usable for single-path diagnostics),2= dynamic bidirectional redundancy (both ends send a full LRW and the receiving side merges as needed). In OP the SDK switches to2automatically when a real secondary port exists, so manual calls are usually unnecessary. This is what actually drives redundancy behavior (written automatically from the topology).- To observe the real redundancy state, use
GetRedundancyStatus()/GetRingMode()(=GetRedundancyRingMode) /CheckRedundancyHealth()/GetSecondaryLinkStatus(), not theForceRedundancyFailover()entry point that always returnsFALSE.
Redundancy WKC (merged WKC)
Under ring redundancy the SDK merges the WKC of both sides to get the number of slaves that really completed a PDO exchange this cycle; the merged value is the authoritative criterion.
| Category | Function | Access | Description |
|---|---|---|---|
| Merged WKC | GetWkcActualMirror(mi) | read-only | Number of slaves that completed the exchange this cycle (authoritative value) |
| Expected WKC | GetExpectedWKC(mi) | read-only | Fixed value determined at configuration time; never lowered to accommodate a degrading bus |
| Deficit | GetWcDeficit(mi) | read-only | 期望 - 合并实测; only >0 means mapped slaves really failed to contribute WKC |
| Redundancy active | GetRingMode(mi) >= 1 | read-only | The break has been covered by redundancy (Dual / Degraded); always false on line / foldback topologies |
Criterion: merged WKC == expected WKC → the network is intact, and in that state a per-port WKC below the expected value is normal. Merged WKC < expected WKC → slaves really are missing; locate them one by one with GetSlaveWcState(mi, si) (0x01 = contributed this cycle / 0x00 = did not contribute / 0xFF = unknown), and the count recovers on its own once the slaves return. The per-port WKC (GetPrimaryWKC() / GetSecondaryWKC(), no mi argument) serves only to locate the break, not to judge health; a secondary value of 0xFFFF = no measurement.
uint16_t merged = GetWkcActualMirror(mi);
uint16_t expected = GetExpectedWKC(mi);
if (merged != expected)
printf("真掉站: 缺额 %u,逐站查 GetSlaveWcState\n", GetWcDeficit(mi)); /* Real slave loss: deficit %u, check GetSlaveWcState slave by slave */
else if (GetRingMode(mi) >= 1)
printf("有断点,冗余已补齐,网络完整\n"); /* There is a line break, redundancy has taken over and the network is complete */
Full example
Enable redundancy and monitor health
#include "ethercat.h"
#include <stdio.h>
#include <windows.h>
static void on_mode_change(uint16_t mi, int old_mode, int new_mode) {
const char* names[] = {"Inactive", "Dual", "Degraded"};
printf("冗余模式变化: %s -> %s\n", names[old_mode], names[new_mode]); /* Redundancy mode changed: %s -> %s */
}
int main(void) {
uint16_t mi = Initialize();
SetNetwork(mi, "\\Device\\NPF_{primary-guid}",
"\\Device\\NPF_{secondary-guid}");
RegisterRedundancyModeChangedCallback(on_mode_change);
SetStateSequence(mi, EC_STATE_OPERATIONAL, 10000);
Start(mi);
while (1) {
BOOL healthy = CheckRedundancyHealth(mi);
int mode = GetRingMode(mi);
uint16_t wp = GetPrimaryWKC();
uint16_t ws = GetSecondaryWKC();
ec_redundancy_status_t* st =
(ec_redundancy_status_t*) GetRedundancyStatus(mi);
printf("健康=%d 环模式=%d WKC(P/S)=%u/%u failover=%u\n", /* Health=%d ring mode=%d WKC(P/S)=%u/%u failover=%u */
healthy, mode, wp, ws, st ? st->failover_count : 0u);
if (st) FreeMemory(st); /* the pointer returned by GetRedundancyStatus is freed by the caller */
Sleep(1000);
}
Stop(mi);
Dispose(mi);
return 0;
}
Failover entry point (always false, kept only for source compatibility) / process-data mode switch:
/* always returns FALSE: bidirectional merge handles it, there is no manual switchover path, and it is not a debug/maintenance tool */
BOOL switched = ForceRedundancyFailover(mi);
if (!switched) {
printf("未切换: 双向 merge 不手动切\n"); /* Not switched: a bidirectional merge is never switched manually */
} else {
Sleep(500);
}
/* process-data mode: 0=standard ETG redundancy (default), 1=primary port only, 2=dynamic bidirectional redundancy (dual send + merge) */
SetRedProcessdata(1); /* switch to primary port only, for single-path differential tests */
SetRedProcessdata(2); /* switch back to dynamic bidirectional redundancy (both ends send a full LRW, the receiver merges) */
SetRedProcessdata(0); /* restore the default */