logo-opclabs-new

Professional OPC Development Tools and Services

opc logo

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 Data Access or OPC Unified Architecture (OPC UA) server, or writing a data value can be achieved in just one or two lines of code!

QuickOPC is a radically new approach to access OPC data. Traditionally, OPC programming required complicated code, no matter whether you use OPC custom or automation interfaces. OPC Server objects must be instantiated, OPC Group objects must be created and manipulated, OPC Items must be added and managed properly, and subscriptions must be established and maintained. Too many lines of error-prone code must be written to achieve a simple goal – reading or writing a value. QuickOPC saves you all that hassle.

New: QuickOPC can now connect to OPC UA (Universal Architecture) servers.

QuickOPC Price List

Read More

OPC Delphi Code Samples

QuickOPC is unbelievably easy to use - here is an Object Pascal example which reads and displays OPC data from a common OPC Data Access server:

 

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;

Do you think it can be any simpler? But the simplicity is not the only advantage; the component is also very powerful. Using complex heuristic algorithms to optimize itself on the fly, it provides the most efficient, fastest access to the OPC data.

It is also easy to communicate with an OPC Unified Architecture (OPC-UA) server:

var
  Client: TEasyUAClient;
  Value: OleVariant;
begin
  // Instantiate the client object
  Client := TEasyUAClient.Create(nil);

  Value := Client.ReadValue(
    'http://opcua.demo-this.com:51211/UA/SampleServer',
    'nsu=http://test.org/UA/Data/;i=10853');
  WriteLn('value: ', Value);
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(
    'http://opcua.demo-this.com:51211/UA/SampleServer',
    'nsu=http://test.org/UA/Data/;i=10853',
    1000);

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