C# 获取OPCUA服务器节点
1.创建OPCUA服务器 可以参考 KepServerv设置为OPCUA通讯说明_kepserver opcua-CSDN博客
2.创建控制台工程,Nuget安装OpcUaHelper
3.Demo如下 参考了OpcUaHelper源码 https://github.com/dathlin/OpcUaHelper
using Opc.Ua; using OpcUaHelper; using System; using System.Threading.Tasks; namespace OpcUaHelperGetNoteIdTest { internal class Program { static OpcUaClient opcUaClient; static void Main(string[] args) { opcUaClient = new OpcUaClient(); opcUaClient.UserIdentity = new UserIdentity(new AnonymousIdentityToken()); opcUaClient.ConnectServer(@"opc.tcp://127.0.0.1:49320"); if (opcUaClient.Connected) { //打印Objects节点下所有NodeId Recursive(ObjectIds.ObjectsFolder); Console.WriteLine("======================================================================================================================="); //打印指定节点下所有NodeId Recursive("ns=2;s=通道 1.设备 1"); //浏览一个节点的引用 OpcUaHelper内置方法 Console.WriteLine("======================================================================================================================="); ReferenceDescription[] references = opcUaClient.BrowseNodeReference("ns=2;s=通道 1.设备 1"); foreach (var item in references) { Console.Write(string.Format("{0,-30}", item.NodeClass)); Console.Write(string.Format("{0,-30}", item.BrowseName)); Console.Write(string.Format("{0,-20}", item.DisplayName)); Console.WriteLine(string.Format("{0,-20}", item.NodeId.ToString())); } //浏览一个节点的所有属性 OpcUaHelper内置方法 Console.WriteLine("======================================================================================================================="); OpcNodeAttribute[] dataValue = opcUaClient.ReadNoteAttributes("ns=2;s=通道 1.设备 1.FloatTest"); foreach (var item in dataValue) { Console.Write(string.Format("{0,-30}", item.Name)); Console.Write(string.Format("{0,-20}", item.Type)); Console.Write(string.Format("{0,-20}", item.StatusCode)); Console.WriteLine(string.Format("{0,20}", item.Value)); } } Console.ReadLine(); } /// <summary> /// 通过递归打印指定节点下所有NodeId /// </summary> /// <param name="nodeID"></param> public static void Recursive(NodeId nodeID) { ReferenceDescriptionCollection references = GetReferenceDescriptionCollection(nodeID); foreach (var reference in references) { Console.WriteLine(reference.NodeId); Recursive((NodeId)reference.NodeId); } } /// <summary> /// 获取NodeId下所有节点 /// </summary> /// <param name="sourceId"></param> /// <returns></returns> static ReferenceDescriptionCollection GetReferenceDescriptionCollection(NodeId sourceId) { TaskCompletionSource<ReferenceDescriptionCollection> task = new TaskCompletionSource<ReferenceDescriptionCollection>(); // find all of the components of the node. BrowseDescription nodeToBrowse1 = new BrowseDescription(); nodeToBrowse1.NodeId = sourceId; nodeToBrowse1.BrowseDirection = BrowseDirection.Forward; nodeToBrowse1.ReferenceTypeId = ReferenceTypeIds.Aggregates; nodeToBrowse1.IncludeSubtypes = true; nodeToBrowse1.NodeClassMask = (uint)(NodeClass.Object | NodeClass.Variable | NodeClass.Method | NodeClass.ReferenceType | NodeClass.ObjectType | NodeClass.View | NodeClass.VariableType | NodeClass.DataType); nodeToBrowse1.ResultMask = (uint)BrowseResultMask.All; // find all nodes organized by the node. BrowseDescription nodeToBrowse2 = new BrowseDescription(); nodeToBrowse2.NodeId = sourceId; nodeToBrowse2.BrowseDirection = BrowseDirection.Forward; nodeToBrowse2.ReferenceTypeId = ReferenceTypeIds.Organizes; nodeToBrowse2.IncludeSubtypes = true; nodeToBrowse2.NodeClassMask = (uint)(NodeClass.Object | NodeClass.Variable | NodeClass.Method | NodeClass.View | NodeClass.ReferenceType | NodeClass.ObjectType | NodeClass.VariableType | NodeClass.DataType); nodeToBrowse2.ResultMask = (uint)BrowseResultMask.All; BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); nodesToBrowse.Add(nodeToBrowse1); nodesToBrowse.Add(nodeToBrowse2); // fetch references from the server. ReferenceDescriptionCollection references = FormUtils.Browse(opcUaClient.Session, nodesToBrowse, false); return references; } } }
标签:Console,NodeClass,C#,NodeId,item,CSharpTips,nodeToBrowse2,string,OPCUA From: https://www.cnblogs.com/axiaoshuye/p/17947499