OPC Toolbox 2.1.4
Creating and Configuring Objects
The OPC Toolbox™ uses three objects arranged in a strict hierarchy. This example shows you how to create and configure these three OPC Toolbox objects, and how to configure OPC Toolbox object properties.
Contents
Step 1: Create Client Objects.
You 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);
Step 2: Add Groups to the Client
To add groups to the client you use the addgroup function. The OPC Toolbox automatically assigns a name to the group, if you do not specify one.
grp1 = addgroup(da);
To assign your own name (which must be unique for all the groups in a client) you pass the name as an additional argument.
grp2 = addgroup(da, 'MyGroup');
To view a summary of the group object, type the object name.
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
Step 3: Add Item Objects to the Group
Item objects are added to a group using the additem function. You specify the group object to add the item to, and the Item ID of the server item you want to monitor.
itm1 = additem(grp1, 'Random.Real8');
To specify a data type for the item (for storage of the value in MATLAB®) you specify the data type as a string.
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 :
Step 4: Create Object Vectors
You can use object vectors to store references to multiple OPC Toolbox objects. The following example constructs an object vector from the items added to the group. A summary of the object vector shows information about each object in the vector.
itmVec = [itm1, itm2]
itmVec = OPC Item Object Array: Index: Active: ItemID: Value: Quality: Ti meStamp: 1 on Random.Real8 Bad: Ou... 2 on Random.UInt2 Bad: Ou...
Step 5: Viewing and Changing Object Properties
To view a list of all properties supported by the object, use the get function with no output arguments.
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
To obtain a specific property, pass that property name to the get function.
clientName = get(da,'Name')
clientName = localhost/Matrikon.OPC.Simulation.1
You can get information about a property using the propinfo function. Information includes whether the property is read only, and valid property values for properties that have a predefined set of values.
statusInfo = propinfo(da, 'Status')
statusInfo =
Type: 'string'
Constraint: 'enum'
ConstraintValue: {'disconnected' 'connected'}
DefaultValue: 'disconnected'
ReadOnly: 'always'
To set the value of a property, use the set function.
set(da, 'Timeout', 30)
Step 6: Clean Up
When you are finished using OPC Toolbox objects, you should delete them from the OPC Toolbox engine. Deleting the client object deletes the group and item objects associated with that client.
disconnect(da) delete(da) clear da grp1 grp2 itm1 itm2 itmVec
Store