Why you check the serial port with a timer instead of doing it directly in the run() of the thread?
I use QextSerialPort and I check the available data in a thread like this:
class ReceiveThread
: public QThread{
Q_OBJECT
public:
ReceiveThread(QextSerialPort *adrPort);
~ReceiveThread();
void stopReceiving();
protected:
void run();
signals:
void dataReceived
(const QByteArray);
//!< Data Available from the QextSerialPort
private:
QextSerialPort *d_port; //!< Reference to the serial port to monitor
bool stopped; //!< Specify if the thread is running or not (it is needed to stop the thread)
};
ReceiveThread::ReceiveThread(QextSerialPort *adrPort)
: d_port(adrPort), stopped(false)
{
}
ReceiveThread::~ReceiveThread()
{
if (isRunning())
{
stopReceiving();
wait();
}
}
void ReceiveThread::stopReceiving()
{
stopped = true;
}
//! The Receive Thread Loop
void ReceiveThread::run()
{
int bytesAvailable;
data.reserve(MAX_BUFFER_SIZE);
while(!stopped)
{
mutex.lock();
bytesAvailable = d_port->bytesAvailable();
if (bytesAvailable > 0)
data.append(d_port->read((bytesAvailable<MAX_BUFFER_SIZE)? bytesAvailable: MAX_BUFFER_SIZE));
mutex.unlock();
if (bytesAvailable)
qDebug() << tr("ReceiveThread: %1").arg(bytesAvailable);
if (!data.isEmpty())
{
emit dataReceived(data);
qDebug() << tr("ReceiveThread: Emitted Data Received");
qDebug
() << tr
("ReceiveThread: Data: 0x%1").
arg((QString)data.
toHex());
data.clear();
}
msleep(RECEIVE_THREAD_SLEEP_TIME);
}
}
class ReceiveThread : public QThread
{
Q_OBJECT
public:
ReceiveThread(QextSerialPort *adrPort);
~ReceiveThread();
void stopReceiving();
protected:
void run();
signals:
void dataReceived(const QByteArray); //!< Data Available from the QextSerialPort
private:
QextSerialPort *d_port; //!< Reference to the serial port to monitor
QMutex mutex; //!< Mutex lock
bool stopped; //!< Specify if the thread is running or not (it is needed to stop the thread)
};
ReceiveThread::ReceiveThread(QextSerialPort *adrPort)
: d_port(adrPort), stopped(false)
{
}
ReceiveThread::~ReceiveThread()
{
if (isRunning())
{
stopReceiving();
wait();
}
}
void ReceiveThread::stopReceiving()
{
stopped = true;
}
//! The Receive Thread Loop
void ReceiveThread::run()
{
QByteArray data;
int bytesAvailable;
data.reserve(MAX_BUFFER_SIZE);
while(!stopped)
{
mutex.lock();
bytesAvailable = d_port->bytesAvailable();
if (bytesAvailable > 0)
data.append(d_port->read((bytesAvailable<MAX_BUFFER_SIZE)? bytesAvailable: MAX_BUFFER_SIZE));
mutex.unlock();
if (bytesAvailable)
qDebug() << tr("ReceiveThread: %1").arg(bytesAvailable);
if (!data.isEmpty())
{
emit dataReceived(data);
qDebug() << tr("ReceiveThread: Emitted Data Received");
qDebug() << tr("ReceiveThread: Data: 0x%1").arg((QString)data.toHex());
data.clear();
}
msleep(RECEIVE_THREAD_SLEEP_TIME);
}
}
To copy to clipboard, switch view to plain text mode
To use this class you only nead to istantiete the class and call the run() method to start the monitoring of the serial port.
To stop the monitoring you must call the stopReceiving() method.
The data received is emitted with the signal dataReceived(const QByteArray &), so you have to connect this with a slot to manage the data received.
WARNING
This class does not perform any check on the QextSerialPort provided, you have to perform these check before starting the Receive Thread. Also you have to stop the Receive Thread before close the QextSerialPort.
So, instead to instantiate the Thread directly in the Main you have to instantiate it in your MainWindow after create and open the serialport.
Bookmarks