QuickOPC User's Guide and Reference
SubscribeItem Method (_EasyDAClient)
Example 



View with Navigation Tools
OpcLabs.EasyOpcClassic Assembly > OpcLabs.EasyOpc.DataAccess.ComTypes Namespace > _EasyDAClient Interface : SubscribeItem Method
Name of the machine (empty string for local computer).
Contains ProgID of the OPC server.
ID of the item that will be subscribed to.
How often should the updates be received (number of milliseconds)
Subscribe to OPC item. Specify machine name, server class, item ID, and requested update rate. No state object is specified (this overload is suitable for simple scenarios only).
Syntax
'Declaration
 
Function SubscribeItem( _
   ByVal machineName As String, _
   ByVal serverClass As String, _
   ByVal itemId As String, _
   ByVal requestedUpdateRate As Integer _
) As Integer
 
'Usage
 
Dim instance As _EasyDAClient
Dim machineName As String
Dim serverClass As String
Dim itemId As String
Dim requestedUpdateRate As Integer
Dim value As Integer
 
value = instance.SubscribeItem(machineName, serverClass, itemId, requestedUpdateRate)

Parameters

machineName
Name of the machine (empty string for local computer).
serverClass
Contains ProgID of the OPC server.
itemId
ID of the item that will be subscribed to.
requestedUpdateRate
How often should the updates be received (number of milliseconds)

Return Value

The method returns an integer handle that uniquely identifies the item subscription.
Example

COM

// This example shows how to subscribe to changes of a single item and display the value of the item with each change.

type
  TSubscribeItem_ClientEventHandlers = class
    // Item changed event handler
    procedure OnItemChanged(
      ASender: TObject;
      sender: OleVariant;
      const eventArgs: _EasyDAItemChangedEventArgs);
  end;

procedure TSubscribeItem_ClientEventHandlers.OnItemChanged(
  ASender: TObject;
  sender: OleVariant;
  const eventArgs: _EasyDAItemChangedEventArgs);
begin
  if eventArgs.Succeeded then
    WriteLn(eventArgs.Vtq.ToString)
  else
    WriteLn(Format('*** Failure: %s', [eventArgs.ErrorMessageBrief]));
end;

class procedure SubscribeItem.Main;
var
  Client: TEasyDAClient;
  ClientEventHandlers: TSubscribeItem_ClientEventHandlers;
begin
  // Instantiate the client object and hook events
  Client := TEasyDAClient.Create(nil);
  ClientEventHandlers := TSubscribeItem_ClientEventHandlers.Create;
  Client.OnItemChanged := ClientEventHandlers.OnItemChanged;

  Client.SubscribeItem('', 'OPCLabs.KitServer.2', 'Simulation.Random', 1000);

  WriteLn('Processing item changed events for 1 minute...');
  PumpSleep(60*1000);

  WriteLn('Unsubscribing...');
  Client.UnsubscribeAllItems;

  WriteLn('Waiting for 5 seconds...');
  PumpSleep(5*1000);

  WriteLn('Finished.');
  FreeAndNil(Client);
  FreeAndNil(ClientEventHandlers);
end;
Requirements

Target Platforms: .NET Framework: Windows 10 (selected versions), Windows 11 (selected versions), Windows Server 2016, Windows Server 2022; .NET: Linux, macOS, Microsoft Windows

See Also