OPC UA客户端
将 Delphi、C++Builder 和 .NET 应用程序连接到运行 OPC UA 服务器的 PLC、SCADA 系统和网关:通过签名和加密的 opc.tcp 连接浏览地址空间、读写变量、调用方法,并订阅数据变化和事件。
新增
将 Delphi、C++Builder 和 .NET 应用程序连接到运行 OPC UA 服务器的 PLC、SCADA 系统和网关:通过签名和加密的 opc.tcp 连接浏览地址空间、读写变量、调用方法,并订阅数据变化和事件。
新增
一个组件即可完成 OPC UA 的整个客户端部分:安全通道、会话、服务和订阅。
TsgcOPCUAClient
基于 opc.tcp 的 OPC UA 二进制协议(UA TCP、UA Secure Conversation、UA Binary 编码)
Sign 和 SignAndEncrypt,支持 Basic256Sha256、Aes128_Sha256_RsaOaep 和 Aes256_Sha256_RsaPss;支持匿名、用户名和 X.509 身份
包含在 sgcWebSockets Enterprise(Delphi 和 .NET)和 All-Access 或独立的 sgcIndustrial 软件包中
设置 EndpointUrl,选择安全策略和模式并调用 Connect。每个服务调用都会阻塞,直到服务器应答,失败时会抛出带有 OPC UA 状态码的 EsgcOPCUAError。
uses
sgcIndustrial;
procedure TForm1.ConnectAndRead;
begin
FClient := TsgcOPCUAClient.Create(Self);
FClient.EndpointUrl := 'opc.tcp://192.168.1.60:4840';
FClient.SecurityPolicy := opcspNone;
FClient.SecurityMode := opcsmNone;
FClient.OnError := OnOPCUAError;
FClient.Connect;
// read the Value attribute of a variable
Memo1.Lines.Add('Temperature: ' + VarToStr(FClient.ReadValue('ns=2;s=Line1.Temperature')));
// the data type of the node is read first and used for the written value
FClient.WriteValue('ns=2;s=Line1.Setpoint', 75.5);
end;
procedure TForm1.OnOPCUAError(Sender: TObject; aStatusCode: Cardinal; const aError: string);
begin
Memo1.Lines.Add('Error: ' + aError);
end;
#include "sgcIndustrial.hpp"
void __fastcall TForm1::ConnectAndRead()
{
FClient = new TsgcOPCUAClient(this);
FClient->EndpointUrl = "opc.tcp://192.168.1.60:4840";
FClient->SecurityPolicy = opcspNone;
FClient->SecurityMode = opcsmNone;
FClient->OnError = OnOPCUAError;
FClient->Connect();
// read the Value attribute of a variable
Memo1->Lines->Add("Temperature: " + VarToStr(FClient->ReadValue("ns=2;s=Line1.Temperature")));
// the data type of the node is read first and used for the written value
FClient->WriteValue("ns=2;s=Line1.Setpoint", 75.5);
}
void __fastcall TForm1::OnOPCUAError(TObject *Sender, unsigned aStatusCode, const String aError)
{
Memo1->Lines->Add("Error: " + aError);
}
using System;
using esegece.sgcWebSockets;
class PlantReader
{
TsgcOPCUAClient FClient = new TsgcOPCUAClient();
public void ConnectAndRead()
{
FClient.EndpointUrl = "opc.tcp://192.168.1.60:4840";
FClient.SecurityPolicy = TsgcOPCUASecurityPolicy.opcspNone;
FClient.SecurityMode = TsgcOPCUAMessageSecurityMode.opcsmNone;
FClient.OnError += OnOPCUAError;
FClient.Connect();
// read the Value attribute of a variable
Console.WriteLine("Temperature: " + FClient.ReadValue("ns=2;s=Line1.Temperature"));
// the data type of the node is read first and used for the written value
FClient.WriteValue("ns=2;s=Line1.Setpoint", 75.5);
}
void OnOPCUAError(object Sender, uint aStatusCode, string aError)
{
Console.WriteLine("Error: " + aError);
}
}
安全通道、会话和发布循环均由组件处理,因此您的代码只需处理节点 ID 和数值。
GetEndpoints、Browse 和 BrowseNext(会自动跟随延续点)、TranslateBrowsePath、任意属性的 Read 和 Write,以及用于方法的 Call。ReadValue 和 WriteValue 接受节点 ID 字符串,并使用节点的数据类型。
Basic256Sha256、Aes128_Sha256_RsaOaep 和 Aes256_Sha256_RsaPss,以及为旧服务器保留的已弃用的 Basic128Rsa15 和 Basic256,支持 Sign 或 SignAndEncrypt 模式。客户端会检查服务器签名和 nonce,并拒绝在传输途中被更改的端点列表。
匿名、用户名和密码(在令牌策略要求时用服务器密钥加密),或使用您的私钥签名的 X.509 用户证书。
应用程序实例证书从 DER 或 PEM 文件加载,在首次连接时创建并保存到这些文件,未设置文件时则在内存中创建。除非 OnValidateServerCertificate 接受,否则服务器证书会被拒绝。
数据变化项支持采样间隔、队列大小、绝对或百分比死区和触发器;事件项支持 select 子句和可选的 where 子句。确认以及缺失通知的重新发布均由客户端完成。
WatchDog 在网络中断后重连,适用于 Delphi、C++Builder 和 .NET:它会重新激活同一会话并重新发布错过的内容,或在新会话中转移或重新创建订阅。OnReconnect 会告知采用了哪条路径。
创建订阅,添加要监视的项并处理 OnDataChange。数值无需轮询即可到达,按您选择的发布间隔推送。
procedure TForm1.Subscribe;
var
oSubscription: TsgcOPCUASubscription;
begin
FClient.OnDataChange := OnOPCUADataChange;
// publishing interval 500 ms
oSubscription := FClient.CreateSubscription(500);
// sampling 250 ms, queue of 1, report changes larger than 0.5 only
FClient.AddMonitoredItem(oSubscription, 'ns=2;s=Line1.Temperature', 250, 1, True,
uadbtAbsolute, 0.5);
FClient.AddMonitoredItem(oSubscription, 'ns=2;s=Line1.Pressure');
end;
procedure TForm1.OnOPCUADataChange(Sender: TObject; aSubscription: TsgcOPCUASubscription;
aMonitoredItem: TsgcOPCUAMonitoredItem; const aValue: TsgcOPCUADataValue);
begin
if (aValue.StatusCode and $80000000) = 0 then
Memo1.Lines.Add('Item ' + IntToStr(aMonitoredItem.ClientHandle) + ': ' +
VarToStr(aValue.Value.Value));
end;
void __fastcall TForm1::Subscribe()
{
FClient->OnDataChange = OnOPCUADataChange;
// publishing interval 500 ms
TsgcOPCUASubscription *oSubscription = FClient->CreateSubscription(500);
// sampling 250 ms, queue of 1, report changes larger than 0.5 only
FClient->AddMonitoredItem(oSubscription, "ns=2;s=Line1.Temperature", 250, 1, true,
uadbtAbsolute, 0.5);
FClient->AddMonitoredItem(oSubscription, "ns=2;s=Line1.Pressure");
}
void __fastcall TForm1::OnOPCUADataChange(TObject *Sender, TsgcOPCUASubscription *aSubscription,
TsgcOPCUAMonitoredItem *aMonitoredItem, const TsgcOPCUADataValue &aValue)
{
if ((aValue.StatusCode & 0x80000000) == 0)
Memo1->Lines->Add("Item " + IntToStr((int)aMonitoredItem->ClientHandle) + ": " +
VarToStr(aValue.Value.Value));
}
using System;
using esegece.sgcWebSockets;
class PlantMonitor
{
public void Subscribe(TsgcOPCUAClient aClient)
{
aClient.OnDataChange += OnOPCUADataChange;
// publishing interval 500 ms
TsgcOPCUASubscription oSubscription = aClient.CreateSubscription(500);
// sampling 250 ms, queue of 1, report changes larger than 0.5 only
aClient.AddMonitoredItem(oSubscription, "ns=2;s=Line1.Temperature", 250, 1, true,
TsgcOPCUADeadbandType.uadbtAbsolute, 0.5);
aClient.AddMonitoredItem(oSubscription, "ns=2;s=Line1.Pressure");
}
void OnOPCUADataChange(object Sender, TsgcOPCUASubscription aSubscription,
TsgcOPCUAMonitoredItem aMonitoredItem, TsgcOPCUADataValue aValue)
{
if ((aValue.StatusCode & 0x80000000) == 0)
Console.WriteLine("Item " + aMonitoredItem.ClientHandle + ": " + aValue.Value.Value);
}
}
选择策略和模式,为客户端提供其应用程序证书和用户身份,并决定信任哪些服务器证书。
uses
sgcIndustrial; // Delphi 7 to 2006: add sgcBase_Helpers (TBytes)
procedure TForm1.ConnectSecure;
begin
FClient := TsgcOPCUAClient.Create(Self);
FClient.EndpointUrl := 'opc.tcp://192.168.1.60:4840';
FClient.SecurityPolicy := opcspBasic256Sha256;
FClient.SecurityMode := opcsmSignAndEncrypt;
// application instance certificate: created and saved on the first run
FClient.Application.ApplicationName := 'Line1 Monitor';
FClient.Application.CertificateFile := 'C:\pki\own\client.der';
FClient.Application.PrivateKeyFile := 'C:\pki\own\client.pem';
// user identity
FClient.Authentication.AuthenticationType := opcatUserName;
FClient.Authentication.User := 'operator';
FClient.Authentication.Password := 'secret';
// decide which server certificates you trust
FClient.OnValidateServerCertificate := OnOPCUAValidateServerCertificate;
FClient.Connect;
end;
procedure TForm1.OnOPCUAValidateServerCertificate(Sender: TObject;
const aCertificate: TBytes; var aAccept: Boolean);
begin
// FTrusted holds the DER bytes of the server certificate you trust
aAccept := (Length(aCertificate) > 0) and (Length(aCertificate) = Length(FTrusted)) and
CompareMem(@aCertificate[0], @FTrusted[0], Length(aCertificate));
end;
void __fastcall TForm1::ConnectSecure()
{
FClient = new TsgcOPCUAClient(this);
FClient->EndpointUrl = "opc.tcp://192.168.1.60:4840";
FClient->SecurityPolicy = opcspBasic256Sha256;
FClient->SecurityMode = opcsmSignAndEncrypt;
// application instance certificate: created and saved on the first run
FClient->Application->ApplicationName = "Line1 Monitor";
FClient->Application->CertificateFile = "C:\\pki\\own\\client.der";
FClient->Application->PrivateKeyFile = "C:\\pki\\own\\client.pem";
// user identity
FClient->Authentication->AuthenticationType = opcatUserName;
FClient->Authentication->User = "operator";
FClient->Authentication->Password = "secret";
// decide which server certificates you trust
FClient->OnValidateServerCertificate = OnOPCUAValidateServerCertificate;
FClient->Connect();
}
void __fastcall TForm1::OnOPCUAValidateServerCertificate(TObject *Sender,
const TBytes aCertificate, bool &aAccept)
{
// FTrusted holds the DER bytes of the server certificate you trust
aAccept = (aCertificate.Length > 0) && (aCertificate.Length == FTrusted.Length) &&
CompareMem((void*)&aCertificate[0], &FTrusted[0], aCertificate.Length);
}
using System.IO;
using System.Linq;
using esegece.sgcWebSockets;
class SecureClient
{
TsgcOPCUAClient FClient = new TsgcOPCUAClient();
// DER bytes of the server certificate you trust
byte[] FTrusted = File.ReadAllBytes(@"C:\pki\trusted\server.der");
public void ConnectSecure()
{
FClient.EndpointUrl = "opc.tcp://192.168.1.60:4840";
FClient.SecurityPolicy = TsgcOPCUASecurityPolicy.opcspBasic256Sha256;
FClient.SecurityMode = TsgcOPCUAMessageSecurityMode.opcsmSignAndEncrypt;
// application instance certificate: created and saved on the first run
FClient.Application.ApplicationName = "Line1 Monitor";
FClient.Application.CertificateFile = @"C:\pki\own\client.der";
FClient.Application.PrivateKeyFile = @"C:\pki\own\client.pem";
// user identity
FClient.Authentication.AuthenticationType = TsgcOPCUAAuthenticationType.opcatUserName;
FClient.Authentication.User = "operator";
FClient.Authentication.Password = "secret";
// decide which server certificates you trust
FClient.OnValidateServerCertificate += OnOPCUAValidateServerCertificate;
FClient.Connect();
}
void OnOPCUAValidateServerCertificate(object Sender, byte[] aCertificate, ref bool aAccept)
{
aAccept = aCertificate != null && aCertificate.SequenceEqual(FTrusted);
}
}
任何带有 opc.tcp 端点的 OPC UA 服务器:内置服务器的 PLC 和控制器、SCADA 和 MES 系统以及协议网关。客户端已针对基于 asyncua(一个独立的开源 OPC UA 协议栈)构建的参考服务器,在其全部十一个端点上完成测试。
使用 Browse 从 Objects 文件夹 (i=85) 开始浏览,或使用 TranslateBrowsePath 解析诸如 /2:Line1/2:Speed 之类的路径。节点 ID 以文本形式书写:字符串 ID 如 ns=2;s=Line1.Speed,命名空间 0 中的数字 ID 如 i=2258。
默认值为 opcspNone,大多数服务器仅在测试时允许使用。请设置 SecurityPolicy 和 SecurityMode 以进行签名或加密。除非 OnValidateServerCertificate 接受,否则服务器证书会被拒绝,Basic128Rsa15 和 Basic256 已被弃用,仅为旧服务器保留。
会,请设置 WatchDog.Enabled:客户端会重新激活会话并重新发布错过的通知,或打开新会话并转移或重新创建订阅。它在 Delphi、C++Builder 和 .NET 中的工作方式相同,OnReconnect 会告知采用了哪条路径。
该组件是使用二进制编码、基于 opc.tcp 的 OPC UA 客户端。它不包含 OPC UA 服务器、HTTPS、JSON 和 WebSocket 传输、HistoryRead、PubSub、Reverse Connect 以及 Global Discovery Server。
它作为 sgcIndustrial 包的一部分,包含在 sgcWebSockets Enterprise(Delphi 和 .NET)和 All-Access 中。在 Delphi 中,请将 sgcIndustrial 添加到 uses 子句。
直达组件参考,打开演示项目并阅读技术文档。
| Online Help: TsgcOPCUAClient OPC UA 客户端的完整属性、方法和事件参考。 | 打开 | |
| Online Help: OPC UA Subscriptions 被监视项、死区、事件过滤器、保活和重连行为。 | 打开 | |
| 演示项目:Industrial\05.OPCUA_Client 适用于 Delphi、C++Builder 和 .NET 的 OPC UA 客户端。随 sgcWebSockets 软件包一同提供。 | 打开 | |
| 技术文档 (PDF) 仅针对此组件的功能、快速入门、代码示例和一手资料参考。 | 打开 | |
| 用户手册 (PDF) 涵盖库中每个组件的综合手册。 | 打开 |