Exporting Device Data

The main purpose with an OPC-UA server is to provide access to real world objects. External information is stored in the address space in the form of variable nodes. Variable nodes have value attributes. The following section shows how to connect attributes with real world objects.

Value Callback

The value callback is a function and is used for both reading and writing.

function callback(nodeId, value)
end

Write Callback Parameters

nodeId of type Node Identifiers (NodeID)

The Node ID of the node to modify.

value of type :DataValue

For write The new variable value set by the client. Nil for read operation

return DataValue

Data from device for read operation. Ignored for write operation.

Read Callback Parameters

nodeId of type Node Identifiers (NodeID)

The Node ID of the node to read.

return of type Variant

The return value is returned to the client.

Error reporting

To report an error, the callback should raise a LUA error with a suitable OPC-UA status code.

function callback(nodeId, newValue)
  if newValue ~= nil then
    -- writing data
    ua.Tools.printTable("newValue", newValue, trace)
    ua.Tools.printTable("deviceData", deviceData, trace)

    if value.Value.Int32 == nil then
      error(ua.StatusCode.BadInvalidArgument)
    end

    deviceData = newValue
  else
    -- reading data
    ua.Tools.printTable("read deviceData", deviceData, trace)
    return deviceData
  end
end

Full source

Custom data source example

server:setVariableSource(dataNodeId, callback)

-- Read data from device
local attrs = server:read(dataNodeId)
ua.Tools.printTable("dataNodeId", attrs)

local nodes = {
  NodesToWrite = {
    {
      NodeId = dataNodeId,
      AttributeId = ua.Types.AttributeId.Value,
      Value = {   -- DataValue
        Value = { -- Variant
          Int32 = 0
        }
      }
    }
  }
}

local results = server:write(nodes)

Full source