You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用OPCFoundation.NetStandard.Opc.Ua.Server定义OPC-UA枚举类型

使用OPCFoundation.NetStandard.Opc.Ua.Server定义OPC-UA枚举类型的示例

以下是完整的实现步骤和代码示例,帮你在OPC-UA服务器中创建自定义枚举类型:

1. 定义C#枚举类型

先创建对应的C#枚举,可通过EnumMember属性指定UA客户端显示的名称:

using System.Runtime.Serialization;

public enum DeviceStatus
{
    [EnumMember(Value = "Idle")]
    Idle = 0,
    [EnumMember(Value = "Running")]
    Running = 1,
    [EnumMember(Value = "Fault")]
    Fault = 2
}

2. 配置服务器自定义命名空间

在服务器启动前的配置中,添加你的自定义命名空间(用于区分默认OPC-UA命名空间):

using Opc.Ua;
using Opc.Ua.Server;
using System.Collections.Specialized;

var serverConfig = new ApplicationConfiguration
{
    ApplicationName = "CustomEnumServer",
    ApplicationUri = "urn:CustomEnumServer",
    NamespaceUris = new StringCollection { "http://your-company.com/opcua/custom" },
    // 其他必要配置(如安全、日志等)
};
await serverConfig.Validate(ApplicationType.Server);

3. 在NodeManager中注册枚举类型

自定义NodeManager,将C#枚举注册为OPC-UA地址空间中的枚举类型:

public class CustomNodeManager : NodeManager
{
    private readonly int _customNamespaceIndex;

    public CustomNodeManager(IServerInternal server, ApplicationConfiguration config)
        : base(server, config)
    {
        // 获取自定义命名空间的索引(对应步骤2中添加的命名空间)
        _customNamespaceIndex = Server.NamespaceUris.GetIndex("http://your-company.com/opcua/custom");
        
        // 注册枚举类型到地址空间
        RegisterEnumerationType();
    }

    private void RegisterEnumerationType()
    {
        // 创建枚举类型的NodeId
        var enumNodeId = new NodeId("DeviceStatus", _customNamespaceIndex);
        
        // 从C#枚举提取枚举项的显示名称
        var enumStrings = Enum.GetValues(typeof(DeviceStatus))
            .Cast<DeviceStatus>()
            .Select(v => v.ToString())
            .ToArray();

        // 添加枚举类型到地址空间
        var enumTypeNode = AddEnumerationType(
            enumNodeId,
            "DeviceStatus",
            _customNamespaceIndex,
            enumStrings);
    }

    // 重写CreateAddressSpace方法,创建使用枚举的变量节点
    public override void CreateAddressSpace(IDictionary<NodeId, INode> addressSpace)
    {
        base.CreateAddressSpace(addressSpace);

        // 创建使用自定义枚举的变量节点
        var statusVarNode = new VariableNode(
            new NodeId("DeviceCurrentStatus", _customNamespaceIndex),
            new QualifiedName("DeviceCurrentStatus", _customNamespaceIndex),
            new LocalizedText("Device Current Status"),
            new LocalizedText("Current operational status of the device"),
            NodeId.Null,
            new DataValue((int)DeviceStatus.Idle), // 初始值
            new NodeId("DeviceStatus", _customNamespaceIndex), // 关联自定义枚举类型
            AttributeValues.Default);

        // 将变量节点添加到地址空间
        AddNode(statusVarNode, addressSpace);
    }
}

4. 启动服务器并关联NodeManager

最后在服务器启动时,将自定义NodeManager添加到服务器:

var server = new StandardServer(serverConfig);
server.AddNodeManager(server => new CustomNodeManager(server, serverConfig));

await server.Start();
Console.WriteLine("OPC-UA Server running. Press any key to stop...");
Console.ReadKey();
await server.Stop();

关键说明

  • 枚举的数值需与C#枚举一致,OPC-UA会将枚举值以整数形式传输,客户端会根据注册的枚举字符串解析显示
  • 自定义命名空间索引需确保与配置中的命名空间顺序对应
  • 若需要动态更新枚举变量的值,可通过WriteValue方法修改变量节点的Value属性

内容的提问来源于stack exchange,提问作者Apollo3zehn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 03:46:16