Reading and Writing Data

Reading Attributes

Reading attributes is done with the read method. This method exists for both the client and the server.

Read parameters

NodeId | NodeID[]:

(NodeId) A signle identifier of a node or an array of node identifiers. it will try to read all attributes of a node. For existing attributes will be returned value, for absent attrubutes of a noe will be returned status code BadAttributeIdInvalid.

NodesToRead[]:

An array of tables with the following fields:

NodeID (NodeId) The identifier of a node

AttributeId (uint32) Node Attribute Attribute of node to read

With these parameters you can read exact attributes of a node, for example, the value of a variable. values of several variable nodes.

Read Attributes Of One Node

local ObjectsFolder = "i=85"
local TypesFolder = "i=86"

-- Read all possible attributes of the any node
-- For part of attributes will be returned a valus
-- and for part of attributes will be returned a status code BadAttributeIdInvalid
resp,err = client:read(ObjectsFolder)
for i,result in ipairs(resp.Results) do
  if result.StatusCode == 0 then
    ua.Tools.printTable("result", result.Value)
  else
    trace(string.format("Read attributes error: 0x%X", result.StatusCode))
  end
end

Full source

Read Attributes Of Several Nodes

local ObjectsFolder = "i=85"
local TypesFolder = "i=86"

-- Read all possible attributes of several nodes
-- For part of attributes will be returned a valus
-- and for part of attributes will be returned a status code BadAttributeIdInvalid
resp,err = client:read({ObjectsFolder, TypesFolder})
for i,result in ipairs(resp.Results) do
  if result.StatusCode == 0 then
    ua.Tools.printTable("result", result.Value)
  else
    trace(string.format("Read value error: 0x%X", result.StatusCode))
  end
end

Full source

Read Variables Values

Variables in the OPCUA holds valuable data in the attribute Value. It is possible to read several values at once.

local Server_ServerStatus_BuildInfo_SoftwareVersion = "i=2260"

local readParams = {
  NodesToRead = {
    {
      NodeId = Server_ServerStatus_CurrentTime,
      AttributeId = ua.Types.AttributeId.Value
    },
    {
      NodeId = Server_ServerStatus_BuildInfo_SoftwareVersion,
      AttributeId = ua.Types.AttributeId.Value
    },
  }
}

resp,err = client:read(readParams)
for i,result in ipairs(resp.Results) do
  if result.StatusCode == 0 then
    ua.Tools.printTable("result", result.Value)
  else
    trace(string.format("Read value '%s' error: 0x%X", nodes.NodesToRead[i].NodeId, result.StatusCode))
  end
end

Full source

Writing Attributes

Changing the values of attributes is done with the Write method.

Write Parameters

NodesToWrite

Array of nodes and attributes to write

NodeId

NodeId

The identifier of a node

AttributeId

uint32

Node Attribute

Value

DataValue

New value of the attribute

Write example

local Server_ServerStatus_StartTime = "i=2257"

local nodes = {
    NodesToWrite = {
      {
        NodeId = ua.NodeIds.Server_ServerStatus_StartTime,
        AttributeId = AttributeId.Value,
        Value = {   -- DataValue
          Value = { -- Variant
            DateTime = 0.0
          }
        }
      }
    }
  }

local response = svc:write(nodes)
if response.Results[1] ~= ua.StatusCode.Good then
  print(string.format("Changing attribute value failed: 0x%X", response.Results[1]))
  return
end

Node Attribute

Every node in the address space is represented by a set of attributes.

Base node attribute numbers

Number

Attribute name

Data type

1

NodeId

nodeID

2

NodeClass

uint32

3

BrowseName

qualifiedName

4

DisplayName

localizedText

5

Description

localizedText

6

WriteMask

uint32

7

UserWriteMask

uint32

24

RolePermissions

structure (TBD)

25

UserRolePermissions

structure (TBD)

26

AccessRestrictions

uint16

Object node attribute numbers

Number

Attribute name

Data type

Base attributes

12

EventNotifier

byte

Object Type node attribute numbers

Number

Attribute name

Data type

Base attributes

8

IsAbstract

boolean

Variable node attribute numbers

Number

Attribute name

Data type

Base attributes

13

Value

DataValue

14

DataType

NodeID

15

Rank

int32

16

ArrayDimensions

int32[]

17

AccessLevel

byte

18

UserAccessLevel

byte

19

MinimumSamplingInterval

double

20

Historizing

boolean

27

AccessLevelEx

uint32

Variable Type node attribute numbers

Number

Attribute name

Data type

Base attributes

8

IsAbstract

boolean

13

Value

DataValue

14

DataType

NodeID

15

Rank

int32

16

ArrayDimensions

int32[]

Reference Type node attribute numbers

Number

Attribute name

Data type

Base attributes

8

IsAbstract

boolean

9

Symmetric

boolean

10

InverseName

localizedText

Data Type node attribute numbers

Number

Attribute name

Data type

Base attributes

8

IsAbstract

boolean

23

DataTypeDefinition

nodeID

Method node attribute numbers

Number

Attribute name

Data type

Base attributes

21

Executable

boolean

22

UserExecutable

boolean

View node attribute numbers

Number

Attribute name

Data type

Base attributes

11

ContainsNoLoops

boolean

12

EventNotifier

byte

List of all attribute numbers

Number

Attribute name

Data type

1

NodeId

nodeID

2

NodeClass

uint32

3

BrowseName

qualifiedName

4

DisplayName

localizedText

5

Description

localizedText

6

WriteMask

uint32

7

UserWriteMask

uint32

8

IsAbstract

boolean

9

Symmetric

boolean

10

InverseName

localizedText

11

ContainsNoLoops

boolean

12

EventNotifier

byte

13

Value

DataValue

14

DataType

NodeID

15

Rank

int32

16

ArrayDimensions

int32[]

17

AccessLevel

byte

18

UserAccessLevel

byte

19

MinimumSamplingInterval

double

20

Historizing

boolean

21

Executable

boolean

22

UserExecutable

boolean

23

DataTypeDefinition

nodeID

24

RolePermissions

structure (TBD)

25

UserRolePermissions

structure (TBD)

26

AccessRestrictions

uint16

27

AccessLevelEx

uint32