Main Content

Connect to OPC Data Access Servers


After you get information about your OPC servers, described in Discover Available Data Access Servers you can establish a connection to the server by creating an OPC Client object and connecting that client to the server. These steps are described in the following sections.


To run the sample code in the following examples, you must have the Matrikon™ OPC Simulation Server available on your local machine. For information on installing this, see Install an OPC DA or HDA Simulation Server for OPC Classic Examples. The code requires only minor changes work with other servers.

Create a DA Client Object

To create an opcda object, call the opcda function specifying the hostname, and server ID. You retrieved this information using the opcserverinfo function (described in Discover Available Data Access Servers).

This example creates an opcda object to represent the connection to a Matrikon OPC Simulation Server. The opcserverinfo function includes the default opcda syntax in the ObjectConstructor field.

da = opcda('localhost', 'Matrikon.OPC.Simulation.1');

View a Summary of a Client Object

To view a summary of the characteristics of the opcda object you created, enter the variable name you assigned to the object at the command prompt. For example, this is the summary for the object da.


The items in this list correspond to the numbered elements in the object summary:

  1. The title of the Summary includes the name of the opcda client object. The default name for a client object is made up of the 'host/serverID'. You can change the name of a client object using the set function, described in Configure OPC Data Access Object Properties.

  2. The Server Parameters provide information on the OPC server that the client is associated with. The host name, server ID, and connection status are provided in this section. You connect to an OPC server using the connect function, described in Connect a Client to the DA Server.

  3. The Object Parameters section contains information on the OPC Data Access Group (dagroup) objects configured on this client. You use group objects to contain collections of items. Creating group objects is described in Create Data Access Group Objects.

Connect a Client to the DA Server

You connect a client to the server using the connect function.


Once you have connected to the server, the Status information in the client summary display will change from 'disconnected' to 'connected'.

If the client could not connect to the server for some reason (for example, if the OPC server is shut down) an error message will be generated. For information on troubleshooting connections to an OPC server, see Troubleshooting OPC Issues.

When you have connected the client to the server, you can perform the following tasks:

  • Get diagnostic information about the OPC server, such as the server status, last update time, and supported interfaces. You use the opcserverinfo function to obtain this information.

  • Browse the OPC server name space for information on the available server items. See Browse the OPC DA Server Name Space for details.

  • Create group and item objects to interact with OPC server data. See Create OPC Data Access Objects for information.

Browse the OPC DA 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 of the data points with a server item, and then arranging those server items into a name space that provides a unique identifier for each server item.

This section describes how you use a connected client object to browse the name space and find information about each server item. These activities are described in the following sections:

Get the DA Server Name Space

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

The example below retrieves the name space of the Matrikon OPC Simulation Server installed on the local host.

da = opcda('localhost','Matrikon.OPC.Simulation.1');
ns = getnamespace(da)

ns = 
3x1 struct array with fields:

The fields of the structure are described in the following table.




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. You 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 example above, exploring the name space shows.


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


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

From the information above, 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, you need to 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'. You use the fully qualified ID to refer to that specific node in the server name space when creating items.

You can use the flatnamespace function to flatten a hierarchical name space.

Get Information about a Specific Server Item

In addition to publishing a name space to all clients, an OPC server provides information about the properties of each of the server items in the name space. These properties provide information on the data format used by the server to store the server item value, a description of the server item, and additional properties configured when the server item was created. The additional properties can include information on the range of the server item, the maximum rate at which the server can update that server item value, etc. See OPC DA Server Item Properties.

You access a property using a defined set of property IDs. A property ID is simply a number that defines a specific property of the server item. Property IDs are divided into three categories:

  • OPC Specific Properties (1-99) that every OPC server must provide. The OPC Specific Properties include the server item’s Value, Quality, and Timestamp. For more information on understanding OPC data, see OPC Data: Value, Quality, and TimeStamp.

  • OPC Recommended Properties (100-4999) that OPC servers can provide. These properties include maximum and minimum values, a description of the server item, and other commonly used properties..

  • Vendor Specific Properties (5000 and higher) that an OPC server can define and use. These properties may be different for each OPC server, and provide a space for OPC server manufacturers to define their own properties.

You query properties of a server item using the serveritemprops function, specifying the client object, the fully qualified item ID of the server item you are interested in, and an optional vector of property IDs that you want to retrieve. If you do not specify the property IDs, all properties defined for that server item are returned.


You obtain the fully qualified item ID from the server using the getnamespace function or the serveritems function, which simply returns all fully qualified item IDs in a cell array of character vectors.

The following example queries the Item Description property (ID 101) of the server item 'Bucket Brigade.ArrayOfReal8' from the example in Get the DA Server Name Space.

p = serveritemprops(da, 'Bucket Brigade.ArrayOfReal8', 101)

p = 
             PropID: 101
    PropDescription: 'Item Description'
          PropValue: 'Bucket brigade item.'

For a list of OPC Foundation property IDs, see OPC DA Server Item Properties.