Acquire Data from an OPC Data Access Server
This example shows how to use Industrial Communication Toolbox™ to acquire data from an OPC server.
PREREQUISITES:
Create OPC Data Access Object Hierarchy
Create an opcda
object associated with the required server and connect to the server.
da = opcda('localhost','Matrikon.OPC.Simulation.1') connect(da)
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
Create a group object to manage the required items.
grp = addgroup(da,'DemoGroup')
grp = Summary of OPC Data Access Group Object: DemoGroup 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 Real8
item from Saw-Toothed Waves
and the Real8
and UInt2
items from Triangle Waves
to the group.
itmIDs = {'Saw-toothed Waves.Real8', ... 'Triangle Waves.Real8', ... 'Triangle Waves.UInt2'}; itm = additem(grp,itmIDs)
itm = OPC Item Object Array: Index: Active: ItemID: Value: Quality: TimeStamp: 1 on ...hed Waves.Real8 Bad: Ou... 2 on ...gle Waves.Real8 Bad: Ou... 3 on ...gle Waves.UInt2 Bad: Ou...
Configure OPC Object Properties
Configure the group to log 60 seconds of data at 0.2 second intervals.
logDuration = 60; logRate = 0.2; numRecords = ceil(logDuration./logRate) grp.UpdateRate = logRate; grp.RecordsToAcquire = numRecords;
numRecords = 300
Acquire OPC Server Data
Start the acquisition task, and wait for the task to complete before continuing execution of any MATLAB™ code.
start(grp) wait(grp)
Note that while waiting for a logging task to complete, MATLAB continues to process callbacks from OPC objects (and other objects that include callback functionality).
Retrieve the logged data into separate arrays for the time stamps, quality, and values.
[logIDs,logVal,logQual,logTime,logEvtTime] = getdata(grp,'double');
Examine the workspace for the sizes of the data.
whos log*
Name Size Bytes Class Attributes logDuration 1x1 8 double logEvtTime 300x1 2400 double logIDs 1x3 438 cell logQual 300x3 126004 cell logRate 1x1 8 double logTime 300x3 7200 double logVal 300x3 7200 double
Plot the Data
You can now plot this data all on one set of axes.
logTime = datetime(logTime,'ConvertFrom','datenum'); plot(logTime,logVal); axis tight lgd = legend(logIDs); lgd.AutoUpdate = 'off';
The value data does not provide the full picture. You should always examine the quality of the data to determine the validity of the value array.
Annotate the plot with markers where the quality is not Good
.
hold on isBadQual = strncmp(logQual,'Bad',3); isRepeatQual = strncmp(logQual,'Repeat',6); for k = 1:size(logQual,2) badInd = isBadQual(:,k); plot(logTime(badInd,k),logVal(badInd,k),'ro', ... 'MarkerFaceColor','r','MarkerEdgeColor','k') repInd = isRepeatQual(:,k); plot(logTime(repInd, k),logVal(repInd,k),'ro', ... 'MarkerFaceColor',[0.8 0.5 0],'MarkerEdgeColor','k') end hold off
Bad quality is marked in red, and Repeat quality is marked in orange.
Clean Up
Disconnect and delete the client object from the OPC engine.
disconnect(da) delete(da)