A new example illustrating how to browse and read is available in QuickOPC-NET Bonus Pack 5.04.104.1; the source looks like this:
// BrowseAndReadValues: Console application that recursively browses and displays the nodes in the OPC address space, and
// attempts to read and display values of all OPC items it finds.
using OpcLabs.EasyOpc;
using OpcLabs.EasyOpc.DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BrowseAndReadValues
{
class Program
{
const string serverClass = "OPCLabs.KitServer.2";
static EasyDAClient client = new EasyDAClient();
static void BrowseAndReadFromNode(string parentItemId)
{
// Obtain all node elements under parentItemId
var nodeFilter = new DANodeFilter(); // no filtering whatsoever
Dictionary<string, DANodeElement> nodeDictionary = client.BrowseNodes("", serverClass, parentItemId, nodeFilter);
// Note that BrowseNodes(...) may also throw OpcException; a production code should contain handling for it, here
// omitted for brevity.
foreach (DANodeElement nodeElement in nodeDictionary.Values)
{
// If the node is a leaf, it might be possible to read from it
if (nodeElement.IsLeaf)
{
// Determine what the display - either the value read, or exception message in case of failure.
string display;
try
{
object value = client.ReadItemValue("", serverClass, nodeElement.ItemId);
display = String.Format("{0}", value);
}
catch (OpcException exception)
{
display = String.Format("** {0} **", exception.GetBaseException().Message);
}
Console.WriteLine("{0} -> {1}", nodeElement.ItemId, display);
}
// If the node is not a leaf, just display its itemId
else
Console.WriteLine("{0}", nodeElement.ItemId);
// If the node is a branch, browse recursively into it.
if (nodeElement.IsBranch)
BrowseAndReadFromNode(nodeElement.ItemId);
}
}
static void Main(string[] args)
{
Console.WriteLine("Browsing and reading values...");
// Set timeout to only wait 1 second - default would be 1 minute to wait for good quality that may never come.
client.Timeouts.ReadItem = 1000;
// Do the actual browsing and reading, starting from root of OPC address space (denoted by empty string for itemId)
BrowseAndReadFromNode("");
Console.WriteLine("Press Enter to continue...");
Console.ReadLine();
}
}
}