Important: Please read the Qt Code of Conduct -

QSerialPort doesn't actually print to port

  • I'm trying to make a very simple app in Qt that prints a "G" to a comport, that is connected to a driver through rs232. After sending the G, the light should turn on, and I should get a response from the driver. These are the snippets of code that deals with the serialport:

    The setup:

    ledDriver = new QSerialPort;

    and the writing that happens when you click a button:

        QString temp = "G";
        qDebug() << temp;
        QByteArray ba = temp.toLocal8Bit();
        if (ledDriver->waitForBytesWritten(1000)) {
        } else {
        qDebug() << "Write timed out";
        qDebug() << "Serial is not writable";
    if (ledDriver->waitForReadyRead(1000)) {
        QByteArray responseData = ledDriver->readAll();
        while (ledDriver->waitForReadyRead(10))
            responseData += ledDriver->readAll();
        QString response(responseData);
        qDebug() << "response:" + response;
    } else {
    qDebug() << "Read timed out";

    Now the only response I get is the exact same phrase I send. So if I send G, the response is "G" instead of the number I'm suppose to be getting.

    If I open Putty afterwards, and send anything (doesn't matter what), THEN the G is sendt, the light turns on, and I get the respons I'm suppose to be getting. So it seems like Qt is writing the signal but not sending it? Am I missing something very obvious here?

    Environment is Windows 10 x64 on Qt 5.5.1 with newest MinGW32.

  • I should also add that I have the same issue when using the blockingmaster example (not a large surprise since that's where I've taken much of the code from). BUT the terminal example works perfectly..

  • Qt Champions 2020

    These same and same and same questions appears hundred thousand times per month. And nobody want to search the solution (by googling) himself, it take about 5 minutes. The terminal example sends the 'CR' character when you press to "Enter" button, but your code does not!

  • I understand the frustration about people asking the same questions, I share it equally, but I have been googling this for absolutely days now (and no, my googling skills do not suck). There is ZERO documentation in the code itself, so it's difficult enough to understand what the fuck is going on let alone debug the thing. I did also look through this forum quickly to see if there was a question similar to mine but couldn't find any.

    I thank you for your answer thought. That should help me plenty :)

  • Can you try!

    ledDriver ->write(command.toStdString().c_str());

  • Lifetime Qt Champion

    @Muzab there's absolutely no need to do all these conversions. write takes a const char * or a QByteArray so the first thing to do in simple cases is just not use QString. As for the problem at hand, as @kuzulis noted there is the CR char that is missing from the commands used by @Excludos.

Log in to reply