Important: Please read the Qt Code of Conduct -

QTSerialPort read/write

  • Hi,
    I'm using QT 4.8 with qtserialport and i am having some trouble communicating with the device. Here is my simple main function
    int main(int argc, char *argv[])
    QCoreApplication a(argc, argv);
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
    qDebug() << "Name : " << info.portName();
    qDebug() << "Description : " << info.description();
    qDebug() << "Manufacturer: " << info.manufacturer();

            // Example use QSerialPort
            QSerialPort serial;
            if ({
                qDebug()<<"Opened successfully";
            if (!serial.waitForReadyRead(1000))
    return a.exec();


    PSN should return the product serial number and the device.
    i keep getting "failed" as an output and readAll() gives me "" in the console. Can anybody give me a piece of code I can use to make it work without redirecting me to other posts or examples that came with qtserialport, i wasted hours there.
    Reading/Writing to the device in MATLAB and putty works as expected. Its just in QT that i get no response.

  • Hi and welcome to devnet

    this code

    qDebug() << QByteArray("\0 1 2 3 4 5 6 7");

    produces "".
    You must check if the received buffer contains or begins with 0x00

    check with

    qDebug() << serial.bytesAvailable();
    qDebug() << serial.readAll().size();

    if the buffer is not empty

  • @mcosta Thanks for the help.
    qDebug() << serial.bytesAvailable();
    qDebug() << serial.readAll().size();
    both output 0 which means the buffer is empty.
    From past experience, the buffer remains empty for about 60-90 ms after a command is sent, i believe waiting for 1000ms using the waitForReadyRead(1000) would have given enough time for the buffer to fill.
    Is the problem thus with the .write() function?

  • Sorry,

    I don't have so much experience with QtSerialPort. but if waitForReadyRead returns true means something is arrived on the port

  • You didn't mention what device you are trying to talk to...

    I'm wondering if PSN is the command to get the product serial number but AFTER that command you need to send a CR or LF? So something like:



    serial.putChar( 0x13 );

    The reason I bring this up is in putty you may have a CR going out without knowing it.

    The other thing you might try is connecting up the serial port readyRead() signal to a slot and see if it ever gets called. This is an indication data is coming in.

  • I have never tried using QSerialPort outside of the message loop (i.e. 'a.exec()' from main() in your program). I do get the impression that it relies on the message loop to function properly so this test may not work.

    If you modify your test program a bit I suspect it will work:

    • Create a class derived from QWidget
    • Add a 'Test' button with associated slot
    • Put all your test code in the test slot.

    In Qt4 you had to use the external QSerialDevice class (this, or some variation of it, was migrated into Qt5 but didn't exist internally as part of Qt4). There was something about the open command mode options that I ran into, I don't remember the details unfortunately. There is a namespace called 'AbstractSerial' with various options, at least in the version I have. You might want to use this instead:

            if ({
                qDebug()<<"Opened successfully";

Log in to reply