Find and Connect to OPC HDA Server
Prerequisites
To interact with an OPC HDA server, you must provide:
The host name of the computer on which the OPC HDA server is installed. Typically the host name is a descriptive term (such as
'plantserver'
) or an IP address (such as192.168.2.205
).The server ID of the server you want to access on that host. Because a single computer can host multiple OPC HDA servers, each server installed on that computer is given a unique ID during installation.
Your network administrator can provide the host names for all computers with OPC
HDA servers on your network. You can also obtain a list of server IDs for each host
on your network, or use the opchdaserverinfo
function to access
server IDs from a host.
Determine HDA Server IDs for a Host
When an OPC server is installed, it must be assigned a unique server ID. This server ID provides a unique name for a particular instance of an OPC server on a host, even if multiple copies of the same server software are installed on that same machine.
To determine the server IDs of the OPC servers installed on a host, call the
opchdaserverinfo
function, specifying
the host name as the only argument.
info = opchdaserverinfo('localhost')
When called with this syntax, the function returns a structure containing information about all the OPC servers available on that host.
info = 1x4 OPC HDA ServerInfo array: index Host ServerID HDASpecification Description ----- --------- --------------------------------- ---------------- ------------------------------------------------ 1 localhost Advosol.HDA.Test.3 HDA1 Advosol HDA Test Server V3.0 2 localhost IntegrationObjects.OPCSimulator.1 HDA1 Integration Objects OPC DX HDA Simulator 2 3 localhost IntegrationObjects.OPCSimulator.1 HDA1 Integration Objects' OPC HDA Server Simulator 4 localhost Matrikon.OPC.Simulation.1 HDA1 MatrikonOPC Server for Simulation and Testing
The fields in the structure provide information listed in this table.
Server Information Returned by
opchdaserverinfo
Field | Description |
---|---|
| Character vector that identifies the name of the host. No name resolution is performed on an IP address. |
| Cell array containing the server IDs of all OPC servers accessible from that host. |
| Cell array containing the OPC specification that the server provides. |
| Cell array containing descriptive text for each server. |
You can examine the returned structure for specific details using the field name. For example, to query for the server IDs of each OPC server use this syntax.
allServers = {hostInfo.ServerID}
allServers = Columns 1 through 3 'Advosol.HDA.Test.3' 'IntegrationObjects.OPCSimulator.1' 'IntegrationObjects.OPCSimulator.1' Column 4 'Matrikon.OPC.Simulation.1'
Create OPC HDA Client Object
After determining the host name and server ID of the OPC HDA server you want to connect to, you can create an OPC HDA client object. The client controls the connection status to the server, stores properties of that server, and allows you to read data from the server.
Create an OPC HDA client using the opchda
function and specifying the host name and server ID
arguments.
hdaClient = opchda('localhost', 'Matrikon.OPC.Simulation.1')
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)
You can also construct client objects directly from an OPC HDA
ServerInfo
object using this code.
hostInfo = opchdaserverinfo('localhost');
hdaClient = opchda(hostInfo(1));
Set Client Properties
You can modify many properties specific to the created client. These include
Timeout
, UserData
, Host
(before connection), and ServerID
(before connection). Modify
these properties as you would any other field of a MATLAB® structure.
Set Timeout Property
As OPC transactions often occur across networks, you might encounter cases
where calls to those servers take some time to return. To change the function
timeout of the OPC HDA client object, assign a new value to its
Timeout
property.
hdaClient.Timeout = 12
hdaClient = OPC HDA Client localhost/Matrikon.OPC.Simulation.1: Host: localhost ServerID: Matrikon.OPC.Simulation.1 Timeout: 12 seconds Status: connected Aggregates: 6 Aggregate Types ItemAttributes: 10 Item Attributes Methods
Connect to OPC HDA Server
OPC HDA client objects do not automatically connect to the server once you create
them. You can see this from the Status
property of the client
object.
Use the connect
function to connect an OPC HDA
client object to the server at the command line.
connect(hdaClient)
After connecting the client to the server, call the client object to view the updated properties.
hdaClient
hdaClient = OPC HDA Client localhost/Matrikon.OPC.Simulation.1: Host: localhost ServerID: Matrikon.OPC.Simulation.1 Timeout: 10 seconds Status: connected Aggregates: 6 Aggregate Types ItemAttributes: 10 Item Attributes
Browse OPC Server Namespace
Use the client object connected to the server to obtain information about the
namespace of that server. The server namespace provides access to all the data
points provided by the OPC server by naming each data point and arranging them
into a namespace that provides a unique identifier for each server item. See
Retrieve OPC HDA Server Namespace. You can also graphically browse the HDA
server namespace using the browseNamespace
function.
Read Item Attributes
Each item that you find on a server might have a given set of item attributes associated with it. These attributes provide information about the item stored on the server. The OPC Foundation defines a set of common item attributes, while specific servers can define server-specific attributes. However, support for item attributes is optional for any server.
You can find the attributes supported by your server by querying the
ItemAttributes
property of a connected HDA client
object:
hdaClient.ItemAttributes
OPC HDA Item Attributes: Name ID Description -------------- ---------- ---------------- DATA_TYPE 1 Data type DESCRIPTION 2 Item Description NORMAL_MAXIMUM 11 High EU NORMAL_MINIMUM 12 Low EU ITEMID 13 Item ID TRIANGLE 4294967291 Triangle Wave SQUARE 4294967292 Square Wave SAWTOOTH 4294967293 Saw-toothed Wave RANDOM 4294967294 Random BUCKET 4294967295 Bucket Brigade
Use the readItemAttributes
function to retrieve the item attributes for a particular item.
For a list of item attributes defined by the OPC Foundation for the OPC HDA specification, see OPC HDA Item Attributes.