Locate and Browse OPC Historical Data Access Servers
This example shows how to browse the network for OPC Historical Data Access servers, and query the server namespace for server items and their properties.
PREREQUISITES:
Step 1: Browse 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 = OPC HDA Server Information object: Host: localhost ServerID: Matrikon.OPC.Simulation.1 Description: MatrikonOPC Server for Simulation and Testing HDASpecification: HDA1
Find the server info entry with a description starting with Matrikon.
hIndex = findDescription(hostInfo,'Matrikon')
hostInfo(hIndex)
hIndex = 1 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 Client Object and Connect to 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: Browse Server Namespace and Select Items
Browse the graphical namespace of the server and select required items. For this example, Sawtoothed-Waves.Real4
item is selected.
ns = browseNameSpace(hdaObj)
ns = 1×1 cell array {'Saw-toothed Waves.Real4'}
Step 4: Query Server Item Attributes
Examine the current normal maximum value of the selected item.
maxVal = readItemAttributes(hdaObj,ns(1),hdaObj.ItemAttributes.NORMAL_MAXIMUM,now,now)
Warning: Saw-toothed Waves.Real4: No history available for attribute. maxVal = struct with fields: ItemID: 'Saw-toothed Waves.Real4' AttributeID: 11 Timestamp: 7.3960e+05 Value: 100
The warning indicates that the item has not yet been stored in the historian database, but the preconfigured item attributes are being returned.
Step 5: Clean Up OPC Objects
Disconnect the client from the server and remove OPC objects from memory when you no longer need them. Deleting the client object also deletes the group and item objects.
disconnect(hdaObj) delete(hdaObj)