跳到主要内容

ENI/DENI 配置加载

用户加载路径只有 SetENI / LoadConfigJson / LoadDeniFile / LoadEniFile。

保存配置 / 启动快照在主站自愈窗口内采用有限等待(秒级上限,短轮询),超时返回错误,不会长时间挂起。

非用户 API

EtherCATMaster 没有公开的 SaveXMLConfiguration / LoadAndApplyXMLConfiguration。SaveXMLConfiguration / SaveEniFile / SaveDeniFile / LoadAndApplyXMLConfiguration 是 GUI/PLC 内部入口,不进入 darra::ethercat 公开 using,不要当作用户 API。

功能概览​

入口作用
SetENIBuilder 登记 ENI/DENI 路径,Build 时走 LoadConfigJson
LoadConfigJson原生引擎:把 JSON 或 DENI/ENI 文本交给 DLL
LoadDeniFile解析 Darra DENI XML 为结构(需 ENABLE_ESI_XML)
LoadEniFile解析 ETG.2100 ENI XML 为结构(需 ENABLE_ESI_XML)
ApplyXmlConfiguration把已解析结构套到运行中的主站

快速开始​

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

运行时加载​

SetENI()​

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

登记 ENI / DENI 文件路径。Build() 读取文件内容后交给 LoadConfigJson(原生检测 DENI XML 并套用网卡 / DC / SDO / 看门狗 / 分组)。

参数:

  • path (const std::string&) — ENI 或 DENI 文件路径

返回值:

  • EtherCATMaster& — 自身引用(链式调用)

示例:

master.SetNetwork("\\Device\\NPF_{...}")
.SetENI("C:/EtherCAT/config.deni")
.Build();

LoadConfigJson()​

int LoadConfigJson(uint16_t masterIndex, const char* jsonOrXml);   // master/config.h 自由函数
int darra::ethercat::master_other::load_config_json(
EtherCATMaster& m, const std::string& json);

原生配置引擎。接受 JSON 字符串,也接受 DENI/ENI XML 文本。SetENI 在 Build() 内走这条路径。返回值 < 0 表示失败。LoadConfigJson 是 master/config.h 的自由函数(dll_t 是纯 C 句柄结构,没有成员方法),master_other::load_config_json 是它按主站对象的转发。

参数:

  • masterIndex / m — 主站实例
  • jsonOrXml / json — JSON 或 DENI/ENI 文本

返回值:

  • int — < 0 失败;否则由原生引擎 best-effort 套用配置

示例:

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

XML 解析​

LoadDeniFile / LoadEniFile / ApplyXmlConfiguration 在 utils/xml.hpp,需启用 pugixml:

#define ENABLE_ESI_XML
#include <pugixml.hpp>
#include "ethercat.hpp"

未定义 ENABLE_ESI_XML 时这些函数不参与编译。公开 using 只导出 LoadDeniFile / LoadEniFile / ApplyXmlConfiguration。pugixml 用随包提供的 v1.14(MIT,third_party/pugixml/):把该目录加进包含路径、把 third_party/pugixml/pugixml.cpp 编进工程即可,无需外部下载或预编译库。

LoadDeniFile()​

namespace darra::ethercat {

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

}

解析 Darra DENI(根节点 DarraEtherCATConfig)。失败时返回默认主站配置和空从站列表。

参数:

  • path (const std::string&) — DENI 文件路径(UTF-8)

返回值:

  • first (MasterXmlConfiguration) — 主站配置
  • second (std::vector<SlaveXmlConfiguration>) — 从站列表

相关结构:

struct MasterXmlConfiguration {
uint32_t CycleTimeUs = 1000; // PDO 周期(微秒)
uint64_t DcCycleNs = 1000000; // DC 同步周期(纳秒)
int CpuAffinity = -1; // CPU 亲和性(-1=自动)
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;
};

示例:

auto [masterCfg, slaves] = darra::ethercat::LoadDeniFile("C:/EtherCAT/config.deni");
printf("周期 %u us, %zu 个从站\n",
masterCfg.CycleTimeUs, slaves.size());

LoadEniFile()​

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

解析 ETG.2100 ENI(根节点 EtherCATConfig)。失败时返回默认主站配置和空从站列表。

参数:

  • path (const std::string&) — ENI 文件路径(UTF-8)

返回值:

  • 与 LoadDeniFile 相同

示例:

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 = 0);

把已解析的 XML 配置套到运行中的主站。按 VendorId + ProductCode 匹配在线从站,应用可选从站标志、帧重复、主站 DC 周期。

参数:

  • dll (dll_t&) — DLL 函数表
  • masterCfg (const MasterXmlConfiguration&) — 主站配置
  • slaves (const std::vector<SlaveXmlConfiguration>&) — 从站配置
  • masterIndex (uint16_t) — 主站实例索引,默认 0

返回值:

  • XmlConfigurationResult — 匹配结果

相关结构:

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

示例:

auto [cfg, slaves] = darra::ethercat::LoadDeniFile("C:/EtherCAT/config.deni");
auto result = darra::ethercat::ApplyXmlConfiguration(
dll, cfg, slaves, master.MasterNumber());
printf("匹配 %d / %d\n", result.MatchedCount, result.ConfigSlaveCount);

ENI 与 DENI​

格式根节点用户入口
ENIEtherCATConfigLoadEniFile 或 SetENI
DENIDarraEtherCATConfigLoadDeniFile 或 SetENI

运行时进主站用 SetENI / LoadConfigJson。只解析结构用 LoadDeniFile / LoadEniFile。

完整示例​

#define ENABLE_ESI_XML
#include <pugixml.hpp>
#include "ethercat.hpp"

using namespace darra;
using namespace darra::ethercat;

int main() {
EtherCATMaster master(dll);
master.SetENI("C:/EtherCAT/MyProject.deni").Build();
master.SetState(EcState::OP);

auto [cfg, slaves] = LoadDeniFile("C:/EtherCAT/MyProject.deni");
printf("DENI: 周期 %u us, %zu 从站\n",
cfg.CycleTimeUs, slaves.size());
return 0;
}