Main Content

Connect to OPC HDA Servers


After getting information about your OPC servers as described in Discover Available HDA Servers, you can establish a connection to the server by creating an OPC HDA client object, and connecting that client to the server. These steps are described next.


To run the sample code in the following steps you need the Matrikon™ OPC Simulation Server on your local machine. For installation details, see Install an OPC DA or HDA Simulation Server for OPC Classic Examples. The code requires only minor changes to work with other servers.

Create an HDA Client Object

Industrial Communication Toolbox™ does not use groups when dealing with HDA server items. Instead, the items themselves are passed to the available functions. These functions are accessible through the OPC HDA client object. In most cases, functions accessed via this HDA client object return an opc.hda.Data object. These data object simplify the display and manipulation of the historical data retrieved from the HDA server.

To create an OPC HDA client object, call the opchda function, specifying the host name and server ID. You retrieved this information using the opchdaserverinfo function (described in Discover Available HDA Servers). This example creates an OPC HDA client object to represent the connection to a Matrikon OPC Simulation Server:

hdaClient = opchda('localhost','Matrikon.OPC.Simulation.1');

View a Summary of a Client Object

To view a summary of the characteristics of the OPC HDA client object you created, enter the variable name you assigned to the object at the command prompt. For example, this is the summary for the hdaClient object:

hdaClient = 
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 an OPC HDA Client Object to the HDA Server

Use the connect function to connect a client to the server:

After connecting to the server, the Status information in the client summary display changes from disconnected to connected. If the client could not connect to the server (for example, if the OPC server is shut down), an error message appears. For information on troubleshooting connections to an OPC server, see Troubleshooting OPC Issues. After connecting to the client to the server, you can request a list of available aggregate types with the hdaClient.Aggregates function, as well as available item attributes with hdaClient.ItemAttributes. While connected you can browse the OPC server name space for information on available server items. See the next section for details on browsing the server name space. You can list the HDA functions with methods(hdaClient).

Browse the OPC Server Name Space

A connected client object allows you to interact with the OPC server to obtain information about the name space of that server. The server name space provides access to all the data points provided by the OPC server by naming each data point with a server item, and then arranging those server items into a name space that provides a unique identifier for each server item.

The next section describes how to obtain a server name space or a partial server name space, using the getnamespace and serveritems functions.

Get an OPC HDA Server Name Space

Use the getnamespace function to retrieve the name space from an OPC HDA server. You must specify the client object that is connected to the server that you are interested in. The name space is returned as a structure array containing information about each node in the name space.

This example retrieves the name space of the Matrikon OPC Simulation Server installed on the local host:

hdaClient = opchda('localhost','Matrikon.OPC.Simulation.1');
ns = getnamespace(hdaClient)
ns = 

3x1 struct array with fields:

This table describes the fields of the structure:




The name of the node, as a character vector.


The fully qualified item ID of the node, as a character vector. The fully qualified item ID is made up of the path to the node, concatenated with '.' characters. Use the fully qualified item ID when creating an item object associated with this node.


The type of node. NodeType can be 'branch' (contains other nodes) or 'leaf' (contains no other branches).


Child nodes. Nodes is a structure array with the same fields as ns, representing the nodes contained in this branch of the name space.

From the previous above, exploring the name space shows:

ans =
                Name: 'Simulation Items'
    FullyQualifiedID: 'Simulation Items'
            NodeType: 'branch'
               Nodes: [8x1 struct]


                Name: 'Clients'
    FullyQualifiedID: 'Clients'
            NodeType: 'leaf'
               Nodes: []

From this information, the first node is a branch node called 'Simulation Items'. Since it is a branch node, it is most likely not a valid server item. The third node is a leaf node (containing no other nodes) with a fully qualified ID of 'Clients'. Since this node is a leaf node, it is most likely a server item that can be monitored by creating an item object. To examine the nodes further down the tree, reference the Nodes field of a branch node. For example, the first node contained within the 'Simulation Items' node is obtained as follows:

ans =
                Name: 'Bucket Brigade'
    FullyQualifiedID: 'Bucket Brigade.'
            NodeType: 'branch'
               Nodes: [14x1 struct]

The returned result shows that the first node of 'Simulation Items' is a branch node named 'Bucket Brigade', and contains 14 nodes.

ans = 
                Name: 'Real8'
    FullyQualifiedID: 'Bucket Brigade.Real8'
            NodeType: 'leaf'
               Nodes: []

The ninth node in 'Bucket Brigade' is named 'Real8' and has a fully qualified ID of 'Bucket Brigade.Real8'. Use the fully qualified ID to refer to that specific node in the server name space when creating items.