QOpcUaClient Class

QOpcUaClient allows interaction with an OPC UA server. More...

Header: #include <QOpcUaClient>
qmake: QT += opcua
Inherits: QObject

Public Types

enum ClientError { NoError, InvalidUrl, AccessDenied, ConnectionError, UnknownError }
enum ClientState { Disconnected, Connecting, Connected, Closing }

Properties

  • error : const ClientError
  • state : const ClientState
  • 1 property inherited from QObject

Public Functions

virtual ~QOpcUaClient()
QString backend() const
void connectToEndpoint(const QUrl &url)
void disconnectFromEndpoint()
QOpcUaClient::ClientError error() const
QStringList namespaceArray() const
QOpcUaNode *node(const QString &nodeId)
QOpcUaNode *node(const QOpcUa::QExpandedNodeId &expandedNodeId)
QOpcUa::QQualifiedName qualifiedNameFromNamespaceUri(const QString &namespaceUri, const QString &name, bool *ok = nullptr) const
bool requestEndpoints(const QUrl &url)
QString resolveExpandedNodeId(const QOpcUa::QExpandedNodeId &expandedNodeId, bool *ok = nullptr) const
QOpcUaClient::ClientState state() const
bool updateNamespaceArray()
QUrl url() const
  • 34 public functions inherited from QObject

Signals

void connected()
void disconnected()
void endpointsRequestFinished(QVector<QOpcUa::QEndpointDescription> endpoints, QOpcUa::UaStatusCode statusCode)
void errorChanged(QOpcUaClient::ClientError error)
void namespaceArrayUpdated(QStringList namespaces)
void stateChanged(QOpcUaClient::ClientState state)

Additional Inherited Members

  • 1 public slot inherited from QObject
  • 1 public variable inherited from QObject
  • 10 static public members inherited from QObject
  • 9 protected functions inherited from QObject
  • 2 protected variables inherited from QObject

Detailed Description

QOpcUaClient allows interaction with an OPC UA server.

QOpcUaClient

QOpcUaClient implements basic client capabilities to communicate with OPC UA enabled devices and applications. This includes connecting, disconnecting and getting QOpcUaNode objects for a node on the server.

Addressing Nodes

For an introduction to nodes and node ids, see QOpcUaNode.

Usage

Create a QOpcUaClient using QOpcUaProvider and call connectToEndpoint() to connect to a server. After the connection is established, a QOpcUaNode object for the root node is requested.

QOpcUaProvider provider;
if (provider.availableBackends().isEmpty())
    return;
QOpcUaClient *client = provider.createClient(provider.availableBackends()[0]);
if (!client)
    return;
// Connect to the stateChanged signal. Compatible slots of QObjects can be used instead of a lambda.
QObject::connect(client, &QOpcUaClient::stateChanged, [client](QOpcUaClient::ClientState state) {
    qDebug() << "Client state changed:" << state;
    if (state == QOpcUaClient::ClientState::Connected) {
        QOpcUaNode *node = client->node("ns=0;i=84");
        if (node)
            qDebug() << "A node object has been created";
    }
});
client->connectToEndpoint(QUrl("opc.tcp://127.0.0.1:4840")); // Connect the client to the server

Member Type Documentation

enum QOpcUaClient::ClientError

This enum type specifies the current error state of the client.

ConstantValueDescription
QOpcUaClient::NoError0No error occurred.
QOpcUaClient::InvalidUrl1The url to connect to has been wrongly specified or a connection to this url failed.
QOpcUaClient::AccessDenied2An attempt to connect to a server using username/password failed due to wrong credentials.
QOpcUaClient::ConnectionError3An error occurred with the connection.
QOpcUaClient::UnknownError4An unknown error occurred.

enum QOpcUaClient::ClientState

This enum type specifies the connection state of the client.

ConstantValueDescription
QOpcUaClient::Disconnected0The client is not connected to a server.
QOpcUaClient::Connecting1The client is currently connecting to a server.
QOpcUaClient::Connected2The client is connected to a server.
QOpcUaClient::Closing3The client has been connected and requests a disconnect from the server.

Property Documentation

error : const ClientError

Specifies the current error state of the client.

Access functions:

QOpcUaClient::ClientError error() const

Notifier signal:

