首页 > 编程语言 >[CSharpTips]C# 获取OPCUA服务器节点

[CSharpTips]C# 获取OPCUA服务器节点

时间:2024-01-05 16:24:51浏览次数:53  
标签:Console NodeClass C# NodeId item CSharpTips nodeToBrowse2 string OPCUA

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

相关文章

  • 【JDK+jenkins+gitee实现CI/CD(之三)】 jenkins拉取gitee代码WebHook返回403或404错误
     先上几张让我纠结了很多天的截图  以上是在我配置好jenkins和gitee令牌后一直出现的错误,查询了很多天资料后,确认造成的主要原因就是权限问题我的Jenkins 是最新的版本2.426.2,说下以下我使用过但无效果的办法,这些方法不能解决我提交代码后Jenkins触发WebHook的403问题......
  • 关于腾讯地图geolocation.getLocation 经常定位失败,定位时间过长的解决方法
    今天遇到个项目,腾讯地图定位出现问题,导致地图无法呈现出最近的目标 这是正常的效果,之前一直出现贵州等地点的信息,查看控制台的网络后,发现腾讯的定位失败,要么就是定位时间过长,要20S左右,但是换EDGE浏览器却能正常加载,除了EDGE浏览器都会出现这个问题。随后我去查阅腾讯地图接口......
  • 智能八电极脂肪秤pcba方案介绍
    智能八电极脂肪秤是一种测量人体脂肪含量的设备,它可以帮助人们了解自己的身体状况,从而更好地进行健康管理和减肥。在本文中,我们将从功能、运用、原理和结构四个方面了解智能八电极脂肪秤方案产品。一、功能智能八电极脂肪秤方案的主要功能:1.测量范围:智能八电极......
  • JavaScript——数组的归并方法
    JavaScript的reduce和reduceRight的作用是通过遍历数组得到一个结果,原理如下:functionmyReduce(execute,initValue){constlength=this.lengthletresultfor(leti=0;i<length;i++){if(i===0){consthasInitValue=initV......
  • Python+Requests+PyTest+Excel+Allure 接口自动化测试实战
    本文主要介绍了Python+Requess+PyTest+Excel+Allure接口自动化测试实战,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧Unittest是Python标准库中自带的单元测试框架,Unittest有时候也被称为PyUnit,就像JUnit......
  • (五十一)C#编程基础复习——C#队列
    在C#中,队列类与堆栈类类似,它代表了一个先进先出的对象结合,当你需要对项目进行先进先出访问时,则可以使用队列。向队列中添加元素称为入队,从堆栈中移除元素称为出队。一、队列类中的属性下表中列出了队列类中的一些常用属性二、队列类中的方法下表列出了队列类的一些常用方法......
  • TDSQL(PostgreSQL版本) benchmark性能测试
    一、准备软件包jdk:地址:https://pan.baidu.com/s/1sbgLPROfd9e_valSfv0YAQ提取码:4qpsbenchmark:地址:https://pan.baidu.com/s/1nAHER-BXpgG0LUnR8NbT7Q提取码:xcbu二、安装1、jdk安装:1.1解压安装到34机器的/data/tbase/jdk目录下1.2配置环境变量如下:[tbase@VM_0_37_cent......
  • typora mac版激活
    前往/Applications/Typora.app/Contents/Resources/TypeMark/page-dist/static/js文件夹打开LicenseIndex.180dd4c7.54395836.chunk.js文件将hasActivated="true"==e.hasActivated修改为hasActivated="true"=="true"重新启动typora......
  • docker-compose.yml转换工具--composerize
    简介composerize是一个CLI工具,可以将dockerrun命令转换为docker-compose配置清单。除了CLI之外还提供了一个可视化界面,有兴趣可以自己看看下载~]#npminstallcomposerize-g使用~]#composerizedockerrun-d-p9000:9000-eASR_MODEL=base-eASR_ENGINE=o......
  • 第5章 在 ASP.NET Core Web 应用进程中使用 EF Core
    本章涵盖在ASP.NETCore中使用EFCore在ASP.NETCore中使用依赖注入在ASP.NET核心MVC操作中访问数据库使用EFCore迁移更新数据库使用async/await提高可扩展性在本章中,您将使用ASP.NETCore构建一个真正的Web应用进程,将所有内容集成在一起。当然,使用ASP.N......