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

能否用OPC UA客户端订阅西门子TP1200 Comfort面板的报警?

解决西门子TP1200 Comfort OPC UA报警订阅的步骤

1. 确认TP1200的OPC UA报警节点结构

西门子TP1200 Comfort的OPC UA服务器报警信息遵循OPC UA标准的**报警与事件(Alarms & Events)模型,不会出现在常规数据标签节点下。你需要在UAExpert中切换到事件视图(Event View)**操作:

  • 在UAExpert菜单栏选择View > Event View
  • 连接TP1200服务器后,右键点击服务器节点,选择Subscribe to Events
  • 此时可查看触发的报警事件,通过事件属性反向定位报警源节点的路径

另外,TP1200的报警节点通常在Objects > Server > Alarms、Objects > SIMATIC HMI > Alarms或Objects > DeviceSet > [你的HMI设备名] > Alarms这类路径下,不同版本可能略有差异。

2. 使用UA-.NETStandard库实现报警订阅

核心代码示例

以下是基于UA-.NETStandard库的基础报警订阅实现框架:

using Opc.Ua;
using Opc.Ua.Client;
using System;
using System.Threading.Tasks;

class HmiAlarmSubscriber
{
    private Session _session;
    private EventSubscription _eventSubscription;

    public async Task InitializeSession(string endpointUrl)
    {
        // 创建客户端配置
        var config = new ApplicationConfiguration
        {
            ApplicationName = "HmiAlarmClient",
            ApplicationType = ApplicationType.Client,
            SecurityConfiguration = new SecurityConfiguration
            {
                ApplicationCertificate = new CertificateIdentifier { StoreType = "Directory", StorePath = "./cert" },
                TrustedIssuerCertificates = new CertificateTrustList { StoreType = "Directory", StorePath = "./issuers" },
                TrustedPeerCertificates = new CertificateTrustList { StoreType = "Directory", StorePath = "./peers" },
                RejectSha1SignedCertificates = false,
                MinimumCertificateKeySize = 1024
            },
            TransportConfigurations = new TransportConfigurationCollection(),
            TransportQuotas = new TransportQuotas { OperationTimeout = 15000 },
            ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 }
        };
        await config.Validate(ApplicationType.Client);

        // 创建会话
        var endpoint = CoreClientUtils.SelectEndpoint(endpointUrl, false, 15000);
        _session = await Session.Create(config, new ConfiguredEndpoint(null, endpoint, EndpointConfiguration.Create(config)), false, "HmiAlarmSession", 60000, null, null);
    }

    public void SubscribeToAlarms()
    {
        // 创建事件订阅
        var subscription = new Subscription(_session.DefaultSubscription)
        {
            PublishingEnabled = true,
            PublishingInterval = 1000,
            LifetimeCount = 30,
            MaxKeepAliveCount = 10,
            Priority = 0
        };

        // 设置事件筛选器:按需订阅报警事件
        var filter = new EventFilter
        {
            SelectClauses = new SimpleAttributeOperand[]
            {
                new SimpleAttributeOperand { TypeDefinitionId = ObjectTypeIds.BaseEventType, BrowsePath = new[] { BrowseNames.Message } },
                new SimpleAttributeOperand { TypeDefinitionId = ObjectTypeIds.BaseEventType, BrowsePath = new[] { BrowseNames.SourceName } },
                new SimpleAttributeOperand { TypeDefinitionId = ObjectTypeIds.BaseEventType, BrowsePath = new[] { BrowseNames.Time } },
                new SimpleAttributeOperand { TypeDefinitionId = ObjectTypeIds.AlarmConditionType, BrowsePath = new[] { BrowseNames.ActiveState, BrowseNames.Id } },
                new SimpleAttributeOperand { TypeDefinitionId = ObjectTypeIds.AlarmConditionType, BrowsePath = new[] { BrowseNames.Severity } }
            },
            WhereClause = new ContentFilter
            {
                Elements = new ContentFilterElement[]
                {
                    // 过滤仅激活状态的报警
                    new ContentFilterElement
                    {
                        FilterOperator = FilterOperator.Equals,
                        FilterOperands = new FilterOperand[]
                        {
                            new SimpleAttributeOperand { TypeDefinitionId = ObjectTypeIds.AlarmConditionType, BrowsePath = new[] { BrowseNames.ActiveState, BrowseNames.Id } },
                            new LiteralOperand { Value = new Variant(true) }
                        }
                    }
                }
            }
        };

        // 添加事件监视项,可替换为具体报警节点NodeId缩小范围
        var monitoredItem = new MonitoredItem(subscription.DefaultItem)
        {
            StartNodeId = ObjectIds.Server,
            AttributeId = Attributes.EventNotifier,
            MonitoringMode = MonitoringMode.Reporting,
            Parameter = filter
        };

        // 绑定报警接收回调
        monitoredItem.Notification += OnAlarmReceived;

        subscription.AddItem(monitoredItem);
        _session.AddSubscription(subscription);
        subscription.Create();

        _eventSubscription = subscription;
    }

    private void OnAlarmReceived(object sender, NotificationEventArgs e)
    {
        foreach (var notification in e.Notifications)
        {
            if (notification is EventNotificationList eventList)
            {
                foreach (var eventFieldList in eventList.Events)
                {
                    // 解析报警数据
                    string message = eventFieldList.Fields[0].Value as string;
                    string sourceName = eventFieldList.Fields[1].Value as string;
                    DateTime time = (DateTime)eventFieldList.Fields[2].Value;
                    bool isActive = (bool)eventFieldList.Fields[3].Value;
                    ushort severity = (ushort)eventFieldList.Fields[4].Value;

                    // 执行业务逻辑:发送邮件、写入数据库等
                    Console.WriteLine($"[{time:yyyy-MM-dd HH:mm:ss}] 报警源: {sourceName}, 信息: {message}, 激活状态: {isActive}, 等级: {severity}");
                }
            }
        }
    }

    public void Cleanup()
    {
        _eventSubscription?.Delete();
        _session?.Close();
        _session?.Dispose();
    }
}

关键注意事项

  • 证书信任配置:西门子HMI的OPC UA服务器默认启用证书验证,需将客户端证书添加到TP1200的信任列表中,可通过HMI的Web管理界面(http://[HMI_IP]/hmimanager)操作。
  • 精确订阅范围:若订阅根节点(ObjectIds.Server)会接收所有系统事件,建议通过UAExpert获取具体报警节点的NodeId,替换StartNodeId来减少无效事件回调。
  • 日志调试:开启UA日志可排查连接或订阅问题,在配置中添加:
    config.TraceConfiguration = new TraceConfiguration { TraceLevel = TraceLevel.Verbose, OutputFilePath = "./ua_client.log" };
    

3. 验证流程

  1. 先在UAExpert中确认事件订阅正常接收报警
  2. 运行代码后触发HMI报警,检查回调是否正常执行,再逐步接入邮件发送、数据库存储等业务逻辑

内容的提问来源于stack exchange,提问作者Álvaro García

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 01:45:30