Skip to main content

EoE (Ethernet over EtherCAT)

The EoE protocol carries Ethernet communication over the EtherCAT bus. It supports configuring IP/MAC/DNS addresses and sending and receiving Ethernet frames, and follows ETG.1000.6 and ETG.1020.

Obtained from slave.GetEoE(), which returns an EoE& reference. Use IsSupported() to check whether the slave supports EoE.​

Honest boundaries​

EoE in this SDK is an Ethernet frame tunnel over the EtherCAT mailbox; it is not an operating-system network adapter.

  • No TAP / tun: it creates no Windows TAP or Linux tun/tap device
  • No DHCP server / NAT: it assigns no addresses and performs no routing or NAT
  • No automatic fragmentation at MTU 1500: the runtime library splits frames into 32-byte blocks according to the slave mailbox capacity; the whole-frame limit is the ETG.1000.6 §5.7 Frame Offset (6 bit × 32 B) = 2016 bytes. At this layer the C++ SendFrame enforces the 2016-byte limit and returns false without sending when it is exceeded
  • Receive buffer: ReceiveFrame copies and returns the frame at its actual length, also capped at 2016 bytes; it is not an arbitrary size

Properties​

The read/write accessors act immediately: the getter reads the current value from the device, and the setter validates the format and writes to the device straight away. A failed read returns an empty std::optional (this read did not succeed, which is not the same as "not configured"); a failed write returns false.

For address parameters the uint32_t value is the four dotted-decimal octets packed as a.b.c.d into 0xAABBCCDD (first octet in the most significant byte): 192.168.1.100 = 0xC0A80164, 255.255.255.0 = 0xFFFFFF00, 8.8.8.8 = 0x08080808.

CategoryPropertyTypeRead/writeDescription
Network configurationCanSendFrame()boolread-onlyWhether sending Ethernet frames is supported (always returns true)
CanReceiveFrame()boolread-onlyWhether receiving Ethernet frames is supported (always returns true)
GetIPConfig / SetIPConfigstd::optional<std::tuple<uint32_t,uint32_t,uint32_t>>read/writeIP / subnet / gateway (uint32_t = 0xAABBCCDD)
GetMAC / SetMACstd::optional<std::array<uint8_t,6>>read/writeMAC address (6 bytes)
GetDNS / SetDNSstd::optional<std::tuple<uint32_t,std::string>>read/writeDNS server + domain name
GetFullParam / SetFullParamstd::optional<FullParam>read/writeReads or writes IP+MAC+DNS+subnet+gateway in one call

Related structure:

struct FullParam {
uint32_t ip; // IP address
uint32_t subnet; // Subnet mask
uint32_t gateway; // Gateway
std::array<uint8_t, 6> mac; // MAC address
uint32_t dns_ip; // DNS server IP
std::string dns_name; // DNS name
};

Example:

auto& eoe = master.GetSlave(1).GetEoE();
if (eoe.IsSupported()) {
auto ip_info = eoe.GetIPConfig();
if (ip_info) {
auto [ip, mask, gw] = *ip_info;
printf("IP=%08X Mask=%08X GW=%08X\n", ip, mask, gw);
}

eoe.SetIPConfig(0xC0A80164, 0xFFFFFF00, 0xC0A80101);
uint8_t mac[6] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
eoe.SetMAC(mac);
eoe.SetDNS(0x08080808, "dns.google");
}

Address filters (ETG.1020)​

GetAddressFilter() / SetAddressFilter()​

std::optional<std::tuple<uint8_t, std::vector<uint8_t>>>
GetAddressFilter(int maxFilters = 16, int timeoutMs = 500) const;

bool SetAddressFilter(uint8_t filterCount, const uint8_t* macFilters, int timeoutMs = 500) const;

Reads the MAC address filter list, returning a {过滤器数量, MAC 地址数据} tuple; or sets the filter list in one call.

Parameters:

  • maxFilters (int) — maximum number of entries to read back, default 16
  • filterCount (uint8_t) — number of entries to write
  • macFilters (const uint8_t*) — contiguous list of MAC addresses, 6 bytes each, filterCount*6 bytes in total
  • timeoutMs (int) — timeout in milliseconds, default 500

Returns:

  • GetAddressFilter — std::optional<std::tuple<uint8_t, std::vector<uint8_t>>>, the entry count followed by the MAC data. An empty optional means this read did not succeed, not "there is no MAC yet"; a successful read with a count of 0 is what an empty filter list looks like
  • SetAddressFilter — bool, whether the write succeeded

