使用定时器的方式,实时更新串口信息
timer = new QTimer; //定时扫描和更新串口
connect(timer,&QTimer::timeout,this,&MainWindow::showValidPort);//实时更新端口号
timer->start(1000); //每1000ms定时检测串口状态
//更新并检测串口函数实现
void MainWindow::showValidPort()
{
QStringList newPortStringList;
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
newPortStringList += info.portName();
if(newPortStringList.size() != portStringList.size())
{
portStringList = newPortStringList;
ui->comnums->clear();
ui->comnums->addItems(portStringList);
}
if(currentCOM != ui->comnums->currentText()) //串口突然断开连接了
{
currentCOM = ui->comnums->currentText();
}
}