Important: Please read the Qt Code of Conduct -

QtSerialPort: readline always misses the first char

  • 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

  • Moderators

    @quentinthornton said:

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

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

  • Sorry, I have this in my constructor:

    connect(&serial, SIGNAL(readyRead()),this, SLOT(readFromPort()));//the code I posted earlier is in the 'readFromPort' function.

  • Moderators

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

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

    any ideas? Thanks

  • Lifetime Qt Champion


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

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

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

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

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


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


  • 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

    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?:


  • Lifetime Qt Champion

    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 ?

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

Log in to reply