Main Content

connect

Connect OPC UA client to server

Description

connect(uaClient) connects the OPC UA client uaClient to its server using anonymous user authentication.

Industrial Communication Toolbox™ generates a user-specific application instance client certificate. (since R2025a)

To connect to an OPC UA server with the new client certificate, export the certificate using the exportClientCertificate function and then transfer the certificate to the trust store of your OPC UA server.

example

connect(uaClient,userName,password) connects the OPC UA client uaClient to its server using username and password authentication. You must include the userName and password arguments, but the password field can be empty.

example

connect(uaClient,publicKeyFilename,privateKeyFileName,privateKeyPassword) connects the OPC UA client uaClient to its server using the User Certificate stored in the public and private key files referenced by publicKeyFilename and privateKeyFilename. privateKeyPassword is the password used to protect the private key file. Private key files for OPC must be password protected.

When the client successfully connects to the server, the client object Status property is set to 'Connected', the first level of the server namespace is retrieved, and properties of the client are read from the server.

If uaClient is a vector of clients, and some but not all clients can connect, a warning is issued. If no clients can connect, an error is generated. You can only connect a vector of clients using the same username and password, or the same certificate parameters. If you need to use different usernames and passwords for different servers, call connect on each of the clients individually.

example

Examples

collapse all

Create a client and connect it to the OPC UA server.

uaClient = opcua("opc.tcp://localhost:53530/OPCUA/SimulationServer1");
connect(uaClient);

Check the connection status.

isConnected(uaClient)
1

Create an OPC UA client.

uaClient = opcua("opc.tcp://localhost:53530/OPCUA/SimulationServer2");

Connection with username token requires a username and password for authentication. Use the setSecret and getSecret functions to securely store and retrieve the password from your MATLAB® vault.

setSecret("TestPassword")

password prompt for OPC UA

Connect the OPC UA client using username and password authentication.

connect(uaClient, "TestUser", getSecret("TestPassword"));

For releases prior to R2024a, specify password as a string.

Check the connection status.

isConnected(uaClient)
1

Create an OPC UA client.

uaClient = opcua("opc.tcp://localhost:53530/OPCUA/SimulationServer1");

Connection with certificate token requires a certificate file, private key file and password. Use the setSecret and getSecret functions to securely store and retrieve the private key password from your MATLAB vault.

setSecret("PrivatePassword")

password prompt for OPC UA certificate password

Connect the OPC UA client using certificate.

connect(uaClient, "C:\MyFolder\TestCertificate.der",...
         "C:\MyFolder\TestPrivateFile.pem", getSecret("PrivatePassword"));

For releases prior to R2024a, specify password as a string.

Check the connection status.

isConnected(uaClient)
1

Input Arguments

collapse all

OPC UA client, specified as an OPC UA client object or array of objects.

Example: connect(uaclient)

User name for connection to the server, specified as a string or character vector.

Example: connect(uaclient, "User", "Password")

Password for connection to the server, specified as a string or character vector. Use the setSecret and getSecret functions to securely store and retrieve the password from your MATLAB vault.

Example: connect(uaclient, "User", "Password")

Certificate file with public key stored in .DER format, specified as a string or character vector.

Example: connect(opcClient, "C:\MyFolder\TestCertificate.der", "C:\MyFolder\TestPrivateFile.pem", "TestKey")

Certificate file with private key stored in .PEM format, specified as a string or character vector.

Example: connect(opcClient, "C:\MyFolder\TestCertificate.der", "C:\MyFolder\TestPrivateFile.pem", "TestKey")

Password to access the certificate file with private key, specified as a string or character vector. Use the setSecret and getSecret functions to securely store and retrieve the password from your MATLAB vault.

Example: connect(opcClient, "C:\MyFolder\TestCertificate.der", "C:\MyFolder\TestPrivateFile.pem", "TestKey")

Version History

Introduced in R2015b