OPC Toolbox

Create and Configure OPC Toolbox Objects

This example shows you how to create and configure OPC Toolbox objects.

PREREQUISITES:

Create Client Objects.

Create a client using the opcda function. You need the host name and the server ID for the OPC Server associated with this client.

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

Add Groups to the Client

Use the addgroup function to add groups to the client object. The OPC Toolbox automatically assigns a name to the group, if you do not specify one.

grp1 = addgroup(da);

Group objects are used to manage collections of daitem objects.

To assign your own name to a group, the name must be unique for all the groups in a client. Pass the name as an additional argument to addgroup.

grp2 = addgroup(da, 'MyGroup');

Type the object name to view a summary of the group object.

grp1
grp1 =
Summary of OPC Data Access Group Object: Group0
   Object Parameters
      Group Type   : private
      Item         : 0-by-1 daitem object
      Parent       : localhost/Matrikon.OPC.Simulation.1
      Update Rate  : 0.5
      Deadband     : 0%
   Object Status
      Active       : on
      Subscription : on
      Logging      : off
   Logging Parameters
      Records      : 120
      Duration     : at least 60 seconds
      Logging to   : memory
      Status       : Waiting for START.
                     0 records available for GETDATA/PEEKDATA

Add Item Objects to the Group

Add the item named Random.Real8 to the group.

itm1 = additem(grp1, 'Random.Real8');

Specify a third argument if you want the value stored in MATLAB® to have a specific data type.

itm2 = additem(grp1, 'Random.UInt2', 'double');

To view a summary of the object, type the name of the object.

itm1
itm1 =
Summary of OPC Data Access Item Object: Random.Real8
   Object Parameters
      Parent        : Group0
      Access Rights : read
   Object Status
      Active        : on
   Data Parameters
      Data Type     : double
      Value         : 
      Quality       : Bad: Out of Service
      Timestamp     : 

Create Object Vectors

References to multiple OPC Toolbox objects can be stored in object vectors.

itmVec = [itm1, itm2]
itmVec =
   OPC Item Object Array:
   Index:  Active:  ItemID:             Value:                Quality:    TimeStamp:
   1       on       Random.Real8        0                     Bad: Ou...  05:33:50
   2       on       Random.UInt2        0                     Bad: Ou...  05:33:50

Displaying the object vector shows information about each object in the vector.

View and Change Object Properties

View a list of all properties supported by the object.

get(da)
  General Settings:
    EventLog = []
    EventLogMax = 1000
    Group = [1x2 dagroup]
    Host = localhost
    Name = localhost/Matrikon.OPC.Simulation.1
    ServerID = Matrikon.OPC.Simulation.1
    Status = connected
    Tag = 
    Timeout = 10
    Type = opcda
    UserData = []

  Callback Function Settings:
    ErrorFcn = @opccallback
    ShutdownFcn = @opccallback
    TimerFcn = []
    TimerPeriod = 10

Obtain information about a specific property.

clientName = da.Name
clientName =
localhost/Matrikon.OPC.Simulation.1

Get information about a property using the propinfo function.

statusInfo = propinfo(da, 'Status')
statusInfo = 
               Type: 'string'
         Constraint: 'enum'
    ConstraintValue: {'disconnected'  'connected'}
       DefaultValue: 'disconnected'
           ReadOnly: 'always'

Information includes whether the property is read only, and valid property values for properties that have a predefined set of values.

Set the value of the Timeout property to 30 seconds.

da.Timeout = 30

Clean Up

Delete objects that you are finished using fro the OPC Toolbox engine.

disconnect(da)
delete(da)

Deleting the client object deletes the group and item objects associated with that client.