添加升级串口打开失败后继续尝试的逻辑。

This commit is contained in:
luocai 2024-08-09 18:10:24 +08:00
parent 9d495d487d
commit 928326efb2
3 changed files with 14 additions and 4 deletions

View File

@ -13,6 +13,7 @@
#include <QProgressBar> #include <QProgressBar>
#include <QPushButton> #include <QPushButton>
#include <QSerialPortInfo> #include <QSerialPortInfo>
#include <QTimer>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <filesystem> #include <filesystem>
@ -53,7 +54,8 @@ Widget::Widget(QWidget *parent) : QWidget(parent) {
setAcceptDrops(true); setAcceptDrops(true);
} }
Widget::~Widget() {} Widget::~Widget() {
}
void Widget::start() { void Widget::start() {
m_progressBar->setValue(0); m_progressBar->setValue(0);
@ -90,6 +92,9 @@ void Widget::start() {
void Widget::onCdcDeviceDiscovered(const QSerialPortInfo &info) { void Widget::onCdcDeviceDiscovered(const QSerialPortInfo &info) {
auto status = m_updater->open(info); auto status = m_updater->open(info);
LOG(info) << "open cdc port: " << info.portName().toStdString() << ", status: " << status; LOG(info) << "open cdc port: " << info.portName().toStdString() << ", status: " << status;
if (!status) {
QTimer::singleShot(0, this, [this]() { m_updater->startSearchDevice(); });
}
} }
void Widget::onSelectButtonClicked() { void Widget::onSelectButtonClicked() {

View File

@ -42,9 +42,7 @@ CdcUpdater::~CdcUpdater() {
void CdcUpdater::start(const QString &path, const QSerialPortInfo &info) { void CdcUpdater::start(const QString &path, const QSerialPortInfo &info) {
if (info.isNull()) { if (info.isNull()) {
if (m_timerId < 0) { startSearchDevice();
m_timerId = startTimer(1000);
}
} else { } else {
open(info); open(info);
} }
@ -54,6 +52,12 @@ void CdcUpdater::start(const QString &path, const QSerialPortInfo &info) {
emit progressChanged(++m_progress); emit progressChanged(++m_progress);
} }
void CdcUpdater::startSearchDevice() {
if (m_timerId < 0) {
m_timerId = startTimer(1000);
}
}
bool CdcUpdater::open(const QSerialPortInfo &info) { bool CdcUpdater::open(const QSerialPortInfo &info) {
m_serialPort = std::make_shared<QSerialPort>(info); m_serialPort = std::make_shared<QSerialPort>(info);
connect(m_serialPort.get(), &QSerialPort::readyRead, this, &CdcUpdater::onReadyRead); connect(m_serialPort.get(), &QSerialPort::readyRead, this, &CdcUpdater::onReadyRead);

View File

@ -37,6 +37,7 @@ public:
CdcUpdater(QObject *parent = nullptr); CdcUpdater(QObject *parent = nullptr);
~CdcUpdater(); ~CdcUpdater();
void start(const QString &path, const QSerialPortInfo &info = QSerialPortInfo()); void start(const QString &path, const QSerialPortInfo &info = QSerialPortInfo());
void startSearchDevice();
bool open(const QSerialPortInfo &info); bool open(const QSerialPortInfo &info);
static std::optional<QSerialPortInfo> searchDevice(); static std::optional<QSerialPortInfo> searchDevice();