Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. (QtSerialPort) Linux Serial port buffer is not empty when opening port

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

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 8.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mostefa
    wrote on last edited by mostefa
    #1

    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...

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kuzulis
      Qt Champions 2020
      wrote on last edited by
      #2

      Try QSerialPort::clear().

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mostefa
        wrote on last edited by
        #3

        @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 !

        1 Reply Last reply
        0
        • M Offline
          M Offline
          michelson
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mostefa
            wrote on last edited by
            #5

            @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 !

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Rondog
              wrote on last edited by
              #6

              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).

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mostefa
                wrote on last edited by mostefa
                #7

                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 !

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kickoune103
                  wrote on last edited by kickoune103
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kickoune103
                    wrote on last edited by
                    #9

                    after my open i have write your loop,
                    but not pass inside the loop.

                    https://bugreports.qt.io/browse/QTBUG-50415
                    https://forum.qt.io/topic/62753/stranges-characters-first-ready-read-qserialport-linux-or-mac

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved