Slave grouping
Slave grouping assigns slaves to different groups (0-7, 0 = unassigned), and each group can be configured with its own PDO cycle divider, so that different groups exchange data at different rates.
Typical use cases:
- Servo drives (group 1): the default group, sent every cycle, 31 µs (= 31,000 ns, derived frequency about 32.26 kHz)
- IO modules (group 2): sent once every 4 cycles, 125 µs 8000Hz
- Sensors (group 3): sent once every 6400 cycles, 200ms 5Hz
- No manual group enabling -- slaves that are not grouped during mapping automatically fall into group 1, and empty groups automatically skip PDO exchange
- Property-style divider -- set directly with
master.Divider(group, value) - Independent exchange per group -- each group corresponds to its own PDO data frame
Quick start
EtherCATMaster master(dll);
master.SetNetwork("\\Device\\NPF_{...}");
master.Build();
// 1. Assign slaves to groups (must happen before SAFE_OP)
// Slaves 1~2 are not set (group=0) - folded into group 1 automatically when mapping (the default group, sent every cycle)
master.GetSlave(3).Group(2); // IO module -> group 2
master.GetSlave(4).Group(2);
master.GetSlave(5).Group(3); // Sensor -> group 3
// 2. Set the group divider (group-level configuration accepts group numbers 1-7 only)
master.Divider(2, 4); // Group 2: sent every 4 cycles
master.Divider(3, 10); // Group 3: sent every 10 cycles
// Group 1 (default group) divider defaults to 1, no extra setting needed
// 3. Switch to OP -- send and receive on the group divider, empty groups are skipped automatically
master.SetState(EcState::OP);
master.Start();
// 4. View group status
printf("活跃组数: %d\n", master.ActiveGroupCount()); // Active group count: %d\n
Group semantics
| Group number | Meaning |
|---|---|
| 0 | Unassigned marker -- stays 0 when not set; automatically joins group 1 before the SAFE_OP mapping and does not exchange independently |
| 1 | Default group -- ungrouped slaves all end up here, divider defaults to 1 (every cycle) |
| 2-7 | Explicit grouping -- assigned manually by the user, group numbers need not be contiguous |
slave.Group()accepts 0-7; group-level configuration and queries (Divider, group enable, group WKC, group slave count) only accept 1-7; setting group 0 has no effect- Group numbers need not be contiguous (for example 2, 4, 6); the system skips empty groups
- When entering the SafeOp mapping, ungrouped slaves join group 1 and each group divider defaults to 1; empty groups generate no PDO data segment
Slave group assignment
Set group membership through the slave object's Group() method.
slave.Group()
uint8_t Group() const;
void Group(uint8_t grp);
Get or set the slave's group membership (0-7, 0 = unassigned, automatically joins group 1 before mapping). Must be set before SAFE_OP.
Example:
// Assign slaves to groups
master.GetSlave(1).Group(1); // Explicitly assigned to group 1
master.GetSlave(2).Group(2); // Explicitly assigned to group 2
// master.GetSlave(3) is not set (group=0, folded into group 1 when mapping)
// Read the group the slave belongs to
uint8_t group = master.GetSlave(1).Group();
Master-level group properties
Divider()
uint8_t Divider(uint8_t group) const;
void Divider(uint8_t group, uint8_t value);
Get or set the PDO cycle divider (1-255) for the specified group. A divider value N means the group exchanges PDO data once every N cycles. Group numbers only accept 1-7 (group 0 is the unassigned marker; setting it has no effect); each group divider defaults to 1.
1-- send every cycle (1000Hz with a 1ms master cycle)2-- send every 2 cycles (500Hz)4-- send every 4 cycles (250Hz)10-- send every 10 cycles (100Hz)
Example:
// Set the divider (group number 1-7)
master.Divider(2, 4); // Group 2: every 4 cycles
master.Divider(3, 10); // Group 3: every 10 cycles
// Group 1 (default group) divider defaults to 1 (every cycle), no need to set it
// Read the divider
uint8_t div = master.Divider(3); // 10
ActiveGroupCount()
uint8_t ActiveGroupCount() const;
Get the number of active groups that currently have slaves assigned. Computed by scanning the group membership of all slaves.
Example:
printf("活跃组数: %d\n", master.ActiveGroupCount()); // Active group count: %d\n
get_group_slave_count()
namespace darra::ethercat::master_other {
uint16_t get_group_slave_count(const EtherCATMaster& m, uint8_t group);
}
Get the number of slaves currently contained in the specified group. This is a free function in the darra::ethercat::master_other namespace, not a member method of EtherCATMaster.
Parameters:
m(const EtherCATMaster&) — master referencegroup(uint8_t) — group number (1-7; group 0 always returns 0)
Return value:
uint16_t— number of assigned slaves in the group; 0 for an empty group
Example:
using namespace darra::ethercat::master_other;
for (uint8_t g = 1; g < 8; ++g) { // Group-level queries accept 1-7 only
uint16_t cnt = get_group_slave_count(master, g);
if (cnt) printf("组 %u: %u 个从站\n", g, cnt); // Group %u: %u slaves\n
}
The C++ SDK does not provide an entry such as GetGroupSlave(group, slotIndex) that gets a Slave& directly by group plus index within the group. To iterate the slaves of a group, iterate the whole network by 1-based index and filter by slave.Group():
// Iterate over all slaves of group 1 (the default group)
for (uint16_t i = 1; i <= master.SlaveCount(); ++i) {
auto& s = master.GetSlave(i); // GetSlave always uses the whole-network 1-based index
if (s.Group() == 1) {
printf("组1 从站 %u 状态: %d\n", i, static_cast<int>(s.State())); // Group 1 slave %u state: %d\n
}
}
Automatic enable/disable
In the mapping phase of entering SafeOp, ungrouped slaves (Group()=0) automatically join group 1, and the divider of each group (1-7) defaults to 1:
- Groups with slaves / with a PDO data segment -> participate in exchange per the divider
- Empty groups -> no data segment is generated, PDO exchange is skipped
No manual enable/disable call is needed (master_other::set_group_enabled is the explicit multi-group switch), and group numbers need not be contiguous either.
Group diagnostics
Each group tracks PDO frame loss statistics independently.
Query frame loss by group
auto& diag = master.GetDiagnostics();
// Query the frame loss statistics of a given group (GetPDOFrameLossStats lives on the Diagnostics class)
auto stats = diag.GetPDOFrameLossStats(1); // Group 1
printf("组1: 累计丢帧=%u, 连续=%u\n", stats.total_lost, stats.consecutive_lost); // Group 1: total frame loss=%u, consecutive=%u\n
auto stats2 = diag.GetPDOFrameLossStats(2); // Group 2
printf("组2: 累计丢帧=%u\n", stats2.total_lost); // Group 2: total frame loss=%u\n
Default parameter (group = 0)
// With no argument group defaults to 0 - group 0 is the unassigned marker and has no PDO data segment,
// Frame loss statistics of the default group land on group 1, which the mapping folds into
auto g1 = master.GetDiagnostics().GetPDOFrameLossStats(1);
printf("组1 (默认组) 丢帧: %u\n", g1.total_lost); // Group 1 (default group) frame loss: %u\n
Group WKC diagnostics (new in 2.5.x)
Each group tracks the Working Counter (WKC) independently. The group WKC accessors are in the darra::ethercat::master_other namespace and return the true values under the EtherCAT reliability rules — expected_wkc is a fixed true value of the topology and is never tampered with, actual_wkc is the value measured in the current cycle, and actual < expected means a slave in that group has dropped out.
namespace darra::ethercat::master_other {
// Group expected WKC (the fixed truth from the topology, never lowered to accommodate a degraded bus)
uint16_t get_group_expected_wkc(const EtherCATMaster& m, uint8_t group);
// Group actual WKC (measured this cycle)
uint16_t get_group_actual_wkc(const EtherCATMaster& m, uint8_t group);
// Group WKC diagnostic trio: consecutive shortfall count / accumulated TX frames / accumulated mismatched frames
bool get_group_diag(const EtherCATMaster& m, uint8_t group,
uint16_t* consecutive_miss,
uint32_t* total_frames,
uint32_t* mismatch_frames);
// Single-value wrapper around the three items above
uint16_t get_group_consecutive_miss(const EtherCATMaster& m, uint8_t group);
uint32_t get_group_total_frames(const EtherCATMaster& m, uint8_t group);
uint32_t get_group_mismatch_frames(const EtherCATMaster& m, uint8_t group);
}
Example:
using namespace darra::ethercat::master_other;
for (uint8_t g = 1; g < 8; ++g) { // Group-level queries accept 1-7 only
uint16_t expected = get_group_expected_wkc(master, g);
if (expected == 0) continue; // Empty group
uint16_t actual = get_group_actual_wkc(master, g);
printf("组 %u WKC: %u / %u", g, actual, expected); // Group %u WKC: %u / %u
if (actual < expected)
printf(" <- 该组有从站掉线 (连续不足 %u 周期)", get_group_consecutive_miss(master, g)); // <- a slave in this group has dropped (WKC short for %u consecutive cycles)
printf("\n");
}
A group with actual < expected means a slave in that group has dropped out — the SDK raises an alarm and diagnostics, and does not lower expected and does not stop OP. After the slave is repaired, actual recovers naturally to match the fixed expected, and the alarm clears automatically.
PDOFrameLoss event
The group parameter of the PDO consecutive frame loss event identifies which group lost frames (registered with OnPDOFrameLoss()):
master.Events().OnPDOFrameLoss([](uint16_t masterIndex, uint8_t group,
uint32_t consecutive, uint32_t total) {
printf("组 %d 丢帧: 连续=%u, 累计=%u\n", group, consecutive, total); // Group %d frame loss: consecutive=%u, total=%u
if (group == 1) {
// Frame loss on the default group (servos), handle as an emergency
printf("警告: 伺服组通信异常!\n"); // Warning: servo group communication fault!\n
}
});
Configuration sequence
INIT → PRE_OP → SAFE_OP → OP
| | |
| | +-- Group configuration locked, PDO starts sending per group
| |
| +-- Set slave group assignment: slave.Group(N)
| Set group divider: master.Divider(N, divider)
| (at mapping time, ungrouped slaves go to group 1; divider defaults to 1)
|
+-- Initialize master
slave.Group()takes effect when entering SAFE_OP- Group dividers should be set after
Build()and beforeSetState(OP) - Group configuration is locked automatically after entering SAFE_OP
Full example
#include "ethercat.hpp"
#include <cstdio>
using namespace darra;
int main() {
try {
EtherCATMaster master(dll);
master.SetNetwork("\\Device\\NPF_{...}");
master.Build();
printf("发现 %d 个从站\n", master.SlaveCount()); // Found %d slaves
// 1. Assign slaves to groups
// Slaves 1~2 are not set (servo drives) - folded into group 1 when mapping (the default group)
for (int i = 3; i <= 4; i++)
master.GetSlave(i).Group(2); // IO module -> group 2
master.GetSlave(5).Group(3); // Sensor -> group 3
// 2. Set the group divider
master.Divider(2, 4); // Group 2: 250 Hz
// Group 1 (default group) divider defaults to 1 (1000 Hz), no need to set it
// 3. Cycle time
master.LoopCycle(1000000); // 1ms
// 4. PDO frame-loss monitoring (independent per group)
master.Events().OnPDOFrameLoss([](uint16_t mi, uint8_t group,
uint32_t consecutive, uint32_t total) {
printf("组 %d 丢帧: 连续=%u\n", group, consecutive); // Group %d frame loss: consecutive=%u\n
});
// 5. Start -- send and receive on the group divider, empty groups are skipped automatically
master.SetState(EcState::OP);
master.Start();
// 6. View group status
printf("活跃组: %d\n", master.ActiveGroupCount()); // Active groups: %d\n
// 7. Process by group inside the cycle callback
master.Events().SetPDOCallbackSync([&](uint16_t mi) {
// Servo control of group 1 (the default group)
std::vector<uint8_t> inFrame;
darra::SlavePDO::CopyInputFrame(dll, master.MasterNumber(), 1, inFrame);
});
printf("运行中... 按 Enter 停止\n"); // Running... press Enter to stop
getchar();
} catch (const ethercat::DarraException& e) {
printf("错误: %s\n", e.what()); // Error: %s
return -1;
}
return 0;
}
Performance recommendations
| Scenario | Recommended configuration |
|---|---|
| Servo drives (high real-time requirements) | Group 1 (default group), divider = 1 |
| Digital IO modules | Group 2, divider = 4~10 |
| Analog sensors | Group 3, divider = 10~100 |
| Slow devices (temperature sensors and so on) | Group 4+, divider = 100+ |