Main Content

Monitor Logging Progress with Callbacks

This example shows you how to use callbacks to monitor an OPC Data Access logging task.

Use callbacks to log or report events in a logging task, to update graphical user interfaces to show status of logging, or to graphically display logged data during the logging task.


Step 1: Configure OPC Objects

Create the client, connect, and create associated objects for a logging task.

da = opcda('localhost','Matrikon.OPC.Simulation.1');
grp = addgroup(da,'CallbackTest');
additem(grp,{'Random.Real8','Saw-toothed Waves.UInt2'});

Step 2: Configure the Logging Task Properties

Set the group to acquire 20 records at 0.5 second intervals.

grp.RecordsToAcquire = 20;
grp.UpdateRate = 0.5;
Summary of OPC Data Access Group Object: CallbackTest

   Object Parameters
      Group Type   : private
      Item         : 2-by-1 daitem object
      Parent       : localhost/Matrikon.OPC.Simulation.1
      Update Rate  : 0.5
      Deadband     : 0%

   Object Status
      Active       : on
      Subscription : on
      Logging      : off

   Logging Parameters
      Records      : 20
      Duration     : at least 10 seconds
      Logging to   : memory
      Status       : Waiting for START.
                     0 records available for GETDATA/PEEKDATA

Step 3: Configure the Callbacks

Use the default callback, opccallback, to display the start event (StartFcn property), the stop event (StopFcn property), and when each consecutive 5 records have been acquired (RecordsAcquiredFcn and RecordsAcquiredFcnCount properties).

grp.StartFcn = @opccallback;
grp.StopFcn = @opccallback;
grp.RecordsAcquiredFcn = @opccallback;
grp.RecordsAcquiredFcnCount = 5;

Step 4: Start the Logging Task

Start the logging task, and wait for it to complete.

OPC Start event occurred at local time 14:22:38
	Group 'CallbackTest': 0 records acquired.

OPC RecordsAcquired event occurred at local time 14:22:41
	Group 'CallbackTest': 5 records acquired.

OPC RecordsAcquired event occurred at local time 14:22:44
	Group 'CallbackTest': 10 records acquired.

OPC RecordsAcquired event occurred at local time 14:22:47
	Group 'CallbackTest': 15 records acquired.

Step 5: Clean Up OPC Objects

Disconnect the client from the server and remove OPC objects from memory when you no longer need them. Deleting the client object also deletes the group and item objects.