OPC UA
opcua
module implements communication over OPC UA protocol.
opcua.client()
#
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.
#
Exampleclient
Object#
client:read()
#
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
.
#
Exampleclient:write()
#
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.
#
Exampleclient:call_method()
#
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#
Request Objectsopcua.read_request()
#
Creates a new read request instance with the given node IDs to read.
#
Exampleopcua.write_request()
#
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.
#
Exampleopcua.method_request()
#
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#
Type ConversionWhen 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.