Main Content

View the OPC Event Log

This example shows you how to examine the OPC event log after a logging task.

PREREQUISITES:

Step 1: Configure OPC 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,'Triangle Waves.Real8');

Step 2: Configure and Execute a Logging Task

Configure the group to log only 10 records, then start the task and wait for it to complete.

grp.RecordsToAcquire = 10;
start(grp)
wait(grp)

Step 3: View the Event Log

Access the EventLog property of the client object.

events = da.EventLog
events = 

  1×2 struct array with fields:

    Type
    Data

The execution of the group logging task generated two events: start and stop. The value of the EventLog property is a 1-by-2 array of event structures.

List the events that are recorded in the EventLog property, by examining the contents of the Type field.

{events.Type}
ans =

  1×2 cell array

    {'Start'}    {'Stop'}

Access the Data field to get information about the stop event.

stopdata = events(2).Data
stopdata = 

  struct with fields:

     LocalEventTime: [2020 10 19 11 38 3.8710]
          GroupName: 'CallbackTest'
    RecordsAcquired: 10

Calculate the time between the stop event and the start event.

waitDuration = datetime(events(2).Data.LocalEventTime)...
               - datetime(events(1).Data.LocalEventTime);
waitSeconds = seconds(waitDuration)
waitSeconds =

    5.3740

Note: waitSeconds is not necessarily the time between the first and last sample in the logged data set. The LocalEventTime property is the time that MATLAB® processed the event received from the server; there can be some delay between the server sending the notification and MATLAB processing it. You should consult the TimeStamp property of the logged data for accurate time information related to the data.

Step 4: Clean Up

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.

disconnect(da)
delete(da)