C# OPC UA如何实现数据通信

   2024-09-30 3700
核心提示:OPC Unified Architecture (UA) 是一种用于工业自动化的开放标准,可以实现设备之间的数据通信。在 C# 中,你可以使用 OPC UA 客

OPC Unified Architecture (UA) 是一种用于工业自动化的开放标准,可以实现设备之间的数据通信。在 C# 中,你可以使用 OPC UA 客户端和服务器库来实现数据通信。以下是一个简单的示例,展示了如何使用 OPC UA 进行数据通信:

首先,你需要安装 OPC UA 客户端和服务器库。你可以使用 OPC Foundation 提供的官方库,或者使用第三方库,如 OPC UA .NET Standard。

创建一个 OPC UA 服务器,用于接收和发送数据。以下是一个简单的 OPC UA 服务器示例:

using Opc.Ua;using Opc.Ua.Server;using System;using System.Threading;namespace OpcUaServer{    class Program    {        static void Main(string[] args)        {            // 创建一个 ApplicationInstance            ApplicationInstance application = new ApplicationInstance();            // 配置 ApplicationInstance            application.ApplicationName = "My OPC UA Server";            application.ApplicationType = ApplicationType.Server;            application.ConfigSectionName = "Opc.Ua.MyServer";            // 加载应用程序配置            application.LoadApplicationConfiguration("MyServer.config", false).Wait();            // 检查应用程序证书            application.CheckApplicationInstanceCertificate(false, 0).Wait();            // 创建一个 StandardServer            StandardServer server = new StandardServer();            // 初始化 StandardServer            server.Initialize(application);            // 启动 StandardServer            server.Start();            Console.WriteLine("Server started. Press any key to stop.");            Console.ReadKey();            // 停止 StandardServer            server.Stop();        }    }}
创建一个 OPC UA 客户端,用于连接到服务器并读取/写入数据。以下是一个简单的 OPC UA 客户端示例:
using Opc.Ua;using Opc.Ua.Client;using System;namespace OpcUaClient{    class Program    {        static void Main(string[] args)        {            // 创建一个 ApplicationInstance            ApplicationInstance application = new ApplicationInstance();            // 配置 ApplicationInstance            application.ApplicationName = "My OPC UA Client";            application.ApplicationType = ApplicationType.Client;            application.ConfigSectionName = "Opc.Ua.MyClient";            // 加载应用程序配置            application.LoadApplicationConfiguration("MyClient.config", false).Wait();            // 检查应用程序证书            application.CheckApplicationInstanceCertificate(false, 0).Wait();            // 创建一个 Session            Session session = null;            try            {                // 创建一个 EndpointDescription                EndpointDescription endpoint = new EndpointDescription();                endpoint.EndpointUrl = "opc.tcp://localhost:4840";                // 创建一个 Session                session = Session.Create(application.ApplicationConfiguration, endpoint, true, "MySession", 60000, new UserIdentity(), null);                // 读取节点值                NodeId nodeId = new NodeId("ns=2;s=MyNode");                DataValue dataValue = session.ReadValue(nodeId);                Console.WriteLine($"Node value: {dataValue.Value}");                // 写入节点值                session.WriteValue(new WriteValue() { NodeId = nodeId, Value = new DataValue(123) });            }            catch (Exception ex)            {                Console.WriteLine($"Error: {ex.Message}");            }            finally            {                // 关闭 Session                if (session != null)                {                    session.Close();                }            }        }    }}

这个示例展示了如何使用 C# 和 OPC UA 库实现数据通信。你可以根据自己的需求修改代码,以满足不同的场景和需求。

 
举报打赏
 
更多>同类物流大全
推荐图文
推荐物流大全
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号