Blocking Master Example | Qt Serial Port 5.15.18

- Qt 5.15
- Qt Serial Port
- Qt Serial Port Examples
- Blocking Master Example
- Qt 5.15.18 Reference Documentation

# Blocking Master Example

Shows how to use the synchronous API of QSerialPort in a worker thread.

Blocking Master shows how to create an application for a serial interface using the synchronous API of QSerialPort in a worker thread.

QSerialPort supports two programming alternatives:

- The asynchronous (non-blocking) alternative. Operations are scheduled and performed when the control returns to the Qt event loop. The QSerialPort class emits a signal when the operation is finished. For example, the write() method returns immediately. When the data is sent to the serial port, the QSerialPort class emits the bytesWritten() signal.
- The synchronous (blocking) alternative. In headless and multithreaded applications, the wait method can be called (in this case, waitForReadyRead()) to suspend the calling thread until the operation has completed.

In this example, the synchronous alternative is demonstrated. The Terminal example illustrates the asynchronous alternative.

The purpose of this example is to demonstrate how to simplify your serial programming code without losing the responsiveness of the user interface. The blocking serial programming API often leads to simpler code, but it should only be used in non-GUI threads to keep the user interface responsive.

This application is the master which demonstrates the work paired with the slave application Blocking Slave Example.

The master application initiates the transfer request via the serial port to the slave application and waits for response.

     class MasterThread : public QThread
     {
         Q_OBJECT

     public:
         explicit MasterThread(QObject *parent = nullptr);
         ~MasterThread();

         void transaction(const QString &portName, int waitTimeout, const QString &request);

     signals:
         void response(const QString &s);
         void error(const QString &s);
         void timeout(const QString &s);

     private:
         void run() override;

         QString m_portName;
         QString m_request;
         int m_waitTimeout = 0;
         QMutex m_mutex;
         QWaitCondition m_cond;
         bool m_quit = false;
     };

MasterThread is a QThread subclass that provides API for scheduling requests to the slave. This class provides signals for responding and reporting errors. The transaction() method can be called to start up the new master transaction with the desired request. The result is provided by the response() signal. In case of any issues, the error() or timeout() signal is emitted.

Note, the transaction() method is called in the main thread, but the request is provided in the MasterThread thread. The MasterThread data members are read and written concurrently in different threads, thus the QMutex class is used to synchronize the access.

     void MasterThread::transaction(const QString &portName, int waitTimeout, const QString &request)
     {
         const QMutexLocker locker(&m_mutex);
         m_portName = portName;
         m_waitTimeout = waitTimeout;
         m_request = request;
         if (!isRunning())
             start();
         else
             m_cond.wakeOne();
     }

The transaction() method stores the serial port name, timeout and request data. The mutex can be locked with QMutexLocker to protect this data. The thread can be started then, unless it is already running. The wakeOne() method is discussed later.

     void MasterThread::run()
     {
         bool currentPortNameChanged = false;

         m_mutex.lock();
         QString currentPortName;
         if (currentPortName != m_portName) {
             currentPortName = m_portName;
             currentPortNameChanged = true;
         }

         int currentWaitTimeout = m_waitTimeout;
         QString currentRequest = m_request;
         m_mutex.unlock();

In the run() function, the first is to lock the QMutex object, then fetch the serial port name, timeout and request data by using the member data. Having that done, the QMutex lock is released.

Under no circumstance should the `transaction()` method be called simultaneously with a process fetching the data. Note, while the QString class is reentrant, it is not thread-safe. Thereby, it is not recommended to read the serial port name in a request thread, and timeout or request data in another thread. The MasterThread class can only handle one request at a time.

The QSerialPort object is constructed on stack in the run() method before entering the loop:

         QSerialPort serial;

         if (currentPortName.isEmpty()) {
             emit error(tr("No port name specified"));
             return;
         }

         while (!m_quit) {

This makes it possible to create an object while running the loop. It also means that all the object methods are executed in the scope of the run() method.

It is checked inside the loop whether or not the serial port name of the current transaction has changed. If this has changed, the serial port is reopened and then reconfigured.

             if (currentPortNameChanged) {
                 serial.close();
                 serial.setPortName(currentPortName);

                 if (!serial.open(QIODevice::ReadWrite)) {
                     emit error(tr("Can't open %1, error code %2")
                                .arg(m_portName).arg(serial.error()));
                     return;
                 }
             }

The loop will continue to request data, write to the serial port and wait until all data is transferred.

             // write request
             const QByteArray requestData = currentRequest.toUtf8();
             serial.write(requestData);
             if (serial.waitForBytesWritten(m_waitTimeout)) {

Warning: As for the blocking transfer, the waitForBytesWritten() method should be used after each write method call. This will process all the I/O routines instead of the Qt event loop.

The timeout() signal is emitted if a timeout error occurs when transferring data.

             } else {
                 emit timeout(tr("Wait write request timeout %1")
                              .arg(QTime::currentTime().toString()));
             }

There is a waiting period for response after a successful request, and then it is read again.

                 // read response
                 if (serial.waitForReadyRead(currentWaitTimeout)) {
                     QByteArray responseData = serial.readAll();
                     while (serial.waitForReadyRead(10))
                         responseData += serial.readAll();

                     const QString response = QString::fromUtf8(responseData);
                     emit this->response(response);

Warning: As for the blocking alternative, the waitForReadyRead() method should be used before each read() call. This will processes all the I/O routines instead of the Qt event loop.

The timeout() signal is emitted if a timeout error occurs when receiving data.

                 } else {
                     emit timeout(tr("Wait read response timeout %1")
                                  .arg(QTime::currentTime().toString()));
                 }

When a transaction has been completed successfully, the response() signal contains the data received from the slave application:

                     emit this->response(response);

Afterwards, the thread goes to sleep until the next transaction appears. The thread reads the new data after waking up by using the members and runs the loop from the beginning.

             m_mutex.lock();
             m_cond.wait(&m_mutex);
             if (currentPortName != m_portName) {
                 currentPortName = m_portName;
                 currentPortNameChanged = true;
             } else {
                 currentPortNameChanged = false;
             }
             currentWaitTimeout = m_waitTimeout;
             currentRequest = m_request;
             m_mutex.unlock();
         }

#### Running the Example

To run the example from Qt Creator, open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.

Example project @ code.qt.io

See also Terminal Example and Blocking Slave Example.