This example shows you how to create and configure OPC Toolbox™ objects.
PREREQUISITES:
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);
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 the item named Random.Real8 to the group.
itm1 = additem(grp1,'Random.Real8');
If you want the value stored in MATLAB® to have a specific data type, specify it as the third argument.
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 : 0
Quality : Bad: Out of Service
Timestamp : 12-Apr-2016 16:19:50
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... 16:19:50 2 on Random.UInt2 Bad: Ou...
Displaying the object vector shows information about each object in the vector.
View a list of all properties supported by the object.
get(da)
General Settings:
EventLog = []
EventLogMax = 1000
Group = [1×2 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'
The information includes whether the property is read-only, and lists the valid values for properties that have a predefined set of values.
Set the value of the Timeout property to 30 seconds.
da.Timeout = 30
da =
Summary of OPC Data Access Client Object: localhost/Matrikon.OPC.Simulation.1
Server Parameters
Host : localhost
ServerID : Matrikon.OPC.Simulation.1
Status : connected
Timeout : 30 seconds
Object Parameters
Group : 2-by-1 dagroup object
Event Log : 0 of 1000 events
Delete objects that you are finished using from the OPC Toolbox engine.
disconnect(da) delete(da)
Deleting the client object also deletes the group and item objects associated with that client.