Slave API
All slave functions take uint16_t master_index and uint16_t slave_index (1-based) parameters.
Quick start
#include "ethercat.h"
uint16_t master = Initialize();
uint16_t count = SetNetwork(master, "\\Device\\NPF_{GUID}", "");
printf("从站总数: %u\n", count); /* Total slaves: %u */
/* read slave information (slave_index is 1-based) */
char name[128];
GetSlaveName(master, 1, name, sizeof(name));
uint32_t vid = GetSlaveVendorId(master, 1);
uint32_t pid = GetSlaveProductCode(master, 1);
printf("从站1: %s VID=0x%08X PID=0x%08X\n", name, vid, pid); /* Slave1: %s VID=0x%08X PID=0x%08X */
Index and group access
/* access directly through master_index + slave_index; slave_index is 1-based */
char name[128];
GetSlaveName(master, 1, name, sizeof(name));
/* slave count of group 1 (the default group) */
uint16_t group_count = GetGroupSlaveCount(master, 1);
Address and identity
/* physical configuration address (auto-assigned) */
uint16_t addr = GetSlaveConfigAddr(master, 1); /* for example 0x1001 */
uint16_t alias = GetSlaveAliasAddr(master, 1); /* EEPROM alias */
/* identity */
char name[128];
GetSlaveName(master, 1, name, sizeof(name));
uint32_t vid = GetSlaveVendorId(master, 1);
uint32_t pid = GetSlaveProductCode(master, 1);
uint32_t rev = GetSlaveRevision(master, 1);
uint32_t sn = GetSlaveSerialNumber(master, 1); /* native snapshot: 0 may mean "not read" or a real 0; use EEPROM for the TryGetSlaveSerialNumber real value */
I/O area and mailbox properties
/* PDO area bit count / byte count / IOmap offset / start bit */
uint16_t in_bits = GetSlaveInputBits(master, 1);
uint32_t in_bytes = GetSlaveInputBytes(master, 1);
uint32_t in_off = GetSlaveInputOffset(master, 1);
uint8_t in_sbit = GetSlaveInputStartBit(master, 1);
uint16_t out_bits = GetSlaveOutputBits(master, 1);
uint32_t out_bytes = GetSlaveOutputBytes(master, 1);
uint32_t out_off = GetSlaveOutputOffset(master, 1);
uint8_t out_sbit = GetSlaveOutputStartBit(master, 1);
/* DC state and timing */
BOOL has_dc = GetSlaveHasDC(master, 1);
int32_t cycle0 = GetSlaveDCCycle0(master, 1);
int32_t shift = GetSlaveDCShift(master, 1);
int32_t pd = GetSlavePropagationDelay(master, 1);
/* mailbox protocol and buffer */
uint16_t mbx_proto = GetSlaveMailboxProto(master, 1);
uint16_t mbx_wr_len = GetSlaveMailboxWriteLength(master, 1);
uint16_t mbx_rd_len = GetSlaveMailboxReadLength(master, 1);
Protocol access
| Protocol | Description | Documentation |
|---|---|---|
| CoE | CANopen over EtherCAT | SDO object dictionary |
| CiA 402 | Servo drives | State machine and enable |
| CiA 401 | Generic I/O modules | Digital/analog I/O |
| SoE | Servo over EtherCAT | SERCOS parameters |
| FoE | File over EtherCAT | File transfer |
| EoE | Ethernet over EtherCAT | Ethernet tunnel |
| AoE | ADS over EtherCAT | Beckhoff ADS |
| VoE | Vendor over EtherCAT | Vendor protocol |
| FSoE | Functional Safety | Functional safety |
| MDP | Modular devices | Module discovery |
PDO data read/write
After entering OP, use GetIO to map packed structs onto the process image, and let multiple threads modify fields directly:
#pragma pack(push, 1)
typedef struct {
uint16_t status_word;
int32_t actual_position;
} in_pdo_t;
typedef struct {
uint16_t control_word;
int32_t target_position;
} out_pdo_t;
#pragma pack(pop)
int32_t out_size = 0, in_size = 0;
uint8_t *out_ptr = NULL, *in_ptr = NULL;
uint8_t st = GetIO(master, 1, &out_size, &out_ptr, &in_size, &in_ptr);
if (ec_pdo_io_is_ok(st) && in_ptr && out_ptr
&& in_size >= (int32_t)sizeof(in_pdo_t) && out_size >= (int32_t)sizeof(out_pdo_t)) {
in_pdo_t *in = (in_pdo_t *)in_ptr;
out_pdo_t *out = (out_pdo_t *)out_ptr;
out->control_word = 0x000F;
printf("位置: %d\n", in->actual_position); /* Position: %d */
}
EMCY emergency messages
An EtherCAT slave sends EMCY (Emergency) messages when it detects an exception. The SDK caches the EMCY message history of each slave automatically.
#pragma pack(push, 1)
typedef struct {
uint16_t error_code; /* error code */
uint8_t error_register; /* error register */
uint8_t data[5]; /* vendor-defined data */
uint16_t slave_index; /* slave index */
uint32_t timestamp_ms; /* timestamp (milliseconds) */
} ec_emcy_record_t;
#pragma pack(pop)
int EmcyGetCount(uint16_t master, uint16_t slave);
/* the caller supplies an ec_emcy_record_t buffer; returns the number of records written */
int EmcyGetHistory(uint16_t master, uint16_t slave,
ec_emcy_record_t* out, int max);
void EmcyClearHistory(uint16_t master, uint16_t slave);
Example:
ec_emcy_record_t hist[16];
int n = EmcyGetHistory(master, 1, hist, 16);
for (int i = 0; i < n; i++)
printf("EMCY 错误码=0x%04X 寄存器=0x%02X\n", /* EMCY error code=0x%04X register=0x%02X */
hist[i].error_code, hist[i].error_register);
EmcyClearHistory(master, 1);
Topology query
The SDK builds and queries the slave network topology, exposing the parent/child relations and the tree structure between slaves.
int TopologyBuild(uint16_t master_index, ec_topology_node_t* out_nodes, int max_nodes);
int TopologyGetRoots(uint16_t master_index, uint16_t* out_roots, int max_roots);
int TopologyGetChildren(uint16_t master_index, uint16_t parent_index,
uint16_t* out_children, int max_children);
Call TopologyBuild() once, usually after entering OP. TopologyGetRoots() returns the list of root slaves connected directly to the master.
Example — print the topology tree:
void print_tree(uint16_t slave_idx, int depth) {
char name[128];
GetSlaveName(master, slave_idx, name, sizeof(name));
for (int i = 0; i < depth * 2; i++) printf(" ");
printf("[%d] %s\n", slave_idx, name);
uint16_t children[64];
int n = TopologyGetChildren(master, slave_idx, children, 64);
for (int i = 0; i < n; i++)
print_tree(children[i], depth + 1);
}
ec_topology_node_t nodes[64];
TopologyBuild(master, nodes, 64);
uint16_t roots[64];
int n = TopologyGetRoots(master, roots, 64);
for (int i = 0; i < n; i++) print_tree(roots[i], 0);
Subpages
| Page | Description |
|---|---|
| Properties and methods | Basic slave properties, validation, topology, ESI |
| PDO inputs and outputs | Zero-copy pointers, typed reads and writes, batch operations |
| DC synchronization | Slave-level DC configuration |
| Events | Slave state changes, hot-plug, emergency messages |