Example:

uint8_t macs[12] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
if (!eoe.SetAddressFilter(2, macs))
std::printf("写过滤器失败\n"); // Writing the filter failed

auto filters = eoe.GetAddressFilter();
if (filters) {
auto [count, data] = *filters;
std::printf("过滤器 %d 条, 数据 %zu 字节\n", count, data.size()); // %d filters, %zu bytes of data
}

AddAddressFilter / RemoveAddressFilter / ClearAddressFilters​

bool AddAddressFilter(const uint8_t mac[6], int timeoutMs = 500) const;
bool RemoveAddressFilter(const uint8_t mac[6], int timeoutMs = 500) const;
bool ClearAddressFilters(int timeoutMs = 500) const;

Adds or removes a single MAC address filter, or clears all filters. AddAddressFilter first reads the existing list and then appends and writes it back; RemoveAddressFilter keeps only the entries that are not equal to that MAC and writes the list back. Both return false when the existing list cannot be read.

Parameters:

  • mac (const uint8_t[6]) — MAC address, 6 bytes
  • timeoutMs (int) — timeout in milliseconds, default 500

Returns:

  • bool — whether it succeeded

Example:

auto& eoe = master.GetSlave(1).GetEoE();
uint8_t mac[6] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
eoe.AddAddressFilter(mac);

auto filters = eoe.GetAddressFilter();
if (filters) {
auto [count, data] = *filters;
printf("过滤器数量: %d\n", count); // Filter count: %d
}

eoe.ClearAddressFilters();

Sending and receiving Ethernet frames​

SendFrame() / SendFrameToPort()​

bool SendFrame(const std::vector<uint8_t>& frame, int timeoutMs = 500) const;

bool SendFrameToPort(const std::vector<uint8_t>& frame, uint8_t port, int timeoutMs = 500) const;

Sends an Ethernet frame (optionally on a specified port). Fragmentation is handled by the runtime library (ETG.1000.6 §5.7). The whole frame may be larger than the slave mailbox. The complete-frame limit is the ETG Frame Offset, 2016 bytes; at this layer the C++ SendFrame enforces the 2016-byte limit and returns false without sending when it is exceeded. There is no TAP/DHCP/NAT, and no automatic fragmentation by MTU 1500.

Parameters:

  • frame (const std::vector<uint8_t>&) — complete Ethernet frame (including the Ethernet header), 1..2016 bytes; an empty frame fails immediately
  • port (uint8_t) — EoE port number; SendFrame uses the port from construction (GetEoE() returns port 0)
  • timeoutMs (int) — timeout in milliseconds, default 500

Returns:

  • bool — true = sent; false = invalid frame length, timeout or send failure

Example:

std::vector<uint8_t> frame = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x08, 0x00};
if (!eoe.SendFrame(frame))
std::printf("发送失败\n"); // Send failed

SendFrameWithTimestamp()​

bool SendFrameWithTimestamp(const std::vector<uint8_t>& frame, uint64_t timestamp,
int timeoutMs = 500) const;

Sends an Ethernet frame with an 8-byte DC timestamp appended (ETG.1000.6 §5.7 TimeAppended). When the frame length plus 8 bytes exceeds 2016, it returns false without sending.

Parameters:

  • frame (const std::vector<uint8_t>&) — Ethernet frame data (without the timestamp)
  • timestamp (uint64_t) — DC timestamp, nanoseconds
  • timeoutMs (int) — timeout in milliseconds, default 500

Returns:

  • bool — whether the send succeeded

Example:

uint64_t ts = 123456789ULL;      // Nanosecond timestamp taken from the DC clock
if (!eoe.SendFrameWithTimestamp(frame, ts))
std::printf("带时间戳发送失败\n"); // Timestamped send failed

ReceiveFrame() / ReceiveFrameFromPort()​

std::vector<uint8_t> ReceiveFrame(int timeoutMs = 500) const;

std::vector<uint8_t> ReceiveFrameFromPort(uint8_t port, int timeoutMs = 500) const;

Receives an EoE frame; returns an empty vector on failure. The returned byte count is the frame length, also capped at 2016 bytes.

Parameters:

  • port (uint8_t) — EoE port number; ReceiveFrame uses the port from construction
  • timeoutMs (int) — wait timeout in milliseconds, default 500

