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<uint8_t>(msg.at(i)); + m_buff = ((m_buff << 8) & 0xFF00) | byte; + bufferArray.append(static_cast<char>((m_buff >> 8) & 0xFF)); // MSB + bufferArray.append(static_cast<char>(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 <QMainWindow> #include <QMessageBox> @@ -15,6 +16,10 @@ #include <QtCharts/QChart> #include <QtCharts/QValueAxis> +#include <QTime> +#include <QTimer> +#include <QDateTime> + 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 @@ </rect> </property> <property name="windowTitle"> - <string>Serialicon by Tmithy Yin</string> + <string>Serialicon by Timothy Yin</string> </property> <widget class="QWidget" name="centralwidget"> <widget class="QWidget" name="horizontalLayoutWidget_10"> @@ -1309,33 +1309,15 @@ </property> </widget> </item> - <item> - <layout class="QHBoxLayout" name="horizontalLayout_9"> - <item> - <widget class="QLabel" name="label_gas_current"> - <property name="text"> - <string>当前气体浓度: 0.00</string> - </property> - </widget> - </item> - <item> - <widget class="QLabel" name="label_gas_max"> - <property name="text"> - <string>最高气体浓度: 0.00</string> - </property> - </widget> - </item> - </layout> - </item> <item> <layout class="QHBoxLayout" name="horizontalLayout_10"> <item> - <widget class="QLineEdit" name="lineEdit"/> - </item> - <item> - <widget class="QPushButton" name="pushButton_gas_details"> + <widget class="QLabel" name="label_time"> <property name="text"> - <string>气体监测详情</string> + <string>0000-00-00 00:00:00</string> + </property> + <property name="alignment"> + <set>Qt::AlignmentFlag::AlignCenter</set> </property> </widget> </item> @@ -1389,7 +1371,7 @@ <item> <widget class="QComboBox" name="comboBox_baud"> <property name="currentIndex"> - <number>0</number> + <number>1</number> </property> <item> <property name="text"> @@ -1436,6 +1418,19 @@ </layout> </widget> </widget> + <widget class="QPushButton" name="pushButton"> + <property name="geometry"> + <rect> + <x>50</x> + <y>400</y> + <width>80</width> + <height>26</height> + </rect> + </property> + <property name="text"> + <string>一键启动</string> + </property> + </widget> </widget> <widget class="QMenuBar" name="menubar"> <property name="geometry">