• No se han encontrado resultados

OPC Toolbox software does not log data directly to the MATLAB workspace. When logging to memory, the data is buffered in the toolbox engine in a storage-efficient way. When logging to disk, the data is logged in ASCII format. To analyze your data, you need to extract the data from the toolbox engine or from a disk file into MATLAB for processing. This section describes how to get your logged data into the MATLAB workspace. The following sections describe this process:

• “Retrieve Data from Memory” on page 7-23, discusses how to retrieve data from the toolbox engine into MATLAB.

• “Retrieve Data from Disk” on page 7-25, discusses how to retrieve data from a disk file into MATLAB.

Whether you log data to memory or to disk, you can retrieve that logged data in one of two formats:

• Structure format: This format stores each data change event in a structure. Data from a logging task is simply an array of such structures.

• Array format: To visualize and analyze your data, you will need to work with the time series of each of the items in the group. The array format is the logged structure data,

“unpacked” into separate arrays for the Value, Quality, and TimeStamp.

Retrieve Data from Memory

You retrieve data from memory using the getdata function, passing the dagroup object as the first argument, and the number of records you want to retrieve as the second argument. The data is returned as a structure containing data from each data change event in the logging task. For example, to retrieve 20 records for the dagroup object grp:

7

Reading, Writing, and Logging OPC Data

s = getdata(grp, 20);

If you do not supply a second argument, getdata will try to retrieve the number of records specified by the RecordsToAcquire property of the dagroup object. If the OPC Toolbox software engine contains fewer records for the group than the number requested, a warning is generated and all of the available records will be retrieved.

To retrieve data in array format, you must indicate the data type of the returned values.

You pass a string defining that data type as an additional argument to the getdata function. Valid data types are any MATLAB numeric data type (for example, 'double' or 'uint32') plus 'cell' to denote the MATLAB cell array data type.

When you specify a numeric data type or cell array as the data type for getdata, the logged data is returned in separate arrays for the item IDs logged, the value, quality, time stamp, and the local event time of each data change event logged. You must therefore specify up to five output arguments for the getdata function when retrieving data in array format.

For example, to retrieve 20 records of logged data in double array format from dagroup object grp.

[itmID,val,qual,tStamp,evtTime] = getdata(grp,20,'double');

Once you have retrieved data to the MATLAB workspace using getdata, the records are removed from the toolbox engine to free up memory for additional logged records.

If you specify a smaller number of records than those available in memory, getdata will retrieve the oldest records. You can use the RecordsAvailable property of the dagroup object to determine how many records the toolbox engine has stored for that group.

During a logging task, you can examine the most recently acquired records using the peekdata function, passing the dagroup object as the first argument, and the number of records to retrieve as the second argument. Data is returned in a structure. You cannot return data into separate arrays using peekdata. You can convert the structure returned by peekdata into separate arrays using the opcstruct2array function. Data retrieved using peekdata is not removed from the toolbox engine.

For an example of using getdata and peekdata during a logging task, see “Overview of a Logging Task” on page 7-16.

When you delete a dagroup object, the data stored in the toolbox engine for that object is also deleted.

Log OPC Server Data

7-25 Retrieve Data from Disk

You can retrieve data from a disk file into the MATLAB workspace using the opcread function. You pass the name of the file containing the logged OPC data as the first argument. The data stored in the log file is returned as a structure array, in the same format as the structure returned by getdata. Records retrieved from a log file into the MATLAB workspace are not removed from the log file.

You can specify a number of additional arguments to the opcread function, that control the records that are retrieved from the file. The additional arguments must be specified by an option name and the option value. The following options are available.

Option Name Option Value Description

'items' Specify a cell array of item IDs that you want returned. Items not in this list will not be read.

'dates' Specify a date range for the event times. The range must be [startDt endDt] where startDt and endDt are MATLAB date numbers.

'records' Specify the index of records to retrieve as [startRec endRec].

Records outside these indices will not be read.

'datatype' Specify the data type, as a string, that should be used for the returned values. Valid data type strings are the same as for

getdata. If you specify a numeric data type or 'cell', the output will be returned in separate arrays. If you specify a numeric array data type such as 'double' or 'uint32', and the logged data contains arrays or strings, an error will be generated and no data will be returned.

The following example retrieves the data logged during the example on page “Overview of a Logging Task” on page 7-16, first into a structure array, and then records 3 to 6 are retrieved into separate arrays for Value, Quality, and TimeStamp.

sDisk = opcread('LoggingExample.olf')

7

Reading, Writing, and Logging OPC Data

'records',[3,6], 'datatype','double') i =

'Good: Non-specific' 'Good: Non-specific' 'Good: Non-specific' 'Good: Non-specific' 'Good: Non-specific' 'Good: Non-specific' 'Good: Non-specific' 'Good: Non-specific' 'Good: Non-specific' 'Good: Non-specific' 'Good: Non-specific' 'Good: Non-specific' t =

Note For a record to be returned by opcread, it must satisfy all the options passed to opcread.

8

Documento similar