Returns:

  • std::vector<uint8_t> — the received frame data; an empty vector on failure

Example:

auto& eoe = master.GetSlave(1).GetEoE();
std::vector<uint8_t> frame = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x08, 0x00};
eoe.SendFrame(frame);

auto recv = eoe.ReceiveFrame();
if (!recv.empty()) {
printf("收到 %zu 字节帧\n", recv.size()); // Received a %zu byte frame
}

Ping test​

Ping()​

EoEPingResult Ping(uint32_t targetIp, int timeoutMs = 3000) const;
EoEPingResult Ping(const std::string& targetIpStr, int timeoutMs = 3000) const;

Sends an ICMP ping over EoE and waits for the response. Both the uint32_t and the string form of the IP are supported; the uint32_t form uses the same address encoding as SetIPConfig (0xAABBCCDD).

Parameters:

  • targetIp (uint32_t) — target IP address, 192.168.1.1 = 0xC0A80101
  • targetIpStr (std::string) — dotted-decimal string, for example "192.168.1.1"
  • timeoutMs (int) — timeout in milliseconds, default 3000

Returns:

  • EoEPingResult — when Success is true, RoundTripTimeMs / TTL are valid; when it is false, read ErrorMessage (for the string form, an invalid address produces the "invalid IP address" error)

Related structure:

struct EoEPingResult {
bool Success = false; // Whether it succeeded
double RoundTripTimeMs = 0; // Round trip time (milliseconds)
std::string TargetAddress; // Target address (filled in by the string overload only)
uint8_t TTL = 0; // Time to live
std::string ErrorMessage; // Error message
};

Example:

auto& eoe = master.GetSlave(1).GetEoE();
auto result = eoe.Ping(static_cast<uint32_t>(0xC0A80101)); // 192.168.1.1
if (result.Success) {
printf("Ping 成功: %.1f ms, TTL=%d\n", result.RoundTripTimeMs, result.TTL); // Ping succeeded: %.1f ms, TTL=%d
} else {
printf("Ping 失败: %s\n", result.ErrorMessage.c_str()); // Ping failed: %s
}
Ping with the string form

Ping(const std::string&) parses the dotted address and forwards to the uint32_t overload; in the current version the parsed byte order is the reverse of the SetIPConfig convention used by 0xAABBCCDD, so it may ping a byte-reversed address. When the behaviour must be predictable, use the uint32_t form.

Full example​

Network configuration and ping​

#include "ethercat.hpp"
using namespace darra::ethercat;

int main() {
dll_t dll;
EtherCATMaster master(dll);
master.SetNetwork("\\Device\\NPF_{...}")
.SetENI("config.deni")
.Build();
master.SetState(EcState::OP);
master.Start();

auto& eoe = master.GetSlave(1).GetEoE();
if (!eoe.IsSupported()) {
printf("从站不支持 EoE\n"); // Slave does not support EoE
return 1;
}

// Configure IP / subnet / gateway / DNS (uint32_t = 0xAABBCCDD)
eoe.SetIPConfig(0x0A000064, 0xFFFFFF00, 0x0A000001);
eoe.SetDNS(0x0A000001);

// Read the complete configuration
auto param = eoe.GetFullParam();
if (param) {
printf("IP: %08X, 子网: %08X, 网关: %08X\n", // IP: %08X, subnet: %08X, gateway: %08X
param->ip, param->subnet, param->gateway);
}

// Ping test (uint32_t form, 10.0.0.1)
auto ping = eoe.Ping(static_cast<uint32_t>(0x0A000001));
if (ping.Success) {
printf("Ping 成功: %.1f ms\n", ping.RoundTripTimeMs); // Ping succeeded: %.1f ms
}

getchar();
return 0;
}
The C++ EoE class does not provide ARP cache management

The C++ EoE class has no ARP methods. The ARP cache functions (eoe_arp_set / eoe_arp_clear / eoe_arp_lookup) are provided by the C header ethercat_advanced.h shipped with the package and can be called directly after including ethercat.hpp; for the signatures see the C SDK EoE page. If you need the ARP frames themselves, you can also build them with the EoE frame send and receive methods.

Timing and threading​

  • Frames travel through the slave mailbox, so the master must already be loaded and in a state where communication is possible (the examples use it after entering OP); each call bounds its wait with timeoutMs.
  • Synchronous methods run on the calling thread; asynchronous methods run on a background thread.
  • SetReceiveHook / ClearReceiveHook change master-level state and take effect for the whole network after one call; the other methods are addressed per slave instance. For the callback thread and its constraints see "Asynchronous frame receive hook".

