首页 > 编程语言 >IOT-OPC UA Client C# 实现方式->账号密码形式

IOT-OPC UA Client C# 实现方式->账号密码形式

时间:2024-02-20 16:11:46浏览次数:28  
标签:Console string C# IOT Client WriteLine ns TB OEE

本文只要记录OPC UA 方式读取PLC数据,默认opc server已经配置成功;
、外部引用 opcuahelper

using Opc.Ua;
using OpcUaHelper;

、源码如下;

> 点击查看代码
using Opc.Ua;
using OpcUaHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static OpcUaClient opcUaClient = new OpcUaHelper.OpcUaClient();
        static string url = @"opc.tcp://172.30.62.74:8020";
        static string username = "Admin";
        static string password = "123456";
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("采集系统启动中.....");
            //连接OPC UA 且获取连接状态
            bool result = OpcConnectByPwdandUid();
            if (result)
            {
                //当result为true时:OPC UA SERVER CONNECT SUCCESSFUL;
                ExecuteAction();
                Console.WriteLine("-----------------");
                Console.WriteLine("-----------------");
                Console.WriteLine("");
                ReadPoint();
            }
            else
            {
                Console.WriteLine("OPC UA 连接失败");
                Console.ReadKey();
            }
        }
        /// <summary>
        /// 用户名密码方式连接OPC UA 服务器
        /// </summary>
        /// <returns></returns>
        private static bool OpcConnectByPwdandUid()
        {
            bool result = false;
            opcUaClient.UserIdentity = new UserIdentity(username, password);
            try
            {
                opcUaClient.ConnectServer(url).Wait();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("OPC SERVER 连接成功");
                result = true;
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("异常提示:"+ex.ToString());
                Console.ReadKey();
            }
            return result;
        }

        static void ExecuteAction()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("开始采集..");
            //批量采集方式
            try
            {
                NodeId[] nodes = new NodeId[]
                {
                    "ns=2;s=TB_OEE_1",
                    "ns=2;s=TB_OEE_2",
                    "ns=2;s=TB_OEE_3",
                    "ns=2;s=TB_OEE_4",
                    "ns=2;s=TB_OEE_5",
                    "ns=2;s=TB_OEE_6",
                    "ns=2;s=TB_OEE_7",
                    "ns=2;s=TB_OEE_8",
                    "ns=2;s=TB_OEE_9",
                    "ns=2;s=TB_OEE_10",
                    "ns=2;s=TB_OEE_11",
                    "ns=2;s=TB_OEE_12",
                    "ns=2;s=TB_OEE_13",
                    "ns=2;s=TB_OEE_14",
                    "ns=2;s=TB_OEE_15",
                    "ns=2;s=TB_OEE_16",
                    "ns=2;s=TB_OEE_17",
                    "ns=2;s=TB_OEE_18",
                    "ns=2;s=TB_OEE_19",
                    "ns=2;s=TB_OEE_20"
                };
                foreach (Opc.Ua.DataValue item in opcUaClient.ReadNodes(nodes))
                {
                    object data = item.WrappedValue.Value;
                    Console.ForegroundColor = ConsoleColor.Green;
                    if (data != null)
                    {
                        Console.WriteLine(data.ToString());
                    }
                    else
                    {
                        Console.WriteLine("获取到的值为NULL");
                    } 
                }
                //Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                //异常时,释放OPC ua对象
                opcUaClient.Disconnect();
                Console.ReadKey();
            }
        }

        /// <summary>
        /// 通过设备清单关联点位地址读取方式TODO
        /// </summary>
        /// <param name="equipmentNo">设备标号</param>
        /// <param name="equipmentName">设备名称</param>
        /// <param name="pointLocation">采集地址</param>
        /// <param name="pointName">采集地址描述</param>
        /// <param name="values">返回采集值</param>
        /// <returns></returns>
        static string EquipmentCollection(string equipmentNo,string equipmentName,string pointLocation,string pointName,out string values)
        {
            values = "1";
            return "";
        }
        /// <summary>
        /// 
        /// </summary>
        static void ReadPoint()
        {
            try
            {
                ///①读取一个节点的关联节点,包含了几个简单的基本信息
                ReferenceDescription[] references = opcUaClient.BrowseNodeReference("ns=2;s=root.C2200_103");
                foreach (var item in references)
                {
                    string des = opcUaClient.ReadNoteAttributes(item.NodeId.ToString())[3].Value.ToString();
                    //根据得到的节点,读取当前值,下面2行代码未测试,需要调试
                    DataValue dv = opcUaClient.ReadNode(item.NodeId.ToString());
                    string currentValue = dv.WrappedValue.Value.ToString();
                    // 输出节点及参数
                    string str = $"节点ID:{item.NodeId},节点描述:{des},当前值:{currentValue}";
                    Console.WriteLine(str);
                }
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

三、两种模式区别演示
** 1、登录系统**

2、第一种批量读取模式

3、第二种批量读取模式

个人比较喜欢用第二中模式,维护简单,结合设备使用非常爽!!

后续将更新 匿名模式与证书模式的实例;

标签:Console,string,C#,IOT,Client,WriteLine,ns,TB,OEE
From: https://www.cnblogs.com/wangjuncheng/p/18023309

相关文章

  • SVN报错“Failed to run the WC DB work queue associated with”解决办法
    最近在checkSVN上的iOS代码时,失败报错:  FailedtoruntheWCDBworkqueueassociatedwith“目录/文件”,cleanup同样报错。最后在网上找到了解决方案并解决了问题,解决方法如下:一、安装sqlite31下载我是window1032位,下载以下文件:1.下载 sqlite-dll-win32-x86-......
  • err code 90030,err msg webhook over limit
    钉钉群机器人发文本消息提示"webhookoverlimit"从2024.02.01钉钉修改了自定义机器人推送政策,免费额度,由之前的不限制,改为:目前钉钉已经限制了免费版的发送次数每个企业每月5000条(之前创建的外部群不限制,但是已经不能新增机器人了。)。钉钉收费版是9800元每年,然后每月5w条(如果......
  • 李宏毅《机器学习》总结 - 2022 HW8(Anomaly Detection、ResNet) Strong Baseline
    重新学习了一下ResNet。。这作业平均一跑就是3、4个小时题目大意是让你做异常检测(anomalydetection),即给你一些正常的图片,再让你测试图片是正常的还是异常的(可以理解为2分类问题,只不过其中一个类别是无限大的)代码:https://www.kaggle.com/code/skyrainwind/hw8-anomaly-detec......
  • windows server 2019/2022安装WSUS更新服务器配置System.Runtime.InteropServices.COM
    现象: 2024-02-1814:41:10Postinstallstarted2024-02-1814:41:10Detectedroleservices:Api,UI,WidDatabase,Services2024-02-1814:41:10Start:LoadSettingsFromXml2024-02-1814:41:10Start:GetConfigValuewithfilename=UpdateServices-Services.xmlit......
  • 绕过disable_functions的限制
    https://github.com/AntSwordProject/AntSword-Labs/tree/master/bypass_disable_functionshttps://wiki.luoyunhao.com/web/Bypassdisable_function绕过disable_functions的限制disable_functions是php.ini中的一个设置选项,可以用来设置PHP环境禁止使用某些函数,通常是网站......
  • 【转载】linux利用crontab添加定时任务详解
    crontab作用:添加,查询,删除系统计划任务的指令。[root@localhost~]#crontab[选项]选项:   -e:   编辑crontab定时任务   -l:   查询crontab任务   -r:   删除当前用户所有的crontab任务1234512345[root@localhost~]#crontab-e......
  • echarts绘制世界地图的时候显示南海诸岛的局部放大图
    ehcarts在绘制中国地图的时候,判断geo的map为china的时候会自动加上南海诸岛的局部放大图。产品想在绘制世界地图的时候也加上南海诸岛,搞了半天没搞定,请教的同事搞定了。同事在此:简书ID:昊桐_260c。解决办法就是把南海诸岛的地图数据直接放在注册的JSON里面。{     ......
  • C语言循环队列详解
    前言相比于链队列,循环队列有着内存固定,效率高等特点,因而广泛应用于计算机的各个层面。本文主要介绍循环队列的概念和特点,列举一些循环队列的应用场景,以及给出用数组用C语言实现循环队列的代码。一、什么是循环队列?循环队列是一种特殊的线性表,特殊之处在于它只允许在表......
  • https://repo.radeon.com/rocm/apt/6.0.2 jammy/main amd64 下载太慢
    获取:1https://repo.radeon.com/rocm/apt/6.0.2jammy/mainamd64comgramd642.6.0.60002-115~22.04[51.7MB]获取:2https://repo.radeon.com/rocm/apt/6.0.2jammy/mainamd64composablekernel-devamd641.1.0.60002-115~22.04[109MB]获取:3https://repo.radeon.com/ro......
  • C#Sqlite插入/更新并返回数据
    关键词:returning返回所有列:returning*;返回指定列:returningcolumnname;删除命令不支持returning参考代码:stringpath=@"DataSource=D:\Data\data.sqlite;Version=3";using(SQLiteConnectionconn=newSQLiteConnection(path)){conn.Open();stringsql=......