Quantcast

OPC Toolbox

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 have 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's 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's 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 last 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  2013-02-28 13:42:19.321  2013-02-28 13:42:27.430  1 unique quality [Raw]
    Random.Real8             9 double values  2013-02-28 13:42:19.321  2013-02-28 13:42:27.430  1 unique quality [Raw]
    Random.UInt2             9 uint16 values  2013-02-28 13:42:19.321  2013-02-28 13:42:27.430  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.

data(1).showValues
OPC HDA Data object for item Saw-toothed Waves.Real8:
           TIMESTAMP             VALUE       QUALITY
    =======================  =============  ==========
    2013-02-28 13:42:19.321       3.141593  Raw (Good)
    2013-02-28 13:42:20.336       6.283185  Raw (Good)
    2013-02-28 13:42:21.351       9.424778  Raw (Good)
    2013-02-28 13:42:22.361      12.566371  Raw (Good)
    2013-02-28 13:42:23.376      15.707963  Raw (Good)
    2013-02-28 13:42:24.391      18.849556  Raw (Good)
    2013-02-28 13:42:25.406      21.991149  Raw (Good)
    2013-02-28 13:42:26.420      25.132741  Raw (Good)
    2013-02-28 13:42:27.430      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 to use.

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  2013-02-28 13:41:28.486  2013-02-28 13:42:18.486  1 unique quality [Calculated]
    Random.Real8             6 double values  2013-02-28 13:41:28.486  2013-02-28 13:42:18.486  1 unique quality [Calculated]
    Random.UInt2             6 uint16 values  2013-02-28 13:41:28.486  2013-02-28 13:42:18.486  1 unique quality [Calculated]

Use the showValues method to display all values.

Display the values for the Random.Real8 item.

itmInd = pData.getIndexFromID('Random.Real8');
pData(itmInd).showValues
OPC HDA Data object for item Random.Real8:
           TIMESTAMP             VALUE             QUALITY
    =======================  =============  ======================
    2013-02-28 13:41:28.486    5073.986105  Calculated (Uncertain)
    2013-02-28 13:41:38.486    5073.986074  Calculated (Uncertain)
    2013-02-28 13:41:48.486    5073.986085  Calculated (Uncertain)
    2013-02-28 13:41:58.486    5073.986108  Calculated (Uncertain)
    2013-02-28 13:42:08.486    5073.986227  Calculated (Uncertain)
    2013-02-28 13:42:18.486    7322.794834  Calculated (Uncertain)

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

Clean Up

Once you have finished with the OPC Toolbox objects, you must delete them from the OPC Toolbox engine. Although deleting an HDA Client object automatically disconnects the object from the server, disconnecting is shown explicitly in this example.

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

The client object is now invalid.

isvalid(hdaObj)
ans =
     0