Read and Write Data to an OPC Data Access Server

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


Connect to Server and Create Objects

Create an opcda client and connect that client to the OPC server.

da = opcda('localhost', 'Matrikon.OPC.Simulation.1');

Add a group to the client, and an item to the group.

grp = addgroup(da);
itm1 = additem(grp, 'Random.Real8');

Perform Synchronous Read Operations

The default read operation gets values from the server cache.

r = read(itm1)
r = 
       ItemID: 'Random.Real8'
        Value: 0
      Quality: 'Bad: Out of Service'
    TimeStamp: [2012 3 28 5 38 18.6930]
        Error: ''

Specify the source of the read as 'device' to force the server to read a value from the device. This process may take long if the OPC server is on the network or the device takes some time to produce a value.

r = read(itm1, 'device')
r = 
       ItemID: 'Random.Real8'
        Value: 20.8848
      Quality: 'Good: Non-specific'
    TimeStamp: [2012 3 28 5 38 18.7430]
        Error: ''

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 to the item.

write(itm2, 10)

Read the value back into MATLAB®.

r = read(itm2, 'device')
r = 
       ItemID: 'Bucket Brigade.Real8'
        Value: 10
      Quality: 'Good: Non-specific'
    TimeStamp: [2012 3 28 5 38 18.7830]
        Error: ''

Read From Multiple Items

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

r = read(grp)
r = 
2x1 struct array with fields:

Extract individual item information using indexing.

ans = 
       ItemID: 'Random.Real8'
        Value: 20.8848
      Quality: 'Good: Non-specific'
    TimeStamp: [2012 3 28 5 38 18.7430]
        Error: ''

Use concatenation of MATLAB list creation operations to extract multiple values.

itmIDs = {r.ItemID}
vals = [r.Value]
itmIDs = 
    'Random.Real8'    'Bucket Brigade.Real8'
vals =
   20.8848   10.0000

Write to Multiple Items

Write to multiple items, passing the values for the items in the group as a cell array.

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 operation.' 

This particular example returns 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.

r = read(itm2, 'device')
r = 
       ItemID: 'Bucket Brigade.Real8'
        Value: 5.4320
      Quality: 'Good: Non-specific'
    TimeStamp: [2012 3 28 5 38 18.8230]
        Error: ''

Clean Up

Disconnect from the server and delete the client object.


Deleting the client object automatically deletes the group and item objects.

Was this topic helpful?