Extensions described in this article allow you to block the execution and wait until certain condition, based on OPC-DA item data, is fulfilled.
Waiting until item data satisfy some condition
This functionality is not available under (or the text does not apply to) COM development platform.
With methods in this group, you can subscribe to specified OPC-DA items, and wait by monitoring their data until specified conditions (predicates) become true, and then automatically unsubscribe. The methods also return when the specified time elapses. The item data that satisfied the specified condition is returned by the method.
In order to use the described functionality, call the IEasyDAClientExtension2.WaitForMultipleItemValues method. There are multiple overloads.
Waiting until item data have some minimum quality
It is not uncommon that an OPC server needs considerable time before it can provide the data requested. When you Read an item or Subscribe to an item with some OPC servers, they will first deliver data with "bad" or "uncertain" quality, and only after a while, data with "good" quality are provided. This behavior, of course, creates a problem if all your code needs is that actual, "good" data value, for further processing. You cannot just do a Read; you need additional logic to overcome the possibility of these initial "uncertain" or "bad" results or updates.
QuickOPC has ready-made methods that contain the necessary logic. The IEasyDAClientExtension2.WaitForMultipleItemValues method is similar in its usage to ReadMultipleItemValues Method , and the IEasyDAClientExtension2.WaitForItemValue method is similar in its usage to ReadItemValue Method. Internally, however, they use subscriptions to possibly observe the data over a period of time, and automatically overcome the initial "unreliable" updates of the value.
The methods have various overloads that allow you to leave out some of the arguments and use their default values instead. In the full form, you need to specify:
- Subscription (group) parameters, such as the requested update rate, used by the subscription to observe the incoming data.
- The minimum required quality.
- The timeout for the operation.
The waiting for item data completes immediately if an error occurs. For example, if an error occurs indicating that the server is inaccessible, or the item does not exist, the methods do not attempt to wait further on that item. You cannot use methods in this group to wait for quality data if errors are expected. Instead, use the wait methods that allow you to specify a condition (predicate), and write a code that formulates the termination condition based on your needs.
Example
// This example shows how to wait on an item until a value with "good" quality becomes available.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
using System;
using OpcLabs.EasyOpc.DataAccess;
using OpcLabs.EasyOpc.DataAccess.Extensions;
using OpcLabs.EasyOpc.OperationModel;
namespace DocExamples.DataAccess._EasyDAClientExtension
{
class WaitForItemValue
{
public static void Main1()
{
// Instantiate the client object.
var client = new EasyDAClient();
Console.WriteLine("Waiting until an item value with \"good\" quality becomes available...");
object value;
try
{
value = client.WaitForItemValue("", "OPCLabs.KitServer.2", "Demo.Unreliable",
groupParameters: 100, // this is the requested update rate
millisecondsTimeout: 60*1000);
}
catch (OpcException opcException)
{
Console.WriteLine("*** Failure: {0}", opcException.GetBaseException().Message);
return;
}
// Display the obtained item value.
Console.WriteLine($"value: {value}");
}
}
}
# This example shows how to wait on an item until a value with "good" quality becomes available.
#
# 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 .NET namespaces.
from OpcLabs.EasyOpc.DataAccess import *
from OpcLabs.EasyOpc.DataAccess.Extensions import *
from OpcLabs.EasyOpc.OperationModel import *
# Instantiate the client object
client = EasyDAClient()
print('Waiting until an item value with "good" quality becomes available...')
try:
value = IEasyDAClientExtension2.WaitForItemValue(client, '', 'OPCLabs.KitServer.2',
DAItemDescriptor('Demo.Unreliable'),
DAGroupParameters(100), # this is the requested update rate
60*1000) # timeout in milliseconds
except OpcException as opcException:
print('*** Failure: ' + opcException.GetBaseException().Message)
exit()
# Display the obtained item value.
print('value: ', value, sep='')
' This example shows how to wait on an item until a value with "good" quality becomes available.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
Imports OpcLabs.EasyOpc.DataAccess
Imports OpcLabs.EasyOpc.DataAccess.Extensions
Imports OpcLabs.EasyOpc.OperationModel
Namespace DataAccess._EasyDAClientExtension
Friend Class WaitForItemValue
Public Shared Sub Main1()
' Instantiate the client object.
Dim client = New EasyDAClient()
Console.WriteLine("Waiting until an item value with ""good"" quality becomes available...")
Dim value As Object
Try
value = client.WaitForItemValue("", "OPCLabs.KitServer.2", "Demo.Unreliable",
groupParameters:=100, ' this Is the requested update rate
millisecondsTimeout:=60 * 1000)
Catch opcException As OpcException
Console.WriteLine("*** Failure: {0}", opcException.GetBaseException().Message)
Exit Sub
End Try
' Display the obtained item value.
Console.WriteLine($"value: {value}")
End Sub
End Class
End Namespace
See Also
Examples - OPC DA Layered Extensions