Monitor Logging Progress with OPC Toolbox 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.

PREREQUISITES:

Step 1: Configure OPC Toolbox™ Objects

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

da = opcda('localhost', 'Matrikon.OPC.Simulation.1');
connect(da);
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;
disp(grp)
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, opccallbackopccallback, 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.

start(grp)
wait(grp)
OPC Start event occurred at local time 02:06:26
	Group 'CallbackTest': 0 records acquired.
OPC RecordsAcquired event occurred at local time 02:06:29
	Group 'CallbackTest': 5 records acquired.
OPC RecordsAcquired event occurred at local time 02:06:32
	Group 'CallbackTest': 10 records acquired.
OPC RecordsAcquired event occurred at local time 02:06:35
	Group 'CallbackTest': 15 records acquired.
OPC RecordsAcquired event occurred at local time 02:06:38
	Group 'CallbackTest': 20 records acquired.
OPC Stop event occurred at local time 02:06:38
	Group 'CallbackTest': 20 records acquired.

Step 5: Clean Up OPC Toolbox™ objects

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

disconnect(da)
delete(da)
Was this topic helpful?