Skip to main content

ENI/DENI configuration management

An ENI is the EtherCAT network configuration file: which slaves are on the bus, how the PDO is mapped, and how DC and the startup parameters are set are all written into this file. DENI is the version Darra builds on a standard ENI by adding the deni: extension.

Why are there two? A standard ENI is the industry-common format: any EtherCAT configuration tool can export it, and the SDK can load it directly. But it has none of these engineering semantics: "this slot may be absent", "bind a slot by identity", "replay the startup parameters after a part swap". DENI adds the deni: extension on top of it; the file body (slaves, PDO, DC, startup parameters) is still standard ENI structure.

ItemStandard ENIDENI
Typical sourceExported by other EtherCAT configuration toolsExported by the Darra configuration tool
Slaves, PDO, DC, startup parametersYesYes (the same body)
Optional slot <deni:Optional>NoYes
Identity-bound slots, hot-plug replayNoYes
NICMust be specified with SetNetworkThe file carries NIC information, so the card can be selected automatically
Recommended useCompatibility format for importing an existing projectFirst choice for a new project
Recommended workflow
  1. Connect the slaves with the Darra configuration tool and configure PDO/DC/startup parameters
  2. Export the DENI file (.deni)
  3. Load the configuration in code with SetENI() + Build()
Only covers public API

This page only describes the use of the public API.

Not a user API

The two members on the master object for "saving an XML configuration / applying one after loading" are not public API. They are for use inside Darra products only and must not be treated as a user API.

Feature overview​

Entry pointPurpose
SetENIBuilder step that records the ENI/DENI path; load_config_json is used at Build time
load_config_jsonConfiguration engine: hands JSON or DENI/ENI text to the runtime library
LoadDeniFileParses only the legacy offline format into structures; does not read product DENI
LoadEniFileParses ETG.2100 ENI XML into structures
ApplyXmlConfigurationApplies the parsed offline structures to a running master

Quick start​

dll_t dll;
EtherCATMaster master(dll);
master.SetENI("C:/EtherCAT/MyProject.deni").Build();

Loading a configuration​

EtherCATMaster::SetENI(path) only records the path; the file is actually read and applied at Build(). To load a configuration onto an existing master right away, use master_other::load_config_json(master, 内容) (or darra::ethercat::SetENI(master, path), which reads the file and goes through the same path).

Build() hands the file contents to the configuration engine; the engine decides which path to take from the file contents themselves:

  • DENI (EtherCATConfig + the deni: extension): takes the full engineering semantics — identity-bound slots, optional slots, handling of absent / extra stations, grouping / watchdog / DC contracts, and hot-plug replay. When the file carries NIC information it selects the NIC itself, live-scans the slaves and applies the whole configuration.
  • Standard ENI (the same EtherCATConfig body, without the deni: extension): the compatibility path — startup parameters and device features are applied by declared position. It has no optional-slot concept, and it offers neither identity-bound slots nor automatic hot-plug promote; with this format you must specify the port yourself with SetNetwork.
  • Other formats (neither DENI nor a standard ENI): fail directly and list the supported formats, no guessing. A file that cannot be read or whose contents are empty, a file that is not well-formed XML, and contents whose structure is invalid are three different failures; take the reason from LastError() and the log.

Besides XML text, master_other::load_config_json also accepts JSON configuration text.

Port override rule: Build() first applies the configuration from the file contents and then the NIC binding from SetNetwork, so the NIC bound by an explicit SetNetwork call always overrides the port in the file, regardless of the order of the two calls. The rest of the DENI (slaves, PDO, DC, SDO, watchdog, grouping) takes effect as usual. When SetNetwork is not called, the port in the file is used.

Build() applies the NIC, slaves, PDO, DC, SDO, watchdog and grouping configuration from the file. The build fails when no slaves can be scanned.

SetENI(const std::string& path)​

EtherCATMaster& SetENI(const std::string& path);

