Hello,
i use this code to browse all tags in a DA server:
public class OpcDANode
{
public bool IsRoot { get; set; } = false;
public List<OpcDANode> ChildNodes { get; set; } = new List<OpcDANode>();
public DANodeElement Node { get; set; }
public bool HasChildNodes => ChildNodes.Count > 0;
public string TagNameFull { get; set; }
public string TagNameLastPart => Node?.Name ?? string.Empty;
public bool IsLeaf => Node?.IsLeaf ?? false;
public OpcDANode(DANodeElement node = null)
{
Node = node;
TagNameFull = node?.ItemId ?? string.Empty;
}
}
public async Task<List<OpcDANode>> GetAllNodesAsync()
{
var rootNode = new OpcDANode
{
IsRoot = true,
TagNameFull = "Root"
};
try
{
BrowseRecursiveNodesPro(rootNode, true);
}
catch (Exception ex)
{
Log.Error("Error while browsing OPC DA items: {ErrorMessage}", ex.Message);
throw;
}
return new List<OpcDANode>() { rootNode };
}
private void BrowseRecursiveNodesPro(OpcDANode parentNode, bool isFirstRun = true, bool skipFirstlevelFromTagName = true)
{
if (isFirstRun)
{
parentNode.IsRoot = true;
parentNode.TagNameFull = parentNode.Node?.ItemId ?? "Root";
}
var nodeFilter = new DABrowseParameters();
DANodeElementCollection nodeElementCollection = new DANodeElementCollection();
try
{
nodeElementCollection = _client.BrowseNodes(Server, isFirstRun ? "" : parentNode.TagNameFull, nodeFilter);
}
catch (OpcException oex)
{
Log.Logger.Error(oex, $"OPC Exception while browsing nodes at node {parentNode?.TagNameFull ?? "root"}.");
return;
}
catch (Exception ex)
{
Log.Logger.Error(ex, $"Error browsing OPC nodes at node {parentNode?.TagNameFull ?? "root"}.");
return;
}
foreach (var nodeElement in nodeElementCollection)
{
if (nodeElement != null)
{
var childNode = new OpcDANode
{
Node = nodeElement,
IsRoot = false
};
if (isFirstRun)
{
childNode.TagNameFull = $"{nodeElement.ItemId}";
}
else
{
childNode.TagNameFull = $"{parentNode.TagNameFull}.{nodeElement.ItemId}";
}
parentNode.ChildNodes.Add(childNode);
if (nodeElement.IsBranch && nodeElement.ItemId != "SimulateEvents")
{
BrowseRecursiveNodesPro(childNode, false);
}
}
}
}
I install a Matrikon OPC Server Simulator for testing.
Please see the attachment for folder (branch) structure.
The problem is that the "Simulation Items" is not part of the tagname, therefore my code throws exception because it will not find the tagname that contains "Simulation Items".
So my code generates this tagname: "Simulation Items.Bucket Brigade.ArrayOfReal8", but the correct tagname is: "Bucket Brigade.ArrayOfReal8".
I try to define the difference at object level between "Simulation Items" and "Bucket Brigade", but there are no property difference. Both is a branch.
Can you help me please what is this "Simulation Items" part, it is a namespace or something? Why the full tagname not contains this part?
How can I detect programatically that full tagname must contains "Simulation Items" or not?
Thank you!