// This example shows how to subscribe to changes of multiple items and obtain the item changed events by pulling them.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// OPC client and subscriber examples in Object Pascal (Delphi) on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-OP .
// 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.
class procedure PullItemChanged.MultipleItems;
var
Arguments: OleVariant;
Client: OpcLabs_EasyOpcClassic_TLB._EasyDAClient;
EndTick: Cardinal;
EventArgs: _EasyDAItemChangedEventArgs;
HandleArray: OleVariant;
ItemSubscriptionArguments1: _EasyDAItemSubscriptionArguments;
ItemSubscriptionArguments2: _EasyDAItemSubscriptionArguments;
ItemSubscriptionArguments3: _EasyDAItemSubscriptionArguments;
ItemSubscriptionArguments4: _EasyDAItemSubscriptionArguments;
begin
ItemSubscriptionArguments1 := CoEasyDAItemSubscriptionArguments.Create;
ItemSubscriptionArguments1.ServerDescriptor.ServerClass := 'OPCLabs.KitServer.2';
ItemSubscriptionArguments1.ItemDescriptor.ItemID := 'Simulation.Random';
ItemSubscriptionArguments1.GroupParameters.RequestedUpdateRate := 1000;
ItemSubscriptionArguments2 := CoEasyDAItemSubscriptionArguments.Create;
ItemSubscriptionArguments2.ServerDescriptor.ServerClass := 'OPCLabs.KitServer.2';
ItemSubscriptionArguments2.ItemDescriptor.ItemID := 'Trends.Ramp (1 min)';
ItemSubscriptionArguments2.GroupParameters.RequestedUpdateRate := 1000;
ItemSubscriptionArguments3 := CoEasyDAItemSubscriptionArguments.Create;
ItemSubscriptionArguments3.ServerDescriptor.ServerClass := 'OPCLabs.KitServer.2';
ItemSubscriptionArguments3.ItemDescriptor.ItemID := 'Trends.Sine (1 min)';
ItemSubscriptionArguments3.GroupParameters.RequestedUpdateRate := 1000;
// Intentionally specifying an unknown item here, to demonstrate its behavior.
ItemSubscriptionArguments4 := CoEasyDAItemSubscriptionArguments.Create;
ItemSubscriptionArguments4.ServerDescriptor.ServerClass := 'OPCLabs.KitServer.2';
ItemSubscriptionArguments4.ItemDescriptor.ItemID := 'SomeUnknownItem';
ItemSubscriptionArguments4.GroupParameters.RequestedUpdateRate := 1000;
Arguments := VarArrayCreate([0, 3], varVariant);
Arguments[0] := ItemSubscriptionArguments1;
Arguments[1] := ItemSubscriptionArguments2;
Arguments[2] := ItemSubscriptionArguments3;
Arguments[3] := ItemSubscriptionArguments4;
// Instantiate the client object
Client := CoEasyDAClient.Create;
// In order to use event pull, you must set a non-zero queue capacity upfront.
Client.PullItemChangedQueueCapacity := 1000;
WriteLn('Subscribing item changes...');
TVarData(HandleArray).VType := varArray or varVariant;
TVarData(HandleArray).VArray := PVarArray(
Client.SubscribeMultipleItems(Arguments));
WriteLn('Processing item changes for 1 minute...');
EndTick := Ticks + 60*1000;
while Ticks < EndTick do
begin
EventArgs := Client.PullItemChanged(2*1000);
if EventArgs <> nil then
// Handle the notification event
if eventArgs.Succeeded then
WriteLn(eventArgs.Arguments.ItemDescriptor.ItemId, ': ', eventArgs.Vtq.ToString)
else
WriteLn(eventArgs.Arguments.ItemDescriptor.ItemId, ' *** Failure: ', eventArgs.ErrorMessageBrief);
end;
WriteLn('Unsubscribing item changes...');
Client.UnsubscribeAllItems;
VarClear(HandleArray);
VarClear(Arguments);
WriteLn('Finished.');
end;
# This example shows how to subscribe to changes of multiple items and obtain the item changed events by pulling them.
#
# The Python for Windows (pywin32) extensions package is needed. Install it using "pip install pypiwin32".
# CAUTION: We now recommend using Python.NET package instead. Full set of examples with Python.NET is available!
#
# Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
import time
import win32com.client
# Instantiate the client object
client = win32com.client.Dispatch('OpcLabs.EasyOpc.DataAccess.EasyDAClient')
# In order to use event pull, you must set a non-zero queue capacity upfront.
client.PullItemChangedQueueCapacity = 1000
print('Subscribing item changes...')
itemSubscriptionArguments1 = win32com.client.Dispatch('OpcLabs.EasyOpc.DataAccess.OperationModel.EasyDAItemSubscriptionArguments')
itemSubscriptionArguments1.ServerDescriptor.ServerClass = 'OPCLabs.KitServer.2'
itemSubscriptionArguments1.ItemDescriptor.ItemID = 'Simulation.Random'
itemSubscriptionArguments1.GroupParameters.RequestedUpdateRate = 1000
itemSubscriptionArguments2 = win32com.client.Dispatch('OpcLabs.EasyOpc.DataAccess.OperationModel.EasyDAItemSubscriptionArguments')
itemSubscriptionArguments2.ServerDescriptor.ServerClass = 'OPCLabs.KitServer.2'
itemSubscriptionArguments2.ItemDescriptor.ItemID = 'Trends.Ramp (1 min)'
itemSubscriptionArguments2.GroupParameters.RequestedUpdateRate = 1000
itemSubscriptionArguments3 = win32com.client.Dispatch('OpcLabs.EasyOpc.DataAccess.OperationModel.EasyDAItemSubscriptionArguments')
itemSubscriptionArguments3.ServerDescriptor.ServerClass = 'OPCLabs.KitServer.2'
itemSubscriptionArguments3.ItemDescriptor.ItemID = 'Trends.Sine (1 min)'
itemSubscriptionArguments3.GroupParameters.RequestedUpdateRate = 1000
# Intentionally specifying an unknown item here, to demonstrate its behavior.
itemSubscriptionArguments4 = win32com.client.Dispatch('OpcLabs.EasyOpc.DataAccess.OperationModel.EasyDAItemSubscriptionArguments')
itemSubscriptionArguments4.ServerDescriptor.ServerClass = 'OPCLabs.KitServer.2'
itemSubscriptionArguments4.ItemDescriptor.ItemID = 'UnknownItem'
itemSubscriptionArguments4.GroupParameters.RequestedUpdateRate = 1000
arguments = [
itemSubscriptionArguments1,
itemSubscriptionArguments2,
itemSubscriptionArguments3,
itemSubscriptionArguments4,
]
client.SubscribeMultipleItems(arguments)
print('Processing item changes for 1 minute...')
endTime = time.time() + 60
while time.time() < endTime:
eventArgs = client.PullItemChanged(2*1000)
if eventArgs is not None:
# Handle the notification event
if (eventArgs.Succeeded):
print(eventArgs.Arguments.ItemDescriptor.ItemId, ': ', eventArgs.Vtq, sep='')
else:
print(eventArgs.Arguments.ItemDescriptor.ItemId, ' *** Failure: ', eventArgs.ErrorMessageBrief, sep='')
print('Unsubscribing item changes...')
client.UnsubscribeAllItems()
print('Finished.')
REM This example shows how to subscribe to changes of multiple items and obtain the item changed events by pulling them.
REM
REM Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
REM OPC client and subscriber examples in Visual Basic on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-VB .
REM Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
REM a commercial license in order to use Online Forums, and we reply to every post.
Private Sub PullItemChanged_MultipleItems_Command_Click()
OutputText = ""
Dim eventArgs As EasyDAItemChangedEventArgs
Dim itemSubscriptionArguments1 As New EasyDAItemSubscriptionArguments
itemSubscriptionArguments1.serverDescriptor.ServerClass = "OPCLabs.KitServer.2"
itemSubscriptionArguments1.ItemDescriptor.itemId = "Simulation.Random"
itemSubscriptionArguments1.GroupParameters.requestedUpdateRate = 1000
Dim itemSubscriptionArguments2 As New EasyDAItemSubscriptionArguments
itemSubscriptionArguments2.serverDescriptor.ServerClass = "OPCLabs.KitServer.2"
itemSubscriptionArguments2.ItemDescriptor.itemId = "Trends.Ramp (1 min)"
itemSubscriptionArguments2.GroupParameters.requestedUpdateRate = 1000
Dim itemSubscriptionArguments3 As New EasyDAItemSubscriptionArguments
itemSubscriptionArguments3.serverDescriptor.ServerClass = "OPCLabs.KitServer.2"
itemSubscriptionArguments3.ItemDescriptor.itemId = "Trends.Sine (1 min)"
itemSubscriptionArguments3.GroupParameters.requestedUpdateRate = 1000
' Intentionally specifying an unknown item here, to demonstrate its behavior.
Dim itemSubscriptionArguments4 As New EasyDAItemSubscriptionArguments
itemSubscriptionArguments4.serverDescriptor.ServerClass = "OPCLabs.KitServer.2"
itemSubscriptionArguments4.ItemDescriptor.itemId = "SomeUnknownItem"
itemSubscriptionArguments4.GroupParameters.requestedUpdateRate = 1000
Dim arguments(3) As Variant
Set arguments(0) = itemSubscriptionArguments1
Set arguments(1) = itemSubscriptionArguments2
Set arguments(2) = itemSubscriptionArguments3
Set arguments(3) = itemSubscriptionArguments4
' Instantiate the client object
Dim client As New EasyDAClient
' In order to use event pull, you must set a non-zero queue capacity upfront.
client.PullItemChangedQueueCapacity = 1000
OutputText = OutputText & "Subscribing item changes..." & vbCrLf
Dim handleArray() As Variant
handleArray = client.SubscribeMultipleItems(arguments)
OutputText = OutputText & "Processing item changes for 1 minute..." & vbCrLf
Dim endTick As Long
endTick = GetTickCount + 60000
While GetTickCount < endTick
Set eventArgs = client.PullItemChanged(2 * 1000)
If Not eventArgs Is Nothing Then
' Handle the notification event
If eventArgs.Succeeded Then
OutputText = OutputText & eventArgs.arguments.ItemDescriptor.itemId & ": " & eventArgs.vtq & vbCrLf
Else
OutputText = OutputText & eventArgs.arguments.ItemDescriptor.itemId & " *** Failure: " & eventArgs.ErrorMessageBrief & vbCrLf
End If
End If
Wend
OutputText = OutputText & "Unsubscribing item changes..." & vbCrLf
client.UnsubscribeAllItems
OutputText = OutputText & "Finished." & vbCrLf
End Sub