From 3eebd8fe5287d9e87cf9b56a8cd6de2fbe76f0cc Mon Sep 17 00:00:00 2001 From: HoshinoSuzumi Date: Thu, 28 Nov 2024 04:04:59 +0800 Subject: [PATCH] feat: serial communication with mcu and temperature charts --- Serialicon.pro | 4 +- mainwindow.cpp | 121 ++++++++++++++++++++++++++++++++++++++++++++++++- mainwindow.h | 37 +++++++++++++-- mainwindow.ui | 26 ++++++++--- mydialog.cpp | 17 +++++++ mydialog.h | 20 ++++++++ 6 files changed, 211 insertions(+), 14 deletions(-) create mode 100644 mydialog.cpp create mode 100644 mydialog.h diff --git a/Serialicon.pro b/Serialicon.pro index e000256..776ba8a 100644 --- a/Serialicon.pro +++ b/Serialicon.pro @@ -10,10 +10,12 @@ CONFIG += c++17 console SOURCES += \ main.cpp \ - mainwindow.cpp + mainwindow.cpp \ + mydialog.cpp HEADERS += \ mainwindow.h \ + mydialog.h \ serial_command.h FORMS += \ diff --git a/mainwindow.cpp b/mainwindow.cpp index 4198148..24b6f9c 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -8,6 +8,9 @@ MainWindow::MainWindow(QWidget *parent) m_serialPort = new QSerialPort(); ui->setupUi(this); + + ui->lcdNumber_temp->setDigitCount(8); + initFigure(); } MainWindow::~MainWindow() @@ -30,10 +33,116 @@ void MainWindow::msgBox(QWidget* ctx, QString content) { } void MainWindow::receiveData() { - QByteArray data = m_serialPort->readAll(); - qDebug() << "\nreceived data:" << data.length() << data.toHex(); + QByteArray msg = m_serialPort->readAll(); + qDebug() << "\nreceived data:" << msg.count() << msg.toHex(); + + uint16_t LSB, MSB; + + int count = 0; + if (msg.count() % 2 == 0) { + count = msg.count() / 2; + for (int i = 0; i < count; i ++) { + MSB = ((uchar) msg.at(2 * i)) << 8; + LSB = (uchar) msg.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; + + if (t_cmd == MCU_CMD_TEMP) { + processTempData(t_data); + } + if (t_cmd == MCU_CMD_BODY) { + processBodyData(t_data); + } + if (t_cmd == MCU_CMD_GAS_STATE) { + processGasState(t_data); + } + if (t_cmd == MCU_CMD_GAS_DATA) { + processGasData(t_data); + } + } + } else { + qDebug() << "\n [!] invalid command received"; + } } +void MainWindow::initFigure() { + m_chart = new QChart; + m_chart->adjustSize(); + m_chartView = new QChartView(m_chart); + m_chartView->adjustSize(); + m_series = new QLineSeries; + m_chart->addSeries(m_series); + + axisX = new QValueAxis; + axisX->setRange(0, 200); + axisX->setLabelFormat("%g"); + axisX->setTitleText("采样时刻"); + axisY = new QValueAxis; + axisY->setRange(0, 50); + axisY->setTitleText(tr("温度值")); + + m_chart->setAxisX(axisX, m_series); + m_chart->setAxisY(axisY, m_series); + m_chart->legend()->hide(); + m_chart->setTitle(tr("实时温度采集数据")); +} + +void MainWindow::processTempData(uint data) { + static uchar s_flag = 0; + float computed = 0.0; + + if (data > 0x800) { + computed = (0xFFF - data) * (-0.0625); + } else { + computed = data * 0.0625; + } + + ui->lcdNumber_temp->display(computed); + + if (s_flag == 0) { + m_maxTemp = computed; + m_minTemp = computed; + s_flag = 1; + ui->label_max_temp->setText(tr("最高温度: %1 ℃").arg(m_maxTemp)); + ui->label_min_temp->setText(tr("最低温度: %1 ℃").arg(m_minTemp)); + } + if (computed > m_maxTemp + 0.1) { + m_maxTemp = computed; + ui->label_max_temp->setText(tr("最高温度: %1 ℃").arg(m_maxTemp)); + } + if (computed < m_minTemp - 0.1) { + m_minTemp = computed; + ui->label_max_temp->setText(tr("最低温度: %1 ℃").arg(m_minTemp)); + } + + QVector newPoints; + QVector oldPoints = m_series->pointsVector(); + + if (oldPoints.count() <= (_DISPLAY_DATA_LENGTH_ - 1)) { + oldPoints.append(QPointF(oldPoints.count(), computed)); + m_series->replace(oldPoints); + } else { + for (int i = 0; i < (_DISPLAY_DATA_LENGTH_ - 1); i ++) { + newPoints.append(QPointF(i, oldPoints.at(i + 1).y())); + } + newPoints.append(QPointF(newPoints.count(), computed)); + m_series->replace(newPoints); + } +} + +void MainWindow::processBodyData(uint data) { + +} + +void MainWindow::processGasState(uint data) { + +} + +void MainWindow::processGasData(uint data) { + +} void MainWindow::on_btn_serial_search_clicked() { @@ -234,3 +343,11 @@ void MainWindow::on_comboBox_gas_fetch_currentIndexChanged(int index) m_serialPort->write(&buff, sizeof(buff)); } + +void MainWindow::on_pushButton_temp_details_clicked() +{ + myDialog *dialog = new myDialog(); + dialog->myWidget->addWidget(m_chartView); + dialog->exec(); +} + diff --git a/mainwindow.h b/mainwindow.h index 9121ff2..eb8c0ea 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -9,7 +9,20 @@ #include #include +#include +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE +class QLineSeries; +class QChart; +QT_END_NAMESPACE +QT_USE_NAMESPACE + #include +#include QT_BEGIN_NAMESPACE namespace Ui { @@ -28,10 +41,10 @@ public: void msgBox(QWidget* ctx, QString content); void initFigure(); - void processTempData(); - void processBodyData(); - void processGasState(); - void processGasData(); + void processTempData(uint data); + void processBodyData(uint data); + void processGasState(uint data); + void processGasData(uint data); private slots: void receiveData(); @@ -50,9 +63,25 @@ private slots: void on_comboBox_gas_fetch_currentIndexChanged(int index); + void on_pushButton_temp_details_clicked(); + private: Ui::MainWindow *ui; QSerialPort* m_serialPort; + + QChart *m_chart; + QChartView *m_chartView; + QLineSeries *m_series; + QValueAxis *axisX; + QValueAxis *axisY; + float m_maxTemp; + float m_minTemp; + + QChart *m_chartGas; + QChartView *m_chartViewGas; + QLineSeries *m_seriesGas; + QValueAxis *axisXGas; + QValueAxis *axisYGas; }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index 385c117..50191ae 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -1260,16 +1260,22 @@ - false + true 6 + + QLCDNumber::Mode::Dec + - QLCDNumber::SegmentStyle::Filled + QLCDNumber::SegmentStyle::Flat - 30.125000000000000 + 0.000000000000000 + + + 0 @@ -1280,7 +1286,7 @@ - 最高温度: 85C + 最高温度: -- ℃ @@ -1290,7 +1296,7 @@ - 最低温度: 29.5C + 最低温度: -- ℃ @@ -1308,14 +1314,14 @@ - 当前气体浓度: 0.00122 + 当前气体浓度: 0.00 - 最高气体浓度: 0.001220 + 最高气体浓度: 0.00 @@ -1382,6 +1388,9 @@ + + 1 + 2400 @@ -1391,6 +1400,9 @@ 4800 + + + diff --git a/mydialog.cpp b/mydialog.cpp new file mode 100644 index 0000000..f960bd3 --- /dev/null +++ b/mydialog.cpp @@ -0,0 +1,17 @@ +#include "mydialog.h" +#include +#include +myDialog::myDialog(QWidget *parent) : QDialog(parent) +{ + QHBoxLayout *hLayout = new QHBoxLayout(); + myWidget = new QStackedWidget(this); + OKbtn = new QPushButton(tr("OK"),this); + Cancelbtn = new QPushButton(tr("Cancel"),this); + hLayout->addWidget(OKbtn); + hLayout->addWidget(Cancelbtn); + QVBoxLayout *vLayout = new QVBoxLayout(this); + vLayout->addWidget(myWidget); + vLayout->addLayout(hLayout); + connect(OKbtn,SIGNAL(clicked()),this,SLOT(close())); + connect(Cancelbtn,SIGNAL(clicked()),this,SLOT(close())); +} diff --git a/mydialog.h b/mydialog.h new file mode 100644 index 0000000..e84ad88 --- /dev/null +++ b/mydialog.h @@ -0,0 +1,20 @@ +#ifndef MYDIALOG_H +#define MYDIALOG_H + +#include +#include +#include +class myDialog : public QDialog +{ + Q_OBJECT +public: + explicit myDialog(QWidget *parent = 0); + QPushButton* OKbtn; + QPushButton* Cancelbtn; + QStackedWidget *myWidget; +signals: + +public slots: +}; + +#endif // MYDIALOG_H