[Company Logo Image]

Home AR4 Palletiser OPC Programming Wintek32 Delphi Downloads

Very Simple OPC Client


Version 1.14 of the prOpcKit includes a unit which implements a very easy to use OPC client, which I call the 'Very Simple Client'. This page describes its use.

The very simple client is appropriate in the common situation when you know the name of the server to which you want to connect, and you know the names of the tags that you wish to access. This is typical of many practical applications. Generic clients, which can browse for servers and tags are great as tools, but are not particularly useful as control applications.

We need to add the unit prOpcVSC to the 'uses' clause of unit in which we intend to use the client.

unit MainUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, prOpcVSC;

It is also necessary to declare a variable to in which to store the client. This can be of type Variant or IOpcClient. We will use a Variant which makes the client a bit easier to use.

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
    VSC: Variant; { Very Simple Client }
  end;

The client must be initialised before it can be used. To do this, use the function 'OpcClient' exported by prOpcVSC.

procedure TForm1.FormCreate(Sender: TObject);
begin
  VSC:= OpcClient('', 'Wizzy1.TWizzyServer.1')
end;

The first parameter is the name of the computer on which the server runs; we can pass a null string to indicate the local machine. The second parameter is the 'ProgId' of the server. There is a third, optional, parameter which indicates the rate at which you want the data polled on the server, and hence the maximum rate at which updates will be sent to the client. You can omit this parameter if you don't want updates sent from the server; which is fine if you don't mind making a round trip to the server every time you want to read a tag.

The 'Wizzy1.TWizzyServer.1' exports two tags - FormWidth and FormHeight - representing the size of the server's main form. For the source code to this server see 'An Introduction to the Opc Server Wizard'.

We will use our client application to adjust the size of the server's main form, using four buttons, arranged like this.

To increase the width of the server form by 20 pixels (demonstrating both a syncnronous read and a synchronous write) we can write.

procedure TForm1.WidthGrowButtonClick(Sender: TObject);
begin
  VSC.FormWidth:= VSC.FormWidth + 20
end;

When you are using a Variant to store your client connection, you can read or write to any tag on the server by using the notation ClientConnection.TagName, where ClientConnection is the initialised variant and TagName is the actual name of the tag on the server. This only works if the TagNames are valid pascal identifiers; if this is not the case then you should declare your ClientConnection as IOpcClient and access the tags using the properties of that interface.

'On click' handlers for the remaining buttons are similar. The finished unit is as follows.

unit MainUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, prOpcVSC;

type
  TForm1 = class(TForm)
    WidthShrinkButton: TSpeedButton;
    WidthGrowButton: TSpeedButton;
    HeightShrinkButton: TSpeedButton;
    HeightGrowButton: TSpeedButton;
    procedure FormCreate(Sender: TObject);
    procedure WidthShrinkButtonClick(Sender: TObject);
    procedure HeightGrowButtonClick(Sender: TObject);
    procedure HeightShrinkButtonClick(Sender: TObject);
    procedure WidthGrowButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    VSC: Variant;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  VSC:= OpcClient('', 'Wizzy1.TWizzyServer.1')
end;

procedure TForm1.WidthGrowButtonClick(Sender: TObject);
begin
  VSC.FormWidth:= VSC.FormWidth + 20
end;

procedure TForm1.WidthShrinkButtonClick(Sender: TObject);
begin
  VSC.FormWidth:= VSC.FormWidth - 20
end;

procedure TForm1.HeightGrowButtonClick(Sender: TObject);
begin
  VSC.FormHeight:= VSC.FormHeight + 20
end;

procedure TForm1.HeightShrinkButtonClick(Sender: TObject);
begin
  VSC.FormHeight:= VSC.FormHeight - 20
end;

end.

If you want the source code to this example, it is included in the prOpcKit evaluation download.


Return to prOpcKit

Return to PREL Home