(QtSerialPort) Linux Serial port buffer is not empty when opening port



  • Hello,

    I Working on Bluetooth Communication using Qt Serial port (with QT 4),

    But when i am opening a port, i directly have bytesAvailabe,and after deep test and research i conluded that the problem is from buffer of linux , actually the buffer is not empty , even when i made just a

    cat /dev/NumberOfMyPort 
    

    My question is what is the best way to correctly clear buffer when opening my serial port ?

    I am doing this in my program

    Constructor()
    {
        cmdSerial = new QSerialPort();
        cmdSerial->setPortName("ttyUSB0");
        cmdSerial->setBaudRate(QSerialPort::Baud115200);
        cmdSerial->setParity(QSerialPort::NoParity);
        cmdSerial->setDataBits(QSerialPort::Data8);
        cmdSerial->setStopBits(QSerialPort::OneStop);
        cmdSerial->setFlowControl(QSerialPort::HardwareControl);
        cmdSerial->open(QIODevice::ReadWrite);
        cmdSerial->flush();
    }
    
    
    ~Destructor()
    {
        if (cmdSerial->isOpen())
        {
            cmdSerial->flush();
            cmdSerial->close();
    
            delete cmdSerial;
        }
    }
    

    But this does not seem to be the right way to do...



  • Try QSerialPort::clear().



  • @kuzulis

    Thank you to your fast reply,

    Well, like you suggest, i tried to clear my serial port and i've made it like that:

    cmdSerial->open(QSerialPort::ReadWrite);
    cmdSerial->clear(QSerialPort::AllDirections);
    

    and although, i made the same thing just before closing my port on my destructor

        if (cmdSerial->isOpen())
        {
           cmdSerial->clear(cmdSerial->AllDirections);
            cmdSerial->close();
            delete cmdSerial;
        }
    

    But when i close , and open my program again i am still having the same problem ,

    It seems like the port is never closed ,even when i made myport->close()

    Any other suggestion?

    Regards !



  • http://doc.qt.io/qt-5/qserialport.html#flush
    mayby because of this one in constructor? Why you flush something there? From the code i can see you didnt write before so the purpose of this line is not clear to me. Get rid of it and give it a try.



  • @michelson

    Thank you for your fast reply,

    But i already tried to replace port->flush with port->clear() , as mentioned in my second reply, and that does not change anything.

    Thanks anyway,

    Regards !



  • What if you were to simply call 'readAll()' after opening the port. If there is data sitting in the read buffer this should clear it. I assume the write buffer is empty at this point.

    I have noticed the QSerialPort (from Qt5) uses buffers. The one I used for Qt4 (QExtSerialPort ?) didn't buffer the data. When I was porting from Qt4 to Qt5 and switched to QSerialPort I had some problems related to this. If it is the write buffer you can call 'waitForBytesWritten(-1)' to block until the write buffer is emptied before closing the port (or some variation of this that doesn't have the potential of freezing the application if data cannot be written).



  • Hi to all,

    The buffer was never empty, cause the readAll() function was not working outside of the SLOTS that are not connected to the readyRead SIGNAL.

    I bypassed the problem using the waitForReadyRead function.

    here is my solution:

    Constructor()
    {
        cmdSerial = new QSerialPort();
        cmdSerial->setPortName("ttyUSB0");
        cmdSerial->setBaudRate(QSerialPort::Baud115200);
        cmdSerial->setParity(QSerialPort::NoParity);
        cmdSerial->setDataBits(QSerialPort::Data8);
        cmdSerial->setStopBits(QSerialPort::OneStop);
        cmdSerial->setFlowControl(QSerialPort::HardwareControl);
        cmdSerial->open(QIODevice::ReadWrite);
    
        connect(this,SIGNAL(isDataPortOpenedChanged(bool)),this,SLOT(checkDataGarbage(bool)));
    }
    

    ...

    void Constructor::checkDataGarbage(bool portOpened)
    {
        if(portOpened)
        {
            while(cmdSerial->waitForReadyRead(100));
            {
                dataSerial->clear(QSerialPort::AllDirections);
            }
    
            connect(cmdSerial,SIGNAL(readyRead()),this,SLOT(onDataReadyRead()));
        }
    }
    

    My buffer is then cleared at the start of the port immediately.

    Problem solved !



  • i have i think, the same problem in QT5.6 beta.

    i have software who received data.
    at the beginning data is not correctly.

    i tried clear after opening() , more data is not correctly.....
    not solved problem.

    i will try this solution.
    problem in linux or MAC.




Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.