void errorChanged(QOpcUaClient::ClientError error)

state : const ClientState

Specifies the current connection state of the client.

Access functions:

QOpcUaClient::ClientState state() const

Notifier signal:

void stateChanged(QOpcUaClient::ClientState state)

Member Function Documentation

[virtual] QOpcUaClient::~QOpcUaClient()

Destroys the QOpcUaClient instance.

QString QOpcUaClient::backend() const

Returns the name of the backend used by this instance of QOpcUaClient, e.g. "open62541".

void QOpcUaClient::connectToEndpoint(const QUrl &url)

Connects to the OPC UA endpoint given in url.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

See also disconnectFromEndpoint().

[signal] void QOpcUaClient::connected()

This signal is emitted when a connection has been established.

void QOpcUaClient::disconnectFromEndpoint()

Disconnects from the server.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

See also connectToEndpoint().

[signal] void QOpcUaClient::disconnected()

This signal is emitted when a connection has been closed following to a close request.

[signal] void QOpcUaClient::endpointsRequestFinished(QVector<QOpcUa::QEndpointDescription> endpoints, QOpcUa::UaStatusCode statusCode)

This signal is emitted after a requestEndpoints() operation has finished. statusCode contains the result of the operation. If the result is Good, endpoints contains the descriptions of all endpoints that are available on the server.

QOpcUaClient::ClientError QOpcUaClient::error() const

Returns the current error state of the client.

Note: Getter function for property error.

QStringList QOpcUaClient::namespaceArray() const

Returns the cached value of the namespace array.

The value is only valid after the namespaceArrayUpdated() signal has been emitted.

See also updateNamespaceArray() and namespaceArrayUpdated().

[signal] void QOpcUaClient::namespaceArrayUpdated(QStringList namespaces)

This signal is emitted after an updateNamespaceArray operation has finished. namespaces contains the content of the server's namespace table. The index of an entry in namespaces corresponds to the namespace index used in the node id.

QOpcUaNode *QOpcUaClient::node(const QString &nodeId)

Returns a QOpcUaNode object associated with the OPC UA node identified by nodeId. The caller becomes owner of the node object.

If the client is not connected, nullptr is returned. The backends may also return nullptr for other error cases (for example for a malformed node id).

QOpcUaNode *QOpcUaClient::node(const QOpcUa::QExpandedNodeId &expandedNodeId)

Returns a QOpcUaNode object associated with the OPC UA node identified by expandedNodeId. The caller becomes owner of the node object.

If the node is not on the currently connected server, the namespace can't be resolved, the node id is malformed or the client is not connected, nullptr is returned.

See also updateNamespaceArray().

QOpcUa::QQualifiedName QOpcUaClient::qualifiedNameFromNamespaceUri(const QString &namespaceUri, const QString &name, bool *ok = nullptr) const

Attempts to create a qualified name from namespaceUri and the name string name. Returns the resulting qualified name. An empty qualified name is returned if namespaceUri can't be resolved.

ok will be set to true if the namespace URI resolution has been successful. If the namespace URI could not be resolved, ok will be set to false.

bool QOpcUaClient::requestEndpoints(const QUrl &url)

Starts an asynchronous GetEndpoints request to read a list of available endpoints from the server at url. Returns true if the asynchronous call has been successfully dispatched.

The endpoint information is returned in the endpointsRequestFinished() signal.

QString QOpcUaClient::resolveExpandedNodeId(const QOpcUa::QExpandedNodeId &expandedNodeId, bool *ok = nullptr) const

Attempts to resolve expandedNodeId to a node id string with numeric namespace index. Returns the node id string if the conversion was successful.

An empty string is returned if the namespace index can't be resolved or if the identifier part of the expanded node id is malformed. ok will be set to true if the conversion has been successful. If the expanded node id could not be resolved, ok will be set to false.

bool QOpcUaClient::updateNamespaceArray()

Requests an update of the namespace array from the server. Returns true if the operation has been successfully dispatched.

The namespaceArrayUpdated() signal is emitted after the operation is finished.

See also namespaceArray() and namespaceArrayUpdated().

QUrl QOpcUaClient::url() const

Returns the URL of the OPC UA server the client is currently connected to or was last connected to.

© 2018 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.