I am trying to make QT automatically connect to my ST32 MCU by serial. MCU is keep sending data for each second. So I thought I can read all the available COM Ports and when the line is not null, I will detect that COM Port is MCU. But All COM Ports are null. Here is related piece of code I am using in QT:
Qt Code:
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QtSerialPort>
  4. #include <QDebug>
  5. QSerialPort *serial;
  6. MainWindow::MainWindow(QWidget *parent)
  7. : QMainWindow(parent)
  8. , ui(new Ui::MainWindow)
  9. {
  10. ui->setupUi(this);
  11. serial = new QSerialPort(this);
  12. foreach(QSerialPortInfo port, QSerialPortInfo::availablePorts())
  13. {
  14. serial->setPortName(port.portName());
  15. serial->setBaudRate(QSerialPort::Baud115200);
  16. serial->setDataBits(QSerialPort::Data8);
  17. serial->setParity(QSerialPort::NoParity);
  18. serial->setStopBits(QSerialPort::OneStop);
  19. serial->setFlowControl(QSerialPort::NoFlowControl);
  20. serial->open(QIODevice::ReadOnly);
  21.  
  22. if(!(serial->readLine().isNull()))
  23. {
  24. qDebug()<<port.portName()<<"is opened";
  25. }
  26.  
  27. }
  28. }
To copy to clipboard, switch view to plain text mode 

I believe 1 second is not enough for QT to detect that. Maybe I should use timeout in QT. Any idea how can I solve this problem?