其他分享
首页 > 其他分享> > QT -- 通讯协议

QT -- 通讯协议

作者:互联网

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();
 
};

标签:通讯,QT,--,QSerialPort,buffer,mutex,串口,serial,data
来源: https://blog.csdn.net/wishfly/article/details/122727057