ENI/DENI 配置管理
支持 ENI 标准格式 (EtherCATConfig) 和 DENI 扩展格式 (DarraEtherCATConfiguration)。
推荐工作流
- 使用 Darra 配置工具连接从站、配置 PDO/DC/启动参数
- 导出 DENI 文件 (
.deni) - 在代码中使用
SetENI()+Build()加载配置
加载配置
LoadXMLConfiguration(string path)
public static (MasterXMLConfiguration config, string message) LoadXMLConfiguration(string path)
从文件解析 ENI/DENI 配置,不应用到主站。
返回值:
config— 配置对象,解析失败时为nullmessage— 提示信息(成功时包含从站数量,失败时包含具体原因)
示例:
var (config, msg) = DarraEtherCAT.LoadXMLConfiguration(@"C:\config.deni");
if (config == null)
{
Console.WriteLine($"加载失败: {msg}");
return;
}
Console.WriteLine(msg); // "已加载 ENI 配置: 7 个从站"
ApplyXMLConfiguration(config, applyNetwork, applyStartupParams)
public XMLConfigurationResult ApplyXMLConfiguration(
MasterXMLConfiguration config,
bool applyNetwork = false,
bool applyStartupParams = true)
将已解析的配置应用到当前主站。按 Index 优先、VID/PID 回退的策略匹配从站。
返回值:
XMLConfigurationResult— 配置应用结果
相关结构:
public class XMLConfigurationResult
{
public bool Success { get; } // 是否至少一个从站匹配成功
public int ConfigSlaveCount { get; } // 配置文件中的从站数量
public int ActualSlaveCount { get; } // 当前网络中的从站数量
public int MatchedCount { get; } // 匹配成功的从站数量
public List<string> SlaveDetails { get; } // 逐从站匹配结果
public List<string> Warnings { get; } // 警告信息
}
示例:
var result = master.ApplyXMLConfiguration(config);
Console.WriteLine($"匹配: {result.MatchedCount}/{result.ConfigSlaveCount}");
foreach (var detail in result.SlaveDetails)
Console.WriteLine($" {detail}");
便捷方法
LoadAndApplyXMLConfiguration(string path, bool applyStartupParams)
public (bool success, string message) LoadAndApplyXMLConfiguration(
string path,
bool applyStartupParams = true)
加载并应用配置(合并 LoadXMLConfiguration + ApplyXMLConfiguration)。
示例:
var (ok, msg) = master.LoadAndApplyXMLConfiguration(@"C:\config.deni");
if (!ok) Console.WriteLine($"导入失败: {msg}");
LoadAndAutoConfigure(string path, bool applyStartupParams, bool applyDCSync)
public (bool success, string message) LoadAndAutoConfigure(
string path,
bool applyStartupParams = true,
bool applyDCSync = true)
加载配置并自动应用所有设置(循环时间、DC 同步、启动参数等)。
示例:
var (ok, msg) = master.LoadAndAutoConfigure(@"C:\config.deni");
Console.WriteLine(msg);
保存配置
仅支持 Darra 产品内部使用
导出运行时配置为 DENI 文件的功能由 Darra 主界面和 PLC 中间层提供(菜单"导出 DENI"),不通过 SDK 对外开放。
SDK 用户的标准工作流是:
- 在 Darra 主界面或 PLC 中完成配置并导出 DENI 文件
- 在代码中使用
SetENI(path).Build()加载已有的 ENI/DENI 文件
如果需要"扫描当前硬件并自动生成配置文件",请使用 Darra 主界面或 PLC 端的向导功能。
完整示例
// 方式 1: 使用 Build 流式 API(推荐)
var master = new DarraEtherCAT()
.SetENI(@"C:\EtherCAT\MyProject.deni")
.Build();
// 方式 2: 运行时导入配置
var (config, loadMsg) = DarraEtherCAT.LoadXMLConfiguration(@"C:\config.deni");
if (config != null)
{
var result = master.ApplyXMLConfiguration(config);
if (result.MatchedCount < result.ConfigSlaveCount)
{
// 部分匹配,检查详情
foreach (var detail in result.SlaveDetails)
Console.WriteLine(detail);
}
}
// 方式 3: 一步到位
var (ok, msg) = master.LoadAndAutoConfigure(@"C:\config.deni");
相关功能
ENI/DENI 配置与初始化与构造(SetENI() / Build() 流式初始化)和 ESI 文件管理配合使用。Hot-Connect 组管理详见 Hot-Connect。