Client and Subscriber Development > Development Models > Imperative Programming Model > Imperative Programming Model for OPC Classic A&E > Subscribing to Information (OPC A&E) > Changing Existing Subscription (OPC A&E) |
It is not necessary to unsubscribe and then subscribe again if you want to change parameters of existing subscription (such as its notification rate). Instead, change the parameters by calling the ChangeEventSubscription method, passing it the subscription handle, and the new parameters (notification rate, and optionally a filter and an “active” flag).
// This example shows how to subscribe to events and later change the notification rate. // // Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html . using System; using System.Threading; using OpcLabs.EasyOpc.AlarmsAndEvents; using OpcLabs.EasyOpc.AlarmsAndEvents.OperationModel; namespace DocExamples.AlarmsAndEvents._EasyAEClient { class ChangeEventSubscription { public static void Main1() { // Instantiate the client object. using (var client = new EasyAEClient()) { var eventHandler = new EasyAENotificationEventHandler(client_Notification); client.Notification += eventHandler; Console.WriteLine("Subscribing..."); int handle = client.SubscribeEvents("", "OPCLabs.KitEventServer.2", 500); Console.WriteLine("Waiting for 10 seconds..."); Thread.Sleep(10 * 1000); Console.WriteLine("Changing subscription..."); client.ChangeEventSubscription(handle, 5 * 1000); Console.WriteLine("Waiting for 50 seconds..."); Thread.Sleep(50 * 1000); client.UnsubscribeEvents(handle); } } // Notification event handler static void client_Notification(object sender, EasyAENotificationEventArgs e) { if (!e.Succeeded) { Console.WriteLine("*** Failure: {0}", e.ErrorMessageBrief); return; } if (!(e.EventData is null)) Console.WriteLine(e.EventData.Message); } } }
# This example shows how to subscribe to events and later change the notification rate. # # Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html . # OPC client and subscriber examples in Python on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-Python . # The QuickOPC package is needed. Install it using "pip install opclabs_quickopc". import opclabs_quickopc import time # Import .NET namespaces. from OpcLabs.EasyOpc import * from OpcLabs.EasyOpc.AlarmsAndEvents import * # Notification event handler def notification(sender, e): if not e.Succeeded: print('*** Failure: ', e.ErrorMessageBrief, sep='') return else: if e.EventData is not None: print(e.EventData.Message) # Instantiate the client object client = EasyAEClient() client.Notification += notification print('Subscribing events...') handle = IEasyAEClientExtension.SubscribeEvents(client, '', 'OPCLabs.KitEventServer.2', 500) print('Waiting for 10 seconds...') time.sleep(10) print('Changing event subscription...') IEasyAEClientExtension.ChangeEventSubscription(client, handle, 5*1000) print('Waiting for 50 seconds...') time.sleep(50) print('Unsubscribing events...') client.UnsubscribeAllEvents() client.Notification -= notification print('Finished.')
' This example shows how to subscribe to events and later change the notification rate. ' ' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html . Imports System.Threading Imports OpcLabs.EasyOpc.AlarmsAndEvents Imports OpcLabs.EasyOpc.AlarmsAndEvents.OperationModel Namespace AlarmsAndEvents._EasyAEClient Friend Class ChangeEventSubscription Public Shared Sub Main1() Using client = New EasyAEClient() Dim eventHandler = New EasyAENotificationEventHandler(AddressOf client_Notification) AddHandler client.Notification, eventHandler Console.WriteLine("Subscribing...") Dim handle As Integer = client.SubscribeEvents("", "OPCLabs.KitEventServer.2", 500) Console.WriteLine("Waiting for 10 seconds...") Thread.Sleep(10 * 1000) Console.WriteLine("Changing subscription...") client.ChangeEventSubscription(handle, 5 * 1000) Console.WriteLine("Waiting for 50 seconds...") Thread.Sleep(50 * 1000) client.UnsubscribeEvents(handle) End Using End Sub ' Notification event handler Private Shared Sub client_Notification(ByVal sender As Object, ByVal e As EasyAENotificationEventArgs) If Not e.Succeeded Then Console.WriteLine("*** Failure: {0}", e.ErrorMessageBrief) Exit Sub End If If e.EventData IsNot Nothing Then Console.WriteLine(e.EventData.Message) End If End Sub End Class End Namespace
Rem This example shows how to change the notification rate of an existing subscription. Rem Rem Find all latest examples here : https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html . Option Explicit Dim Client: Set Client = CreateObject("OpcLabs.EasyOpc.AlarmsAndEvents.EasyAEClient") WScript.ConnectObject Client, "Client_" WScript.Echo "Subscribing..." Dim ServerDescriptor: Set ServerDescriptor = CreateObject("OpcLabs.EasyOpc.ServerDescriptor") ServerDescriptor.ServerClass = "OPCLabs.KitEventServer.2" Dim SubscriptionParameters: Set SubscriptionParameters = CreateObject("OpcLabs.EasyOpc.AlarmsAndEvents.AESubscriptionParameters") SubscriptionParameters.NotificationRate = 500 Dim handle: handle = Client.SubscribeEvents(ServerDescriptor, SubscriptionParameters, True, Nothing) WScript.Echo "Waiting for 10 seconds..." WScript.Sleep 10*1000 WScript.Echo "Changing subscription..." Client.ChangeEventSubscription handle, 5*1000, SubscriptionParameters.Filter, True WScript.Echo "Waiting for 50 seconds..." WScript.Sleep 50*1000 Client.UnsubscribeEvents handle Rem Notification event handler Sub Client_Notification(Sender, e) If Not (e.Succeeded) Then WScript.Echo "*** Failure: " & e.ErrorMessageBrief Exit Sub End If If Not e.EventData Is Nothing Then WScript.Echo e.EventData.Message End Sub
Copyright © 2004-2024 CODE Consulting and Development, s.r.o., Plzen. All rights reserved. Web page: www.opclabs.com
Send Documentation Feedback. Resources: Knowledge Base, Product Downloads. Technical support: Online Forums, FAQ.Missing some example? Ask us for it on our Online Forums! You do not have to own a commercial license in order to use Online Forums, and we reply to every post.