Slave events
All event callbacks carry master_index and slave_index parameters; the caller filters as needed.
FSoE safety events are registered through a separate callback.
Feature overview
| Category | Registration function | Read/write | Description |
|---|---|---|---|
| State changes | RegisterSlaveStateChangeCallbackSync/Async | Write-only (register) | Slave EtherCAT state changes |
| State changes with AL code | RegisterSlaveStateChangeCallbackWithAl | Write-only (register) | State change plus the AL code at the moment of the change (16-bit raw code + whether it is trustworthy) |
| Emergency messages | RegisterEmergencyEventCallback | Write-only (register) | CoE Emergency messages |
| Hot-plug | RegisterSlaveDiscoveryCallbackSync/Async | Write-only (register) | Slave online/offline |
| DC sync lost | SetDCSyncLostCallback | Write-only (register) | DC synchronization deviation exceeds the threshold |
| PreOP reconfiguration | RegisterSlavePreOpReconfigCallback | Write-only (register) | Automatic recovery after hot-plug |
PDO process data is exchanged by the real-time communication layer. The mechanism that detected input data changes on every cycle and fired a callback has been removed; this SDK no longer provides RegisterInputDataChangedCallback. The recommended way to read slave inputs is to call GetIO once after entering OP to map the packed struct onto the process image, then read fields directly from a callback or any thread; CopySlaveInputPdo is the snapshot alternative.
State changes
void RegisterSlaveStateChangeCallbackSync(state_change_callback_t callback);
void RegisterSlaveStateChangeCallbackAsync(state_change_callback_t callback);
Notification of slave EtherCAT state changes.
Callback type:
typedef void (*state_change_callback_t)(uint16_t master_index, uint16_t slave_index,
int old_state, int new_state);
State changes with AL code
void RegisterSlaveStateChangeCallbackWithAl(state_change_with_al_callback_t callback);
void UnregisterSlaveStateChangeCallbackWithAl(void);
This is a parallel channel to RegisterSlaveStateChangeCallbackSync/Async: both share the same state-change listener table underneath and are each delivered once on the same tick; it does not replace the old callback.
Callback type:
typedef void (*state_change_with_al_callback_t)(
uint16_t master_index, uint16_t slave_index,
int old_state, int new_state,
uint16_t al_status_code, BOOL al_status_code_known);
Callback parameters:
master_index/slave_index— master / slave indexold_state/new_state— EtherCAT state before / after the changeal_status_code— the full 16-bit AL Status Code at the moment of the change; it is always0xFFFFwhenal_status_code_known == FALSE, which only means "the AL code is unavailable at this moment", not any error codeal_status_code_known— whether the AL code is trustworthy
al_status_code_known == FALSE conditions: slave index 0 (master aggregate state, which carries no AL code) / shim unavailable / null slave pointer / read failure / GetSlaveIsLost() is true / value == 0xFFFF. In that case al_status_code must not be read as "no error"; for the live truth "right now", use GetSlaveALStatusCodeLiveRead().
The AL code comes from a native cached field (zero frames, no extra bus frame), so even after the slave has dropped out of OP and recovered you still get the reason code from the moment it dropped.
Registration and unregistration must live in the same .c file (the header-only trampoline is file-static); passing NULL unregisters. Unregistering repeatedly is safe and does not fail.
Example:
static void on_state_changed_with_al(uint16_t mi, uint16_t si,
int old_state, int new_state,
uint16_t al_code, BOOL known)
{
if (known)
printf("[%u:%u] 状态 0x%02X -> 0x%02X, AL=0x%04X\n", mi, si, old_state, new_state, al_code); /* [%u:%u] state 0x%02X -> 0x%02X, AL=0x%04X */
else
printf("[%u:%u] 状态 0x%02X -> 0x%02X, AL 码不可信\n", mi, si, old_state, new_state); /* [%u:%u] state 0x%02X -> 0x%02X, AL code cannot be trusted */
}
RegisterSlaveStateChangeCallbackWithAl(on_state_changed_with_al);
/* before exit */
UnregisterSlaveStateChangeCallbackWithAl();
Emergency messages
void RegisterEmergencyEventCallback(emergency_callback_t callback);
CoE Emergency message notification.
Callback type:
typedef void (*emergency_callback_t)(uint16_t master_index, uint16_t slave_index,
uint16_t error_code, uint16_t error_reg, uint8_t b1, uint16_t w1, uint16_t w2);
Callback parameters:
error_code(uint16_t) — error code (CANopen Emergency Error Code)error_reg(uint16_t) — error register (object 0x1001)b1(uint8_t) — manufacturer-specific data (byte 3)w1(uint16_t) — manufacturer-specific data (bytes 4-5)w2(uint16_t) — manufacturer-specific data (bytes 6-7)
Slave discovery/removal (hot-plug)
void RegisterSlaveDiscoveryCallbackSync(discovery_callback_t callback);
void RegisterSlaveDiscoveryCallbackAsync(discovery_callback_t callback);
Slave online/offline notification.
Callback type:
typedef void (*discovery_callback_t)(uint16_t master_index, uint16_t slave_index, BOOL is_found);
DC sync lost
void SetDCSyncLostCallback(dc_sync_lost_callback_t callback);
Notification when the DC synchronization deviation exceeds the threshold.
Callback type:
typedef void (*dc_sync_lost_callback_t)(uint16_t master_index, uint16_t slave_index, int diff_ns);
PreOP reconfiguration
void RegisterSlavePreOpReconfigCallback(preop_reconfig_callback_t callback);
Notification when a slave needs PreOP reconfiguration (automatic recovery after hot-plug).
Callback type:
typedef void (*preop_reconfig_callback_t)(uint16_t master_index, uint16_t slave_index);
Identity mismatch / port link changes
void RegisterSlaveIdentityMismatchCallback(identity_mismatch_callback_t callback);
void RegisterSlavePortLinkChangedCallback(port_link_changed_callback_t callback);
- Identity mismatch: fires when the Vendor/Product read after a power cycle and re-insertion does not match the configuration (the revision is not part of the identity check); the slave enters
IDENT_REJECTEDandAcknowledgeSlaveReplacement(mi, si)must be called so the SDK probes it again. - Port link change: fires when ESC port P0-P3 goes down or recovers, used to locate a faulty cable segment.
Callback type:
typedef void (*identity_mismatch_callback_t)(uint16_t master_index, uint16_t slave_index,
uint32_t expected_vendor, uint32_t expected_product, uint32_t expected_revision,
uint32_t actual_vendor, uint32_t actual_product, uint32_t actual_revision);
typedef void (*port_link_changed_callback_t)(uint16_t master_index, uint16_t slave_index,
uint8_t port, BOOL is_up);
Unregistering subscriptions
Every event type has an unregister entry that corresponds one-to-one with its registration (master/events.h; pass the original callback pointer; passing NULL is a no-op):
| Register | Unregister |
|---|---|
RegisterSlaveStateChangeCallbackSync / RegisterSlaveStateChangeCallbackAsync | UnregisterSlaveStateChangeCallbackSync / UnregisterSlaveStateChangeCallbackAsync |
RegisterSlaveStateChangeCallbackWithAl | UnregisterSlaveStateChangeCallbackWithAl |
RegisterEmergencyEventCallback | UnregisterEmergencyEventCallback |
RegisterSlaveDiscoveryCallbackSync / RegisterSlaveDiscoveryCallbackAsync | UnregisterSlaveDiscoveryCallbackSync / UnregisterSlaveDiscoveryCallbackAsync |
SetDCSyncLostCallback | UnsetDCSyncLostCallback |
RegisterSlavePreOpReconfigCallback | UnregisterSlavePreOpReconfigCallback |
RegisterSlaveIdentityMismatchCallback | UnregisterSlaveIdentityMismatchCallback |
RegisterSlavePortLinkChangedCallback | UnregisterSlavePortLinkChangedCallback |
To clear the whole table for a master, use darra_unregister_master_callbacks(master_index) / darra_unregister_all_callbacks() from master/callback_router.h.
Thread safety
Synchronous callbacks (*Sync) run directly on the real-time PDO thread; do not perform lengthy operations there (such as file I/O, network requests, lock contention).
Asynchronous callbacks (*Async) run on a separate thread: slightly higher latency, but safer.
Avoid lengthy operations inside a callback. To process a large amount of data, put it on a queue and handle it asynchronously:
static void on_state_sync(uint16_t mi, uint16_t si, int old_s, int new_s) {
/* enqueue quickly; no lengthy work here */
enqueue_event(mi, si, old_s, new_s);
}
Full example
#include "ethercat.h"
#include <stdio.h>
/* ===== state events ===== */
static void on_state(uint16_t mi, uint16_t si, int old_s, int new_s) {
printf("从站 %d 状态变化: %d -> %d\n", si, old_s, new_s); /* Slave %d state change: %d -> %d */
}
/* ===== hot-plug events ===== */
static void on_discovery(uint16_t mi, uint16_t si, BOOL found) {
printf("从站 %d %s\n", si, found ? "上线" : "离线"); /* Slave %d %s (online / offline) */
}
/* ===== emergency message events ===== */
static void on_emcy(uint16_t mi, uint16_t si,
uint16_t code, uint16_t reg, uint8_t b1, uint16_t w1, uint16_t w2) {
printf("从站 %d 紧急消息: 错误码=0x%04X, 寄存器=0x%04X\n", si, code, reg); /* Slave %d emergency message: error code=0x%04X, register=0x%04X */
}
/* ===== DC sync lost events ===== */
static void on_dc_sync_lost(uint16_t mi, uint16_t si, int diff_ns) {
printf("从站 %d DC 同步丢失: 偏差 %d ns\n", si, diff_ns); /* Slave %d lost DC sync: deviation %d ns */
}
/* ===== PreOP reconfiguration events ===== */
static void on_preop_reconfig(uint16_t mi, uint16_t si) {
printf("从站 %d 需要 PreOP 重配置\n", si); /* Slave %d needs PreOP reconfiguration */
}
int main(void) {
/* register all slave event callbacks */
RegisterSlaveStateChangeCallbackSync(on_state);
RegisterSlaveDiscoveryCallbackSync(on_discovery);
RegisterEmergencyEventCallback(on_emcy);
SetDCSyncLostCallback(on_dc_sync_lost);
RegisterSlavePreOpReconfigCallback(on_preop_reconfig);
uint16_t master = Initialize();
SetNetwork(master, "\\Device\\NPF_{...}", "");
SetStateSequence(master, EC_STATE_OPERATIONAL, 10000);
Start(master);
printf("按回车退出...\n"); /* Press Enter to exit... */
getchar();
Stop(master);
Dispose(master);
return 0;
}