Skip to main content

C++ SDK overview

A header-only C++17 wrapper library with the main namespace darra:: (darra::ethercat provides an alias for the same names, and the two can be mixed). RAII lifetimes, move semantics, std::function callbacks, std::optional returns and exception handling.

Current version 2.13.16

This documentation covers SDK 2.13.16. Capabilities from 2.5.x are retained, including automatic MDP configuration on entering OP, per-group WKC accessors and slave mailbox health observability; each chapter marks them as "new in 2.5.x / interface change".

What the C++ layer provides​

FeatureC++ SDK
Resource managementAutomatic release in the RAII destructor
Type safetyStrongly typed classes
Callback mechanismstd::function + lambda
Error handlingExceptions (DarraException)
StringsManaged automatically with std::string
Data containersstd::vector / std::optional
Slave accessmaster.GetSlave(1).GetCoE().SDORead(...)
Copy semanticsCopying is disabled; only moves are supported
#include "ethercat.hpp"
using namespace darra::ethercat;

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

master.Events().SetPDOCallbackSync([&](uint16_t mi) {
std::vector<uint8_t> inFrame;
darra::SlavePDO::CopyInputFrame(dll, master.MasterNumber(), 1, inFrame);
});

// leaving scope: the destructor calls Stop + Dispose automatically
}

On the hot path, map once into the real-time process image with InputsMapping / OutputsMapping; CopyInputFrame takes a whole-frame snapshot, it is not zero-copy.

Installation​

Get the SDK package from the downloads page (it contains the headers and the runtime libraries).

#include "ethercat.hpp"

CMake configuration:

add_executable(my_app main.cpp)
target_compile_features(my_app PRIVATE cxx_std_17)
target_include_directories(my_app PRIVATE ${SDK_PATH}/include)

Requirements​

