This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Retrieve Event Information

Event Structures

Each event has a set of information associated with that event. The information is generated by the OPC server or the OPC Toolbox™ software, and stored in an event structure. This information includes the event type, the time the event occurred, and other event-specific information. For some events, the toolbox records event information in the opcda client object's EventLog property. You can also access the event structure associated with an event in a callback function.

For information about accessing event information in a callback function, see Create and Execute Callback Functions.

An event structure contains two fields: Type and Data. For example, this is an event structure for a start event.

Type: 'Start'
Data: [1x1 struct]

The Type field is a character vector that specifies the event type. For a start event, this field contains the value 'Start'.

The Data field is a structure that contains information about the event. The composition of this structure varies, depending on which type of event occurred. For details about the information associated with specific events, see the following sections:

Data Fields for Cancel Async, Data Change, Error, Read Async, and Write Async Events

For cancel async, data change, error, read async, and write async events, the Data structure contains these fields.

Field Name

Description

GroupName

The name of the group associated with the event.

LocalEventTime

Absolute time the event occurred, returned in MATLAB® date vector format:

[year month day hour minute seconds]

TransID

The transaction ID for the operation. In the case of a cancel async event, TransID contains the transaction ID that was cancelled.

Items

A structure array containing information about each item in the asynchronous operation. The cancel async event structure does not contain this field.

The Items structure array for read async events contains the following fields.

Field Name

Description

ItemID

The item ID for this record in the structure array.

Value

The data value.

Quality

The data quality as a character vector.

TimeStamp

The time the OPC server updated the value and quality. The time is returned in MATLAB date vector format:

[year month day hour minute seconds]

The Items structure array for write async events contains one field: ItemID.

The Items structure array for error events contains the ItemID field and an Error field, containing a character vector describing the error that occurred for that item.

Data Fields for Start, Stop, and Records Acquired Events

For start, stop, and records acquired events, the Data structure contains these fields.

Field Name

Description

GroupName

The name of the group associated with the event.

LocalEventTime

Absolute time the event occurred, returned in MATLAB date vector format:

[year month day hour minute seconds]

RecordsAcquired

The total number of records acquired in the current logging session.

Data Fields for Shutdown Events

For shutdown events, the Data structure contains these fields.

Field Name

Description

LocalEventTime

Absolute time the event occurred, returned in MATLAB date vector format:

[year month day hour minute seconds]

Reason

A character vector containing the reason the OPC server provided for shutting down.

Data Fields for Timer Events

For timer events, the Data structure contains these fields.

Field Name

Description

LocalEventTime

Absolute time the event occurred, returned in MATLAB date vector format:

[year month day hour minute seconds]

Access Data in the Event Log

While an opcda client object is connected, the toolbox stores event information in the opcda client object's EventLog property. The value of this property is an array of event structures. Each structure represents one event. For detailed information about the composition of an event structure for each type of event, see Event Structures.

The toolbox adds event structures to the EventLog array in the order in which the events occur. The first event structure reflects the first event recorded, the second event structure reflects the second event recorded, and so on.

Note

Data change events, records acquired events, and timer events are not included in the EventLog. Event structures for these events (and all the other events) are available to callback functions. For more information, see Create and Execute Callback Functions.

To illustrate the event log, this example creates an OPC Toolbox object hierarchy, executes a logging task, and then examines the object's EventLog property:

Step 1: Create the OPC Toolbox Object Hierarchy

This example creates a hierarchy of OPC Toolbox objects for the Matrikon™ Simulation Server. To run this example on your system, you must have the Matrikon Simulation Server installed. Alternatively, you can replace the values used in the creation of the objects with values for a server you can access.

da = opcda('localhost','Matrikon.OPC.Simulation.1');
connect(da);
grp = addgroup(da,'CallbackTest');
itm1 = additem(grp,'Triangle Waves.Real8');

Step 2: Start the Logging Task

Start the dagroup object. By default, the object acquires 120 records at 0.5-second intervals, and then stops. Wait for the object to stop logging data.

start(grp)
wait(grp)

Step 3: View the Event Log

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

events = da.EventLog
events = 

1x2 struct array with fields:
    Type
    Data

To list the events that are recorded in the EventLog property, examine the contents of the Type field.

{events.Type}
ans = 
    'Start'    'Stop'

To get information about a particular event, access the Data field in that event structure. The example retrieves information about the stop event.

stopdata = events(2).Data
stopdata = 
     LocalEventTime: [2004 3 2 21 33 45.8750]
          GroupName: 'CallbackTest'
    RecordsAcquired: 120

Step 4: Clean Up

Always remove toolbox objects from memory, and the variables that reference them, when you no longer need them. Deleting the opcda client object also deletes the group and item objects.

disconnect(da)
delete(da)
clear da grp itm1