2025-10-11T11:07:19

This commit is contained in:
yuntang 2025-10-11 11:07:20 +08:00
parent 13d2432c71
commit 605bed0ce7
5 changed files with 61 additions and 6 deletions

View File

@ -9,7 +9,7 @@ CONFIG+=precompile_header
PRECOMPILED_HEADER=stable.h
#
VERSION = 1.3.0
VERSION = 1.3.4
# 设置目标文件名,包含版本号
TARGET = DSCAnalysisTool_$${VERSION}

View File

@ -127,6 +127,8 @@ void MainWindow::connections() {
connect(SerialPort::instance(), &SerialPort::sigStartExperiment,
this, &MainWindow::slotStartExperiment);
connect(SerialPort::instance(), &SerialPort::sigDeviceDisconnected,
this, &MainWindow::slotDeviceDisconnected);
#endif
// mode
@ -422,7 +424,7 @@ void MainWindow::startExperimentByDeviceInfo() {
Global::_mode = Global::Mode::Experiment;
_manuallyStopTheExperimentFlag = false;
_centralWidget->startExperiment();
// 实验开始,清除上一个实验所有数据。
Global::clearExperimentData();
}
@ -594,7 +596,7 @@ void MainWindow::on_actionPeakSynthesisAnalysis_triggered() {
void MainWindow::on_actionClearAllData_triggered() {
// 实验过程中,不允许清除数据。
if(Global::_mode == Global::Mode::Experiment) {
if (Global::_mode == Global::Mode::Experiment) {
return;
}
@ -805,3 +807,19 @@ void MainWindow::showMesgBox(const QString str) {
isShow = false;
}
}
void MainWindow::slotDeviceDisconnected() {
// logde<<"slotDeviceDisconnected...1";
// 如果当前是实验模式时,需要先停止实验。
if (SerialPort::instance()->isOpen()) {
// logde<<"slotDeviceDisconnected...2";
// ui更新断开连接
ui->actionConnectToDev->setIcon(QIcon(":/images/connect.png"));
ui->actionConnectToDev->setText("连接设备");
// 停止实验
on_actionStop_triggered();
SerialPort::instance()->closeSp();
}
}

View File

@ -43,6 +43,8 @@ public slots:
void slotOITAutoAnalysis(const double,const double);
void slotStartExperiment();
void slotDeviceDisconnected();
protected:
void closeEvent(QCloseEvent *event) override;
private slots:

View File

@ -19,7 +19,13 @@ const u16 conVid = 1155; // 0x0483
const u16 conPid = 22336; // 0x5740
SerialPort::SerialPort(QObject *parent)
: QObject(parent), _sp(new QSerialPort(this)) {
: QObject(parent), _sp(new QSerialPort(this)),
_portCheckTimer(new QTimer(this)) {
//
connect(_portCheckTimer, &QTimer::timeout,
this, &SerialPort::slotPortCheck);
_portCheckTimer->start(1000); // 每秒检测一次
// displayPortInfo();
#if 0
@ -80,9 +86,13 @@ SerialPort::~SerialPort() {
logde << "serialport destructor.";
closeSp();
//
delete _sp;
_sp = nullptr;
//
_portCheckTimer->stop();
delete _portCheckTimer;
_portCheckTimer = nullptr;
}
void SerialPort::slotReadData() {
@ -594,3 +604,22 @@ void SerialPort::slotCloseSp() {
_sp->close();
}
}
void SerialPort::slotPortCheck() {
// logde<<"slotPortCheck...";
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
u16 pid = info.productIdentifier();
u16 vid = info.vendorIdentifier();
if ((pid == conPid) && (vid == conVid)) {
// logde<<"slotPortCheck... device connected.";
return;
}
}
// Device disconnected
// logde<<"device disconnected.";
emit sigDeviceDisconnected();
// logde<<"slotPortCheck... device disconnected.";
}

View File

@ -4,6 +4,7 @@
#include <QObject>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <qtimer.h>
#include "defines.h"
#include "protocol.h"
@ -36,12 +37,14 @@ signals:
void sigUpdateStatusbarMsg(const QString &);
void sigSaveExperimentalDataMsgBox();
void sigStartExperiment();
void sigDeviceDisconnected();
public slots:
void slotDeliverData(const QByteArray &);
void slotSendData(const QByteArray &);
void slotCloseSp();
private slots:
void slotReadData();
void slotPortCheck();
private:
void updateStatus(const CommonData &cd);
void updateAxis();
@ -52,6 +55,9 @@ private:
SerialPort(QObject *parent = nullptr);
QSerialPort *_sp;
QByteArray _lastWriteBa;
// port check
QTimer* _portCheckTimer = nullptr;
};
#endif // SERIALPORT_H
#endif // SERIALPORT_H