ItemRequirement
Operating systemWindows 10/11 x64 (this win-x64 package contains Windows binaries only)
CompilerMSVC 2019+ (a #pragma comment in the headers links darra_ethercat.lib automatically); GCC / Clang can compile the headers but you must link the import library manually
C++ standardC++17 or later
PrivilegesAdministrator

The real-time driver must be installed (it comes with the installer).

SDK readiness check​

The SDK initializes itself the first time a business entry point is called; no manual loading is needed.

inline bool shell_ready() noexcept;

true = ready; false = not ready. While it is not ready, every business entry point returns its own failure value instead of silently succeeding; just make sure the matching SDK and runtime are installed. It calls no business entry point, it only reads the readiness state, and it may be called repeatedly from any thread.

Quick start​

Use the Darra configuration tool
  1. Open the Darra configuration tool and scan the EtherCAT network
  2. Configure the slave parameters (PDO mapping, DC settings and so on)
  3. Export the DENI configuration file
  4. Load the DENI file in your code to initialize the master
#include <iostream>
#include "ethercat.hpp"

int main() {
// ★ Recommended: use a DENI file (exported by the Darra configuration tool)
darra::dll_t dll;
darra::EtherCATMaster master(dll); // the constructor takes a dll_t&
master.SetENI("C:\\EtherCAT\\MyProject.deni")
.Build(); // Build() is required after SetENI()
master.SetState(darra::EcState::OP);

auto& slave = master.GetSlave(1);
auto data = slave.GetCoE().SDORead(0x6041, 0x00); // GetCoE(), not coe()
if (data.size() >= 2) {
uint16_t status = data[0] | (data[1] << 8);
std::cout << "Statusword=0x" << std::hex << status << "\n";
}
return 0; // RAII destroys automatically
}

Alternative: automatic NIC discovery (without DENI)​

Without DENI, use GetNetworkInfo() to scan all NICs automatically and take the one that has slaves:

#include <iostream>
#include <algorithm>
#include "ethercat.hpp"

int main() {
dll_t dll;
// scan all NICs and probe the slave count (takes ~500 ms-1.5 s)
auto nets = darra::ethercat::GetAvailableNetworks(dll, /*isRedundant=*/false,
/*needSlaveCount=*/true);

// pick the NIC that has slaves (AvailableNetworkInfo.slaveCount)
auto it = std::find_if(nets.begin(), nets.end(),
[](const auto& n){ return n.slaveCount > 0; });
if (it == nets.end()) {
std::cerr << "未发现 EtherCAT 网络\n"; // No EtherCAT network found
return -1;
}
std::cout << "找到: " << it->desc << " (从站 " << it->slaveCount << " 台)\n"; // Found: <desc> (<slave count> slaves)

darra::EtherCATMaster master(dll);
master.SetNetwork(it->name);
master.Build();
master.SetState(darra::EcState::OP);
return 0;
}
Which one to use
  • DENI — production; one line loads the complete ESI/PDO/DC parameter set. Recommended.
  • Automatic discovery — demos, tool-type applications, and cases where you do not need preconfigured startup parameters.

Feature overview​

FeatureC++ classDescription
Master managementEtherCATMasterBuilder-style initialization, state machine, configuration, RAII lifetime
Slave accessSlave80+ PascalCase property methods, state, watchdog
PDO read/writeSlavePDOAfter entering OP, InputsMapping / OutputsMapping map once and then reference the real-time process image directly (reading a field gives the current value); CopyInputFrame / WriteOutputFrame take whole-frame snapshots
CoECoESDORead / SDOWrite and their typed versions
CiA 401CiA401Generic I/O profile
CiA 402CiA402Servo drive profile, Enable / Disable / FaultReset
SoESoESERCOS parameter read/write and typed versions
FoEFoEFile transfer (Download / Upload)
EoEEoEEthernet tunnel (IP / MAC configuration, frame send and receive)
AoEAoEADS protocol (ReadWrite / SendCommand)
VoEVoEVendor protocol (Send / Receive / SendAndReceive)
FSoEFSoEFunctional safety (Initialize / ReadSafeInput / WriteSafeOutput)
MDPMDPModular device configuration, set up automatically on entering OP (ETG.5001)
DiagnosticsDiagnostics / MasterDiagnosticsInfoCommunication statistics, PDO frame loss, CycleCount, real-time core binding
ConfigurationMasterConfigCycle time, watchdog, VLAN, frame priority and so on
Mailbox gatewayMailboxGatewayServiceETG.8200 UDP mailbox gateway (public user API)

Core design principles​

Multiple instances: each instance owns one NIC, and CPU cores are assigned automatically.

C++-specific sugar​

Beyond the standard API, the C++ SDK offers an optional set of extensions in the darra::sugar namespace that build on C++ language features (the headers are in the SDK's internal/sugar/ subdirectory). Include <internal/sugar/sugar.hpp> to pull them all in. The extensions include ScopedStart/State/LockIO RAII guards / SlavesView for STL-friendly iteration / sugar::slave_opt(master, i), which returns std::optional<std::reference_wrapper<Slave>> / sugar::idx(master, i) index sugar (EtherCATMaster itself has no operator[] overload) / auto [vendor, product, rev] = darra::sugar::identity_tuple(slave) structured binding / std::string_view zero-copy string parameters / StartAsync/StopAsync std::future async / C++20 concepts (conditionally compiled). These are optional and do not affect use of the standard API.

Error code descriptions​

The language used for AL Status / SDO / SoE error code descriptions depends on the SDK; all six languages follow the same rule:

  • AL Status — the AL Status text has two independent implementations, Chinese and English, and neither forwards to the other; the public wrapper is the AL text function in protocol_codes.
  • SDO Abort — returns Chinese text (protocol_codes::sdo_abort_description / statics::sdo_error_description); both entry points return the same text. An English version is not available at present.
  • SoE — statics::soe_error_description is a local English table; protocol_codes::soe_error_description returns Chinese.

FSoE state and error code descriptions are bilingual: the _cn suffix is Chinese and _en is English; the versions without a suffix return English for FSoE states and Chinese for FSoE errors:

using namespace darra::ethercat::statics;

std::string en = al_status_description(code); // AL Status
std::string zh = protocol_codes::sdo_abort_description(0x06020000u); // SDO Abort
const char* fs = fsoe_state_description_cn(st); // FSoE state (Chinese)
const char* fe = fsoe_error_description_en(err); // FSoE error (English)

Version compatibility​

The current SDK is 2.13.16. The runtime, the real-time driver and the configuration tool are upgraded together under the same version number; a MINOR/MAJOR upgrade requires reinstalling the matching driver.