Skip to main content

OPC UA

opcua module implements communication over OPC UA protocol.

opcua.client()#

-- @param url string OPC UA connection string
-- @return table OPC UA client
function opcua.client(url)
end

Creates a new OPC UA client instance with the given connection string.

The underlying TCP connection is established on-demand when the first request is initiated, so this method does not return any error.

Example#

local client = opcua.client('opc.tcp://opcuademo.sterfive.com:26543')

client Object#

client:read()#

-- @param request table Read request object
-- @return table, string Response object and error string
function client:read(request)
end

Performs a read request initiated with opcua.read_request() method. Returns two values: results and error. When the performed request failed (e.g. network timeout) error will contain non-nil string. If any response is obtained, error will be nil.

results is a list with results for every requested value. The number of results is equal to the number of values in the read request. Every result contain an error and value fields. error is nil if read succeeded or contains a string with error if it failed.

OPC UA types will be automatically converted to appropriate Lua types like number, string or table.

Example#

-- Initiate request object that will read two values
local request = opcua.read_request({ 'ns=1;i=1678', 'ns=1;i=1348' })
-- Send request
local results, err = client:read(request)
if err then
enapter.log('read failed: '..err, 'error')
else
for i, r in ipairs(results) do
if r.error then
enapter.log('failed: '..r.error, 'error')
else
enapter.log('value #'..i..': '..tostring(r.value))
end
end
end

client:write()#

-- @param request table Write request object
-- @return table, string Response object and error string
function client:write(request)
end

Performs a write request initiated with opcua.write_request() method. Returns two values: results and error. When the performed request failed (e.g. network timeout) error will contain non-nil string. If any response is obtained, error will be nil.

results is a list with results for every written value. The number of results is equal to the number of values in the write request. Every result contain an error field. error is nil if write succeeded or contains a string with error if it failed.

Example#

-- Initiate request object that will write three values of different types
local request = opcua.write_request({
{node_id = 'ns=2;s=Some.Double', value = 12.5},
{node_id = 'ns=2;s=Some.String', value = 'test'},
{node_id = 'ns=2;s=Some.Number', value = 59},
})
-- Send request
local results, err = client:write(request)
if err then
enapter.log('write failed: '..err, 'error')
else
for i, r in ipairs(results) do
if r.error then
enapter.log('failed: '..r.error, 'error')
else
enapter.log('write #'..i..' succeeded')
end
end
end

client:call_method()#

-- @param request table Method request object
-- @return table, string Return values and error string
function client:call_method(request)
end

Performs a method call request initiated with opcua.method_request() method. Returns two values: values (an array of values returned by method call) and error. When the performed request failed (e.g. network timeout) error will contain non-nil string. If any response is obtained, error will be nil.

Methods in OPC UA may return several values, thus returned values is always an array (even of one element). Since the method call may return a nil values, it's advisable to access return values using positional array access (e.g. values[1]) due to Lua specifics in working with nil values (iteration using ipairs will stop on the first nil value and will ignore the rest).

Example#

-- Initiate request object object node, method node, and method arguments
local request = opcua.method_request(
'ns=2;s=Some',
'ns=2;s=Some.Multiply',
{2, 2}
)
local values, err = client:call_method(request)
if err then
enapter.log('method failed: '..err, 'error')
else
enapter.log('result: '..tostring(values[1]))
end

Request Objects#

opcua.read_request()#

-- @param node_ids table Node IDs to read
-- @return table Request object
function opcua.read_request(node_ids)
end

Creates a new read request instance with the given node IDs to read.

Example#

local request = opcua.read_request({
'ns=1;i=1678',
'ns=2;s=Some.Value'
})

opcua.write_request()#

-- @param write_pairs table ID-value pairs to write
-- @return table Request object
function opcua.write_request(write_pairs)
end

Creates a new write request instance. It accepts the write_pairs table where keys are node IDs and values are values to write.

Lua types are converted to appropriate OPC UA types according to type conversion logic.

Example#

local request = opcua.write_request({
{node_id = 'ns=2;s=Some.Double', value = 12.5},
{node_id = 'ns=2;s=Some.String', value = 'test'},
{node_id = 'ns=2;s=Some.Number', value = 59},
{node_id = 'ns=2;s=Some.Guid', value = opcua.types.guid('5C97A069-F8A0-48AC-A66F-446475ABB704')},
{node_id = 'ns=2;s=Some.NodeId', value = opcua.types.node_id('ns=2;s=ServerStatus')},
{node_id = 'ns=2;s=Some.StatusCode', value = opcua.types.status_code(12)},
})

opcua.method_request()#

-- @param object_id string Node ID of object that contains method
-- @param method_id string Node ID of method to call
-- @param args table Array of method arguments
-- @return table Request object
function opcua.method_request(object_id, method_id, args)
end

Creates a new method call request with object ID, method ID, and method arguments.

Lua types are converted to appropriate OPC UA types according to type conversion logic.

Example#

local request = opcua.method_request(
'ns=2;s=Some', -- object that contains method
'ns=2;s=Some.Multiply', -- method node
{2, 2} -- an array with method arguments
)

Type Conversion#

When performing write or method call requests some Lua types are automatically converted to appropriate OPC UA types, others should be explicitly given.

Supported node value types:

  • numeric Lua values are automatically converted to appropriate subtypes of OPC UA Number,
  • string Lua values are automatically converted to OPC UA String,
  • opcua.types.guid() initiates OPC UA GUID values,
  • opcua.types.status_code() initiates OPC UA StatusCode values,
  • opcua.types.node_id() initiates OPC UA NodeId values.
Hardware diversity is welcome. Integrate any device into a unified energy network.
© 2021 Enapter
Developer toolkit
DocumentationReference
Community
GithubTindie