read and write data on opc da server
introduction to reading and writing
you can exchange data with the opc da server using individual items, or using the
dagroup
object to perform the operation on multiple items. the reading
and writing operation can be performed synchronously, so that your
matlab® session waits for the operation to complete, or
asynchronously, allowing your matlab session to continue processing while the operation takes place in the
background.
read data from an item
you can read data from any item that is associated with a connected client. when you perform the read operation on an item, the server will return information about the server item associated with that item id. the read operation can be performed synchronously or asynchronously:
use synchronous read operations describes how to perform synchronous read operations. synchronous read operations can request data from the server's cache, or directly from the device.
use asynchronous read operations describes how to perform asynchronous read operations.
use synchronous read operations
a synchronous read operation means that matlab waits for the server to return data from a read request before continuing processing. the data returned by the server can come from the server's cache, or you can request that the server read values from the device that the server item refers to.
you use the function to perform synchronous read
operations, passing the daitem
object associated with the server item
you want to read. if the read operation is successful, the data is returned in a structure
containing information about the read operation, including the value of the server item,
the quality of that value, and the time that the server obtained that value. the item's
value
, quality
and timestamp
properties are also updated to reflect the values obtained from the read operation.
the following example creates an opcda
client object and configures
a group with one item, 'random.real8'
. a synchronous read operation is
then performed on the item.
da = opcda('localhost','matrikon.opc.simulation.1'); connect(da); grp = addgroup(da); itm1 = additem(grp,'random.real8'); r = read(itm1)
r = itemid: 'random.real8' value: 4.3252e 003 quality: 'good: non-specific' timestamp: [2004 3 2 9 50 26.6710] error: ''
specify the source of the read operation. by default, a synchronous read operation will return data from the opc server's
cache. by reading from the cache, you do not have to wait for a possibly slow device to
provide data to the server. you can specify the source of the synchronous read operation
as the second parameter to the read
function. if the source is
specified as 'device'
, the server will read a value from the device,
and return that value to you (as well as updating the server cache with that
value).
note
reading from the device may be slow. if the read operation generates a time-out
error, you may need to increase the value of the timeout
property
of the opcda
client object associated with the group or item in
order to support synchronous reads from the device.
the following example reads data from the device associated with
itm1
.
r = read(itm1,'device')
r = itemid: 'random.real8' value: 9.1297e 003 quality: 'good: non-specific' timestamp: [2004 3 2 10 8 20.2650] error: ''
read from the cache with inactive items. in order to reduce communication traffic and speed up data access, opc servers do
not store all server item values in their cache. only those server items that are
active will be stored in the server cache. therefore, synchronous
read operations from the cache on an inactive item will return data that may not
correspond to the current device value. if you attempt to read data from an inactive
item using the read
function, and do not specify
'device'
as the source, the quality
will be set
to 'bad: out of service'
.
you control the active status of an item using the property.
the following example sets the active
property of the item to
'off'
and attempts to read from the cache.
itm1.active = 'off';
r = read(itm1)
warning: one or more items is inactive. (type "warning off opc:read:iteminactive" to suppress this warning.) r = itemid: 'random.real8' value: 8.4278e 003 quality: 'bad: out of service' timestamp: [2004 3 2 10 17 19.9370] error: ''
use asynchronous read operations
an asynchronous read operation creates a request to read data, and then sends that request to the server. once the request has been accepted, matlab continues processing the next instruction without waiting to receive any values from the server. when the data is ready to be returned, the server sends the data back to matlab by generating a read async event. matlab will handle that event as soon as it is able to perform that task.
asynchronous read operations always return data from the device.
by using an asynchronous read operation, you can continue performing tasks in matlab while the value is being read from the device, and then process the returned value when the server is able to provide it back to matlab.
you perform asynchronous read operations using the readasync
function, passing the daitem
object that you want to read from. if
successful, the function will return a transaction id, a unique
identifier for that asynchronous transaction. you can use that transaction id to identify
the read operation when it is returned through the read async event.
when an asynchronous read operation is processed in matlab, the item's value
, quality
and
timestamp
properties are also updated to reflect the values obtained
from the asynchronous read operation.
the following example of using an asynchronous read operation uses the default
callback for a read async event. the default callback is set to the
opccallback
function, which displays information about the event in
the command line.
tid = readasync(itm1)
tid = 3
the transaction id for this operation is 3. a little while later, the default callback function displays the following information at the command line.
opc readasync event occurred at local time 10:44:49 transaction id: 3 group name: group0 1 items read.
you can change the read async event callback function by setting the
readasyncfcn
property of the dagroup
object.
write data to an item
you can write data to individual items, or to groups of items. this section describes
how to write data to individual items. see read and write multiple values for information on
using dagroup
objects to write data to multiple items.
you can write data to an opc server using a synchronous write operation, in which case matlab will wait for the server to acknowledge that the write operation succeeds, or using an asynchronous write operation, in which case matlab is free to continue performing other tasks while the write operation takes place. because write operations always apply directly to the device, a synchronous write operation may take a significant amount of time, particularly if the device that you are writing to has a slow connection to the opc server.
use synchronous write operations
you use the function to perform synchronous write
operations. the first argument is the daitem
object that represents the
server item you want to write to. the second argument is the value that you want to write
to that server item. the write
function does not return any results,
but will generate an error if the write operation is not successful.
the following example creates an item with item id 'bucket
brigade.real8'
and writes the value 10.34
to the item. the
value is then read using a synchronous read operation.
itm2 = additem(grp,'bucket brigade.real8'); write(itm2, 10.34) r = read(itm2,'device')
you do not need to ensure that the data type of the value you are writing, and the
data type of the daitem
object, are the same. industrial communication toolbox™ relies on the server to perform the conversion from the data type you
provide, to the data type required for that server item. for information on how the
toolbox handles different data types, see .
use asynchronous write operations
an asynchronous write operation creates a request to write data, and then sends that request to the server. once the request has been accepted, matlab continues processing the next instruction without waiting for the data to be written. when the write operation completes on the server, the server notifies matlab that the operation completed by generating a write async event containing information on whether the write operation succeeded, and an error message if applicable. matlab will handle that event as soon as it is able to perform that task.
you use the function to write values to the
server asynchronously. the first argument is the daitem
object that
represents the server item you want to write to. the second argument is the value you want
to write to that server item. the return value is the transaction id
of the operation. you can use the transaction id to identify the write operation when it
is returned through the write async event.
the following example uses asynchronous operations to write the value
57.8
to the item 'bucket brigade.real8'
created
earlier.
tid = writeasync(itm2, 57.8)
tid = 4
a while later, the standard callback (opccallback
) will display the
results of the write operation to the command line.
opc writeasync event occurred at local time 11:15:27 transaction id: 4 group name: group0 1 items written.
you can change the write async event callback function by setting the
writeasyncfcn
property of the dagroup
object.
read and write multiple values
when you use the read and write operation on a single daitem
object,
you read or write a single value per transaction. industrial communication toolbox allows you to perform one operation to read multiple item values, or to write
multiple values. you can also use a dagroup
object to read and write
values using all items in the group, or you can perform read and write operations on item
object vectors.
a daitem
object vector is a single variable in the matlab workspace containing more than one daitem
object. you can
construct item vectors using any of the standard concatenation techniques available in
matlab. see for information on creating and working with toolbox object vectors.
when you perform any read or write operation on a dagroup
object, it
is the equivalent of performing the operation on the item
property of
that group, which is a daitem
object vector representing all items that
are contained within the dagroup
object.
the following sections describe how to perform read and write operations on multiple items:
read multiple values describes how to read multiple values from an item vector or
dagroup
object.write multiple values describes how to write multiple values to an item vector or
dagroup
object.error handling for multiple item read and write operations explains errors when performing read and write operations on multiple objects.
read multiple values
the following sections describe how synchronous read operations and asynchronous read operations behave for multiple items.
synchronous read operations. when you read multiple values using the function, the returned value will be a structure array. each element of the structure will contain the same fields. one of the fields is the item id that the information in that element of the structure refers to.
the following example performs a synchronous read operation on the
dagroup
object created in the previous examples in this
section.
r = read(grp)
r = 2x1 struct array with fields: itemid value quality timestamp error
to display the first record in the structure array, use indexing into the structure.
r(1)
ans = itemid: 'random.real8' value: 3.7068e 003 quality: 'good: non-specific' timestamp: [2004 3 2 11 49 52.5460] error: ''
to display all values of a particular field, you can use the list generation syntax in matlab. enclosing that list in a cell array groups the values into one variable.
{r.value}
ans = {3.7068e 003 10}
asynchronous read operations. when you read multiple values using the function, the return value is still a single transaction id. the
multiple values will be returned in the read async event structure passed to the
readasyncfcn
callback. for information on the structure of the read
async event, see .
write multiple values
when you perform a write operation on multiple items you need to specify multiple values, one for each item you are writing to. these multiple values must be in a cell array, because the data types for each value might be different.
note
even if you are using the same data type for every value being written to the
dagroup
object or daitem
object vector, you must
still use a cell array to specify the individual values. use the function to convert numeric arrays to cell arrays.
the following example writes values to a dagroup
object containing
two items.
write(grp, {1.234, 5.43})
error handling for multiple item read and write operations
when reading and writing with multiple items, an error generated by performing the operation on one item will not automatically generate an error in matlab. the following rules apply to reading and writing with multiple items:
if all items fail the operation, an error will be generated. the error message will contain specific information for each item about why the item failed.
if some items fail but some succeed, the operation does not error, but generates a warning, listing which items failed and the reason for failure.
note that for asynchronous read and write operations, items may fail early (during the request for the operation) or late (when the information is returned from the server). if any items fail late, an error event will be generated in addition to the read async event or write async event.