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

Synchronous Reading and Writing

This example demonstrates how to use the OPC Toolbox™ synchronous read and write operations to exchange data with an OPC server.

Contents

Step 1: Connect to Server and Create Objects

In order to interact with an OPC server, you must create an opcda client and connect that client to the OPC server.

da = opcda('localhost', 'Matrikon.OPC.Simulation.1');
connect(da);
grp = addgroup(da);
itm1 = additem(grp, 'Random.Real8');

Step 2: Perform Synchronous Read Operations

The default read operation gets values from the server cache.

r = read(itm1)
r =
       ItemID: 'Random.Real8'
        Value: 9.8067e+003
      Quality: 'Bad: Out of Service'
    TimeStamp: [2006 11 21 8 18 29.9060]
        Error: ''

To read from the device (which may take long if the OPC server is on the network and not on the local host) specify the source of the read as 'device'.

r = read(itm1, 'device')
r =
       ItemID: 'Random.Real8'
        Value: 4.5621e+003
      Quality: 'Good: Non-specific'
    TimeStamp: [2006 11 21 8 20 54.6090]
        Error: ''

Step 3: Perform Synchronous Write Operations

Add a writable item to the group.

itm2 = additem(grp, 'Bucket Brigade.Real8')
itm2 =
Summary of OPC Data Access Item Object: Bucket Brigade.Real8
   Object Parameters
      Parent        : Group0
      Access Rights : read/write
   Object Status
      Active        : on
   Data Parameters
      Data Type     : double
      Value         :
      Quality       : Bad: Out of Service
      Timestamp     :

Write the value 10.

write(itm2, 10)

Read the value back into MATLAB®.

r = read(itm2)
r =
       ItemID: 'Bucket Brigade.Real8'
        Value: -0.5440
      Quality: 'Good: Non-specific'
    TimeStamp: [2006 11 21 8 15 55.5150]
        Error: ''

Step 4: Read From Multiple Items

You can read data from multiple items using the group object.

r = read(grp)
r =
2x1 struct array with fields:
    ItemID
    Value
    Quality
    TimeStamp
    Error

Each element of the returned structure array contains information about each item. Extract the item information using indexing.

r(1)
ans =
       ItemID: 'Random.Real8'
        Value: 4.5621e+003
      Quality: 'Good: Non-specific'
    TimeStamp: [2006 11 21 8 20 54.6090]
        Error: ''

To obtain the values, use concatenation of MATLAB list creation operations.

itmIDs = {r.ItemID}
vals = [r.Value]
itmIDs =
    'Random.Real8'    [1x20 char]
vals =
  1.0e+003 *
    4.5621   -0.0005

Step 5: Write to Multiple Items

You can also write to multiple items. However, you must pass the values for the items in the group as a cell array. This particular example will return a warning, since the first item will not allow you to write data to the item. However, the second item will have the value 5.432 written.

write(grp, {1.234, 5.432})
Warning: One or more items could not be written.
    Random.Real8 returned 'The item's access rights do not allow the operati
on.'

Read the value of the written item.

r = read(itm2)
r =
       ItemID: 'Bucket Brigade.Real8'
        Value: -0.5440
      Quality: 'Good: Non-specific'
    TimeStamp: [2006 11 21 8 15 55.5150]
        Error: ''

Step 6: 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)
clear da grp itm1 itm2
Contact sales
Trial software
E-mail this page

Get Pricing and
Licensing Options