I am trying to create a thread that will try to connect to a serial port. In debug mode, none of the signals emitted from within Serial is ever fired. connectToPort() is also never entered.

Am I missing something here?

I am used to subclassing QThread and this is my first attempt at using the moveToThread() method.

Here is the code:

Bluetooth.cpp:

void Bluetooth::deviceSelected()
{
    QStringList comPort;

    comPort = bluetoothSelectedDevice->split(":");

    qDebug() << comPort.at(0);

    //Create COM port object to be used with QSerialPort by providing COM port in text.
    QSerialPortInfo temp(comPort.at(0));

    if(temp.isBusy())
    {
        qDebug() << "COM port is already open!";
        //TODO: Notify user that COM port is already open and that he should close it and try again.
        return;
    }

    //Create serial port object.
    serialPort = new QSerialPort(temp);

    //Instantiate a serial class object.
    serial = new Serial(serialPort);

    //Create a thread to be used with the serial object.
    serialWorkerThread = new QThread();

    //Move serial object to serial worker thread.
    serial->moveToThread(serialWorkerThread);

    QObject::connect(serialWorkerThread, SIGNAL(started()), serial, SLOT(connectToPort()));
    QObject::connect(serialWorkerThread, SIGNAL(finished()), serialWorkerThread, SLOT(deleteLater()));
    QObject::connect(serial, SIGNAL(connected()), this, SLOT(on_connected()));
    QObject::connect(serial, SIGNAL(couldNotConnect()), this, SLOT(on_couldNotConnect()));
    QObject::connect(serial, SIGNAL(portSettingsFailed()), this, SLOT(on_portSettingsFailed()));
}

Serial.h:

#ifndef SERIAL_H
#define SERIAL_H

#include <QObject>
#include <QSerialPort>

class Serial : public QObject
{
    Q_OBJECT
public:
    explicit Serial(QSerialPort *serialPort);

signals:
    void connected();
    void couldNotConnect();
    void portSettingsFailed();

public slots:
    void connectToPort();

private:
    QSerialPort *serialPort;

};

#endif // SERIAL_H

Serial.cpp:

#include "serial.h"
#include <QSerialPort>
#include <QDebug>

Serial::Serial(QSerialPort *serialPort)
{
    this->serialPort = serialPort;
}

void Serial::connectToPort()
{
    //Try to connect 5 times.
    for(int i = 0; i < 5; i++)
    {
        if(!serialPort->open(QIODevice::ReadWrite))
        {
            qDebug() << "Failed to open port";
            if(i == 4)
            {
                emit couldNotConnect();
                return;
            }
        }
        else
        {
            break;
        }
    }

    //Set port settings.
    if(serialPort->setBaudRate(QSerialPort::Baud9600) &&
       serialPort->setDataBits(QSerialPort::Data8) &&
       serialPort->setParity(QSerialPort::NoParity) &&
       serialPort->setStopBits(QSerialPort::OneStop) &&
       serialPort->setFlowControl(QSerialPort::NoFlowControl) != true)
    {
        qDebug() << "Failed to configure port";
        emit portSettingsFailed();
    }

    emit connected();
}
有帮助吗?

解决方案

Did you paste all your code? Are you verifying the execution is actually getting to the end of your deviceSelected method? It looks like you never start your thread.

i.e. after your connections add:

serialWorkerThread->start();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top