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

本文只要记录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、第二种批量读取模式

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

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

posted @ 2024-02-20 16:08  王俊成  阅读(217)  评论(0编辑  收藏  举报