// This example shows how to read items while subscribed.
using System;
using System.Diagnostics;
using System.Threading;
using OpcLabs.EasyOpc.DataAccess;
using OpcLabs.EasyOpc.DataAccess.OperationModel;
namespace DocExamples.DataAccess._EasyDAClient
{
partial class SubscribeMultipleItems
{
public static void WithRead()
{
// Instantiate the client object.
using (var client = new EasyDAClient())
{
client.ItemChanged += client_Read_ItemChanged;
Console.WriteLine("Subscribing item changes...");
client.SubscribeMultipleItems(
new[] {
new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Random", 1000, null),
new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Trends.Sine (1 min)", 1000, null),
});
Console.WriteLine("Waiting for 10 seconds...");
Thread.Sleep(10 * 1000);
Console.WriteLine("Reading items...");
DAVtqResult[] vtqResults = client.ReadMultipleItems("OPCLabs.KitServer.2",
new DAItemDescriptor[]
{
"Trends.Ramp (1 min)", "Trends.Sine (1 min)"
});
for (int i = 0; i < vtqResults.Length; i++)
{
Debug.Assert(vtqResults[i] != null);
if (vtqResults[i].Succeeded)
Console.WriteLine("vtqResults[{0}].Vtq: {1}", i, vtqResults[i].Vtq);
else
Console.WriteLine("vtqResults[{0}] *** Failure: {1}", i, vtqResults[i].ErrorMessageBrief);
}
Console.WriteLine("Waiting for 10 seconds...");
Thread.Sleep(10 * 1000);
Console.WriteLine("Unsubscribing item changes...");
}
Console.WriteLine("Finished.");
}
// Item changed event handler
static void client_Read_ItemChanged(object sender, EasyDAItemChangedEventArgs e)
{
if (e.Succeeded)
Console.WriteLine($"< {e.Arguments.ItemDescriptor.ItemId}: {e.Vtq}");
else
Console.WriteLine($"< {e.Arguments.ItemDescriptor.ItemId} *** Failure: {e.ErrorMessageBrief}");
}
}
}