Slave grouping
Slave grouping assigns slaves to different groups (1-7; group=0 means unassigned, and such slaves are folded into group 1 before mapping). Each group has its own PDO cycle divider, so different groups exchange data at different rates.
Typical use cases (main cycle 31 µs, the smallest cycle the product supports — the specification floor):
- Servo drives (group 1): the default group (slaves not explicitly assigned land here automatically); sent every cycle, 31 µs (= 31,000 ns, derived rate about 32.26 kHz)
- IO modules (group 2): sent every 4 cycles, 125 µs, 8000 Hz
- Sensors (group 3): sent every 6400 cycles, 200 ms, 5 Hz
- No manual group enabling — on entering SafeOp the system enables the groups that have slaves and disables empty groups
- Each group sends and receives independently — every group has its own PDO data frame
Quick start
uint16_t master = Initialize();
SetNetwork(master, "\\Device\\NPF_{GUID}", "");
/* 1. assign slaves to groups (must happen before SAFE_OP) */
SetSlaveGroup(master, 3, 1); /* slave 3 -> group 1 */
SetSlaveGroup(master, 4, 1);
SetSlaveGroup(master, 5, 2);
/* 2. set the group cycle dividers */
SetGroupCycleDivider(master, 1, 4); /* group 1: every 4 cycles */
SetGroupCycleDivider(master, 2, 10); /* group 2: every 10 cycles */
/* slaves not explicitly assigned land in group 1 */
/* 3. switch to OP — the system enables the groups that have slaves */
SetStateSequence(master, EC_STATE_OPERATIONAL, 10000);
Start(master);
Group semantics
| Group | Meaning |
|---|---|
| 0 | Unassigned marker — passing 0 to SetSlaveGroup means unassigned; such a slave is folded into group 1 before mapping, and the group query / configuration interfaces do not accept 0 |
| 1-7 | Valid PDO groups — assigned by the user; group numbers need not be consecutive, and slaves not explicitly assigned also land in group 1 |
- The valid range is 1-7 (the group query / configuration interfaces do not accept 0), and every group sends and receives its own PDO data
- Group numbers need not be consecutive (for example 1, 3, 5); the system skips empty groups
- On entering SafeOp the system enables the groups that have slaves and disables empty groups
Assigning slaves to groups
BOOL SetSlaveGroup(uint16_t master_index, uint16_t slave_index, uint8_t group);
uint8_t GetSlaveGroup(uint16_t master_index, uint16_t slave_index);
uint8_t GetActiveGroupCount(uint16_t master_index);
uint16_t GetGroupSlaveCount(uint16_t master_index, uint8_t group);
uint16_t GetGroupExpectedWKC(uint16_t master_index, uint8_t group);
SetSlaveGroup() must be called before SAFE_OP (group accepts 0-7, where 0 = unassigned). GetSlaveGroup() reads the group a slave belongs to. GetActiveGroupCount() returns the number of active groups, GetGroupSlaveCount() returns the number of slaves in a group, and GetGroupExpectedWKC() returns the expected WKC of a group. Of these, the interfaces that take a group argument accept only 1-7.
Group divider
BOOL SetGroupCycleDivider(uint16_t master_index, uint8_t group, uint8_t divider);
uint8_t GetGroupCycleDivider(uint16_t master_index, uint8_t group);
Sets the PDO cycle divider of a group (1-255). For example divider=4 means the group performs a PDO exchange every 4 main cycles.
Divider to frequency (with a 1 ms main cycle):
1— 1000 Hz (every cycle)2— 500 Hz4— 250 Hz10— 100 Hz100— 10 Hz
Example:
SetGroupCycleDivider(master, 1, 4); /* group 1: every 4 cycles */
SetGroupCycleDivider(master, 2, 10); /* group 2: every 10 cycles */
Automatic enable and disable
On entering SafeOp the system scans every group (1-7) for slaves:
- A group with slaves → enabled automatically, with the divider forced to at least 1
- An empty group → disabled automatically, and skipped in PDO sending and receiving
You do not need to call SetGroupEnabled manually, and group numbers need not be consecutive.
BOOL SetGroupEnabled(uint16_t master_index, uint8_t group, BOOL enabled);
BOOL GetGroupEnabled(uint16_t master_index, uint8_t group);
Use it only to override the automatic management manually.
Group diagnostics
Every group (1-7) tracks its own PDO frame-loss statistics. Read the expected WKC and the frame-loss counters through the public interfaces:
uint16_t GetGroupExpectedWKC(uint16_t master_index, uint8_t group);
int GetPDOFrameLossStats(uint16_t master_index, uint8_t group,
uint32_t* out_total,
uint32_t* out_consecutive_miss,
uint32_t* out_max_consecutive_miss);
GetGroupExpectedWKC() returns the expected WKC of the group; GetPDOFrameLossStats() returns the cumulative TX frame count, the current consecutive missed-frame count and the maximum consecutive missed-frame count for that group (all cleared by ResetDiagnostics()). For the actual WKC and for frame-loss alarms, subscribe to the PDOFrameLoss event instead of polling per-frame values on the real-time path.
Example:
uint32_t total = 0, miss = 0, max_miss = 0;
GetPDOFrameLossStats(master, 1, &total, &miss, &max_miss);
printf("组1: 期望 WKC=%u, 连续丢=%u, 累计帧=%u\n", /* Group 1: expected WKC=%u, consecutive losses=%u, total frames=%u */
GetGroupExpectedWKC(master, 1), miss, total);
The group argument of the PDOFrameLoss event identifies which group lost frames:
static void on_frame_loss(uint16_t mi, 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 */
}
RegisterPDOFrameLossCallback(on_frame_loss);
Configuration sequence
INIT → PRE_OP → SAFE_OP → OP
│ │ │
│ │ └─ group configuration is locked; PDO starts sending per group
│ │
│ └─ SetSlaveGroup(master, slave, group)
│ SetGroupCycleDivider(master, group, divider)
│ (groups that have slaves are enabled automatically on entering SafeOp)
│
└─ Initialize / SetNetwork
SetSlaveGroup()takes effect when the master enters SAFE_OP- Set the group cycle dividers after
SetNetwork()and beforeSetStateSequence() - The group configuration locks automatically once SAFE_OP is entered
Full example
#include "ethercat.h"
#include <stdio.h>
static void on_frame_loss(uint16_t mi, 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 */
}
int main(void) {
uint16_t master = Initialize();
SetNetwork(master, "\\Device\\NPF_{GUID}", "");
/* 1. assign slaves to groups */
SetSlaveGroup(master, 3, 1); /* IO modules */
SetSlaveGroup(master, 4, 1);
/* 2. set the group cycle divider */
SetGroupCycleDivider(master, 1, 4); /* group 1: 250 Hz */
/* 3. set the cycle time */
SetMasterLoopCycleTime(master, 1000000); /* 1ms */
/* 4. register the callback */
RegisterPDOFrameLossCallback(on_frame_loss);
/* 5. start */
SetStateSequence(master, EC_STATE_OPERATIONAL, 10000);
Start(master);
/* 6. read group status (the group interfaces accept only 1-7; unassigned slaves land in group 1) */
printf("活跃组数: %d\n", GetActiveGroupCount(master)); /* Active group count: %d */
printf("组 1: %d 个从站, 分频=%d\n", /* Group 1: %d slaves, cycle divider=%d */
GetGroupSlaveCount(master, 1), GetGroupCycleDivider(master, 1));
printf("组 2: %d 个从站, 分频=%d\n", /* Group 2: %d slaves, cycle divider=%d */
GetGroupSlaveCount(master, 2), GetGroupCycleDivider(master, 2));
getchar();
Stop(master);
Dispose(master);
return 0;
}