Redundancy
EtherCAT redundancy provides link-level fault tolerance through two NICs plus a ring topology: the primary port carries primary frames and the secondary port carries secondary frames, and when any segment breaks the SDK merges the slave states completed by the WKC from both sides, so the whole network keeps running.
Access redundancy status through master.GetDiagnostics(); enable redundancy by specifying both NICs during initialization.
Redundancy is set up during initialization by specifying both NICs with SetNetwork(primary, redundant); after that it is enabled automatically by topology detection + RedProcessdata, and at runtime there is no need and no way to switch it on or off manually. The redundancy management functions live in the darra::ethercat::master_redundancy namespace and provide GetRedundancyStatus / IsRedundancyActive / ForceFailover / CheckRedundancyHealth / GetBreakPoints — of these, ForceFailover has no manual switch-over path (always returns false, see below); observe the state with GetRedundancyStatus / IsRedundancyActive / CheckRedundancyHealth / GetBreakPoints.
- Redundancy link state change events:
RedundancyModeChanged - NIC scan / redundant pair detection:
darra::ethercat::statics::GetNetworkInfo(); withneed_slave_count = truea redundant pair is detected byRedundantSlaveCount > 0
RedundancyState
RedundancyState has four values: None(0) no redundancy / Primary(1) primary network only / Secondary(2) secondary network only / Both(3) both networks active in redundancy.
RedundancyStatus
Detailed redundancy runtime status.
struct RedundancyStatus {
RedundancyState state; // Overall redundancy state
bool primaryLinkUp; // Whether the primary link is online
bool secondaryLinkUp; // Whether the secondary link is online
uint32_t primaryTxFrames; // Frames sent on the primary NIC
uint32_t primaryRxFrames; // Frames received on the primary NIC
uint32_t secondaryTxFrames; // Frames sent on the secondary NIC
uint32_t secondaryRxFrames; // Frames received on the secondary NIC
uint32_t failoverCount; // Failover count
uint32_t lastFailoverTime; // Time of the last switch (Unix timestamp)
};
struct RedundancyBreakPoint {
uint16_t slaveIndex; // Slave index where the break is
uint8_t port; // Break port number (0-3)
};
Forced failover ForceFailover
The public C++ API is named ForceFailover and lives in darra::ethercat::master_redundancy.
bool ForceFailover(EtherCATMaster& m); // Always returns false; there is no manual switch path
There is no such thing as a manual switch-over: under bidirectional redundancy (transmit on both sides + merge) frames run on both sides at the same time and the merge handles the transmit and receive paths automatically, so there is no manual "move the transmit path to the secondary port" path. The lower-level ForceRedundancyFailover always returns FALSE, so ForceFailover always yields false — this is not a "switch-over failure"; that entry point simply does nothing. Do not call it as a debugging or planned-maintenance tool; redundancy behaviour is handled automatically by the topology layer, and the state is observed with GetRedundancyStatus() / IsRedundancyActive() / CheckRedundancyHealth().
bool switched = master_redundancy::ForceFailover(master); // Always false
if (!switched)
printf("未切换: 双向 merge 不手动切\n"); // Not switched: dual-port merge never switches manually\n
Redundant WKC (merged WKC)
Under ring redundancy the SDK merges the WKC from both sides to obtain the number of slaves that really completed a PDO exchange in this cycle; the merged value is the authoritative criterion. The mirror is read through the dm:: free functions in slave/diag_mirror.hpp.
| Category | Accessor | Read/write | Description |
|---|---|---|---|
| Merged WKC | dm::wkc_actual_mirror(mi) | Read-only | Number of slaves that completed the exchange in this cycle (the authoritative value) |
| Expected WKC | dm::wkc_expected_mirror(mi) | Read-only | Fixed true value determined at configuration time, never lowered to accommodate a degraded bus |
| Deficit | dm::wc_deficit(mi) | Read-only | 期望 - 合并实测; only >0 means a slave really dropped out |
| Redundancy active | diag.RedundancyActive() | Read-only | The break point has already been covered by redundancy; always false on linear / foldback topologies |
Criterion: merged WKC == expected WKC → the network is intact, and a single-port WKC below the expectation is normal in that case; merged WKC < expected WKC → a slave really dropped out, locate it per slave with dm::slave_wc_contributed(mi, si), and it returns to zero automatically once the slave recovers. Single-port WKC (PrimaryWKC() / SecondaryWKC()) is only used to locate the break point, not to judge health; 0xFFFF on the secondary port = no measurement.
#include "slave/diag_mirror.hpp"
namespace dm = darra::ethercat::diag_mirror;
uint16_t mi = master.MasterNumber();
if (dm::wkc_actual_mirror(mi) != dm::wkc_expected_mirror(mi))
printf("真掉站: 缺额 %u,逐站查 dm::slave_wc_contributed\n", dm::wc_deficit(mi)); // Real slave loss: deficit %u, check dm::slave_wc_contributed slave by slave\n
else if (master_redundancy::IsRedundancyActive(master))
printf("有断点,冗余已补齐,网络完整\n"); // There is a cable break, redundancy has taken over, the network is complete\n
Complete example
Enable redundancy and monitor health
#include "ethercat.hpp"
#include <chrono>
#include <thread>
using namespace darra::ethercat;
int main() {
// Identify redundancy pairs by RedundantSlaveCount > 0 (the criterion is only >0; do not read it as a station count)
auto adapters = statics::GetNetworkInfo(dll, /*need_slave_count*/ true);
std::string primary, redundant;
int found = 0;
for (auto& a : adapters) {
if (a.RedundantSlaveCount > 0) {
(found++ == 0 ? primary : redundant) = a.Name;
if (found >= 2) break;
}
}
if (found < 2) return -1;
EtherCATMaster master(dll);
master.SetNetwork(primary, redundant)
.SetENI("C:/config.deni");
// redundancy is enabled automatically by topology detection; there is no manual switch
master.Build();
// Subscribe to redundancy mode changes (register with OnRedundancyModeChanged)
master.Events().OnRedundancyModeChanged([](uint16_t mi, int oldM, int newM) {
printf("冗余模式: %d -> %d\n", oldM, newM); // Redundancy mode: %d -> %d\n
});
master.SetState(EcState::OP);
master.Start();
// Periodic health check
auto& base = master.GetDiagnostics(); // Diagnostics&
MasterDiagnosticsInfo diag(base, master.MasterNumber()); // High-level wrapper; RingMode() returns the darra::RingMode enum
while (true) {
auto status = master_redundancy::GetRedundancyStatus(master);
if (status) {
printf("冗余: ring_mode=%d active=%d primary=%d secondary=%d failover=%u\n", // Redundancy: ring_mode=%d active=%d primary=%d secondary=%d failover=%u\n
static_cast<int>(diag.RingMode()),
master_redundancy::IsRedundancyActive(master),
status->primaryLinkUp, status->secondaryLinkUp,
status->failoverCount);
}
auto bps = master_redundancy::GetBreakPoints(master);
for (auto& bp : bps) {
printf("故障点: 从站[%u] 端口=%u\n", bp.slaveIndex, bp.port); // Fault point: slave[%u] port=%u\n
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
return 0;
}
- ETG.1500 §5.7 Cable Redundancy