Main Content

Acquire Data from an OPC Historical Data Access Server

This example shows you how to acquire data from an OPC Historical Data Access (HDA) server.

PREREQUISITES:

Start Historical Data Logging on the Server

NOTE: You do not normally need to execute this step on a production server.

This example uses a simulation server that only logs historical data for items that are subscribed using an OPC Data Access client. Load the client object from a MAT file and reconnect the client.

daObjs = load('opcdemoHDAConfigure.mat');
connect(daObjs.opcdemoHDAConfigure);

Wait a while for the server to log some data.

pause(10);

Create an OPC HDA Client Object

Create an OPC HDA Client associated with the OPC HDA server.

hdaObj = opchda('localhost','matrikon.OPC.Simulation')
hdaObj = 

OPC HDA Client localhost/matrikon.OPC.Simulation:
               Host: localhost
           ServerID: matrikon.OPC.Simulation
            Timeout: 10 seconds

             Status: disconnected

         Aggregates: -- (client is disconnected)
     ItemAttributes: -- (client is disconnected)

The client object manages the connection with the server, allows you to retrieve information about the server, browse the server name space, and to read data stored on the server.

At this point, the client is not yet connected to the server. Connect the client to the server.

connect(hdaObj);

To confirm that the client is connected, display the client Status property.

hdaObj.Status
ans =

connected

Define Items of Interest

This example uses the Real8 items from Saw-toothed Waves and the Real8 and UInt2 items from Random. Make a cell array of item names for ease-of-use.

itmIDs = {'Saw-toothed Waves.Real8', ...
    'Random.Real8', ...
    'Random.UInt2'};

Read Raw Data from the Server

Read the raw data values from the historical server over the past day.

data = readRaw(hdaObj,itmIDs,now-1,now)
data = 

1-by-3 OPC HDA Data object:

            ItemID                Value           Start TimeStamp           End TimeStamp              Quality        
    -----------------------  ---------------  -----------------------  -----------------------  ----------------------
    Saw-toothed Waves.Real8  9 double values  2016-04-12 16:30:17.662  2016-04-12 16:30:25.776  1 unique quality [Raw]
    Random.Real8             9 double values  2016-04-12 16:30:17.662  2016-04-12 16:30:25.776  1 unique quality [Raw]
    Random.UInt2             9 uint16 values  2016-04-12 16:30:17.662  2016-04-12 16:30:25.776  1 unique quality [Raw]

Use the showValues method to display all values.

Note: The Matrikon server retains only the last 200 simulated values for each item.

Display the values of the first data element.

showValues(data(1))
OPC HDA Data object for item Saw-toothed Waves.Real8:

           TIMESTAMP             VALUE       QUALITY
    =======================  =============  ==========
    2016-04-12 16:30:17.662       3.141593  Raw (Good)
    2016-04-12 16:30:18.677       6.283185  Raw (Good)
    2016-04-12 16:30:19.692       9.424778  Raw (Good)
    2016-04-12 16:30:20.707      12.566371  Raw (Good)
    2016-04-12 16:30:21.717      15.707963  Raw (Good)
    2016-04-12 16:30:22.732      18.849556  Raw (Good)
    2016-04-12 16:30:23.747      21.991149  Raw (Good)
    2016-04-12 16:30:24.761      25.132741  Raw (Good)
    2016-04-12 16:30:25.776      28.274334  Raw (Good)

Read Processed Data from the Server

Query the Aggregates property of the HDA Client object to find out what aggregate types the server supports.

hdaObj.Aggregates
ans = 

OPC HDA Aggregate Types:
          Name         ID                                          Description                                        
    -----------------  --  -------------------------------------------------------------------------------------------
    INTERPOLATIVE      1   Retrieve interpolated values.
    TIMEAVERAGE        4   Retrieve the time weighted average data over the resample interval.
    MINIMUMACTUALTIME  7   Retrieve the minimum value in the resample interval and the timestamp of the minimum value.
    MINIMUM            8   Retrieve the minimum value in the resample interval.
    MAXIMUMACTUALTIME  9   Retrieve the maximum value in the resample interval and the timestamp of the maximum value.
    MAXIMUM            10  Retrieve the maximum value in the resample interval.

The Matrikon server supports the time weighted average value, so we will use that aggregate type on 10 seconds of data for the last 1 minute. Note below how the Aggregates property can be used to specify the aggregate type.

pData = readProcessed(hdaObj,itmIDs,hdaObj.Aggregates.TIMEAVERAGE,10,now-1/24/60,now)'
pData = 

1-by-3 OPC HDA Data object:

            ItemID                Value           Start TimeStamp           End TimeStamp                  Quality           
    -----------------------  ---------------  -----------------------  -----------------------  -----------------------------
    Saw-toothed Waves.Real8  6 double values  2016-04-12 16:29:26.840  2016-04-12 16:30:16.840  1 unique quality [Calculated]
    Random.Real8             6 double values  2016-04-12 16:29:26.840  2016-04-12 16:30:16.840  1 unique quality [Calculated]
    Random.UInt2             6 uint16 values  2016-04-12 16:29:26.840  2016-04-12 16:30:16.840  1 unique quality [Calculated]

Use the showValues method to display all values.

Display the values for the Random.Real8 item.

itmInd = getIndexFromID(pData,'Random.Real8');
showValues(pData(itmInd))
OPC HDA Data object for item Random.Real8:

           TIMESTAMP             VALUE             QUALITY
    =======================  =============  ======================
    2016-04-12 16:29:26.840    5073.986117  Calculated (Uncertain)
    2016-04-12 16:29:36.840    5073.986074  Calculated (Uncertain)
    2016-04-12 16:29:46.840    5073.986105  Calculated (Uncertain)
    2016-04-12 16:29:56.840    5073.986137  Calculated (Uncertain)
    2016-04-12 16:30:06.840    5073.986227  Calculated (Uncertain)
    2016-04-12 16:30:16.840    7322.794889  Calculated (Uncertain)

The last value has a quality of 'Uncertain' because the time interval is not a complete 10 seconds.

Clean Up

When you have finished with the OPC objects, delete them from the OPC engine. Although deleting an HDA Client object automatically disconnects the object from the server, this example explicitly shows it.

disconnect(hdaObj)
delete(hdaObj)
disconnect(daObjs.opcdemoHDAConfigure);
delete(daObjs.opcdemoHDAConfigure);

The client object is now invalid.

isvalid(hdaObj)
ans =

   0