Connection QML Type

Connects to a server. More...

Import Statement: import QtOpcUa
Since: QtOpcUa 5.12

Properties

Signals

Methods

Detailed Description

The main API uses backends to make connections. You have to set the backend before any connection attempt.

import QtOpcUa as QtOpcUa

QtOpcUa.Connection {
    backend: "open62541"
}

Component.onCompleted: {
    connection.connectToEndpoint("opc.tcp://127.0.0.1:43344");
}

Property Documentation

authenticationInformation : AuthenticationInformation

Set the authentication information to this connection. The authentication information has to be set before calling connectToEndpoint. If no authentication information is set, the anonymous mode will be used. It has no effect on the current connection. If the client is disconnected and then reconnected, the new credentials are used. Reading and writing this property before a backend is set, writes are ignored and reads return and invalid AuthenticationInformation.


availableBackends : stringlist [read-only]

Returns the names of all available backends as a list. These are used to select a backend when connecting.

See also Connection::backend.


backend : string

Set the backend to use for a connection to the server. Has to be set before any connection attempt.

See also Connection::availableBackends.


connected : bool [read-only]

Status of the connection. true when there is a connection, otherwise false.


connection : QOpcUaClient

This property is used only to inject a connection from C++. In case of complex setup of a connection you can use C++ to handle all the details. After the connection is established it can be handed to QML using this property. Ownership of the client is transferred to QML.

class MyClass : public QObject {
    Q_OBJECT
    Q_PROPERTY(QOpcUaClient* connection READ connection NOTIFY connectionChanged)

public:
    MyClass (QObject* parent = nullptr);
    QOpcUaClient *connection() const;

signals:
    void connectionChanged(QOpcUaClient *);

Emitting the signal connectionChanged when the client setup is completed, the QML code below will use the connection.

import QtOpcUa as QtOpcUa

MyClass {
    id: myclass
}

QtOpcUa.Connection {
    connection: myclass.connection
 }

currentEndpoint : QOpcUaEndpointDescription

An endpoint description of the server to which the connection is connected to. When the connection is not established, an empty endpoint description is returned.


defaultConnection : bool

Makes this the default connection. Usually each node needs to be given a connection to use. If this property is set to true, this connection will be used in all cases where a node has no connection set. Already established connections are not affected. If defaultConnection is set to true on multiple connection the last one is used.

QtOpcUa.Connection {
    ...
    defaultConnection: true
    ...
}

See also Node.


namespaces : stringlist [read-only]

List of strings of all namespace URIs registered on the connected server.


supportedSecurityPolicies : stringlist

A list of strings containing the supported security policies

This property is currently available as a Technology Preview, and therefore the API and functionality provided may be subject to change at any time without prior notice.


supportedUserTokenTypes : array[tokenTypes]

An array of user token policy types of all supported user token types.

This property is currently available as a Technology Preview, and therefore the API and functionality provided may be subject to change at any time without prior notice.


Signal Documentation

nodeChanged()

Emitted when the underlying node has changed. This happens when the namespace or identifier of the NodeId changed.

Note: The corresponding handler is onNodeChanged.


readNodeAttributesFinished(readResults)

Emitted when the read request, started using readNodeAttributes(), is finished. The readResults parameter is an array of ReadResult entries, containing the values requested from the server.

connection.onReadNodeAttributesFinished(results) {
    for (var i = 0; results.length; i++) {
        if (results[i].status.isGood) {
            console.log(results[i].value);
        } else {
            // handle error
        }
    }
}

Note: The corresponding handler is onReadNodeAttributesFinished.

See also readNodeAttributes() and ReadResult.


writeNodeAttributesFinished(writeResults)

Emitted when the write request started using writeNodeAttributes() is finished. The writeResults parameter is an array of WriteResult entries, containing the values requested from the server.

for (var i = 0; i < writeResults.length; i++) {
    console.log(writeResults[i].nodeId);
    console.log(writeResults[i].namespaceName);
    console.log(writeResults[i].attribute);

    if (writeResults[i].status.isBad) {
        // value was not written
    }
}

Note: The corresponding handler is onWriteNodeAttributesFinished.

See also writeNodeAttributes() and WriteResult.


Method Documentation

connectToEndpoint(endpointDescription)

Connects to the endpoint specified with endpointDescription.

See also EndpointDescription.


disconnectFromEndpoint()

Disconnects an established connection.


readNodeAttributes(valuesToBeRead)

This function is used to read multiple values from a server in one go. Returns true if the read request was dispatched successfully.

The valuesToBeRead parameter must be a JavaScript array of ReadItem entries.

// List of items to read
var readItemList = [];
// Item to be added to the list of items to be read
var readItem;

// Prepare an item to be read

// Create a new read item and fill properties
readItem = QtOpcUa.ReadItem.create();
readItem.ns = "http://qt-project.org";
readItem.nodeId = "s=Demo.Static.Scalar.Double";
readItem.attribute = QtOpcUa.Constants.NodeAttribute.DisplayName;

// Add the prepared item to the list of items to be read
readItemList.push(readItem);

// Add further items
[...]

if (!connection.readNodeAttributes(readItemList)) {
    // handle error
}

The result of the read request are provided by the signal readNodeAttributesFinished().

See also readNodeAttributesFinished() and ReadItem.


writeNodeAttributes(valuesToBeWritten)

This function is used to write multiple values to a server in one go. Returns true if the write request was dispatched successfully.

The valuesToBeWritten parameter must be a JavaScript array of WriteItem entries.

// List of items to write
var writeItemList = [];
// Item to be added to the list of items to be written
var writeItem;

// Prepare an item to be written

// Create a new write item and fill properties
writeItem = QtOpcUa.WriteItem.create();
writeItem.ns = "http://qt-project.org";
writeItem.nodeId = "s=Demo.Static.Scalar.Double";
writeItem.attribute = QtOpcUa.Constants.NodeAttribute.Value;
writeItem.value = 32.1;
writeItem.valueType = QtOpcUa.Constants.Double;

// Add the prepared item to the list of items to be written
writeItemList.push(writeItem);

// Add further items
[...]

if (!connection.writeNodeAttributes(writeItemList)) {
    // handle error
}

The result of the write request are provided by the signal Connection::writeNodeAttributesFinished().

See also Connection::writeNodeAttributesFinished() and WriteItem.


© 2024 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.