This commit is contained in:
Timothy Yin 2024-12-12 19:29:08 +08:00
parent 18d7395f11
commit 558dfbcfc6
3 changed files with 82 additions and 34 deletions

View File

@ -11,6 +11,16 @@ MainWindow::MainWindow(QWidget *parent)
ui->lcdNumber_temp->setDigitCount(8); ui->lcdNumber_temp->setDigitCount(8);
initFigure(); 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() MainWindow::~MainWindow()
@ -18,7 +28,7 @@ MainWindow::~MainWindow()
if (m_serialPort->isOpen()) { if (m_serialPort->isOpen()) {
m_serialPort->clear(); m_serialPort->clear();
m_serialPort->close(); m_serialPort->close();
qDebug() << "exiting, clear and close serial port..."; qDebug() << "[!] exiting, close and clear serial port...";
} }
delete m_serialPort; delete m_serialPort;
delete ui; delete ui;
@ -32,22 +42,39 @@ void MainWindow::msgBox(QWidget* ctx, QString content) {
msgBox(ctx, "title", content); msgBox(ctx, "title", content);
} }
uint16_t m_buff = 0x0000;
void MainWindow::receiveData() { void MainWindow::receiveData() {
QByteArray msg = m_serialPort->readAll(); 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; uint16_t LSB, MSB;
int count = 0; int count = 0;
if (msg.count() % 2 == 0) { if (bufferArray.count() % 2 == 0) {
count = msg.count() / 2; count = bufferArray.count() / 2;
for (int i = 0; i < count; i ++) { for (int i = 0; i < count; i ++) {
MSB = ((uchar) msg.at(2 * i)) << 8; MSB = ((uchar) bufferArray.at(2 * i)) << 8;
LSB = (uchar) msg.at(2 * i + 1); LSB = (uchar) bufferArray.at(2 * i + 1);
uint16_t tmp = MSB + LSB; uint16_t tmp = MSB + LSB;
uchar t_cmd = tmp >> 12; uchar t_cmd = tmp >> 12;
uint16_t t_data = tmp & 0xFFF; 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) { if (t_cmd == MCU_CMD_TEMP) {
processTempData(t_data); processTempData(t_data);
@ -133,7 +160,15 @@ void MainWindow::processTempData(uint data) {
} }
void MainWindow::processBodyData(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) { void MainWindow::processGasState(uint data) {
@ -351,3 +386,14 @@ void MainWindow::on_pushButton_temp_details_clicked()
dialog->exec(); 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);
}

View File

@ -2,6 +2,7 @@
#define MAINWINDOW_H #define MAINWINDOW_H
#define _DISPLAY_DATA_LENGTH_ 201 #define _DISPLAY_DATA_LENGTH_ 201
#define COMPATIBILITY_MODE 1
#include <QMainWindow> #include <QMainWindow>
#include <QMessageBox> #include <QMessageBox>
@ -15,6 +16,10 @@
#include <QtCharts/QChart> #include <QtCharts/QChart>
#include <QtCharts/QValueAxis> #include <QtCharts/QValueAxis>
#include <QTime>
#include <QTimer>
#include <QDateTime>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QLineSeries; class QLineSeries;
class QChart; class QChart;
@ -65,6 +70,8 @@ private slots:
void on_pushButton_temp_details_clicked(); void on_pushButton_temp_details_clicked();
void on_pushButton_clicked();
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;

View File

@ -14,7 +14,7 @@
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>Serialicon by Tmithy Yin</string> <string>Serialicon by Timothy Yin</string>
</property> </property>
<widget class="QWidget" name="centralwidget"> <widget class="QWidget" name="centralwidget">
<widget class="QWidget" name="horizontalLayoutWidget_10"> <widget class="QWidget" name="horizontalLayoutWidget_10">
@ -1309,33 +1309,15 @@
</property> </property>
</widget> </widget>
</item> </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> <item>
<layout class="QHBoxLayout" name="horizontalLayout_10"> <layout class="QHBoxLayout" name="horizontalLayout_10">
<item> <item>
<widget class="QLineEdit" name="lineEdit"/> <widget class="QLabel" name="label_time">
</item>
<item>
<widget class="QPushButton" name="pushButton_gas_details">
<property name="text"> <property name="text">
<string>气体监测详情</string> <string>0000-00-00 00:00:00</string>
</property>
<property name="alignment">
<set>Qt::AlignmentFlag::AlignCenter</set>
</property> </property>
</widget> </widget>
</item> </item>
@ -1389,7 +1371,7 @@
<item> <item>
<widget class="QComboBox" name="comboBox_baud"> <widget class="QComboBox" name="comboBox_baud">
<property name="currentIndex"> <property name="currentIndex">
<number>0</number> <number>1</number>
</property> </property>
<item> <item>
<property name="text"> <property name="text">
@ -1436,6 +1418,19 @@
</layout> </layout>
</widget> </widget>
</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>
<widget class="QMenuBar" name="menubar"> <widget class="QMenuBar" name="menubar">
<property name="geometry"> <property name="geometry">