Loads a DENI or ENI configuration file. The path takes effect at Build().

Parameters:

  • path (const std::string&) — path to the DENI or ENI configuration file

Returns:

  • EtherCATMaster& — a reference to itself (chaining)

Example:

EtherCATMaster master(dll);
master.SetENI("C:/EtherCAT/MyProject.deni");

if (!master.Build())
{
printf("初始化失败:%s (错误码 %ld)\n", // Initialization failed: %s (error code %ld)\n
master.LastBuildRejectText(), master.LastError());
return 1;
}

master.SetState(EcState::OP);
Exporting a DENI is provided by Darra products

The SDK offers no interface for "saving a configuration" or "scanning the current hardware to generate a DENI". Use the Darra main application or the wizard on the PLC side to complete the configuration and export the DENI, then load it in code with SetENI(path) + Build().

When you have no DENI and need ESI auto-matching, use the ESI loading and binding entry points + EnableAutoStartup().

master_other::load_config_json(EtherCATMaster& m, const std::string& json)​

int darra::ethercat::master_other::load_config_json(
EtherCATMaster& m, const std::string& json);

The runtime library's configuration engine. It accepts a JSON string and also accepts DENI/ENI XML text. SetENI goes through this path inside Build(). A return value < 0 means failure. On the C++ side use master_other::load_config_json (it forwards through the master object).

Parameters:

  • m (EtherCATMaster&) — master instance
  • json (const std::string&) — JSON or DENI/ENI text

Returns:

  • int — < 0 on failure; otherwise the configuration engine applies the configuration best-effort

Example:

std::ifstream file("C:/EtherCAT/config.deni");
std::ostringstream oss;
oss << file.rdbuf();
int rc = darra::ethercat::master_other::load_config_json(master, oss.str());
if (rc < 0) {
printf("load_config_json 失败: %d\n", rc); // load_config_json failed: %d\n
}

Validate()​

EtherCATMaster::ValidationResult Validate() const;

Validates whether the configuration is complete; it does not run Build() and is meant for a pre-check.

Returns:

struct ValidationResult {
bool IsValid; // Whether validation passed
std::vector<std::string> Errors; // List of validation errors (empty when it passes)
};

What is checked today: a NIC or an ENI configuration has been set, the ENI file exists, and the master instance limit has not been reached.

Passing Validate() does not mean Build() will succeed: it only checks "does the file exist, is a NIC set, is the instance count within limit". It does not parse the file contents and it does not touch the bus. Slave declarations, identities and PDO conflicts inside the file only surface at Build().

Example:

EtherCATMaster master(dll);
master.SetENI("C:/EtherCAT/MyProject.deni");

auto check = master.Validate();
if (!check.IsValid)
{
for (const auto& err : check.Errors)
printf("%s\n", err.c_str());
return 1;
}

XML parsing​

LoadDeniFile (legacy offline format) / LoadEniFile / ApplyXmlConfiguration are in utils/xml.hpp and are compiled together with the SDK:

#include "ethercat.hpp"

The public using declarations export only LoadDeniFile / LoadEniFile / ApplyXmlConfiguration. pugixml is the bundled v1.14 (MIT): add that component to the include path and no external download or prebuilt library is needed.

LoadDeniFile()​

namespace darra::ethercat {

std::pair<MasterXmlConfiguration, std::vector<SlaveXmlConfiguration>>
LoadDeniFile(const std::string& path);

}

Parses the legacy offline structure format. This function does not read product DENI; to load a product DENI use SetENI(path).Build(). On failure it returns a default master configuration and an empty slave list (the reason is in errorOut / the log).

Parameters:

  • path (const std::string&) — legacy offline format file path (UTF-8)

Returns:

  • first (MasterXmlConfiguration) — master configuration
  • second (std::vector<SlaveXmlConfiguration>) — slave list

Related structures:

