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. canReadLine QIODevice
Qt 6.11 is out! See what's new in the release blog

canReadLine QIODevice

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.1k 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.
  • W Offline
    W Offline
    Wuzi
    wrote on last edited by Wuzi
    #1

    Hello,

    I'm trying to implement a serial connection with QIODevice (SerialPort). I would like to parse line by line (each line consists of multiple columns of data) and for this I found canReadLine().
    But as I understand I have to use startTransaction, because otherwise the buffer will not be used. I'm I correct?

    bool QIODevice::canReadLine() const
    {
        Q_D(const QIODevice);
        return d->buffer.indexOf('\n', d->buffer.size(),
                                 d->isSequential() ? d->transactionPos : Q_INT64_C(0)) >= 0;
    }
    

    So to read a full line, I have to open a QSerialPort and then start a Transaction. Then I have to wait a little bit (till data is in the buffer) and then I can use canReadLine. Am I correct? Is there a better way instead to wait a little bit? Or is it the wrong way to go?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Why not use QSerialPort, cumulate arrived data in a QByteArrsy and then apply the line check to the cumulated data ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • W Offline
        W Offline
        Wuzi
        wrote on last edited by
        #3

        I'm using QSerialPort. But then I have to implement a ringbuffer my self to parse always complete lines

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

          What are problem in this approach?

          Foo::onReadyRead()
          {
              while (m_serial->canReadLine()) {
                  const QByteArray line = m_serial->readLine();
              }
          }
          
          W 1 Reply Last reply
          2
          • K kuzulis

            What are problem in this approach?

            Foo::onReadyRead()
            {
                while (m_serial->canReadLine()) {
                    const QByteArray line = m_serial->readLine();
                }
            }
            
            W Offline
            W Offline
            Wuzi
            wrote on last edited by
            #5

            And otherwise I return without doing something and wait for the next onReadyRead signal?
            I will try it out

            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