Main Content

opcua

Create OPC UA client object

Description

uaClient = opcua(serverInfoObj) creates an OPC UA client object associated with the server specified by serverInfoObj. You can create server info objects with the opcuaserverinfo function.

example

uaClient = opcua(serverUrl) creates a client object associated with the server referenced by the URL specified in serverUrl.

example

uaClient = opcua(hostname,portnum) creates an OPC UA client object associated with the server at port portnum on the host identified by hostname.

example

uaClient = opcua(___,Name=Value) specifies additional configuration options to create an OPC UA client object. (since R2025a)

By default, the client selects the most secure endpoint configuration from the list of all available connection configurations retrieved from the server. If the attempt to retrieve endpoints fails, an error is generated. You can override the default settings by using the name-value arguments.

example

Examples

collapse all

Create a client for the first server found on the local host.

s = opcuaserverinfo("localhost");
uaClient = opcua(s(1));

Create a client using the discovery URL of the server.

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

Create a client for the server at port 53530 on the local host.

uaClient = opcua("localhost",53530)

Since R2025a

Create a client using the discovery URL of the server and configure the security model.

uaClient = opcua("opc.tcp://MyMachine-4u6B:53530/OPCUA/SimulationServer",...
 MessageSecurityMode="SignAndEncrypt", ChannelSecurityPolicy="Aes256_Sha256_RsaPss");

View client details.

uaClient
uaClient = 

OPC UA Client:

   Server Information:
                     Name: 'SimulationServer@MyMachine-4u6B'
                 Hostname: 'MyMachine-4u6B'
                     Port: 53530
              EndpointUrl: 'opc.tcp://MyMachine-4u6B:53530/OPCUA/SimulationServer'

   Connection Information:
                  Timeout: 10
                   Status: 'Disconnected'
              ServerState: '<Not connected>'

   Security Information:
      MessageSecurityMode: SignAndEncrypt
    ChannelSecurityPolicy: Aes256_Sha256_RsaPss
                Endpoints: [1×11 opc.ua.EndpointDescription]

Since R2025a

Create a client using the discovery URL of the server.

uaClient = opcua("opc.tcp://240.19.17.56:53530/OPCUA/SimulationServer");

View the endpoint URL returned by the server.

uaClient.EndpointUrl
ans =

    'opc.tcp://192.168.1.2:53530/OPCUA/SimulationServer'

Re-create the client and replace the endpoint URL hostname with discovery URL hostname.

uaClient = opcua("opc.tcp://240.19.17.56:53530/OPCUA/SimulationServer", UseDiscoveryHostname=true);

Verify if the endpoint URL is same as the discovery URL of the client.

uaClient.EndpointUrl
ans =

    'opc.tcp://240.19.17.56:53530/OPCUA/SimulationServer'

Input Arguments

collapse all

OPC UA server information object, specified as an opc.ua.ServerInfo object.

Data Types: object

OPC UA server URL, specified as a character vector or string. The server URL must use the opc.tcp protocol; Industrial Communication Toolbox™ does not support http or https connections to an OPC UA server.

Data Types: char | string

Server hostname or IP address, specified as a character vector or string. A hostname can be short or a fully qualified domain name.

Example: 'localhost'

Data Types: char | string

Server host port number, specified as a numeric value.

Example: 5000

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Name-Value Arguments

collapse all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: uaClient = opcua("opc.tcp://localhost:53530/OPCUA/SimulationServer", MessageSecurityMode="Sign", ChannelSecurityPolicy="Basic256Sha256");

Since R2025a

Client message security mode, specified as "SignAndEncrypt" or "Sign" or "None". By default, the function chooses the highest message security mode available on the server.

Example: uaClient = opcua("localhost", 53530, MessageSecurityMode="Sign");

Data Types: char | string

Since R2025a

Client channel security policy, specified as "Aes256_Sha256_RsaPss", "Basic256Sha256", "Aes128_Sha256_RsaOaep" or "None". By default, the function chooses the highest channel security policy available on the server.

Example: uaClient = opcua("localhost", 53530, ChannelSecurityPolicy="Basic256Sha256");

Data Types: char | string

Note

Security Considerations: To enable a secure communication with an OPC UA server, use a strong channel security policy and message security mode.

You can expose your system to security risks when you set the MessageSecurityMode and ChannelSecurityPolicy to "None". Use this setting only in a trusted environment.

If you need to connect to an OPC UA server that does not support a security policy, request the server administrator to enable a strong channel security policy and message security mode for the server. These settings enable encryption and authentication and safeguard your data and operations.

Since R2025a

Option to replace the endpoint URL hostname with the input hostname or IP address provided in serverUrl, specified as one of these:

  • true: The function replaces the hostname in the endpoint URL returned by the server with the input hostname or IP address provided in serverUrl.

  • false: The function retains the endpoint URL provided by the server.

Example: uaClient = opcua("opc.tcp://DNShost:53530/OPCUA/SimulationServer", UseDiscoveryHostname=true);

Data Types: logical

Note

Security Considerations: You modify the endpoint URL returned by the server when you set the UseDiscoveryHostname to true. Use this setting only in a trusted environment.

If you need to connect to an OPC UA server through a NAT router, ensure that the server returns all discoverable endpoints to a calling client.

Output Arguments

collapse all

OPC UA client, returned as an opc.ua.Client object, with these properties.

PropertyDescription
Server Information
NameServer name.
HostnameServer hostname or IP address.
PortPort number used for TCP/IP connections to the server.
EndpointUrlURL to use for connection to the server.
Connection Information
TimeoutTime to wait for all operations on the server to complete.
StatusStatus of the client: 'Connected' or 'Disconnected'.
ServerStateState of the server: 'Running', 'Failed', 'No Configuration', 'Suspended', 'Shutdown', 'Test', 'Comms Fault', 'Unknown'.
Security Information
MessageSecurityModeMessage security mode specified for connection.
ChannelSecurityPolicyChannel security policy specified for connection.
EndpointsAvailable endpoints for server.
Server Limits (These properties are available only after you connect the client to the server)
MinSampleRateMinimum sample rate in seconds that the server can generally support.
MaxReadNodesMaximum number of nodes supported per read operation.
MaxWriteNodesMaximum number of nodes supported per write operation.
MaxHistoryReadNodesMaximum number of nodes supported by historical read operations.
MaxHistoryValuesPerNodeMaximum history values returned per node in historical read operations.

Version History

Introduced in R2015b

expand all