The example below logs OPC Alarms and Events notifications into an XML file.
The main program:
// Logs OPC Alarms and Events notifications into an XML file. // // Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html . // OPC client and subscriber examples in C# on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-CSharp . // Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own // a commercial license in order to use Online Forums, and we reply to every post. using System; using System.Diagnostics; using System.Xml; using System.Xml.Serialization; using OpcLabs.BaseLib.Runtime.InteropServices; using OpcLabs.EasyOpc.AlarmsAndEvents; using OpcLabs.EasyOpc.AlarmsAndEvents.OperationModel; namespace XmlEventLogger { class Program { static void Main() { ComManagement.Instance.AssureSecurityInitialization(); Console.WriteLine("Starting up..."); var xmlSerializer = new XmlSerializer(typeof(EasyAENotificationEventArgs)); var xmlWriter = XmlWriter.Create("OpcEvents.xml", new XmlWriterSettings { Indent = true, CloseOutput = true }); // The root element can have any name you need, but the name below also allows reading the log back as .NET array xmlWriter.WriteStartElement("ArrayOfEasyAENotificationEventArgs"); Console.WriteLine("Logging events for 30 seconds..."); int handle = EasyAEClient.SharedInstance.SubscribeEvents("", "OPCLabs.KitEventServer.2", 100, (_, eventArgs) => { Debug.Assert(!(eventArgs is null)); Console.Write("."); xmlSerializer.Serialize(xmlWriter, eventArgs); }); System.Threading.Thread.Sleep(30 * 1000); Console.WriteLine(); Console.WriteLine("Shutting down..."); EasyAEClient.SharedInstance.UnsubscribeEvents(handle); xmlWriter.WriteEndElement(); // not really necessary - XmlWriter would write the end tag for us anyway xmlWriter.Close(); Console.WriteLine("Finished."); } } }
' Logs OPC Alarms and Events notifications into an XML file. ' ' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html . ' OPC client and subscriber examples in VB.NET on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-VBNET . ' Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own ' a commercial license in order to use Online Forums, and we reply to every post. Imports System.Xml Imports System.Xml.Serialization Imports OpcLabs.BaseLib.Runtime.InteropServices Imports OpcLabs.EasyOpc.AlarmsAndEvents Imports OpcLabs.EasyOpc.AlarmsAndEvents.OperationModel Friend Class Program Shared WithEvents _client As New EasyAEClient Shared ReadOnly XmlSerializer As New XmlSerializer(GetType(EasyAENotificationEventArgs)) Shared _xmlWriter As XmlWriter <MTAThread> ' needed for COM security initialization to succeed Shared Sub Main() ComManagement.Instance.AssureSecurityInitialization() Console.WriteLine("Starting up...") _xmlWriter = XmlWriter.Create("OpcEvents.xml", New XmlWriterSettings With {.Indent = True, .CloseOutput = True}) ' The root element can have any name you need, but the name below also allows reading the log back as .NET array _xmlWriter.WriteStartElement("ArrayOfEasyAENotificationEventArgs") Console.WriteLine("Logging events for 30 seconds...") Dim handle As Integer = _client.SubscribeEvents("", "OPCLabs.KitEventServer.2", 100) Threading.Thread.Sleep(30 * 1000) Console.WriteLine() Console.WriteLine("Shutting down...") _client.UnsubscribeEvents(handle) _xmlWriter.WriteEndElement() ' not really necessary - XmlWriter would write the end tag for us anyway _xmlWriter.Close() Console.WriteLine("Finished.") End Sub Private Shared Sub Notification(ByVal sender As Object, ByVal eventArgs As Object) Handles _client.Notification Debug.Assert(eventArgs IsNot Nothing) Console.Write(".") XmlSerializer.Serialize(_xmlWriter, eventArgs) End Sub End Class
Copyright © 2004-2024 CODE Consulting and Development, s.r.o., Plzen. All rights reserved.
Documentation Home, Send Documentation Feedback. Technical Support