深入理解.Net平台下的OPC UA通信Demo操作

需积分: 5 15 下载量 125 浏览量 更新于2024-11-09 收藏 87.75MB ZIP 举报
资源摘要信息:".Net OPC UA通信Demo 连接,断开,读写,订阅,监听心跳" 知识点概述: 本资源提供了一个.NET平台上的OPC UA(Open Platform Communications Unified Architecture)通信示例程序(Demo),用于演示如何实现与工业控制设备或服务器的连接、断开、读取数据、写入数据、数据订阅以及监听心跳等基本通信功能。OPC UA是一种跨平台的、开放的工业通信协议,广泛用于工业自动化领域,实现设备之间的数据交换、信息集成和通信。 详细知识点: 1. OPC UA简介: OPC UA是OPC基金会发布的下一代通信协议,旨在解决原OPC Classic协议中存在的一些问题,如安全性、可扩展性和跨平台支持等。它提供了一套完整的服务集,包括节点管理、命名空间管理、浏览、读写数据、事件处理、加密和认证等。 2. .NET平台下的OPC UA实现: .NET环境下的OPC UA实现通常是通过使用OPC UA的SDK或者是第三方库来完成的。开发者需要在项目中引入这些库,并通过调用相应的API来实现通信协议的各种操作。 3. 连接操作: 连接操作是OPC UA通信的基础,它涉及到了客户端向服务器发起连接请求的过程。在.NET的OPC UA Demo中,会演示如何通过编程方式创建会话(Session),并建立连接。通常会涉及到服务器的URL地址、端口号、安全策略以及认证凭据等参数。 4. 断开操作: 断开操作是指在完成数据交换或是通信不再需要时,客户端主动关闭与服务器的会话连接。这一步骤需要确保所有的资源被正确释放,以及连接不会被意外中断,导致数据丢失或连接异常。 5. 读写操作: 读写操作允许客户端对服务器上的数据节点进行数据的读取和写入。在.NET的OPC UA Demo中,将展示如何通过指定的数据点进行数据访问,包括读取实时值、历史数据、状态信息等,并且能够对特定的数据节点进行写入操作,如更新设备参数或控制命令等。 6. 订阅和监听心跳: 订阅操作通常用于实时数据的监控,客户端可以订阅感兴趣的节点,一旦这些节点的数据发生变化,服务器会主动将更新通知给客户端。监听心跳则是为了保持会话的活跃状态,通过定期检查服务器的心跳响应来确保连接的有效性,这对于长期运行的通信尤为重要。 7. PLC_TEST文件: "PLC_TEST"可能是一个包含测试代码的压缩包文件,用于演示OPC UA通信Demo的具体实现。该文件中可能包含了在.NET环境中测试与PLC(可编程逻辑控制器)通信的代码样本和相关测试数据。 在.NET环境下开发OPC UA通信Demo时,开发者需要具备以下几个方面的知识: ***编程语言(如C#)的知识。 - OPC UA协议规范的理解,特别是会话管理、数据模型、安全性等方面。 - 相关OPC UA SDK或库的使用方法。 - 异常处理和资源管理的编程技巧,以确保通信的稳定性和数据的安全性。 总结,本资源旨在为.NET开发者提供一套完整的OPC UA通信操作示例,以便快速理解和掌握.NET平台下的OPC UA通信机制,进而在工业自动化和设备集成项目中应用OPC UA协议进行高效、稳定的数据交换。
1114 浏览量
Overview This OPC UA reference implementation is targeting the .NET Standard Library. .Net Standard allows developing apps that run on all common platforms available today, including Linux, iOS, Android (via Xamarin) and Windows 7/8/8.1/10 (including embedded/IoT editions) without requiring platform-specific modifications. Furthermore, cloud applications and services (such as ASP.Net, DNX, Azure Websites, Azure Webjobs, Azure Nano Server and Azure Service Fabric) are also supported. Features included 1. Fully ported Core UA stack and SDK (Client, Server, Configuration & Sample assemblies) 2. Sample Servers and Clients, including all required controls, for .Net 4.6, .NetCore and UWP. 3. X.509 certificate support for client and server authentication 4. Anonymous, username, X.509 certificate (experimental) and JWT (experimental) user authentication 5. UA-TCP & HTTPS transports (client and server) 6. Folder certificate-store support 7. Sessions (including UI support in the samples) 8. Subscriptions (including UI support in the samples) Getting Started All the tools you need for .Net Standard come with the .Net Core tools. See here for what you need. How to create self signed certificates for the sample applications On Windows 1. Open a command prompt in the root folder of your repository 2. Run the script CreateAllCerts.cmd in the root folder of your repository to create the certificates for all sample applications. 3. Alternatively, you can run the script CreateCert.cmd in each sample project folder to create new self signed certificates for the application. 4. The self signed certificates are stored in OPC Foundation/CertificateStores/MachineDefault in each application project folder On Linux 1. Open a command prompt 2. Navigate to the project folder of the sample app, e.g. SampleApplications/Samples/NetCoreConsoleClient 3. Run the script ./createcert.sh to create the certificates for the sample applications. 4. The self signed certificates are stored in OPC Foundation/CertificateStores/MachineDefault in each application project folder How to build and run the samples in Visual Studio on Windows 1. Create certificates for all sample applications. 2. Open the solution UA-NetStandard.sln with VisualStudio. 3. Choose a project in the Solution Explorer and set it with a right click as Startup Project. 4. Hit F5 to build and execute the sample. How to build and run the console samples on Windows, Linux and iOS This section describes how to run the NetCoreConsoleClient, NetCoreConsolePublisher and NetCoreConsoleServer sample applications. Please follow instructions in this article to setup the dotnet command line environment for your platform. Prerequisites 1. Once the dotnet command is available, navigate to the root folder in your local copy of the repository and execute dotnet restore. This command calls into NuGet to restore the tree of dependencies. Start the server 1. Open a command prompt 2. Now navigate to the folder SampleApplications/Samples/NetCoreConsoleServer. 3. Run the script ./createcert.sh on Linux or CreateCert.cmd on Windows to create the self signed certificate for the command line application. 4. To run the server sample type dotnet run. The server is now running and waiting for connections. In this sample configuration the server always accepts new client certificates. Start the client 1. Open a command prompt 2. Now navigate to the folder SampleApplications/Samples/NetCoreConsoleClient. 3. Run the script ./createcert.sh on Linux or CreateCert.cmd on Windows to create the self signed certificate for the command line application. 4. To execute the sample type dotnet run to connect to the OPC UA console sample server running on the same host. To connect to another OPC UA server specify the server as first argument and type e.g. dotnet run opc.tcp://myserver:51210/UA/SampleServer. How to build and run the OPC UA Web Telemetry sample • Go to the Azure portal and create a new Storage account. • Open the solution OpcUaWebTelemetry.sln with VisualStudio 2015. • Open the MessageProcessing\Configuration.cs file to configure the app to use your Azure resources (Storage account and IoTHub). // {StorageAccountName} is the name of the storage account and could be found // under Settings->Access keys->Storage account name of your storage account on the Azure portal. // {AccessKey} is the access key of the storage account and could be found // under Settings->Access keys->key1 of your storage account on the Azure portal. public static string StorageConnectionString = "DefaultEndpointsProtocol=https;AccountName={StorageAccountName};AccountKey={AccessKey}"; // {ConsumerGroupName} is the name of a aonsumer group of your IoTHub. The IoTHub you use is the // one you have created for use with the OPC UA Publisher sample. // You need to create this consumer group via the messaging settings of your IoTHub in the Azure portal. // We recommend that you do not share this Consumer group with other consumers, nor that you use the $Default consumer group. public static string EventHubConsumerGroup = "{ConsumerGroupName}"; // {EventHubEndpoint} is the Event Hub compatible endpoint of your IoTHub and could be found // under Settings->Messaging->Event Hub-compatible endpoint of your IoTHub in the Azure portal. // {PrimaryKey} is the IoT Hub primary key for access with iothubowner policy and could be found // under Settings->Shared access policies->iothubowner->Primary key of your IoTHub in the Azure portal. public static string EventHubConnectionString = "Endpoint={EventHubEndpoint};SharedAccessKeyName=iothubowner;{PrimaryKey}"; // {HubName} is the Event Hub compatible name of your IoTHub and could be found // under Settings->Messaging->Event Hub-compatible name of your IoTHub in the Azure portal. public static string EventHubName = "{HubName}"; • Save the file, rebuild the solution and start it. This will start a local instance of the application. • The solution can also be deployed into a Azure App service. Please use VisualStudio 2015's Azure publishing functionality for this purpose. • Now run the OPC UA Publisher sample, connect to a OPC UA server and publish a node. • You should see the node value on the web page after a few seconds. License This repository includes the UA .NetStandard Stack, sample libraries, and sample applications. The UA .NetStandard Stack follows a dual-license: • OPC Foundation Corporate Members: RCL • Everybody else: GPL 2.0 • RCL enables OPC Foundation members to deploy their applications using the UA .NetStandard stack without being required to disclose the application code. Non-members must disclose their application code when using the UA .NetStandard Stack. • Note: Dual license applies to this repository only; GPL 2.0 applies to all derived repositories (for example 'forks'). For details check the License section below. • All samples are provided under the MIT license. Contributing We strongly encourage community participation and contribution to this project. First, please fork the repository and commit your changes there. Once happy with your changes you can generate a 'pull request'. You must agree to the contributor license agreement before we can accept your changes. The CLA and "I AGREE" button is automatically displayed when you perform the pull request. You can preview CLA here.