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  20 double values  2024-04-29 08:59:49  2024-04-29 09:01:10  1 unique quality [Raw]
    Random.Real8             20 double values  2024-04-29 08:59:49  2024-04-29 09:01:10  1 unique quality [Raw]
    Random.UInt2             20 uint16 values  2024-04-29 08:59:49  2024-04-29 09:01:10  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 with a custom date display format using opc.setDateDisplayFormat.

opc.setDateDisplayFormat('yyyy-mm-dd HH:MM:SS');
showValues(data(1))
OPC HDA Data object for item Saw-toothed Waves.Real8:

         TIMESTAMP           VALUE       QUALITY
    ===================  =============  ==========
    2024-04-29 08:59:49       3.141593  Raw (Good)
    2024-04-29 08:59:50       6.283185  Raw (Good)
    2024-04-29 08:59:51       9.424778  Raw (Good)
    2024-04-29 08:59:52      12.566371  Raw (Good)
    2024-04-29 08:59:53      15.707963  Raw (Good)
    2024-04-29 08:59:54      18.849556  Raw (Good)
    2024-04-29 08:59:55      21.991149  Raw (Good)
    2024-04-29 08:59:56      25.132741  Raw (Good)
    2024-04-29 08:59:57      28.274334  Raw (Good)
    2024-04-29 08:59:58      31.415927  Raw (Good)
    2024-04-29 08:59:59      34.557520  Raw (Good)
    2024-04-29 09:01:02      37.699112  Raw (Good)
    2024-04-29 09:01:03      40.840705  Raw (Good)
    2024-04-29 09:01:04      43.982298  Raw (Good)
    2024-04-29 09:01:05      47.123890  Raw (Good)
    2024-04-29 09:01:06      50.265483  Raw (Good)
    2024-04-29 09:01:07      53.407076  Raw (Good)
    2024-04-29 09:01:08      56.548668  Raw (Good)
    2024-04-29 09:01:09      59.690261  Raw (Good)
    2024-04-29 09:01:10      62.831854  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  2024-04-29 09:00:11  2024-04-29 09:01:01  2 unique qualities
    Random.Real8             6 double values  2024-04-29 09:00:11  2024-04-29 09:01:01  2 unique qualities
    Random.UInt2             6 uint16 values  2024-04-29 09:00:11  2024-04-29 09:01:01  2 unique qualities

Use the showValues method to display all values.

Display the values for the Random.Real8 item.

itmInd = getIndexFromID(pData,'Random.Real8');
opc.setDateDisplayFormat('yyyy-mm-dd HH:MM:SS');
showValues(pData(itmInd))
OPC HDA Data object for item Random.Real8:

         TIMESTAMP           VALUE             QUALITY
    ===================  =============  ======================
    2024-04-29 09:00:11    1898.988063  Calculated (Good)     
    2024-04-29 09:00:21    1898.988063  Calculated (Good)     
    2024-04-29 09:00:31    1898.988063  Calculated (Good)     
    2024-04-29 09:00:41    1898.988063  Calculated (Good)     
    2024-04-29 09:00:51    1898.988063  Calculated (Good)     
    2024-04-29 09:01:01    9079.370319  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 =

  logical

   0