Slave grouping
Slave grouping lets you assign slaves to groups (1-7; group=0 means unassigned, and is folded into group 1 before mapping). Each group can carry its own PDO cycle divider, so different groups exchange data at different rates.
Typical use cases:
- Servo drives (group 1): default group, sent every cycle, about 32 kHz at 31 µs
- IO modules (group 2): sent every 4 cycles, 8000 Hz at 125 µs
- Sensors (group 3): sent every 6400 cycles, 5 Hz at 200 ms
- No manual group enabling — on entering SafeOp the system enables groups that have slaves and disables empty groups
- Property-style divider — set it directly through
master.Groups[g].Divider - Independent traffic per group — each group maps to its own PDO data frame
Quick start
var master = new DarraEtherCAT()
.SetNetwork("\\Device\\NPF_{...}")
.SetENI(@"C:\config.deni");
var (success, message) = master.Build();
if (!success) return;
// 1. Assign slaves to groups (must happen before SAFE_OP)
// Leave master.Slaves[0~1] unset: they keep group=0 (unassigned, folded into group 1 before mapping, sent every cycle)
master.Slaves[2].Group = 2; // IO modules → group 2
master.Slaves[3].Group = 2;
master.Slaves[4].Group = 3; // sensors → group 3
// 2. Set the group dividers (group-level configuration accepts group numbers 1-7 only)
master.Groups[2].Divider = 4; // group 2: send every 4 cycles
master.Groups[3].Divider = 10; // group 3: send every 10 cycles
// Group 1 (the default group) has divider=1 by default; nothing to set
// 3. Switch to OP — the system enables the groups that have slaves
master.SetState(EcState.OP);
// 4. Access slaves through the group indexer
var io = master.Groups[2][0]; // first slave in group 2
var sensor = master.Groups[3][0]; // first slave in group 3
Console.WriteLine($"组2: {master.Groups[2].Count} 个从站"); // Group 2: {master.Groups[2].Count} slaves
Console.WriteLine($"组3: {master.Groups[3].Count} 个从站"); // Group 3: {master.Groups[3].Count} slaves
Group semantics
| Group | Meaning |
|---|---|
| 0 | Unassigned marker — stays 0 when you do not set it; folded into group 1 before the SAFE_OP mapping and does not send or receive on its own |
| 1-7 | Valid PDO groups — assigned by the user; group numbers need not be contiguous; slaves never assigned explicitly end up in group 1 |
- Valid group numbers are 1-7; each group sends and receives its own PDO data
slave.Groupaccepts 0-7; group-level configuration and queries (Divider, group enable, group WKC, group slave count) accept 1-7 only — setting them for group 0 has no effect- Group numbers need not be contiguous (for example 1, 3, 5); empty groups are skipped
- Entering SafeOp enables groups that have slaves and disables empty groups
Assigning slaves to groups
Set a slave's group membership through the slave.Group property.
Slave.Group
public byte Group { get; set; }
Gets or sets the slave's group membership (0-7; 0 = unassigned, folded into group 1 before mapping). Must be set before SAFE_OP.
Example:
// assign slaves to groups
master.Slaves[0].Group = 1; // explicitly to group 1
master.Slaves[1].Group = 2; // explicitly to group 2
// master.Slaves[2].Group left unset: stays 0 (unassigned, folded into group 1 before mapping)
// read the group the slave belongs to
byte group = master.Slaves[0].Group;
master.Groups[g][i]
public SlaveGroupAccessor Groups { get; }
Group indexer. master.Groups[组号] returns the slave list of that group, and master.Groups[组号][索引] returns one slave inside the group.
Related properties:
GetEnumerator()(IEnumerator<Slave>) — enumerator over the slaves in the group; this is exactly whatforeach (var slave in master.Groups[1])iterates. Every enumeration takes a snapshot of the current group contents, so group changes during the enumeration throw noInvalidOperationExceptionCount(int) — number of slaves in the groupToList()(IReadOnlyList<Slave>) — read-only copy of the group contents
Example:
// all slaves in group 1
var group1Slaves = master.Groups[1];
// iterate the slaves in the group
foreach (var slave in master.Groups[1])
Console.WriteLine(slave.Name);
// access by index
DarraEtherCAT.Slave firstInGroup1 = master.Groups[1][0];
// number of slaves in the group
int count = master.Groups[1].Count;
// convert to a list
IReadOnlyList<DarraEtherCAT.Slave> list = master.Groups[2].ToList();
ActiveGroupCount
public byte ActiveGroupCount { get; }
Gets the number of active groups (computed by scanning the slave set).
Group divider
Set the divider through master.Groups[g].
Group properties:
| Category | Property | Type | Read/write | Description |
|---|---|---|---|---|
| Configuration | Groups[g].Divider | byte | Read/write | PDO cycle divider of the group (1-255) |
| Topology | Groups[g].Count | int | Read-only | Number of slaves in the group |
| WKC | Groups[g].ActualWKC | ushort | Read-only | Measured WKC of this cycle's PDO exchange for the group (one LRW frame per group, counted independently) |
| Diagnostics | Groups[g].ConsecutiveMiss | ushort | Read-only | Consecutive cycles the group's WKC fell short (actual < expected) |
| Diagnostics | Groups[g].TotalFrames | uint | Read-only | Total TX frames of the group |
| Diagnostics | Groups[g].MismatchFrames | uint | Read-only | Total frames with a WKC mismatch in the group |
Groups[g].Divider
public byte Divider { get; set; }
Gets or sets the PDO cycle divider of the group (1-255). Writing 0 throws ArgumentOutOfRangeException. Group numbers accept 1-7 only (group 0 is the unassigned marker; setting it has no effect); every group's divider defaults to 1.
1— send every cycle (1000 Hz with a 1 ms main cycle)2— send every 2 cycles (500 Hz)4— send every 4 cycles (250 Hz)10— send every 10 cycles (100 Hz)
Example:
// set the divider (group numbers 1-7)
master.Groups[2].Divider = 4; // group 2: every 4 cycles
master.Groups[3].Divider = 10; // group 3: every 10 cycles
// Group 1 (the default group) has divider=1 by default (every cycle); nothing to set
// read the divider
byte div = master.Groups[3].Divider; // 10
Per-group WKC monitoring
Each group has its own LRW frame, so every group maintains its own Working Counter. ActualWKC reports the slaves that actually responded in that group this cycle. It follows the R1 reliability rule — actual < expected means slaves in that group stopped responding, and the expected value is never lowered dynamically to hide the fault.
// monitor the WKC health of group 1
ushort actual = master.Groups[1].ActualWKC;
ushort consecutiveMiss = master.Groups[1].ConsecutiveMiss;
if (consecutiveMiss > 0)
{
Console.WriteLine($"警告: 组1 连续 {consecutiveMiss} 个周期 WKC 不足 (实测 WKC={actual})"); // Warning: group 1 has been short on WKC for {consecutiveMiss} consecutive cycles (measured WKC={actual})
Console.WriteLine($" 累计帧={master.Groups[1].TotalFrames}, 不匹配帧={master.Groups[1].MismatchFrames}"); // total frames={master.Groups[1].TotalFrames}, mismatched frames={master.Groups[1].MismatchFrames}
}
master.Groups[g].ActualWKC is the WKC of a single group. With several groups, querying per group pinpoints which group's slaves stopped responding.
Automatic enable and disable
On entering the SafeOp state the system scans every group (1-7) for slaves:
- Group with slaves → enabled automatically, with the divider forced to at least 1
- Empty group → disabled automatically, PDO traffic skipped
You do not enable or disable groups manually (this is managed automatically), and group numbers need not be contiguous.
Group diagnostics
Each group tracks its own PDO frame-loss statistics.
Query frame loss per group
// frame-loss statistics for one group
var stats = master.Diagnostics.PDO.GetFrameLossStats(1); // group 1
Console.WriteLine($"组1: 累计丢帧={stats.TotalLost}, 连续={stats.ConsecutiveLost}"); // Group 1: total frame loss={stats.TotalLost}, consecutive={stats.ConsecutiveLost}
var stats2 = master.Diagnostics.PDO.GetFrameLossStats(2); // group 2
Console.WriteLine($"组2: 累计丢帧={stats2.TotalLost}"); // Group 2: total frame loss={stats2.TotalLost}
Aggregate all groups
// no argument: returns the aggregated statistics of all groups
var total = master.Diagnostics.PDO.GetFrameLossStats();
Console.WriteLine($"总丢帧: {total.TotalLost}"); // Total frame loss: {total.TotalLost}
PDOFrameLoss event
The group parameter of the PDO consecutive-frame-loss event identifies which group lost frames:
master.Events.PDOFrameLoss += (masterIndex, group, consecutive, total) =>
{
Console.WriteLine($"组 {group} 丢帧: 连续={consecutive}, 累计={total}"); // Group {group} frame loss: consecutive={consecutive}, total={total}
if (group == 1)
{
// frame loss in the servo group: handle immediately
Console.WriteLine("警告: 伺服组通信异常!"); // Warning: servo group communication abnormal!
}
};
Configuration sequence
INIT → PRE_OP → SAFE_OP → OP
│ │ │
│ │ └─ group configuration is locked; PDO starts sending per group
│ │
│ └─ set slave group membership: slave.Group = N
│ set group dividers: master.Groups[N].Divider = divider
│ (entering SafeOp enables the groups that have slaves)
│
└─ initialize the master
slave.Grouptakes effect when entering SAFE_OP- Set the group dividers after
Build()and beforeState = OP - Group configuration is locked automatically after entering SAFE_OP
Full example
var master = new DarraEtherCAT()
.SetNetwork("\\Device\\NPF_{...}")
.SetENI(@"C:\config.deni");
var (success, message) = master.Build();
if (!success) { Console.WriteLine(message); return; }
// 1. Assign slaves to groups
// Leave master.Slaves[0~2] unset: they keep group=0 (servo drives, unassigned, folded into group 1 before mapping)
for (int i = 3; i < 5; i++)
master.Slaves[i].Group = 2; // last 2 slaves → group 2 (IO modules)
// The remaining slaves keep group=0 (folded into group 1 before mapping, sent every cycle)
// 2. Set the group divider (group numbers 1-7)
master.Groups[2].Divider = 4; // group 2: 250 Hz
// Group 1 (the default group) has divider=1 by default (1000 Hz); nothing to set
// 3. Cycle time
master.Config.LoopCycle = 1_000_000; // 1ms
// 4. PDO frame-loss monitoring (independent per group)
master.Events.PDOFrameLoss += (masterIndex, group, consecutive, total) =>
{
Console.WriteLine($"组 {group} 丢帧: 连续={consecutive}"); // Group {group} frame loss: consecutive={consecutive}
};
// 5. Start — groups that have slaves are enabled automatically
if (!master.SetState(EcState.OP, out string err)) { Console.WriteLine(err); return; }
// 6. Inspect the group state
Console.WriteLine($"活跃组: {master.ActiveGroupCount}"); // Active groups: {master.ActiveGroupCount}
Console.WriteLine($"组1: {master.Groups[1].Count} 个从站"); // Group 1: {master.Groups[1].Count} slaves
Console.WriteLine($"组2: {master.Groups[2].Count} 个从站"); // Group 2: {master.Groups[2].Count} slaves
// 7. Cycle callback: handle per group (PDO only inside the synchronous callback; SDO is forbidden here)
master.Events.ProcessDataCyclicSync += (masterIndex) =>
{
// servo control for group 1
foreach (var servo in master.Groups[1])
{
// Read the status word at its PDO offset (offsets follow the DENI mapping; offset 0 is shown here)
ushort status = servo.PDO.In[0].AsUInt16;
}
};
// 8. Parameters / diagnostics go through SDO, outside the callback (blocking mailbox transaction); null means failure
byte[] statusRaw = master.Slaves[0].CoE.SDORead(0x6041, 0, false);
if (statusRaw != null)
Console.WriteLine($"状态字 = 0x{BitConverter.ToUInt16(statusRaw, 0):X4}"); // 0x6041 = UNSIGNED16
else
Console.WriteLine($"SDO 读取失败: {master.Slaves[0].CoE.LastSdoError}"); // SDO read failed: {master.Slaves[0].CoE.LastSdoError}