终于找到一个MIT License的Opc Ua客户端
源代码在这个地方,自己去下载,如果需要
https://github.com/convertersystems/opc-ua-client
老规矩先装包:
dotnet add package Workstation.UaClient --version 3.2.0
实际上官方给到的示例已经足够好用了,我这里记录一下我的实验过程
代码是这些:
using Workstation.ServiceModel.Ua.Channels; using Workstation.ServiceModel.Ua; Console.WriteLine("Hello, World!"); Thread.Sleep(1000); // describe this client application. var clientDescription = new ApplicationDescription { ApplicationName = "Workstation.UaClient.FeatureTests", ApplicationUri = $"urn:{System.Net.Dns.GetHostName()}:Workstation.UaClient.FeatureTests", ApplicationType = ApplicationType.Client }; // create a 'ClientSessionChannel', a client-side channel that opens a 'session' with the server. var channel = new ClientSessionChannel( clientDescription, null, // no x509 certificates new AnonymousIdentity(), // no user identity "opc.tcp://localhost:4840", // the public endpoint of a server at opcua.rocks. SecurityPolicyUris.None); // no encryption try { // try opening a session and reading a few nodes. await channel.OpenAsync(); Console.WriteLine($"Opened session with endpoint '{channel.RemoteEndpoint.EndpointUrl}'."); Console.WriteLine($"SecurityPolicy: '{channel.RemoteEndpoint.SecurityPolicyUri}'."); Console.WriteLine($"SecurityMode: '{channel.RemoteEndpoint.SecurityMode}'."); Console.WriteLine($"UserIdentityToken: '{channel.UserIdentity}'."); // build a ReadRequest. See 'OPC UA Spec Part 4' paragraph 5.10.2 var readRequest = new ReadRequest { // set the NodesToRead to an array of ReadValueIds. NodesToRead = new[] { // construct a ReadValueId from a NodeId and AttributeId. new ReadValueId { // you can parse the nodeId from a string. // e.g. NodeId.Parse("ns=2;s=Demo.Static.Scalar.Double") NodeId = NodeId.Parse(VariableIds.Server_ServerStatus), // variable class nodes have a Value attribute. AttributeId = AttributeIds.Value } } }; // send the ReadRequest to the server. var readResult = await channel.ReadAsync(readRequest); // DataValue is a class containing value, timestamps and status code. // the 'Results' array returns DataValues, one for every ReadValueId. var serverStatus = readResult.Results[0].GetValueOrDefault<ServerStatusDataType>(); Console.WriteLine("\nServer status:"); Console.WriteLine(" ProductName: {0}", serverStatus.BuildInfo.ProductName); Console.WriteLine(" SoftwareVersion: {0}", serverStatus.BuildInfo.SoftwareVersion); Console.WriteLine(" ManufacturerName: {0}", serverStatus.BuildInfo.ManufacturerName); Console.WriteLine(" State: {0}", serverStatus.State); Console.WriteLine(" CurrentTime: {0}", serverStatus.CurrentTime); Console.WriteLine($"\nClosing session '{channel.SessionId}'."); await channel.CloseAsync(); } catch (Exception ex) { await channel.AbortAsync(); Console.WriteLine(ex.Message); } Console.ReadLine();
步骤是这样的:
1. 先启动一个Opc Ua 服务器
这边用的是Opc.UaFx.Advanced这个包做的简单服务器,千万不要学我,这玩意不能直接商用的,买授权折人民币6万左右。
2. 启动上面那段代码的客户端程序
呐,这里把上面那个Opc Ua服务器的信息打印出来了,说明可以用的,这个实验中客户端程序是MIT Licence的,放心商用,服务器端是要购买授权的。
由于我个人版权意识淡漠,用基金会的程序集做项目差点酿成大祸,大家千万不要学我,一定要白嫖MIT License的。
有Opc Ua,ModbusTcp,串口问题可以评论或者联系我。