AoE (ADS over EtherCAT)
The AoE protocol implements ADS (Automation Device Specification) over EtherCAT communication.
Query it with the tri-state ec_slave_get_proto_support_state(master, slave, MBXPROT_AOE) (returns EC_PROTO_SUPPORT_YES/NO/UNKNOWN). Note that AoE has no dedicated details field in SII General: when the mailbox capability bit is 0 the only possible answer is UNKNOWN, never "the device does not support it".
The public AoE interface is aoe_read() / aoe_write() plus the subscription manager aoe_sub_* (no handle, no function-pointer table).
Data read and write
aoe_read() / aoe_write() are the public one-way ADS read and write entry points; they address data by IndexGroup + IndexOffset as defined in ETG.1020.
Notifications
AoE notifications are managed through the public subscription manager aoe_sub_*: after a subscription is added, data changes arrive through an aoe_data_callback_t callback; destroying the manager removes all subscriptions automatically. Notification transfer modes follow the ADS convention: 1 Cyclic, 2 OnChange, 3 CyclicInDevice, 4 OnChangeInDevice.
Cross-protocol gateway
AoE routing provides access to other mailbox protocols (ETG.1020) and supports transparent forwarding for CoE and SoE.
Protocol routing constants:
| Protocol | IndexGroup | IndexOffset encoding |
|---|---|---|
| CoE (CANopen over EtherCAT) | 0xF302 | (index << 16) | subindex |
| SoE (Servo over EtherCAT) | 0xF420 | IDN number |
aoe_read_coe() / aoe_write_coe() / aoe_read_soe() / aoe_write_soe() are not part of the C SDK delivery surface. For CoE, call the public sdo_read_* / sdo_write_* directly; for SoE, call the public soe_read_* / soe_write_* directly. When you really need to go through AoE routing, assemble IndexGroup / IndexOffset yourself with aoe_read() / aoe_write() using the table above.
Example (the same CoE object, using the public SDO interface directly):
#include "ethercat.h"
/* read CoE object 0x6041:00 (status word) */
uint16_t statusWord = 0;
if (sdo_read_u16(master, 1, 0x6041, 0x00, &statusWord) == 0) {
printf("状态字: 0x%04X\n", statusWord); /* Status word: 0x%04X */
}
/* write CoE object 0x6040:00 (control word) */
uint16_t ctrlWord = 0x000F;
sdo_write_u16(master, 1, 0x6040, 0x00, ctrlWord);
AoE read and write (slave/aoe.h)
slave/aoe.h provides one-way static __inline wrappers aoe_read() / aoe_write(). Both are header-inline functions (no handle required).
aoe_read() / aoe_write() are defined in slave/aoe.h; the first parameter is the master index, and the caller releases the returned buffer with FreeMemory().
aoe_result_code_t
ADS result error code enum (slave/aoe.h, ETG.1020 Table 16). Value layout: the high byte is the error class (0x0700 range = ADS device error, 0x0740 range = client error), the low byte is the specific code.
typedef enum {
AOE_RC_NO_ERROR = 0x0000, /* no error */
AOE_RC_INTERNAL_ERROR = 0x0001, /* internal error */
AOE_RC_NO_REALTIME = 0x0002, /* no real-time */
AOE_RC_INSERT_MAILBOX_ERROR = 0x0004, /* mailbox full */
AOE_RC_TARGET_PORT_NOT_FOUND = 0x0006, /* target port not found */
AOE_RC_TARGET_MACHINE_NOT_FOUND = 0x0007, /* target device not found */
AOE_RC_AMS_SYNC_TIMEOUT = 0x0015, /* AMS sync timeout */
/* ADS device error class (0x0700 range) */
AOE_RC_DEVICE_INVALID_GROUP = 0x0700, /* invalid device group */
AOE_RC_DEVICE_INVALID_ACCESS = 0x0702, /* invalid device access */
AOE_RC_DEVICE_BUSY = 0x0706, /* device busy */
AOE_RC_DEVICE_NOT_FOUND = 0x0708, /* device not found */
AOE_RC_DEVICE_TIMEOUT = 0x0712, /* device timeout */
AOE_RC_DEVICE_ACCESS_DENIED = 0x071C, /* device access denied */
/* ADS client error class (0x0740 range) */
AOE_RC_CLIENT_ERROR = 0x0740, /* client error */
AOE_RC_CLIENT_INVOKE_TIMEOUT = 0x0744, /* client invoke timeout */
AOE_RC_CLIENT_PORT_NOT_OPEN = 0x0745 /* client port not open */
/* ... see slave/aoe.h for the full enum, 50+ entries */
} aoe_result_code_t;
The full error code list is in slave/aoe.h and in the Beckhoff ADS Return Codes (Information System). On failure, the error code semantics come from this enum and are interpreted together with the BOOL return value of aoe_read() / aoe_write().
aoe_read()
/* slave/aoe.h — static __inline one-way read */
BOOL aoe_read(uint16_t master_index, uint16_t slave,
uint32_t index_group, uint32_t index_offset,
uint32_t read_len, int timeout,
uint8_t** out_data, uint32_t* out_bytes_read);
One-way ADS read (IndexGroup + IndexOffset).
Parameters:
master_index(uint16_t) — master indexslave(uint16_t) — slave indexindex_group(uint32_t) — index groupindex_offset(uint32_t) — index offsetread_len(uint32_t) — read lengthtimeout(int) — timeout (microseconds)out_data(uint8_t**) — output data pointer, must be released withFreeMemory()out_bytes_read(uint32_t*) — actual number of bytes read (output)
Return value:
BOOL— returnsTRUEon success
aoe_write()
/* slave/aoe.h — static __inline one-way write */
BOOL aoe_write(uint16_t master_index, uint16_t slave,
uint32_t index_group, uint32_t index_offset,
const uint8_t* data, uint32_t data_len, int timeout);
One-way ADS write.
Return value:
BOOL— returnsTRUEon success
Example:
#include "ethercat.h" /* aoe_read / aoe_write are declared in slave/aoe.h, pulled in by the umbrella header */
uint8_t* buf = NULL;
uint32_t got = 0;
if (aoe_read(master, 1, 0x4020, 0, 4, 500000, &buf, &got)) {
printf("读到 %u 字节\n", got); /* Read %u bytes */
FreeMemory(buf); /* call the plain symbol directly, no handle needed */
} else {
/* on failure, see the aoe_result_code_t enum in slave/aoe.h for error code semantics */
printf("AoE 读取失败\n"); /* AoE read failed */
}
uint16_t cmd = 0x0001;
aoe_write(master, 1, 0x4020, 0, (uint8_t*)&cmd, sizeof(cmd), 500000);
AoE subscription manager (ethercat_advanced.h)
aoe_sub_create()
aoe_sub_mgr_t* aoe_sub_create(uint16_t master, uint16_t slave);
Creates an AoE subscription manager.
Return value:
aoe_sub_mgr_t*— manager instance, must be released withaoe_sub_destroy()
aoe_sub_destroy()
void aoe_sub_destroy(aoe_sub_mgr_t* mgr);
Destroys the manager (all subscriptions are removed automatically).
aoe_sub_add()
int aoe_sub_add(aoe_sub_mgr_t* mgr, uint32_t index_group, uint32_t index_offset,
uint32_t length, uint32_t trans_mode, uint32_t max_delay, uint32_t cycle_time,
aoe_data_callback_t callback, void* user_data);
Adds a subscription.
Return value:
int— subscription ID (a value less than 0 means failure)
aoe_sub_remove()
int aoe_sub_remove(aoe_sub_mgr_t* mgr, int subscription_id);
Removes the given subscription.
aoe_sub_remove_all()
int aoe_sub_remove_all(aoe_sub_mgr_t* mgr);
Removes all subscriptions.
aoe_sub_count()
int aoe_sub_count(aoe_sub_mgr_t* mgr);
Returns the number of active subscriptions.
Full example
#include "ethercat.h"
#include "ethercat_advanced.h"
#include <stdio.h>
/* data change callback */
void on_data_changed(uint32_t handle, uint32_t ig, uint32_t io,
const uint8_t* data, uint32_t size, void* user) {
printf("数据变化: handle=%u, size=%u\n", handle, size); /* Data change: handle=%u, size=%u */
}
int main(void)
{
uint16_t master = Initialize();
SetNetwork(master, "\\Device\\NPF_{...}", "");
SetStateSequence(master, EC_STATE_PRE_OP, 5000);
uint16_t slave = 1;
/* read ADS data */
uint8_t* buf = NULL;
uint32_t got = 0;
if (aoe_read(master, slave, 0x4020, 0, 4, 500000, &buf, &got)) {
printf("读到 %u 字节\n", got); /* Read %u bytes */
FreeMemory(buf);
}
/* read CoE object 0x6041:00 (status word) */
uint16_t status_word = 0;
if (sdo_read_u16(master, slave, 0x6041, 0x00, &status_word) == 0) {
printf("状态字: 0x%04X\n", status_word); /* Status word: 0x%04X */
}
/* use the subscription manager */
aoe_sub_mgr_t* mgr = aoe_sub_create(master, slave);
int sub_id = aoe_sub_add(mgr, 0x4020, 0, 4,
2 /* OnChange */, 0, 100000, on_data_changed, NULL);
printf("活跃订阅: %d\n", aoe_sub_count(mgr)); /* Active subscriptions: %d */
/* release resources */
aoe_sub_destroy(mgr);
Dispose(master);
return 0;
}
AoE dx_* interface
The 14 dx_aoe_* functions in slave/aoe.h are the low-level AoE entry points: command exchange, IndexGroup / IndexOffset reads and writes, device information and state, notification subscription, notification listening, and NetID / port configuration. All are inline wrappers in the header; the first parameter is always master_index, so no handle is needed.
| Category | Function | Return type | Read/write | Description |
|---|---|---|---|---|
| Command exchange | dx_aoe_send_command | BOOL | read/write | Sends one ADS command for target_port / command_id; timeout is the timeout. *response is allocated by the runtime and its length is *response_size; it must be released with FreeMemory() |
| Data read/write | dx_aoe_read_write | BOOL | read/write | ADS read/write semantics: located by index_group + index_offset. When write_len is a value other than 0, write_data is sent; read_len is the expected read length. *read_data is allocated by the runtime and must be released with FreeMemory(); the actual byte count is in *bytes_read |
| Device information | dx_aoe_read_device_info | BOOL | read-only | Reads the device version and name: major / minor / build are output parameters; the name buffer is provided by the caller with capacity name_size |
| State | dx_aoe_read_state | BOOL | read-only | Reads two 16-bit output parameters: the ADS state and the device state |
| dx_aoe_write_control | BOOL | write-only | Writes the ADS state / device state, optionally with additional data of data_size | |
| Notification subscription | dx_aoe_add_notification | BOOL | write-only | Adds a DeviceNotification (0x08) subscription by IndexGroup + IndexOffset; trans_mode / max_delay / cycle_time control transmission; *handle is an output parameter used for deletion and registration |
| dx_aoe_del_notification | BOOL | write-only | Deletes that notification subscription by handle | |
| Configuration | dx_aoe_set_config | BOOL | write-only | Sets the NetID / port quadruple: target_net_id + target_port + source_net_id + source_port; this entry point has no timeout parameter |
| dx_aoe_get_config | BOOL | read-only | Reads back the same quadruple: the two NetID buffers are provided by the caller and the two ports are uint16_t output parameters | |
| Notification listening | dx_aoe_start_notification_listener | BOOL | write-only | Starts the notification listener thread for this master |
| dx_aoe_stop_notification_listener | BOOL | write-only | Stops the notification listener thread for this master | |
| dx_aoe_is_notification_listening | BOOL | read-only | Whether the listener thread for this master is running | |
| dx_aoe_register_notification | int | write-only | Registers a callback on this master's listener thread by handle + index_group / index_offset + data_length (callback is of type aoe_notification_callback_t and takes user_data); the return value is the local subscription index to pass to dx_aoe_unregister_notification | |
| dx_aoe_unregister_notification | BOOL | write-only | Unregisters the local subscription by subscription_index |
Division of work and ownership
- add / del and register / unregister are two layers:
dx_aoe_add_notification/dx_aoe_del_notificationwork on slave-side notification subscriptions, andhandleis the credential between them;dx_aoe_register_notification/dx_aoe_unregister_notificationhandle local callback dispatch on this master's listener thread, linking back to the subscription throughhandle. - Listening is per master (the header puts it as "per master: each master has its own thread and subscriptions"): every master has its own listener thread and its own subscription table, so pushes on master A reach only the callbacks registered on A.
- Buffer ownership:
*responsefromdx_aoe_send_commandand*read_datafromdx_aoe_read_writeare both allocated by the runtime and must be released withFreeMemory(); thenamebuffer ofdx_aoe_read_device_infoand the two NetID buffers ofdx_aoe_get_configare provided by the caller. dx_aoe_set_config/dx_aoe_get_configwrite / read back the NetID and port on both the target and source sides in one call; the four components always come as a set. They returnFALSEon failure.
Example
#include "ethercat.h" /* dx_aoe_* are declared in slave/aoe.h and pulled in through the umbrella header*/
#include <stdio.h>
/* aoe_notification_callback_t (ethercat_types.h) */
static void on_aoe_notify(uint16_t master_index, uint16_t slave, uint32_t handle,
uint64_t timestamp, const uint8_t* data, uint32_t data_size,
void* user_data)
{
(void)master_index; (void)slave; (void)timestamp; (void)user_data; (void)data;
printf("通知 handle=%u, %u 字节\n", handle, data_size); /* Notification handle=%u, %u bytes */
}
int main(void)
{
uint16_t master = Initialize();
SetNetwork(master, "\\Device\\NPF_{...}", "");
SetStateSequence(master, EC_STATE_PRE_OP, 5000);
/* 1. Issue the command: *response is allocated by the runtime, call FreeMemory when done*/
uint8_t* resp = NULL;
uint32_t resp_size = 0;
if (dx_aoe_send_command(master, 1, 0xFFFF, 0x0001, NULL, 0, &resp, &resp_size, 5000)) {
printf("响应 %u 字节\n", resp_size); /* Response %u bytes */
FreeMemory(resp);
}
/* 2. IndexGroup / IndexOffset read/write: write_len = 0 means a pure read*/
uint8_t* rd = NULL;
uint32_t got = 0;
if (dx_aoe_read_write(master, 1, 0x4020, 0, 4, 0, NULL, &rd, &got, 5000)) {
printf("读到 %u 字节\n", got); /* Read %u bytes */
FreeMemory(rd);
}
/* 3. Notifications: start the listener first -> add a subscription on the slave side to get a handle -> register the callback locally*/
dx_aoe_start_notification_listener(master);
uint32_t handle = 0;
if (dx_aoe_add_notification(master, 1, 0x4020, 0, 4,
2 /* OnChange */, 0, 100, &handle, 5000)) {
int sub = dx_aoe_register_notification(master, 1, handle, 0x4020, 0, 4,
on_aoe_notify, NULL);
/* ... application loop ...*/
dx_aoe_unregister_notification(master, sub);
dx_aoe_del_notification(master, 1, handle, 5000);
}
dx_aoe_stop_notification_listener(master);
Dispose(master);
return 0;
}