main content

read and write current opc ua server data -凯发k8网页登录

this example shows you how to read and write data to an opc ua server.

to run this example in your matlab® session, you must install and start the prosys opc ua simulation server. for further information, see the getting started section of the industrial communication toolbox™ documentation.

create a client and connect to the server

you create client objects using the results of a query to the local discovery service using opcuaserverinfo, or directly using the host name and port number of the server you are connecting to. in this case, use the host and port number syntax.

uaclient = opcua('localhost',53530);
connect(uaclient)

find the doubledataitem, floatdataitem, and int16dataitem nodes in the staticdata namespace.

staticnode = findnodebyname(uaclient.namespace,'staticdata','-once');
dataitemsnode = findnodebyname(staticnode,'dataitems','-once');
doublenode = findnodebyname(dataitemsnode,'doubledataitem');
floatnode = findnodebyname(dataitemsnode,'floatdataitem');
int16node = findnodebyname(dataitemsnode,'int16dataitem');
nodes = [doublenode,floatnode,int16node]
nodes = 
1x3 opc ua node array:
    index       name       nsind    identifier    nodetype  children
    -----  --------------  -----  --------------  --------  --------
      1    doubledataitem  3      doubledataitem  variable  1
      2    floatdataitem   3      floatdataitem   variable  1
      3    int16dataitem   3      int16dataitem   variable  1

read values from nodes

use readvalue to read the current value of a node. you can query the value, the timestamp when the value was updated, and the quality associated with the value when written.

[v,t,q] = readvalue(uaclient,nodes)
v =
  3×1 cell array
    {[0]}
    {[0]}
    {[0]}
t = 
  3×1 datetime array
   19-mar-2019 02:52:35
   19-mar-2019 02:52:35
   19-mar-2019 02:52:35
q = 
opc ua quality id:
	'good'
	'good'
	'good'

when you read from multiple nodes, the values are returned as a cell array. the class of the data on the server is preserved as much as possible.

valclasses = cellfun(@class,v,'uniformoutput',false)
valclasses =
  3×1 cell array
    {'double'}
    {'single'}
    {'int16' }

the timestamp is returned as a matlab® datetime variable. it represents the time when the source provided the value to the server.

t
t = 
  3×1 datetime array
   19-mar-2019 02:52:35
   19-mar-2019 02:52:35
   19-mar-2019 02:52:35

the quality is returned as an opc ua quality, which displays as a text description.

q
q = 
opc ua quality id:
	'good'
	'good'
	'good'

you can interrogate the quality to determine characteristics of the returned quality. in this example, the quality is good.

isgood(q)
ans =
  3×1 logical array
   1
   1
   1

the value is not interpolated, but is a raw value (stored by the server directly from the sensor).

interpolated = isinterpolated(q)
raw = israw(q)
interpolated =
  3×1 logical array
   0
   0
   0
raw =
  3×1 logical array
   1
   1
   1

write data to nodes

you can write data to any scalar node. when you write to multiple nodes, you must pass a cell array of values, one for each node to be written.

newvalues = {12,65,-4};
writevalue(uaclient,nodes,newvalues);

to verify that the values were written correctly, and retrieve the value again.

servervalues = readvalue(uaclient,nodes)
servervalues =
  3×1 cell array
    {[12]}
    {[65]}
    {[-4]}

you can update values directly within the cell array and write them back to the server.

servervalues{2} = servervalues{2}   1;
writevalue(uaclient,nodes,servervalues);

read and write values with a single node

when working with a single node, you receive and can pass and the value directly, without using a cell array.

dblvalue = readvalue(uaclient, doublenode)
writevalue(uaclient, doublenode, dblvalue 15.6)
newdbl = readvalue(uaclient, doublenode)
dblvalue =
    12
newdbl =
   27.6000

reading and writing to nodes directly

you can write and read directly from the node variable, as long as that node was created from the client (using the namespace property or browsenamespace) or you passed a client to the opcuanode function when creating your node variable.

[vals,ts,qual] = readvalue(nodes)
writevalue(nodes,v)
vals =
  3×1 cell array
    {[27.6000]}
    {[     66]}
    {[     -4]}
ts = 
  3×1 datetime array
   19-mar-2019 02:52:36
   19-mar-2019 02:52:36
   19-mar-2019 02:52:36
qual = 
opc ua quality id:
	'good'
	'good'
	'good'

disconnect from server

when you have finished communicating with the server, disconnect the client from the server. this is also automatically performed when the client variable goes out of scope in matlab.

disconnect(uaclient);
网站地图