[solved] serial port read problem

  • Hello everybody,

    i have a little prblem with serial port respectively with the answer i got from my device.

    i write to the device serial.write("test\r\n");
    and the device answer "test ok".

    this works fine.

    i will analyze the answer in qt to find out which command should send next.
    and here is the problem my testvariable "s" is filled with both the write request and the answer:

    test ok

    Is it possible to get only the answer?

    Here is my code:

    using namespace std;
    QSerialPort serial;

    int main(int argc, char *argv[])
    QCoreApplication a(argc, argv);

     if (!serial.open(QIODevice::ReadWrite))
        qDebug() << "Fehler -- Verbindung konnte nicht hergestellt werden" << endl;
        return 0;
    } else {
         qDebug() << "OK -- Verbindung hergestellt"  << endl;
            QByteArray DataReceive = serial.readLine();
                     DataReceive += serial.readLine();
            QByteArray response = DataReceive;
            QString s(response);
            qDebug() << "Antwort: " << s << "ende";
    return a.exec(&#41;;


    Thanks for helping :-)
    Regards Alex

  • Qt Champions 2020

    if (!serial.open(QIODevice::ReadWrite))
    qDebug() << "Fehler -- Verbindung konnte nicht hergestellt werden" << endl;
    return 0;
    } else {
    bool success = serial.setBaudRate(QSerialPort::Baud9600)
    & serial.setParity(QSerialPort::NoParity)
    & serial.setDataBits(QSerialPort::Data8)
    & serial.setStopBits(QSerialPort::OneStop)
    & serial.setFlowControl(QSerialPort::NoFlowControl);
    qDebug() << success ? "OK" : "FAIL"<< endl;

        if (!success)
            return 0;
        serial.waitForBytesWritten(1000); // or serial.flush()


    Please see examples and read documentaion.

  • Hello kuzulis,

    thanks for you answer :-)

    this is a difrent way to get a serial connection. That is not the problem. My script is working.

    But my output is wrong
    when i write test i got in my anser bot my written test and the return value test ok

    and i have no idea to filter the serial.write command from the return value

  • I think your device is simply echoing the command that it's acknowledging. I know from experience, that some devices do that.
    To test this hypothesis, connect to your serial device via some terminal application (e.g. "putty":http://www.putty.org/). If you see the "test ok" response too, then it indeed is just the device echoing the command.

    From there you have two options:
    Remember the command and literally remove it from the response, if it matches. Something like this:
    @if (s.startsWith(theCommand))
    s.remove(0, theCommand.size());@

    The other option might be to tell the device to stop echoing. Most devices that echo have a command for that.

  • Hi Manu,

    no the device doesn`t echoing the command. I have programmed it by myself and test all with putty before i started programming in qt

    if i type down my commands via putty by hand all works.

    I think the problem is serial.write(); it echos the command to the commandline and serial.readAll(); reads it.

    Output Putty:
    test ok (but i see here what i wrote in by hand)

    your solution to remember the command is a good idea :-)
    but i thought someone knows why qt reads the write line

  • Qt Champions 2020

    Yes, also problem with an echo can be occurs from an "bad" of RS 422/485 converter. I mean that exists such converters where an input/output direction control is processed with an "manual" set/reset level on the DTR pin.

    But it is very cheap and very old converters. Usual, for these devices (good devices) the direction handles automatically by HW himself.

    Of course, this problem can be if you use an such converter.. :)

    UPD: QtSerialPort does not read back an write data! Somewhere you something do wrong... :)

  • I found the solution:

    in qt console i got quotes ("") throug in and output like that:


    and then the output is

    test ok"

    i had to play around with \r and \r\n on both sides.
    This brought me the Solution

Log in to reply