OPC UA クライアント

Delphi、C++Builder、.NET アプリケーションを、OPC UA サーバーを実行する PLC、SCADA システム、ゲートウェイに接続します。署名と暗号化に対応した opc.tcp 接続で、アドレス空間の参照、変数の読み書き、メソッド呼び出し、データ変更とイベントのサブスクライブを行います。

新機能

TsgcOPCUAClient

OPC UA のクライアント側すべてを 1 つのコンポーネントで提供します。セキュアチャネル、セッション、サービス、サブスクリプションに対応しています。

コンポーネントクラス

TsgcOPCUAClient

プロトコル

OPC UA binary over opc.tcp (UA TCP、UA Secure Conversation、UA Binary encoding)

セキュリティ

Basic256Sha256、Aes128_Sha256_RsaOaep、Aes256_Sha256_RsaPss による Sign と SignAndEncrypt。Anonymous、ユーザー名、X.509 の ID に対応

エディション

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 モードで利用できます。クライアントはサーバーの署名とノンスを確認し、途中で改ざんされたエンドポイントリストを拒否します。

ユーザー ID

Anonymous、ユーザー名とパスワード (トークンポリシーが要求する場合はサーバーの鍵で暗号化)、または秘密鍵で署名した 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);
    }
}

署名と暗号化された接続

ポリシーとモードを選択し、クライアントにアプリケーション証明書とユーザー ID を指定して、信頼するサーバー証明書を決定します。

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 UA クライアントに関するよくある質問

opc.tcp エンドポイントを持つ任意の OPC UA サーバーに接続できます。組み込みサーバーを持つ PLC やコントローラー、SCADA や MES システム、プロトコルゲートウェイなどです。クライアントは、独立したオープンソースの OPC UA スタックである asyncua で構築したリファレンスサーバーを相手に、11 個のエンドポイントすべてでテストされています。

Browse で Objects フォルダー (i=85) から参照するか、/2:Line1/2:Speed のようなパスを TranslateBrowsePath で解決します。ノード 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 では uses 句に sgcIndustrial を追加します。

ドキュメント・デモ

コンポーネントリファレンスへ直接移動し、デモプロジェクトを開いて、技術ドキュメントを読めます。

Online Help: TsgcOPCUAClient OPC UA クライアントのプロパティ、メソッド、イベントの完全なリファレンスです。
Online Help: OPC UA Subscriptions 監視アイテム、デッドバンド、イベントフィルター、キープアライブ、再接続の動作。
デモプロジェクト: Industrial\05.OPCUA_Client Delphi、C++Builder、.NET 向けの OPC UA クライアントです。sgcWebSockets パッケージに同梱されています。
技術ドキュメント (PDF) このコンポーネントだけの機能、クイックスタート、コードサンプル、一次情報へのリファレンスです。
ユーザーマニュアル (PDF) ライブラリのすべてのコンポーネントを網羅した総合マニュアルです。
最もお得な選択: All-AccesseSeGeCe の全製品にプレミアムサポートが付いて、年間 €1,059 からご利用いただけます。
All-Access の価格を見る

sgcWebSockets Enterprise に含まれています

sgcWebSockets Enterprise (Delphi と .NET) および All-Access、または単体の sgcIndustrial パックに含まれています。