struct MasterXmlConfiguration {
uint32_t CycleTimeUs = 1000; // PDO cycle (microseconds)
uint64_t DcCycleNs = 1000000; // DC sync period (nanoseconds)
int CpuAffinity = -1; // CPU affinity (-1=automatic)
bool RedundancyEnabled = false;
bool UseUdp = false;
bool MutexProtection = false;
QosConfiguration Qos;
};

struct SlaveXmlConfiguration {
uint16_t Index = 0;
std::string Name;
uint32_t VendorId = 0;
uint32_t ProductCode = 0;
uint32_t Revision = 0;
uint16_t ConfigAddr = 0;
uint32_t InputSize = 0;
uint32_t OutputSize = 0;
};

Example:

/* runtime loading of a product DENI (recommended): SetENI reads the file -> load_config_json -> NIC selection / whole configuration */
master.SetENI("C:/EtherCAT/config.deni").Build();

/* only the legacy offline format is parsed into structures with LoadDeniFile */
auto [masterCfg, slaves] = darra::ethercat::LoadDeniFile("C:/EtherCAT/legacy-offline.deni");
printf("旧离线配置: 周期 %u us, %zu 个从站\n", // Old offline configuration: cycle %u us, %zu slaves\n
masterCfg.CycleTimeUs, slaves.size());

LoadEniFile()​

std::pair<MasterXmlConfiguration, std::vector<SlaveXmlConfiguration>>
LoadEniFile(const std::string& path);

Parses an ETG.2100 ENI file. On failure it returns a default master configuration and an empty slave list.

Parameters:

  • path (const std::string&) — ENI file path (UTF-8)

Returns:

  • The same as LoadDeniFile

Example:

auto [masterCfg, slaves] = darra::ethercat::LoadEniFile("C:/EtherCAT/network.eni");

ApplyXmlConfiguration()​

XmlConfigurationResult ApplyXmlConfiguration(
dll_t& dll,
const MasterXmlConfiguration& masterCfg,
const std::vector<SlaveXmlConfiguration>& slaves,
uint16_t masterIndex);

Applies the parsed offline XML structures to a running master. Online slaves are matched by VendorId + ProductCode, and the optional-slave flag, frame repeat and the master DC cycle are applied. When a product DENI goes through SetENI(path).Build(), the runtime has already applied it, so this function is not needed.

Parameters:

  • dll (dll_t&) — DLL function table
  • masterCfg (const MasterXmlConfiguration&) — master configuration
  • slaves (const std::vector<SlaveXmlConfiguration>&) — slave configuration
  • masterIndex (uint16_t) — master instance index (≥ 1; 0 is invalid, master indexes start at 1); it must be passed explicitly, and master initialization must already be complete before the call. The leftover = 0 default in the header only means "not specified"; there is no master context for 0

Returns:

  • XmlConfigurationResult — matching result

Related structures:

struct XmlConfigurationResult {
bool Success = false;
int ConfigSlaveCount = 0;
int ActualSlaveCount = 0;
int MatchedCount = 0;
std::vector<std::string> Warnings;
};

Example:

/* offline structure path: only the legacy format is parsed with LoadDeniFile and then applied */
auto [cfg, slaves] = darra::ethercat::LoadDeniFile("C:/EtherCAT/legacy-offline.deni");
auto result = darra::ethercat::ApplyXmlConfiguration(
dll, cfg, slaves, master.MasterNumber());
printf("匹配 %d / %d\n", result.MatchedCount, result.ConfigSlaveCount); // Matched %d / %d\n

ENI generation helpers​

A few decision and encoding helpers needed when generating / validating an ENI, consistent with the output of the configuration tool. These helpers are provided by the SDK.

  • InitCmd generation — Generates InitCmd entries from the slave state machine InitCmd decision tree and completes the SM / FMMU register encoding. Returns the number of entries generated; returns a negative value when capacity is insufficient (no partial output).
  • Identity matching — Whether the identity of a scanned slave matches the identity in the configuration. Three modes: ignore the revision number / exact match / configured revision not lower than the scanned value. Returns 1 = match, 0 = no match.
  • ESI type mapping — Maps the ESI DataType to the ENI ProcessImage type name; when the type name is empty it falls back to deriving it from the bit length. Returns the length of the string written; a negative value when capacity is insufficient.
  • Phase string — Converts a phase number into the ENI Transition string (0 = IP / 1 = PS / 2 = SO / 3 = OP; any other value is output as PS). Returns the length of the string written; a negative value when capacity is insufficient.