Asynchronous frame receive hook​

A synchronous SendFrame / ReceiveFrame exchange suits request/response traffic, but a slave may also push through the EoE tunnel on its own (for example uplink Ethernet frames from the slave firmware, or SNMP traps); an application that does not keep calling ReceiveFrame loses frames. SetReceiveHook binds one receive callback to this master and invokes it directly for EoE frames from any slave, so the application thread does not have to poll. This SDK is not a DHCP server.

EoEFrameEvent and ReceiveCallback​

struct EoEFrameEvent {
uint16_t slaveIndex = 0; // Source slave (1-based)
std::vector<uint8_t> data; // Frame bytes (including the Ethernet header, excluding the EtherCAT wrapper)
};

using ReceiveCallback = std::function<void(const EoEFrameEvent&)>;

The event structure carries only the sending slave index and the frame bytes. The frame is copied into a std::vector before the callback, so it is safe to forward it to any thread.

SetReceiveHook()​

bool SetReceiveHook(ReceiveCallback cb);

Registers the EoE frame receive callback. The registration scope is the master: calling it again on the same master replaces the previous callback, and multiple callbacks cannot coexist (passing an empty callback is equivalent to clearing). The callback runs on the PDO main-loop thread; do not block or do long work inside it (it slows the real-time send and receive).

Parameters:

  • cb (ReceiveCallback) — frame arrival callback

Returns:

  • bool — true the registration succeeded; false the runtime context is invalid or the current runtime does not support this entry point

ClearReceiveHook()​

bool ClearReceiveHook();

Clears the receive hook. Destroying the EoE instance does not clear it (the hook scope is the master, not the slave instance); the application must call this explicitly before exiting, otherwise the callback may still fire after the runtime is released, leaving a dangling callback.

Returns:

  • bool — whether the clear succeeded

End-to-end example​

auto& eoe = master.GetSlave(1).GetEoE();

if (!eoe.SetReceiveHook([](const EoE::EoEFrameEvent& ev) {
printf("[EoE] 从站 %u 收到 %zu 字节\n", // [EoE] slave %u received %zu bytes
ev.slaveIndex, ev.data.size());
// Push onto the main thread queue; do no long-running work on the PDO thread
})) {
printf("EoE Hook 注册失败\n"); // EoE hook registration failed
}

master.SetState(EcState::OP);
master.Start();

// ... application running ...

// Must be cleaned up before exiting
eoe.ClearReceiveHook();
Hook vs ReceiveFrame
  • ReceiveFrame() polls synchronously; without a hook, frames queue up waiting to be taken, so the application must consume them periodically or frames are dropped once the queue is full
  • once SetReceiveHook() is registered, frames are no longer queued and the callback fires directly; return from the callback quickly (it runs on the PDO main-loop thread)
  • only one of the two can be used per master: after a hook is registered, calling ReceiveFrame() returns no frames

Asynchronous interface​

Every synchronous method has an asynchronous wrapper that runs on a background thread; get the result with .get(). frame is passed by value, so the caller may destroy the original object afterwards.

SendFrameAsync() / ReceiveFrameAsync() / SendFrameWithTimestampAsync() / PingAsync()​

std::future<bool> SendFrameAsync(std::vector<uint8_t> frame, int timeoutMs = 500) const;

std::future<bool> SendFrameWithTimestampAsync(std::vector<uint8_t> frame, uint64_t timestamp,
int timeoutMs = 500) const;

std::future<std::vector<uint8_t>> ReceiveFrameAsync(int timeoutMs = 500) const;

std::future<EoEPingResult> PingAsync(uint32_t targetIp, int timeoutMs = 3000) const;

std::future<EoEPingResult> PingAsync(const std::string& targetIpStr, int timeoutMs = 3000) const;

Asynchronous versions of the synchronous methods; parameters and return semantics match the synchronous methods.

Parameters:

  • Same as the corresponding synchronous method; frame is taken by value

Returns:

  • std::future<bool> / std::future<std::vector<uint8_t>> / std::future<EoEPingResult> — same result type as the corresponding synchronous method; when the receive fails the future holds an empty vector

Example:

auto fut = eoe.SendFrameAsync(frame, 500);
if (fut.get())
std::printf("已送出\n"); // Sent