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:
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'});
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
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;
Start the logging task, and wait for it to complete.
start(grp) wait(grp)
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.
Disconnect the client from the server and remove OPC Toolbox™ objects from memory when you no longer need them. Deleting the client object also deletes the group and item objects.
disconnect(da) delete(da)