帮我用c#写一个西门子opcua程序
时间: 2023-06-27 17:02:14 浏览: 65
好的,以下是一个简单的示例代码,用于连接到西门子OPC UA服务器并读取节点的值:
```csharp
using Opc.Ua;
using Opc.Ua.Client;
public class SiemensOpcuaClient
{
private static Session session;
public static void Main()
{
// Create endpoint URL and application configuration
var endpointUrl = "opc.tcp://your-siemens-opcua-server-url";
var config = new ApplicationConfiguration
{
ApplicationName = "SiemensOpcuaClient",
ApplicationType = ApplicationType.Client,
SecurityConfiguration = new SecurityConfiguration
{
ApplicationCertificate = new CertificateIdentifier
{
StoreType = @"Windows",
StorePath = @"CurrentUser\My",
SubjectName = "SiemensOpcuaClient"
},
TrustedPeerCertificates = new CertificateTrustList
{
StoreType = @"Windows",
StorePath = @"CurrentUser\TrustedPeople",
}
}
};
// Connect to server
var endpoint = new ConfiguredEndpoint(null, new EndpointDescription(endpointUrl));
var factory = new ChannelFactory(config, new ConfiguredEndpointCollection { endpoint });
session = Session.Create(config, factory, endpointUrl, null, null, null);
// Read node value
var nodeId = new NodeId("your-node-id");
var value = session.ReadValue(nodeId);
Console.WriteLine("Value: " + value.Value);
}
}
```
请注意,您需要安装 `Opc.Ua.Core` NuGet 包,并根据您的OPC UA服务器的URL和节点ID更新代码中的值。此外,您还需要在您的计算机上安装西门子OPC UA服务器软件,并相应地配置安全证书。