1.封装下串口类QSerialPort的线程操作
(Qt)封装下串口类QSerialPort的线程操作_苏州-青蛙-CSDN博客_qserialport线程
2. QT 串口QSerialPort + 解决接收数据不完整问题
QT 串口QSerialPort + 解决接收数据不完整问题_Kelvin_Yan的专栏-CSDN博客_qt串口接收数据不完整
3. Calling QSerialPort methods from two different threads
class SerialThread : public QThread
{
Q_OBJECT
public:
QMutex quit;
SerialThread()
{
}
~SerialThread()
{
quit.lock();
wait();
}
void asyncWrite(QByteArray data)
{
QMutexLocker locker(&mutex);
buffer = data;
}
bool syncWrite(QByteArray &data)
{
QMutexLocker locker(&mutex);
serial->write(data);
if(serial->waitForReadyRead(1000))
{
data = serial->readAll();
return true;
}
return false;
}
private:
QMutex mutex;
QSerialPort * serial;
QByteArray buffer;
virtual void run()
{
mutex.lock();
QSerialPort port;
serial = &port;
port.setPortName("COM10");
port.open(QSerialPort::ReadWrite);
while (quit.tryLock()) {
quit.unlock();
if(!buffer.isEmpty())
{
serial->write(buffer);
buffer.clear();
if(serial->waitForReadyRead(1000))
{
emit onRead(serial->readAll());
}
else
emit one rror();
}
mutex.unlock();
msleep(10);
mutex.lock();
}
}
signals:
void onRead(QByteArray data);
void one rror();
};