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、第二種批量讀取模式

個人比較喜歡用第二中模式,維護簡單,結合設備使用非常爽!!
后續將更新 匿名模式與證書模式的實例;

浙公網安備 33010602011771號