Online Forums
Technical support is provided through Support Forums below. Anybody can view them; you need to Register/Login to our site (see links in upper right corner) in order to Post questions. You do not have to be a licensed user of our product.
Please read Rules for forum posts before reporting your issue or asking a question. OPC Labs team is actively monitoring the forums, and replies as soon as possible. Various technical information can also be found in our Knowledge Base. For your convenience, we have also assembled a Frequently Asked Questions page.
Do not use the Contact page for technical issues.
- Forum
- Discussions
- QuickOPC-Classic in .NET
- Browsing, Browse Dialogs and Controls
- Getting the entire Tree Structure of OPC server
Getting the entire Tree Structure of OPC server
08 Sep 2014 13:22 #2241
by support
Replied by support on topic Getting the entire Tree Structure of OPC server
I am not quite sure what the question or problem is. In order to browse the whole tree, one needs to know how to make the browse on certain branch, and how to proceed to a sub-branch. You may learn from an example that ships with the product - if you open the Examples solution, it is under Console/BrowseAndReadValues, and looks like this:
Instead of reading the items right away, you can construct a data structure that represents a tree in a way you like. In fact, reading items one by one like this is only good for demonstration purposes, but is very inefficient in reality. For OPC servers with usual number of items, even the browsing itself may be time consuming; reading should then be done afterwards, in operations that work with multiple items at once.
// 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 System.Diagnostics;
using JetBrains.Annotations;
using OpcLabs.EasyOpc;
using OpcLabs.EasyOpc.DataAccess;
using System;
namespace BrowseAndReadValues
{
class Program
{
const string ServerClass = "OPCLabs.KitServer.2";
[NotNull]
static readonly EasyDAClient Client = new EasyDAClient();
static void BrowseAndReadFromNode([NotNull] string parentItemId)
{
// Obtain all node elements under parentItemId
var nodeFilter = new DANodeFilter(); // no filtering whatsoever
DANodeElementCollection nodeElementCollection = Client.BrowseNodes("", ServerClass, parentItemId, nodeFilter);
// Remark: that BrowseNodes(...) may also throw OpcException; a production code should contain handling for it, here
// omitted for brevity.
foreach (DANodeElement nodeElement in nodeElementCollection)
{
Debug.Assert(nodeElement != null);
// 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);
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 &&
(nodeElement.ItemId != "SimulateEvents") /* this branch is too big for the purpose of this example */)
BrowseAndReadFromNode(nodeElement);
}
}
static void Main()
{
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.InstanceParameters.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();
}
}
}
Instead of reading the items right away, you can construct a data structure that represents a tree in a way you like. In fact, reading items one by one like this is only good for demonstration purposes, but is very inefficient in reality. For OPC servers with usual number of items, even the browsing itself may be time consuming; reading should then be done afterwards, in operations that work with multiple items at once.
Please Log in or Create an account to join the conversation.
- Vishweshwar_Kapse
- Topic Author
- Offline
- Senior Member
Less
More
- Posts: 5
- Thank you received: 0
08 Sep 2014 12:54 - 08 Sep 2014 13:17 #2239
by Vishweshwar_Kapse
Getting the entire Tree Structure of OPC server was created by Vishweshwar_Kapse
Hi we are planning to purchase the QuickOPC for our project requirement
I need to get the entire Tree (Only Item ID) of the OPC server just like how the demo application shows the tree when we click on Browse Items.
I have created a class in my C# WCF project like this
and I want the _branchList to contain the list of Branches each nodes sub branches or the leaves.
I have tried doing this to get all the tags on the OPC server.
Can you please help me in returning it with a hierarchy of objects that looks like a tree which can be used in my application.
I need to get the entire Tree (Only Item ID) of the OPC server just like how the demo application shows the tree when we click on Browse Items.
I have created a class in my C# WCF project like this
public class OPCDataTreeNodes
{
bool _isBranch = false;
bool _isLeaf = false;
string _itemID = string.Empty;
string _parentItemID = string.Empty;
string _value = string.Empty;
string _quality = string.Empty;
string _timeStamp = string.Empty;
string _itemName = string.Empty;
string _errorWhileAccessingItem = string.Empty;
string _browsePath = string.Empty;
List<OPCDataTreeNodes> _branchList = null;
}
and I want the _branchList to contain the list of Branches each nodes sub branches or the leaves.
I have tried doing this to get all the tags on the OPC server.
private List<OPCDataTreeNodes> GetOPCServerStructure(string ParentItemID, string ServerClass, string MachineName)
{
try
{
DANodeElementCollection NodeElementCollection = new EasyDAClient().BrowseNodes(new ServerDescriptor(MachineName, ServerClass), ParentItemID, new DANodeFilter());
foreach (DANodeElement nodeElement in NodeElementCollection)
{
OPCDataTreeNodes Node = new OPCDataTreeNodes();
Debug.Assert(nodeElement != null);
// If the node is a leaf, it might be possible to read from it
if (nodeElement.IsLeaf)
{
try
{
DAVtq result = Client.ReadItem(MachineName, ServerClass, nodeElement.ItemId);
if (result.Value != null)
{
Node.ItemValue = result.Value.ToString();
}
Node.ItemID = nodeElement.ItemId.ToString();
Node.IsLeaf = true;
Node.IsBranch = false;
Node.ParentItemID = ParentItemID;
Node.Quality = result.Quality.ToString();
Node.TimeStamp = result.Timestamp.ToString();
ListOfLeaves.Add(Node);
}
catch (OpcException exception)
{
Node.ItemReadError = String.Format("** {0} **", exception.GetBaseException().Message);
}
}
// If the node is a branch, browse recursively into it.
if (nodeElement.IsBranch)
{
NoeItems.Add(nodeElement.ItemId);
OPCDataTreeNodes ListOfBranches = new OPCDataTreeNodes();
GetOPCServerStructure(nodeElement, ServerClass, MachineName);
}
}
return ListOfLeaves;
}
catch (Exception ex)
{
throw ex;
}
}
Last edit: 08 Sep 2014 13:17 by support. Reason: code formatting
Please Log in or Create an account to join the conversation.
Moderators: support
- Forum
- Discussions
- QuickOPC-Classic in .NET
- Browsing, Browse Dialogs and Controls
- Getting the entire Tree Structure of OPC server
Time to create page: 0.046 seconds