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>
    #include<QString>
    #include<QTime>
    
    QT_USE_NAMESPACE
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        QList<QSerialPortInfo> com_ports = QSerialPortInfo::availablePorts();
        int Rcount;
        bool test;
        char data[2000]="";
        std::cout<<com_ports.count()<<std::endl;
        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;
             serial.setPort(info);
             if(serial.open(QIODevice::ReadWrite)){
                 test= serial.setBaudRate(QSerialPort::Baud9600)&&
                 serial.setParity(QSerialPort::NoParity)&&
                 serial.setStopBits(QSerialPort::OneStop)&&
                 serial.setFlowControl(QSerialPort::NoFlowControl)&&
                 serial.setDataBits(QSerialPort::Data8);
    
             }
            
             if (test==1)
                 std::cout<<"work\n";
             else
                 std::cout<<"not work\n";
    
             serial.write("*IDN?\n");
             QTime dieTime=QTime::currentTime().addMSecs(1);
    
                   while(QTime::currentTime()<dieTime)
             {
                 QCoreApplication::processEvents(QEventLoop::AllEvents,100);
             }
    
             Rcount=0;
             while(strlen(data)==0 && Rcount<1000000){
                serial.read(data,1999);
                std::cout<<data;
                Rcount++;
             }
             serial.read(data,1999);
    
             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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.