Professional Communication
Software Development Tools

OPC Foundation member and certified logos

lupa

How to develop OPC clients in Delphi

Are you having difficulties incorporating the OPC data into your Delphi (Object Pascal) solution? Need to create an OPC Delphi program quickly and in quality? If so, QuickOPC comes to the rescue.

QuickOPC is a set of components that simplify the task of integrating OPC client functionality into custom applications. Reading a value from OPC Unified Architecture (OPC UA)  or OPC Data Access or server, or writing a data value can be achieved in just one or two lines of code. QuickOPC also supports OPC A&E, OPC XML-DA, and can subscribe to OPC UA PubSub data.

Read More

Useful links: Documentation / Delphi Examples / Delphi in Knowledge Base / Delphi examples GitHub repository

 

OPC Delphi Code Samples

QuickOPC is easy to use - here is an Object Pascal example which reads and displays OPC data from an OPC Unified Architecture (OPC UA) server:
var
  Client: TEasyUAClient;
  Value: OleVariant;
begin
  // Instantiate the client object
  Client := TEasyUAClient.Create(nil);

  Value := Client.ReadValue(
    'opc.tcp://opcua.demo-this.com:51210/UA/SampleServer',
    'nsu=http://test.org/UA/Data/;i=10853');
  WriteLn('value: ', Value);
end;

It is also easy to communicate with OPC Data Access servers:

var
  EasyDAClient: IEasyDAClient;
begin
  { Create EasyOPC-DA component }
  EasyDAClient := CoEasyDAClient.Create;

  { Read item value and display it }
  Edit1.Text := EasyDAClient.ReadItemValue('', 'OPCLabs.KitServer.2',
    'Demo.Single', EmptyParam, EmptyParam);
end;

Application-side subscriptions are possible, too. The Delphi example below shows how subscribe to changes of a single item and display the value of the item with each change.

type
  TClientEventHandlers = class
    procedure OnDataChangeNotification(
      ASender: TObject;
      sender: OleVariant;
      const eventArgs: _EasyUADataChangeNotificationEventArgs);
  end;

procedure TClientEventHandlers.OnDataChangeNotification(
  ASender: TObject;
  sender: OleVariant;
  const eventArgs: _EasyUADataChangeNotificationEventArgs);
begin
  // Display the data
  // Remark: Production code would check eventArgs.Exception before accessing
  // eventArgs.AttributeData.
	WriteLn(eventArgs.Arguments.NodeDescriptor.ToString, ': ',
    eventArgs.AttributeData.ToString);
end;

class procedure SubscribeDataChange.Main;
var
  Client: TEasyUAClient;
  ClientEventHandlers: TClientEventHandlers;
begin
  // Instantiate the client object and hook events
  Client := TEasyUAClient.Create(nil);
  ClientEventHandlers := TClientEventHandlers.Create;
  Client.OnDataChangeNotification := ClientEventHandlers.OnDataChangeNotification;

  WriteLn('Subscribing...');
  Client.SubscribeDataChange(
    'opc.tcp://opcua.demo-this.com:51210/UA/SampleServer',
    'nsu=http://test.org/UA/Data/;i=10853',
    1000);

  WriteLn('Processing data change events for 1 minute...');
  PumpSleep(60*1000);
end;

The PubSub variety of OPC UA (as opposed to client-server) uses message-oriented middleware to deliver the data. QuickOPC supports it as well. Code examples are available in the documentation, and installed with the product. They are also available in the GitHub repository.