Contenuto principale

opcua

Create OPC UA client object

    Description

    An opc.ua.Client object represents an OPC UA client in MATLAB® that connects to an OPC UA server. You can use the client to communicate with the server, including browsing the server namespace, reading and writing current values, and reading historical values from the server.

    Creation

    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)

    The client retrieves a list of all available connection endpoint configurations from the server. By default, the client selects the endpoint configuration with the highest available channel security policy and message security mode. If the attempt to retrieve endpoints fails, an error is generated. You can override the default settings by using the name-value arguments.

    example

    Input Arguments

    expand 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

    expand 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="SignAndEncrypt", ChannelSecurityPolicy="Aes256_Sha256_RsaPss");

    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. For more information, see Message Security Modes.

    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. For more information, see Channel Security Policies.

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

    Data Types: char | string

    Note

    Security Considerations: When you communicate with an OPC UA server, use the highest available channel security policy and message security mode.

    If you set the MessageSecurityMode and ChannelSecurityPolicy to "None", the messages are sent as clear text because they are not signed and encrypted. Use this setting only in a trusted environment.

    For an OPC UA server that does not support a security policy, request the server administrator to enable a channel security policy and message security mode for the server other than None. These settings enable encryption and authentication for 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 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, make sure that the server returns all discoverable endpoints to a calling client.

    Since R2026a

    Option to trust a server temporarily for the current OPC UA client, specified as true or false.

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

    Data Types: logical

    Note

    Security Considerations: You allow connection with a server whose certificate is not included in the MATLAB OPC UA client's trusted certificate store when you set TrustServerTemporarily to true. Use this setting only in a trusted environment.

    If you trust the server source, consider using the opc.ua.trustServerCertificate function to permanently add the server certificate to the MATLAB OPC UA client's trusted certificate store. When a server certificate is included in the MATLAB OPC UA client's trusted certificate store, you can connect to the server from any OPC UA client created from the local system.

    Output Arguments

    expand all

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

    Properties

    expand all

    Server Information

    This property is read-only.

    Name of the OPC UA server the client connects to, specified as a character vector.

    Example: 'SimulationServer@MyMachine-4u6B'

    Data Types: char

    This property is read-only.

    Server hostname or IP address, represented as a character vector.

    Example: 'MyMachine-4u6B'

    Data Types: char

    This property is read-only.

    Port number used for TCP/IP connections to the server, represented as a double.

    Example: 53530

    Data Types: double

    This property is read-only.

    URL used for connecting to the server, represented as a character vector. This URL uniquely identifies the endpoint and includes the security mode, security policy, and transport settings information associated with that endpoint.

    Example: 'opc.tcp://MyMachine-4u6B:53530/OPCUA/SimulationServer'

    Data Types: char

    Connection Information

    This property is read-only.

    Maximum time to wait for all operations on the server to complete, represented as a double. The time is represented in seconds.

    Example: 10

    Data Types: double

    This property is read-only.

    Status of the client, represented as 'Disconnected' or 'Connected'. When you create a client, it is disconnected by default. Use connect to connect the client to the server.

    Example: 'Connected'

    This property is read-only.

    State of the server, represented as a character vector. An OPC UA server can be in one of these states: 'Running', 'Failed', 'No Configuration', 'Suspended', 'Shutdown', 'Test', 'Comms Fault', 'Unknown'. When the client is not connected to the server, the server state is shown as '<Not connected>'.

    Example: 'Running'

    Data Types: char

    Security Information

    This property is read-only.

    Message security mode used for connection, represented as SignAndEncrypt or Sign or None.

    Example: SignAndEncrypt

    Data Types: enumerated

    This property is read-only.

    Channel security policy used for connection, represented as Aes256_Sha256_RsaPss, Basic256Sha256, Aes128_Sha256_RsaOaep or None.

    Example: Basic256Sha256

    Data Types: enumerated

    This property is read-only.

    Endpoints discovered by the OPC UA client, represented as an array of endpoints. To access the description for all the endpoints, use the syntax clientObj.Endpoints.

    Example:

    >> clientObj.Endpoints
    
    ans = 
    
    1×11 OPC UA EndpointDescription array:
        index  Message Security    Channel Security                User Tokens            
        -----  ----------------  ---------------------  ----------------------------------
          1    SignAndEncrypt    Aes256_Sha256_RsaPss   Anonymous | Username | Certificate
          2    Sign              Basic256               Anonymous | Username | Certificate
          3    Sign              Aes128_Sha256_RsaOaep  Anonymous | Username | Certificate
          4    Sign              Basic128Rsa15          Anonymous | Username | Certificate
          5    SignAndEncrypt    Basic256Sha256         Anonymous | Username | Certificate
          6    SignAndEncrypt    Basic128Rsa15          Anonymous | Username | Certificate
          7    None              None                   Anonymous | Username | Certificate
          8    Sign              Basic256Sha256         Anonymous | Username | Certificate
          9    SignAndEncrypt    Basic256               Anonymous | Username | Certificate
         10    Sign              Aes256_Sha256_RsaPss   Anonymous | Username | Certificate
         11    SignAndEncrypt    Aes128_Sha256_RsaOaep  Anonymous | Username | Certificate

    The following properties are available only after you connect the client to the server.

    Server Limits

    This property is read-only.

    Minimum sample rate supported by the server, represented as a duration in seconds.

    Example: 0 sec

    Data Types: duration

    This property is read-only.

    Maximum number of nodes supported per read operation, represented as an integer.

    Example: 10000

    Data Types: uint32

    This property is read-only.

    Maximum number of nodes supported per write operation, represented as an integer.

    Example: 10000

    Data Types: uint32

    This property is read-only.

    Maximum number of nodes supported for historical read operations, represented as an integer.

    Example: 10000

    Data Types: uint32

    This property is read-only.

    Maximum number of history values returned per node during a historical read operation, represented as a double. When the server does not impose a limit, the property value is 0.

    Example: 1000

    Data Types: double

    Object Functions

    setSecurityModelSet security configuration parameters for OPC UA client
    connectConnect OPC UA client to server
    disconnectDisconnect OPC UA client from server
    isConnectedDetermine if OPC UA client object is connected to server
    getServerStatusStatus of OPC UA server
    getNamespaceNamespace of OPC UA server associated with client
    browseNamespaceGraphically browse namespace and select nodes from OPC UA server
    readAtTimeRead historical data from nodes of OPC UA server at specific times
    readHistoryRead historical data from nodes on OPC UA server
    readProcessedRead aggregate data from nodes of an OPC UA server
    readValueRead values from nodes on OPC UA server
    subscribeMonitor nodes for data change notification
    writeValueWrite values to nodes on OPC UA server

    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'

    Version History

    Introduced in R2015b

    expand all