QuickStart
C# 中的 OPC UA,分步说明
引用 esegece.sgcIndustrial 包,连接后读取、浏览或订阅。本页的每个示例都可以针对 .NET 8 编译。
using System;
using esegece.sgcWebSockets;
var opcua = new TsgcOPCUAClient();
opcua.EndpointUrl = "opc.tcp://192.168.1.60:4840";
opcua.Connect();
try
{
Console.WriteLine("Speed: " + opcua.ReadValue("ns=2;s=Line1.Speed"));
opcua.WriteValue("ns=2;s=Line1.Mode", (ushort)2, TsgcOPCUABuiltInType.uabtUInt16);
}
catch (EsgcOPCUAError e)
{
Console.WriteLine($"Status 0x{e.StatusCode:X8}: {e.Message}");
}
finally
{
opcua.Disconnect();
}
using System;
using esegece.sgcWebSockets;
using static esegece.sgcWebSockets.sgcOPCUA_Types;
var opcua = new TsgcOPCUAClient();
opcua.EndpointUrl = "opc.tcp://192.168.1.60:4840";
opcua.Connect();
foreach (TsgcOPCUAReferenceDescription item in opcua.Browse("i=85"))
Console.WriteLine(item.DisplayName.Text + " = " + sgcOPCUANodeIdToString(item.NodeId.NodeId));
TsgcOPCUANodeId speed = opcua.TranslateBrowsePath("i=85", "/2:Line1/2:Speed");
Console.WriteLine(sgcOPCUANodeIdToString(speed));
TsgcOPCUAVariant[] outputs = opcua.Call("ns=2;s=Methods", "ns=2;s=Methods.Add", new[] {
sgcOPCUAVariantSimple(TsgcOPCUABuiltInType.uabtInt32, 2),
sgcOPCUAVariantSimple(TsgcOPCUABuiltInType.uabtInt32, 3) });
Console.WriteLine("Result: " + outputs[0].Value);
opcua.Disconnect();
using System;
using esegece.sgcWebSockets;
using static esegece.sgcWebSockets.sgcOPCUA_Encoding;
class LineMonitor
{
public void Start(TsgcOPCUAClient aClient)
{
aClient.OnDataChange += OnOPCUADataChange;
aClient.OnEvent += OnOPCUAEvent;
TsgcOPCUASubscription subscription = aClient.CreateSubscription(500);
aClient.AddMonitoredItem(subscription, "ns=2;s=Line1.Temperature", 250, 1, true,
TsgcOPCUADeadbandType.uadbtAbsolute, 0.5);
aClient.AddEventMonitoredItem(subscription, "i=2253", new[] { "Message", "Severity", "SourceName" });
}
void OnOPCUADataChange(object Sender, TsgcOPCUASubscription aSubscription,
TsgcOPCUAMonitoredItem aMonitoredItem, TsgcOPCUADataValue aValue)
{
Console.WriteLine("Temperature: " + aValue.Value.Value);
}
void OnOPCUAEvent(object Sender, TsgcOPCUASubscription aSubscription,
TsgcOPCUAMonitoredItem aMonitoredItem, TsgcOPCUAVariant[] aEventFields)
{
Console.WriteLine(sgcOPCUAVariantToValue(aEventFields[2]) + " (" +
sgcOPCUAVariantToValue(aEventFields[1]) + "): " + sgcOPCUAVariantToValue(aEventFields[0]));
}
}
using System.IO;
using System.Linq;
using esegece.sgcWebSockets;
class SecureLine
{
readonly byte[] FTrusted = File.ReadAllBytes(@"C:\pki\trusted\server.der");
public TsgcOPCUAClient Connect()
{
var opcua = new TsgcOPCUAClient();
opcua.EndpointUrl = "opc.tcp://192.168.1.60:4840";
opcua.SecurityPolicy = TsgcOPCUASecurityPolicy.opcspAes256_Sha256_RsaPss;
opcua.SecurityMode = TsgcOPCUAMessageSecurityMode.opcsmSignAndEncrypt;
opcua.Authentication.AuthenticationType = TsgcOPCUAAuthenticationType.opcatCertificate;
opcua.Authentication.CertificateFile = @"C:\pki\user\operator.der";
opcua.Authentication.PrivateKeyFile = @"C:\pki\user\operator.pem";
opcua.OnValidateServerCertificate += OnOPCUAValidateServerCertificate;
opcua.WatchDog.Enabled = true;
opcua.Connect();
return opcua;
}
void OnOPCUAValidateServerCertificate(object Sender, byte[] aCertificate, ref bool aAccept)
{
aAccept = aCertificate != null && aCertificate.SequenceEqual(FTrusted);
}
}