Troubleshooting a failed build​

Start with the return value of Build() and the three diagnostic entry points: LastBuildRejectReason() / LastBuildRejectText() give lifecycle reasons (instance already released, no slaves scanned), while LastError() gives the failure codes for the two stages — configuration loading and NIC binding (a negative value = a detailed code from the configuration loading stage, a positive value = a system error code from the NIC binding stage). Calling Build() a second time on the same instance returns true directly (no re-initialization and no re-scan); with the streaming MasterBuilder you get a BuildResult, whose fields are Success / Message / MasterIndex.

SymptomCommon causeWhat to do
Reports that the file does not existWrong path, or the relative path base is not what you expectUse an absolute path; confirm the runtime working directory
0 slaves scannedWrong NIC, slaves not powered, broken wiring, or the NIC information in the DENI does not match this machine's NICCheck the MAC and the link with statics::GetNetworkInfo(dll); for a standard ENI, specify the NIC with SetNetwork
Identity mismatchThe Vendor/Product declared in the DENI does not match the actual modulePut the correct module back, or change the configuration; a Revision difference on the same model is not an identity mismatch
A required slave is absentThe slot was not declared optionalAdd <deni:Optional>true</deni:Optional>, or plug the module back in
The master instance limit has been reachedThere is still an unclosed master in the same processdarra::ethercat::Close(master) / master.Dispose() the unused instances first
Validate() passes but Build() failsValidate() only checks the file and NIC settings, it does not parse contentsLook at the failure stage in LastError() and in the log
The DENI loads, but a part swap is not promoted automatically afterwardsA standard ENI is in use; or the slot was not declared optional; or the new module's identity is not in the declarationSwitch to DENI, add the optional declaration, or follow the part-swap procedure in Hot-plug

How to tell "same-model recovery" from "part swap": read slave.VendorId(), slave.ProductId() and slave.RevId() for that station, or subscribe to master.Events().OnSlaveIdentityMismatch(...) (the callback carries the expected identity and the measured identity). Matching Vendor/Product = same model (Revision is only recorded, never grounds for rejection); a different Vendor or Product = a part swap. The master restores the former automatically; for the latter the application layer has to pick the configuration.

What to look at when a station will not reach OP: first look at slave.State() and slave.IsLost(), then slave.ErrorCode() (the full AL Status Code). For the meaning of AL codes and the troubleshooting order see Diagnostics; for identity problems see Hot-plug.

Full example​

#include "ethercat.hpp"

using namespace darra;
using namespace darra::ethercat;

int main() {
dll_t dll;
EtherCATMaster master(dll);
master.SetENI("C:/EtherCAT/MyProject.deni");

// Pre-check: does the file exist, is a port set, is the instance count within limit
auto check = master.Validate();
if (!check.IsValid) {
for (const auto& err : check.Errors)
printf("%s\n", err.c_str());
return 1;
}

if (!master.Build()) {
printf("初始化失败:%s (错误码 %ld)\n", // Initialization failed: %s (error code %ld)\n
master.LastBuildRejectText(), master.LastError());
return 1;
}

if (!master.SetState(EcState::OP)) {
printf("切换 OP 失败\n"); // Failed to switch to OP\n
return 1;
}

printf("主站已到 OP,从站 %d 个\n", master.SlaveCount()); // Master reached OP, %d slaves\n
return 0;
}
Related features

Use ENI/DENI configuration together with Initialization and construction (streaming initialization with SetENI() / Build()).