[CSharpTips]C# 获取OPCUA服务器节点
C# 获取OPCUA服务器节点
1.创建OPCUA服务器 可以参考 KepServerv设置为OPCUA通讯说明_kepserver opcua-CSDN博客
2.创建控制台工程,Nuget安装OpcUaHelper
3.Demo如下 参考了OpcUaHelper源码 https://github.com/dathlin/OpcUaHelper
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | 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; } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?