Main Content

connect

Connect OPC UA client to server

Description

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

example

connect(uaClient, userName, password) connects the OPC UA Client uaClient to its server using username and password authentication. The userName and password arguments must be provided, although 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 in .DER and privateKeyFilename in .PEM format. 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 various essential 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.

getSecret("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.

getSecret("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, specified as a string or character vector.

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

Certificate file with private key, 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

See Also

Functions