Important: Please read the Qt Code of Conduct -

I can not read from Serial Port

  • I am a very newbei in QT. My OS is Window 8.1 and the QT version is 5.4.1. I want to control devices with QSerialPort. The way that a device to computer is [device (RS-232)--FDTI --- computer(USB)]. By using the examples in QTCreator, I can confirm the chip, FDTI, on the converter can be recognized by QT. However, when I try to write to and read from a device, nothing is output. Can anyone point out what I am missing?

     #include <QCoreApplication>
    #include <iostream>
    #include <QtSerialPort/QSerialPortInfo>
    #include <QtSerialPort/QSerialPort>
    int main(int argc, char *argv[])
        QCoreApplication a(argc, argv);
        QList<QSerialPortInfo> com_ports = QSerialPortInfo::availablePorts();
        int Rcount;
        bool test;
        char data[2000]="";
        foreach(const QSerialPortInfo &info,com_ports){
             QString qs(info.portName());
             std::string current_locale_text = qs.toLocal8Bit().constData();
             std::cout<< current_locale_text<<std::endl;
             QSerialPort serial;
                 test= serial.setBaudRate(QSerialPort::Baud9600)&&
             if (test==1)
                 std::cout<<"not work\n";
             QTime dieTime=QTime::currentTime().addMSecs(1);
             while(strlen(data)==0 && Rcount<1000000){
             std::cout<<"\n\n"<<Rcount<<"\n\n end"<<std::endl;
        return a.exec();

    [edit: added missing coding tags: 3 ` before and after the code SGaist]

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    First thing, you continue your program flow even if the port opening fails so how do you know it's currently opened ?
    Then "test" is just declared, not use nor initialized so your if won't really tell you anything useful.

    Last thing, your use of QSerialPort is not correct. Have a look at the Terminal example that comes with the module to see how you can use it asynchronously.

  • @SGaist
    Thank you for pointing out the drawback in my code. At first, I just want to simplify the program to find out the problem, since that kind of sequential code work out with BCB in Win XP, which shows that the device is functional.

     By the way, I use the example "Terminal " to test the write and read function to the device. However, It still does not feedback anything. I Even modify the code in Terminal example
    • void MainWindow::writeData(const QByteArray &data)
      serial->write("IDN?\n"); //Write a char command, triggered by any keyboard event
      qDebug() << "error: " << serial->errorString() << endl; // Check whether "writeData" is being executed or not.

    I still can not see anything updated to the console, neither in Win7 nor in Win 8.1.

    Are there other things I should check first?

  • I forgot to say that "Terminal" can catch com_port; However, when I change the baudrate setting as 19200 on device, "Terminal" still show the detected baudrate as 9600.

  • Lifetime Qt Champion

    AFAIK, there's no detection going on, you have to tell the terminal at which speed it should communicate with your device

Log in to reply