Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QtSerialPort: readline always misses the first char
Qt 6.12 LTS is out! See what's new in the release blog

QtSerialPort: readline always misses the first char

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 4 Posters 5.3k Views 3 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.
  • quentinthorntonQ Offline
    quentinthorntonQ Offline
    quentinthornton
    wrote on last edited by
    #1

    Hey guys, I've been messing around with QtSerialPort and my arduino lately, and I've come acrossed a problem that I can't seem to figure out. The problem is that it cuts off the first char in the message I send. So if I sent "hello", it would read "ello" in the variable that I stored it in. This is the part, that I think, is where it goes wrong.

    char buf[200];
    serial.readLine(buf, sizeof(buf));//right here
    

    Any ideas? Thanks

    kshegunovK 1 Reply Last reply
    0
    • quentinthorntonQ quentinthornton

      Hey guys, I've been messing around with QtSerialPort and my arduino lately, and I've come acrossed a problem that I can't seem to figure out. The problem is that it cuts off the first char in the message I send. So if I sent "hello", it would read "ello" in the variable that I stored it in. This is the part, that I think, is where it goes wrong.

      char buf[200];
      serial.readLine(buf, sizeof(buf));//right here
      

      Any ideas? Thanks

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @quentinthornton said:

      serial.readLine(buf, sizeof(buf));//right here
      

      Is this all? What about QSerialPort::waitForReadyRead and/or QSerialPort::canReadLine?

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • quentinthorntonQ Offline
        quentinthorntonQ Offline
        quentinthornton
        wrote on last edited by
        #3

        Sorry, I have this in my constructor:

        connect(&serial, SIGNAL(readyRead()),this, SLOT(readFromPort()));//the code I posted earlier is in the 'readFromPort' function.
        
        kshegunovK 1 Reply Last reply
        0
        • quentinthorntonQ quentinthornton

          Sorry, I have this in my constructor:

          connect(&serial, SIGNAL(readyRead()),this, SLOT(readFromPort()));//the code I posted earlier is in the 'readFromPort' function.
          
          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @quentinthornton
          That's certainly curious. What about checking canReadLine before calling the readLine method? Perhaps if the line is not ready, waiting a bit?

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • quentinthorntonQ Offline
            quentinthorntonQ Offline
            quentinthornton
            wrote on last edited by
            #5

            To test that I put this before I read from the serial:

                if (serial.canReadLine()){
                    ui->label_2->setText("Can read line");
                }
            

            and everytime I send something, label_2 says "can read line".

            Also, it seems like it is receiving the first char, but its....weird. This is exactly what the top of my readFromPort function looks like:

                if (serial.canReadLine()){
                    ui->label_2->setText("Can read line");
                }
            
                serial.readLine(buf, sizeof(buf));
                ui->label->setText(buf);//when I send something to it, this should show everything I sent in the text label, but instead, it shows everything except for the first character.
            
                QString period = ".";
                QString sentence(buf);
            

            I never change the value of sentence, but when I do this:

            ui->textEdit->setText(ui->textEdit->toPlainText() + sentence);//it shows the whole thing.
            //it also doesn't work when I do this:
            ui->textEdit->setText(sentence);
            

            any ideas? Thanks

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

              Hi,

              Maybe a silly question but what would your first char be ? Is it a standard text ? Special character ?

              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
              1
              • quentinthorntonQ Offline
                quentinthorntonQ Offline
                quentinthornton
                wrote on last edited by
                #7

                Standard text. I'm sending sentences, like "hello world".

                1 Reply Last reply
                0
                • quentinthorntonQ Offline
                  quentinthorntonQ Offline
                  quentinthornton
                  wrote on last edited by quentinthornton
                  #8

                  Oh, and by the way, to make sure the arduino was sending the text properly, I've tested it with a program called serial port monitor by eltima. So basically, I'm pretty sure the device sending the data is working fine. On top of that, like I said earlier, when I do this:

                  ui->textEdit->setText(ui->textEdit->toPlainText() + sentence);
                  

                  the first character magically appears back on there.

                  1 Reply Last reply
                  0
                  • quentinthorntonQ Offline
                    quentinthorntonQ Offline
                    quentinthornton
                    wrote on last edited by
                    #9

                    I've just tested again, and now I'm completely confused. It usually cuts off the first character, but when I do longer sentences (more than 2 or 3 words) it cuts off the first little bit. It seems like it's completely random on how much it cuts off. For example, I said "it's a small world" a bunch of times, and it said on the label:
                    first time: ld
                    second time: ld
                    third time: t's a small world
                    fourth time: (nothing)
                    fifth time: d
                    sixth time: d
                    seventh: (nothing)
                    eighth: t's a small world
                    But when I do just one word, it consistently cuts off the first character.

                    1 Reply Last reply
                    0
                    • Paul ColbyP Offline
                      Paul ColbyP Offline
                      Paul Colby
                      wrote on last edited by
                      #10

                      Is it possible your slot is being called multiple times, and the subsequent invocations are overwriting the text?

                      eg try:

                      const qint64 bytesRead = serial.readLine(...);
                      qDebug() << bytesRead << buf;
                      

                      and

                       ui->label->setText(ui->label->text() + buf);
                      

                      Cheers.

                      1 Reply Last reply
                      1
                      • quentinthorntonQ Offline
                        quentinthorntonQ Offline
                        quentinthornton
                        wrote on last edited by quentinthornton
                        #11

                        I tried what you said, and the it printed out:

                        1 i
                        19 t's a small world

                        1 i
                        16 t's a small worl
                        3 d

                        1 i
                        17 t's a small world
                        2

                        The label is now showing the right thing, but it splits it up, like up there, when I try to do what I want with the data. Why does this work:

                        ui->label->setText(ui->label->text() + buf);
                        ui->textEdit->setText(ui->textEdit->toPlainText()+ sentence);
                        
                        

                        and not this?:

                        ui->label->setText(buf);
                        ui->textEdit->setText(sentence);
                        
                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          One thing to take into account in serial port communication is that you might not get the whole data sent from the other side in one go because of hardware/os buffering. That's why there's usually a protocol defining e.g. an end of data packet so you can use a buffer to store incoming bytes and when that end sequence is detected then you parse what you received for further processing.

                          However why you receive only on byte each time is a bit of a mystery…

                          Are you sure the flow control is setup correctly ? Does your device use the data terminal ready signal ?

                          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
                          0
                          • quentinthorntonQ Offline
                            quentinthorntonQ Offline
                            quentinthornton
                            wrote on last edited by
                            #13

                            Everybody that tries to communicate with the arduino, uses no flow control. So that's what I did, no flow control. As of the data terminal ready signal, nobody seems to use it either, so I don't use that too.

                            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
                            • Unsolved