Skip to Main Content Skip to Search
Home |   Select Country  Choose Country  |  Contact Us  |  Cart Store 
Create Account | Log In
Products & Services Solutions Academia Support User Community Company
spacer spacer spacer spacer spacer spacer

 

OPC Toolbox 2.1.4

Getting Started with OPC Toolbox™

This example illustrates the basic steps involved in using the OPC Toolbox™ to acquire data from an OPC Server. This example utilises the Matrikon™ OPC Simulation Server to log data from the Saw-toothed and Triangle Waves signals. The data is then retrieved and plotted to determine if the two signals are related in any way.

Note: In order to run this demo, you must have downloaded and installed the Matrikon OPC Simulation Server, which is available from the Matrikon website (http://www.matrikon.com).

Contents

Step 1: Locate the OPC Server

In order to connect to an OPC server, you need the host name and server ID of that server. Your system administrator can provide you with this information, or you can query a host for the data. In this example, you query the local host for available OPC Servers.

hostInfo = opcserverinfo('localhost')
hostInfo =
                 Host: 'localhost'
             ServerID: {'Matrikon.OPC.Simulation.1'}
    ServerDescription: {'Matrikon OPC Server for Simulation and Testing
'}
     OPCSpecification: {'DA2'}
    ObjectConstructor: {'opcda('localhost', 'Matrikon.OPC.Simulation.1'
)'}

Examine the returned structure in more detail.

allServers = hostInfo.ServerID'
allServers =
    'Matrikon.OPC.Simulation.1'

To connect to the Matrikon server, use the ID '|Matrikon.OPC.Simulation.1|'

Step 2: Create an OPC Data Access™ Client Object

Once you know the host name and server ID of the OPC Server you want to connect to, you can create an opcda object associated with that server.

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

The client object manages the connection with the server, and all groups that you create to read and write data from the server.

At this point, the client is not yet connected to the server.

Step 3: Connect to the Server

connect(da)

To confirm that the client is connected, display the client.

da
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   : 10 seconds
   Object Parameters
      Group     : 0-by-1 dagroup object
      Event Log : 0 of 1000 events

Step 4: Browse the Server Name Space

To find the Fully Qualified ID of the items you wish to examine, you need to browse the server's name space. You can use a partial match to find all items containing the string '|Saw|' and '|Triangle|'

sawtoothItems = serveritems(da, '*Saw*')
triangleItems = serveritems(da, '*Triangle*')
sawtoothItems =
    'Saw-toothed Waves.'
    'Saw-toothed Waves.Int1'
    'Saw-toothed Waves.Int2'
    'Saw-toothed Waves.Int4'
    'Saw-toothed Waves.Money'
    'Saw-toothed Waves.Real4'
    'Saw-toothed Waves.Real8'
    'Saw-toothed Waves.UInt1'
    'Saw-toothed Waves.UInt2'
    'Saw-toothed Waves.UInt4'
triangleItems =
    'Triangle Waves.'
    'Triangle Waves.Int1'
    'Triangle Waves.Int2'
    'Triangle Waves.Int4'
    'Triangle Waves.Money'
    'Triangle Waves.Real4'
    'Triangle Waves.Real8'
    'Triangle Waves.UInt1'
    'Triangle Waves.UInt2'
    'Triangle Waves.UInt4'

This example uses use the Real8 items from Saw-Toothed Waves and the Real8 and UInt2 items from Triangle Waves. Make a cell array of item names so that you can add all the items at once.

itmIDs = {'Saw-toothed Waves.Real8', ...
    'Triangle Waves.Real8', ...
    'Triangle Waves.UInt2'};

Step 5: Create OPC Data Access Group Objects

OPC Group objects contain many items that can be updated, logged, written to, and interacted with using a single object. In this example, a group is created in order to log data from all items simultaneously.

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

Step 6: Add Items to Groups

Once the group object has been created, you can now add items to that group. The next example adds the three items identified in Step 4, using one command. The result is a vector of daitem objects.

itm = additem(grp, itmIDs)
itm =
   OPC Item Object Array:
   Index:  Active:  ItemID:             Value:                Quality:    Ti
meStamp:
   1       on       ...hed Waves.Real8                        Bad: Ou...
   2       on       ...gle Waves.Real8                        Bad: Ou...
   3       on       ...gle Waves.UInt2                        Bad: Ou...

View a summary of the first object in the object vector.

itm(1)
ans =
Summary of OPC Data Access Item Object: Saw-toothed Waves.Real8
   Object Parameters
      Parent        : DemoGroup
      Access Rights : read/write
   Object Status
      Active        : on
   Data Parameters
      Data Type     : double
      Value         :
      Quality       : Bad: Out of Service
      Timestamp     :

Step 7: Configure OPC Toolbox Object Properties

You configure OPC Toolbox object properties using the SET command, and retrieve properties using the GET command. For this example, 2 minutes of data will be logged at 0.2 second intervals.

logDuration = 2*60;
logRate = 0.2;
numRecords = ceil(logDuration./logRate)
numRecords =
   600

You can now configure the group object to acquire this amount of data.

set(grp, 'UpdateRate',logRate, 'RecordsToAcquire',numRecords);

Step 8: Acquire OPC Server Data

To acquire the data, you issue a START function. Because you want all the data to be acquired before this example continues processing, call the WAIT function.

start(grp)
wait(grp)

Now that the data has been logged to memory, you need to retrieve it from the OPC Toolbox engine. Because this example uses the time series data, obtain the logged data into separate arrays.

[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           600x1               4800  double
  logIDs                 1x3                306  cell
  logQual              600x3             172794  cell
  logRate                1x1                  8  double
  logTime              600x3              14400  double
  logVal               600x3              14400  double
  loggingDuration        1x1                  8  double
  loggingSeconds         1x1                  8  double

Step 9: Plot the Data

You can now plot this data all on one axes. The DATETICK function converts the X-axis ticks into formatted date strings.

plot(logTime, logVal);
axis tight
datetick('x', 'keeplimits')
legend(logIDs)

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. This example annotates the plot with markers where the quality is not Good. Bad quality is marked in red, and Repeat quality is marked in orange.

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

Step 10: Clean Up

Once you have finished with the OPC Toolbox objects, you must delete them from the OPC Toolbox engine. Deleting the client object automatically deletes the group and item objects.

disconnect(da)
delete(da)

The objects are now invalid.

isvalid(grp)
ans =
     0

You should clear the objects from the workspace.

clear da grp itm
Contact sales
Trial software
E-mail this page

Get Pricing and
Licensing Options