opcua
OPC UA is a data exchange standard for safe, reliable, manufacturer-independent, and platform-independent industrial communication. It enables secure data exchange between hardware platforms from different vendors and across operating systems. The DolphinDB opcua plugin enables data transfer between DolphinDB and OPC UA servers.
Installation (with installPlugin
)
Required server version: DolphinDB 2.00.10 or higher
Supported OS: Windows x86-64 and Linux x86-64
Installation Steps:
(1) Use listRemotePlugins to check plugin information in the plugin repository.
Note: For plugins not included in the provided list, you can install through precompiled binaries or compile from source. These files can be accessed from our GitHub repository by switching to the appropriate version branch.
login("admin", "123456")
listRemotePlugins()
(2) Invoke installPlugin for plugin installation
installPlugin("opcua")
(3) Use loadPlugin to load the plugin before using the plugin methods.
loadPlugin("opcua")
Note: Before using the plugin methods, load the plugin with loadPlugin ("/path/to/PluginOPCUA/PluginOPCUA.txt")
.
Test Case
- Online Server:
- Server URL: opc.tcp://118.24.36.220:62547/DataAccessServer
- Purpose: With the online service endpoint, users can test the plugin's connection, reading/writing nodes, subscribing, and other functions.
- Local Server:
- Download: prosys opc-ua-simulation-server
- Purpose: By following the user manual, users can specify endpoints, encryption policies, user tokens, manage certificates, etc. It can be used to test the plugin's encrypted communication functions.
Method References
getOpcServerList
Syntax
getOpcServerList(serverUrl)
Parameters
- serverUrl: A string indicating the server URL, such as opc.tcp://127.0.0.1:53530/OPCUA/SimulationServer/.
Details
Get a list of OPC servers. It returns a table with five columns:
- ServerUri: The unique identifier for the server.
- ServerName: The server name.
- ProductUri: The unique identifier for the product.
- Type: The server type.
- DiscoveryUrl: URL(s) for the discovery endpoints. Separate multiple URLs with semicolon ";".
Examples
opcua::getOpcServerList(serverUrl);
getOpcEndPointList
Syntax
getOpcEndPointList(serverUrl)
Parameters
- serverUrl: A string indicating the server URL, such as opc.tcp://127.0.0.1:53530/OPCUA/SimulationServer/.
Details
Get a list of OPC server endpoints. It returns a table with five columns:
- endpointUrl: The endpoint URL.
- transportProfileUri: The unique identifier of the transport configuration.
- securityMode: The security mode.
- securityPolicyUri: The unique identifier of security policy.
- securityLevel: The security level.
Examples
opcua::getOpcEndPointList(serverUrl);
connect
Syntax
connect(endPointUrl,clientUri,[userName],[userPassword],[securityMode],[securityPolicy],[certificatePath],[privateKeyPath])
Parameters
- endPointUrl: A string indicating the endpoint URL to which the client attempted to connect.
- clientUri: A string indicating the unique identifier of client. If certificate is specified, the URI must be consistent.
- userName (optional): A string indicating the username. It can be omitted if server is not set.
- userPassword (optional): A string indicating the user password.
- securityMode (optional): A string indicating the security mode for connection. It must be "None" (default), "Sign", or "SignAndEncrypt".
- securityPolicy (optional): A string indicating the security algorithm for connection. It must be "None" (default), "Basic256", "Basic128Rsa15", or "Basic256Sha256". If using "Basic256", "Basic128Rsa15", or "Basic256Sha256" encryption, certificate and privateKey must also be specified. The certificate and privateKey under ./open62541/cert/ directory can be used, or custom certificate and key can be used (tools of open62541 can be used to generate certificates).
- certificatePath (optional): A string indicating the certificate path. It can be omitted if encrypted communication is not used.
- privateKeyPath (optional): A string indicating the private key path. It can be omitted if encrypted communication is not used.
Details
Connect to an OPC server. It returns a connection that can be explicitly closed by calling the close
function.
If encrypted communication is enabled, a trusted certificate must be specified on the server. If using encryption with the Prosys local simulation server, a user needs to be added in the Users interface with a username and password. For example, for username "user1" and password "123456", trust open62541server@localhost
in the Certificates interface.
Examples
connection = opcua::connect(serverUrl,"myClient");
connection = opcua::connect(serverUrl,"myClient","user1","123456");
connection = opcua::connect(serverUrl,"urn:opcua.client.application","user1","123456","SignAndEncrypt","Basic128Rsa15","./open62541/cert/client_cert.der","./open62541/cert/client_key.der");
browseNode
Syntax
browseNode(connection)
Parameters
- connection: The return value of method
connect
.
Details
View all nodes. It returns a table with 2 columns, nodeNamespace and nodeIdString.
Examples
connection = opcua::connect(serverUrl,"myClient");
opcua::browseNode(connection);
readNode
Syntax
readNode(connection, nodeNamespace, nodeIdString, [table])
Parameters
- connection: The return value of the method
connect
. - nodeNamespace: An INT scalar or vector indicating the node namespace(s).
- nodeIdString: A STRING scalar or array indicating the node ID(s).
- table (optional): A table of an array of tables (whose size matches the number of nodes) used to store the accessed node values.
- If a table is specified, all values are inserted into the table.
- If multiple tables are specified, the value of each node is inserted into the corresponding table.
- If no table is specified, the method returns a table that stores the node values.
Details
Read the node value synchronously. An OPC connection must be established first before using the method. The result returned for each node includes four values:
- node id: The node ID, i.e., nodeNamespace and nodeIdString concatenated by ":".
- value: The node value.
- timestamp: The source timestamp in local time.
- status: The status of the node value.
Examples
t1 = table(200:0,`nodeID`value`timestamp`status, [SYMBOL, INT, TIMESTAMP, SYMBOL])
opcua::readNode(conn, 3, "Counter",t1)
opcua::readNode(conn, 3, ["Counter","Expression","Random","Sawtooth"],t1)
t2 = table(200:0,`nodeID`value`timestamp`status`nodeID`value`sourceTimestamp`status,[SYMBOL, INT, TIMESTAMP, SYMBOL, SYMBOL, INT, TIMESTAMP, SYMBOL])
opcua::readNode(conn, 1, ["test1","test4"],t2)
t3 = table(200:0,`nodeID`value`timestamp`status, [SYMBOL, INT, TIMESTAMP, SYMBOL])
t4 = table(200:0,`nodeID`value`timestamp`status, [SYMBOL, INT, TIMESTAMP, SYMBOL])
t5 = table(200:0,`nodeID`value`timestamp`status, [SYMBOL, INT, TIMESTAMP, SYMBOL])
opc::readNode(conn, 1, ["test1","test4", "test9"],[t3,t4,t5])
writeNode
Syntax
writeNode(connection, nodeNamespace, nodeIdString, value)
Parameters
- connection: The return value of method
connect
. - nodeNamespace: An INT scalar or array indicating the node namespace(s).
- nodeIdString: A STRING scalar or array indicating the node ID(s).
- value: The value(s) to write to the node(s).
Details
Write value(s) to one or multiple nodes synchronously. An exception will be thrown if the write type is incorrect.
Examples
opcua::writeNode(conn,1,"testwrite.test1",1)
opcua::writeNode(conn,1,["testwrite.test5","testwrite.test6"],[33,11])
opcua::writeNode(conn,1,"testwrite.test2",[1,2,3,4]) //one-dimensional array
m = matrix([[1,2,3],[1,2,3]])
opcua::writeNode(conn,1,"testwrite.test3",m) //two-dimensional array
subscribe
Syntax
subscribe(connection, nodeNamespace, nodeIdString, handler, [actionName], [reconnect=false], [resubscribeInterval=0])
Parameters
- connection: The return value of method
connect
. - nodeNamespace: An INT scalar or array indicating the node namespace(s).
- nodeIdString: A STRING scalar or array indicating the node ID(s).
- handler: The callback function or table invoked when value changes.
- actionName: (optional) A string indicating the name of the subscription task, which must not duplicate existing names. Each subscription task must have a name. If unspecified, the system will generate one.
- reconnect: A boolean value, indicating whether to enable automatic reconnection. Defaults to false. When set to true, the method will attempt to re-establish the connection automatically.
- resubscribeInterval: A non-negative integer indicating the time interval (in milliseconds) between resubscription attempts. Defaults to 0.
Details
Subscribe to the changes of node values. Note that currently one subscription uses one connection exclusively, i.e., if subscribe is called on a connection, that connection cannot be used for readNode
, writeNode
, etc.
If reconnect is enabled, reconnection attempts will be made every resubscribeInterval milliseconds when a subscription is disconnected. Existing subscription information will not be cleared during reconnection. If a subscription is re-established, the relevant subscription information will be updated based on previous states. Users are allowed to cancel subscription during reconnection process, and no further reconnection attempts will be made.
Examples
t1 = table(200:0,`nodeID`value`timestamp`status, [SYMBOL, INT, TIMESTAMP, SYMBOL])
conn1=opcua::connect(serverUrl, "myClient")
opcua::subscribe(conn1,1,"test.subscribe",t1)
t2 = table(200:0,`nodeID`value`timestamp`status, [STRING, INT, TIMESTAMP, STRING])
conn2=opcua::connect(serverUrl, "myClient")
opcua::subscribe(conn2, 3, ["Counter","Expression","Random","Sawtooth"],t2)
t3 = table(200:0,`nodeID`value`timestamp`status, [SYMBOL, BOOL, TIMESTAMP, SYMBOL])
def callback1(mutable t, d) {
t.append!(d)
}
conn3=opcua::connect(serverUrl, "myClient")
opcua::subscribe(conn3,2, "testsubscribe",callback1{t3})
unsubscribe
Syntax
unsubscribe(connection|actionName)
Parameters
connection|actionName: The value returned by method connect
or the subscription task name.
Details
Cancel the client's subscription by connection or subscription task name.
Examples
opcua::unsubscribe(subscription)
getSubscriberStat
Syntax
getSubscriberStat()
Details
Get the status of all current subscriptions, including:
- actionName: STRING, the subscription task name.
- subscriptionId: INT, the subscription ID.
- user: STRING, the user who created the subscription.
- endPointUrl: STRING, the endpoint URL of the connection.
- clientUri: STRING, the client identifier of the connection.
- nodeID: STRING, all subscribed nodes, represented as "nodeNamespace:nodeIdString" for each node and separated by a semicolon ';'.
- createTimestamp: NANOTIMESTAMP, the time when the subscription was created.
- isConnected: BOOL, whether the connection is established.
- firstMsgTime: NANOTIMESTAMP, the timestamp of the first received message.
- lastMsgTime: NANOTIMESTAMP, the timestamp of the last received message.
- processedMsgCount: LONG, the number of messages that have been processed.
- failedMsgCount: LONG, the number of messages that failed to be processed.
- lastErrMsg: STRING, the information of the last error message.
- lastFailedTimestamp: NANOTIMESTAMP, the timestamp when the last error occurred.
- lastReconnectTime: NANOTIMESTAMP, the timestamp of the last reconnection attempt.
Examples
opcua::getSubscriberStat()
close
Syntax
close(connection)
Parameters
- connection: The return value of method
connect
.
Details
Close the connection to the OPC server and cancel any node subscriptions, if applicable.
Examples
opcua::close(connection)