OPC Toolbox

Locate and Browse OPC Historical Data Access Servers

This example demonstrates how to use the OPC Toolbox™ to browse the network for OPC Historical Data Access servers, and use OPC Toolbox functions to query the server name space for server items and their properties.

PREREQUISITES:

Step 1: Browse the Network for OPC HDA Servers

You use the opchdaserverinfo function to query a host on the network for available OPC Historical Data Access servers. This example uses the local host.

hostInfo = opchdaserverinfo('localhost')
hostInfo = 
1x2 OPC HDA ServerInfo array:
    index    Host             ServerID           HDASpecification                   Description                 
    -----  ---------  -------------------------  ----------------  ---------------------------------------------
      1    localhost  Advosol.HDA.Test.3         HDA1              Advosol HDA Test Server V3.0
      2    localhost  Matrikon.OPC.Simulation.1  HDA1              MatrikonOPC Server for Simulation and Testing

Find the server info entry with a description starting with Matrikon.

hIndex = findDescription(hostInfo,'Matrikon')
hostInfo(hIndex)
hIndex =
     2
ans = 
OPC HDA Server Information object:
                 Host: localhost
             ServerID: Matrikon.OPC.Simulation.1
          Description: MatrikonOPC Server for Simulation and Testing
     HDASpecification: HDA1

Step 2: Construct a Client Object and Connect to the Server

Use the ServerInfo object returned in the previous step to construct a client object.

hdaObj = opchda(hostInfo(hIndex));

You can also specify the host name and server ID directly

hdaObj = opchda('localhost', 'Matrikon.OPC.Simulation.1')
hdaObj = 
OPC HDA Client localhost/Matrikon.OPC.Simulation.1:
               Host: localhost
           ServerID: Matrikon.OPC.Simulation.1
            Timeout: 10 seconds

             Status: disconnected

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

Connect the client to the server.

connect(hdaObj);

Step 3: Retrieve the Server Name Space

Retrieve the name space of the server.

ns = getNameSpace(hdaObj)
ns = 
4x1 struct array with fields:
    Name
    FullyQualifiedID
    NodeType
    Nodes

Each element of the structure is a node in the server name space.

ns(1)
ans = 
                Name: 'Simulation Items'
    FullyQualifiedID: 'Simulation Items¥'
            NodeType: 'branch'
               Nodes: [8x1 struct]

Step 4: Find Items in the Name Space

Use the serveritems function to find all items in the name space containing the string Real.

realItems = serveritems(ns, '*Real*')
realItems = 
    'Bucket Brigade.ArrayOfReal8'
    'Bucket Brigade.Real4'
    'Bucket Brigade.Real8'
    'Random.ArrayOfReal8'
    'Random.Real4'
    'Random.Real8'
    'Read Error.ArrayOfReal8'
    'Read Error.Real4'
    'Read Error.Real8'
    'Saw-toothed Waves.Real4'
    'Saw-toothed Waves.Real8'
    'Square Waves.Real4'
    'Square Waves.Real8'
    'Triangle Waves.Real4'
    'Triangle Waves.Real8'
    'Write Error.ArrayOfReal8'
    'Write Error.Real4'
    'Write Error.Real8'
    'Write Only.ArrayOfReal8'
...

Step 5: Query Server Item Attributes

Examine the current normal maximum value of the tenth item found.

maxVal = readItemAttributes(hdaObj, realItems{10}, hdaObj.ItemAttributes.NORMAL_MAXIMUM, now, now)
Warning: Saw-toothed Waves.Real4: No history available for attribute. 
maxVal = 
         ItemID: 'Saw-toothed Waves.Real4'
    AttributeID: 11
      Timestamp: 7.3529e+05
          Value: 100

The warning indicates that the item has not yet been stored in the historian database, but the pre-configured item attributes are being returned.

Step 6: Clean Up OPC Toolbox™ Objects

Disconnect the client from the server and remove OPC Toolbox™ objects from memory when you no longer need them. Deleting the client object deletes the group and item objects also.

disconnect(hdaObj)
delete(hdaObj)