0

I have a class with a serial interface which transmit some data and emit a MessageReceived signal when some valid data are received:

Serial.c

void Serial::on_serialPort_readData()
{
        ... Other stuff...

        else
        {
            qDebug() << "Received in serial";
            emit MessageReceived(_mReceivedData);
        }
    }
}

And I have my main window which opens the serial connection and start the message transaction. But I have to receive a lot of data and the transmission will take a while, so I decided to use a QThread for the processing of the received data.

I created a thread and a worker in my mainwindow.cpp (code from here):

mainwindow.cpp

_mThread = new QThread(this);
_mThreadWorker = new Worker();
_mThreadWorker->moveToThread(_mThread);

// Like in the example
connect(_mThread, &QThread::started, _mThreadWorker, &Worker::Progress);
connect(_mThreadWorker, &Worker::Finished, _mThread, &QThread::quit);
connect(_mThreadWorker, &Worker::Finished, _mThreadWorker, &Worker::deleteLater);
connect(_mThread, &QThread::finished, _mThread, &QObject::deleteLater);

connect(_mProtocolInterface, &Serial::MessageReceived, _mThreadWorker, &Worker::UpdateData);

_mThread->start();

// Send the first packet so the receiver will answer

The serial interface will send the data in different packages and I read here that it is a bad idea to use the serial port in a seperate thread, so I decided to use the serial port in the main thread and connect the MessageReceived signal with an UpdateData slot of my worker:

worker.h

class Worker : public QObject
{
    Q_OBJECT

    ...

    public slots:
        void Progress(void);
        void UpdateData(QByteArray Data);

    public:
        Worker(QObject* parent = nullptr);
        ~Worker();
        ...
};

worker.cpp

void Worker::UpdateData(QByteArray Data)
{
    qDebug() << "Received in Worker";
    _mData = Data;
}

void Worker::Progress(void)
{
    while(_mActive)
    {
        switch(_mState)
        {
            case STATE_INIT:
            {
                _mState = STATE_INIT;
                // Just a idle to simulate the data transfer during debugging
                break;
            }
        }
    }
}

But only the slot on_serialPort_readData gets called after I receive the data from the receiver:

>> Received in serial

So why the slot UpdateData of the class Worker doesn´t get called?

Kampi
  • 1,798
  • 18
  • 26
  • Your `MessageReceived` signal declared in `Protocol`, but you emit it from the `Serial` class as if it's `Serial::MessageReveived` signal. – vahancho Apr 09 '20 at 09:34
  • @vahancho sorry this was a typo. I fixed it. – Kampi Apr 09 '20 at 09:36
  • 1
    Your code is incomplete but `Worker::Progress` appears to be a loop that never returns control to the Qt event processing loop. Hence no queued/inter-thread signals will be processed. You *must* have an active event loop on the receiver's thread. – G.M. Apr 09 '20 at 09:44
  • Since QSerialPort is asynchronous I don't see a need for QThread here at all. – chehrlic Apr 09 '20 at 10:26
  • mmh okay. So I have to change the code. Thank you. – Kampi Apr 09 '20 at 10:35

0 Answers0