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:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSerialPort>
#include <QDebug>
QSerialPort *serial;
MainWindow
::MainWindow(QWidget *parent
) , ui(new Ui::MainWindow)
{
ui->setupUi(this);
serial = new QSerialPort(this);
foreach(QSerialPortInfo port, QSerialPortInfo::availablePorts())
{
serial->setPortName(port.portName());
serial->setBaudRate(QSerialPort::Baud115200);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
if(!(serial->readLine().isNull()))
{
qDebug()<<port.portName()<<"is opened";
}
}
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSerialPort>
#include <QDebug>
QSerialPort *serial;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
serial = new QSerialPort(this);
foreach(QSerialPortInfo port, QSerialPortInfo::availablePorts())
{
serial->setPortName(port.portName());
serial->setBaudRate(QSerialPort::Baud115200);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->open(QIODevice::ReadOnly);
if(!(serial->readLine().isNull()))
{
qDebug()<<port.portName()<<"is opened";
}
}
}
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?
Bookmarks