From 558dfbcfc669ad381bc0ab09bf81b512db36701c Mon Sep 17 00:00:00 2001 From: HoshinoSuzumi Date: Thu, 12 Dec 2024 19:29:08 +0800 Subject: [PATCH] final --- mainwindow.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++------- mainwindow.h | 7 ++++++ mainwindow.ui | 45 ++++++++++++++++------------------- 3 files changed, 82 insertions(+), 34 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index bbf49e6..3f7fc3a 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -11,6 +11,16 @@ MainWindow::MainWindow(QWidget *parent) ui->lcdNumber_temp->setDigitCount(8); initFigure(); + + ui->frame_body_status->setAutoFillBackground(true); + ui->frame_gas_status->setAutoFillBackground(true); + + QTimer *clock_tmr = new QTimer(this); + clock_tmr->setTimerType(Qt::PreciseTimer); + clock_tmr->start(); + connect(clock_tmr, &QTimer::timeout, this, [=]() { + ui->label_time->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")); + }); } MainWindow::~MainWindow() @@ -18,7 +28,7 @@ MainWindow::~MainWindow() if (m_serialPort->isOpen()) { m_serialPort->clear(); m_serialPort->close(); - qDebug() << "exiting, clear and close serial port..."; + qDebug() << "[!] exiting, close and clear serial port..."; } delete m_serialPort; delete ui; @@ -32,22 +42,39 @@ void MainWindow::msgBox(QWidget* ctx, QString content) { msgBox(ctx, "title", content); } +uint16_t m_buff = 0x0000; + void MainWindow::receiveData() { QByteArray msg = m_serialPort->readAll(); - qDebug() << "\nreceived data:" << msg.count() << msg.toHex(); + if (msg.isEmpty()) return; + + QByteArray bufferArray; + + if(COMPATIBILITY_MODE == 1) { + for (int i = 0; i < msg.size(); ++i) { + uint8_t byte = static_cast(msg.at(i)); + m_buff = ((m_buff << 8) & 0xFF00) | byte; + bufferArray.append(static_cast((m_buff >> 8) & 0xFF)); // MSB + bufferArray.append(static_cast(m_buff & 0xFF)); // LSB + } + } else { + bufferArray = msg; + } + + qDebug() << "\n[<] received data:" << bufferArray.toHex(); uint16_t LSB, MSB; int count = 0; - if (msg.count() % 2 == 0) { - count = msg.count() / 2; + if (bufferArray.count() % 2 == 0) { + count = bufferArray.count() / 2; for (int i = 0; i < count; i ++) { - MSB = ((uchar) msg.at(2 * i)) << 8; - LSB = (uchar) msg.at(2 * i + 1); + MSB = ((uchar) bufferArray.at(2 * i)) << 8; + LSB = (uchar) bufferArray.at(2 * i + 1); uint16_t tmp = MSB + LSB; uchar t_cmd = tmp >> 12; uint16_t t_data = tmp & 0xFFF; - qDebug() << "CMD: " << t_cmd << " DATA: " << t_data; + qDebug() << "CMD: " << t_cmd << "\t DATA: " << t_data; if (t_cmd == MCU_CMD_TEMP) { processTempData(t_data); @@ -63,7 +90,7 @@ void MainWindow::receiveData() { } } } else { - qDebug() << "\n [!] invalid command received"; + qDebug() << "\n[!] invalid command received"; } } @@ -133,7 +160,15 @@ void MainWindow::processTempData(uint data) { } void MainWindow::processBodyData(uint data) { - + QColor status_color; + if (data == MCU_CMD_BODY_YES) { + status_color.setRgb(255, 0, 0); + ui->frame_body_status->setPalette(status_color); + } + if (data == MCU_CMD_BODY_NO) { + status_color.setRgb(0, 255, 0); + ui->frame_body_status->setPalette(status_color); + } } void MainWindow::processGasState(uint data) { @@ -351,3 +386,14 @@ void MainWindow::on_pushButton_temp_details_clicked() dialog->exec(); } + +void MainWindow::on_pushButton_clicked() +{ + ui->comboBox_sys_mode->setCurrentIndex(0); + ui->comboBox_temp_fetch->setCurrentIndex(0); + ui->comboBox_body_fetch->setCurrentIndex(0); + ui->comboBox_sys_mode->setCurrentIndex(2); + ui->comboBox_temp_fetch->setCurrentIndex(1); + ui->comboBox_body_fetch->setCurrentIndex(2); +} + diff --git a/mainwindow.h b/mainwindow.h index eb8c0ea..098dc66 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -2,6 +2,7 @@ #define MAINWINDOW_H #define _DISPLAY_DATA_LENGTH_ 201 +#define COMPATIBILITY_MODE 1 #include #include @@ -15,6 +16,10 @@ #include #include +#include +#include +#include + QT_BEGIN_NAMESPACE class QLineSeries; class QChart; @@ -65,6 +70,8 @@ private slots: void on_pushButton_temp_details_clicked(); + void on_pushButton_clicked(); + private: Ui::MainWindow *ui; diff --git a/mainwindow.ui b/mainwindow.ui index 232f9e5..6cb6b5c 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -14,7 +14,7 @@ - Serialicon by Tmithy Yin + Serialicon by Timothy Yin @@ -1309,33 +1309,15 @@ - - - - - - 当前气体浓度: 0.00 - - - - - - - 最高气体浓度: 0.00 - - - - - - - - - + - 气体监测详情 + 0000-00-00 00:00:00 + + + Qt::AlignmentFlag::AlignCenter @@ -1389,7 +1371,7 @@ - 0 + 1 @@ -1436,6 +1418,19 @@ + + + + 50 + 400 + 80 + 26 + + + + 一键启动 + +