OPC Toolbox

Locate and Browse OPC Data Access Servers

This example shows you how to use OPC Toolbox™ to browse the network for OPC servers, and query the server name space for server items and their properties.

PREREQUISITES:

Step 1: Browse the Network for OPC Servers

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

hostInfo = opcserverinfo('localhost')
hostInfo = 
                 Host: 'localhost'
             ServerID: {'Matrikon.OPC.Simulation.1'  'OSI.DA.1'  'OSI.HDA.1'}
    ServerDescription: {1x3 cell}
     OPCSpecification: {'DA2'  'DA2'  'DA2'}
    ObjectConstructor: {1x3 cell}

The returned structure provides information about each server:

hostInfo.ServerDescription'
ans = 
    'MatrikonOPC Server for Simulation and Testing'
    'PI OSI DA Server'
    'PI OSI HDA Server'

and the Server ID you use to create a client object.

allID = hostInfo.ServerID'
allID = 
    'Matrikon.OPC.Simulation.1'
    'OSI.DA.1'
    'OSI.HDA.1'

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

Use the host name and server ID found in the previous step to construct a client object.

da = opcda('localhost', 'Matrikon.OPC.Simulation.1')
da =
Summary of OPC Data Access Client Object: localhost/Matrikon.OPC.Simulation.1
   Server Parameters
      Host      : localhost
      ServerID  : Matrikon.OPC.Simulation.1
      Status    : disconnected
      Timeout   : 10 seconds
   Object Parameters
      Group     : 0-by-1 dagroup object
      Event Log : 0 of 1000 events

Connect the client to the server.

connect(da);

Step 3: Retrieve the Server Name Space

Retrieve the name space of the server.

ns = getnamespace(da)
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 Properties

Examine the Canonical Data Type (PropID = 1) and the Item Access Rights (PropID = 5) of the second item found.

canDT = serveritemprops(da, realItems{2}, 1)
accessRights = serveritemprops(da, realItems{2}, 5)
canDT = 
             PropID: 1
    PropDescription: 'Item Canonical DataType'
          PropValue: 'single'
         PropItemID: ''
accessRights = 
             PropID: 5
    PropDescription: 'Item Access Rights'
          PropValue: 'read/write'
         PropItemID: ''

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(da)
delete(da)