当前位置: 首页 > news >正文

C#读取OPCUA节点数据

本人第一次接触OPCUA,如有不对的地方望指正,获取的是公司的OPCUA服务器的数据

方式一:

测试环境:

window11

vs2022

OPCFoundation.NetStandard.Opc.Ua

.net framework 4.8

测试环境如下:

1  新建.net framework 4.8的控制台项目,项目名称为OPCDemo3

2 在nuget中安装OPCFoundation.NetStandard.Opc.Ua,版本选择1.5.376.213,如下图:

3 编写代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Opc.Ua;
using Opc.Ua.Client;
using Opc.Ua.Configuration;
namespace OPCDemo3
{internal class Program{static void Main(string[] args){string endpointUrl = "opc.tcp://ip地址:4840";string nodeId = "ns=4;s=|var|CODESYS Control Win V3 x64.Application.Para.PLC_Inf.wHeatBeat";// 创建应用配置var config = new ApplicationConfiguration(){ApplicationName = "OPCDemo2",ApplicationType = ApplicationType.Client,SecurityConfiguration = new SecurityConfiguration{ApplicationCertificate = new CertificateIdentifier(),AutoAcceptUntrustedCertificates = true},TransportConfigurations = new TransportConfigurationCollection(),TransportQuotas = new TransportQuotas { OperationTimeout = 15000 },ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 }};config.Validate(ApplicationType.Client);// 创建OPC UA会话var selectedEndpoint = CoreClientUtils.SelectEndpoint(endpointUrl, false);var endpointConfiguration = EndpointConfiguration.Create(config);var endpoint = new ConfiguredEndpoint(null, selectedEndpoint, endpointConfiguration);var session = Session.Create(config, endpoint, false, "OPCDemo2", 60000, null, null).Result;Console.WriteLine("打开成功");// 读取节点DataValue value = session.ReadValue(nodeId);if (StatusCode.IsGood(value.StatusCode)){ushort wHeatBeat = Convert.ToUInt16(value.Value);Console.WriteLine($"wHeatBeat:{wHeatBeat}");}else{Console.WriteLine("读取失败");}Console.ReadLine();}}
}

4 程序运行结果如下:

方式二:

测试环境:

window11

vs2022

SuperPLC.OPCUA

.net 8

测试步骤如下:

1 新建.net 8控制台项目,名称为OPCDemo2

2 在Nuget中安装OpcUaHelper,版本选择2.2.1,同时安装SuperPLC.OPCUA,版本选择:1.1.0

但在安装SuperPLC.OPCUA时,很奇怪,Nuget程序包管理界面搜不出来,如下图:

只能通过nuget控制台安装,命令如下:

NuGet\Install-Package SuperPLC.OPCUA -Version 1.1.0

3 编辑代码如下:

// See https://aka.ms/new-console-template for more informationusing SuperPLC.OPCUA;string _prefixPLCApp = $@"ns=4;s=|var|CODESYS Control Win V3 x64.Application.";Plc plc = new Plc("ip地址");
bool ret = plc.Open();
if (!ret)
{Console.WriteLine("打开失败");return;
}
plc.TryRead($"{_prefixPLCApp}Para.PLC_Inf.wHeatBeat", out ushort wHeatBeat);Console.WriteLine($"wHeatBeat:{wHeatBeat}");Console.WriteLine("打开成功");
Console.ReadLine();

4 运行结果如下:

http://www.lqws.cn/news/454627.html

相关文章:

  • API 调试工具校验 JSON Mock 接口(一):无参请求与基础响应验证
  • Android 当apk是系统应用时,无法使用webView的解决方案
  • 汽车加气站操作工考试题库含答案【最新】
  • DB面试题
  • mysql查询使用`_rowid` 虚拟列
  • vtk和opencv和opengl直接的区别是什么?
  • Pinia在多步骤表单中的实践应用
  • 芯谷科技--高性能、高可靠性降压转换器D3502C
  • 泛型方法调用需要显示指定泛型类型的场景
  • 【JUC面试篇】Java并发编程高频八股——线程池
  • 5G 浪潮:发展全景、困境突围与未来航向
  • Fiddler抓包工具使用技巧:如何结合Charles和Wireshark提升开发调试效率
  • conda(Anaconda/Miniconda)常用命令
  • 头部黄金珠宝品牌扬帆狮城,百胜双中台一体化方案护航全球化运营
  • Linux下nginx访问路径页面
  • 【MySQL篇10】:四种分库分表详解
  • 云蝠智能大模型呼叫系统:为企业提供专业的智能客户联络
  • 第3讲、LangChain性能优化:上下文缓存与流式响应实战指南
  • 通过Radius认证服务器实现飞塔/华为防火墙二次认证:原理、实践与安全价值解析
  • django serializer __all__中 额外添加外键里的某一个属性
  • King’s LIMS 系统引领汽车检测实验室数字化转型
  • Unity3d中使用Mirror进行自定义消息通信
  • 解锁决策树:数据挖掘的智慧引擎
  • YOLO系列模型基础与实战(YOLO系列发展线)
  • YOLOv11改进 | RCS-OSA与C3k2融合架构技术详解
  • 容器技术技术入门与Docker环境部署
  • 在 CentOS 7.9 中 Node 18.20.2 安装指南
  • HTTP 请求方法与状态码
  • MySQL慢SQL优化全攻略:从诊断到调优
  • day30 导包