feat: serial communication with mcu and temperature charts

This commit is contained in:
Timothy Yin 2024-11-28 04:04:59 +08:00
parent 3f60026f3c
commit 3eebd8fe52
6 changed files with 211 additions and 14 deletions

View File

@ -10,10 +10,12 @@ CONFIG += c++17 console
SOURCES += \ SOURCES += \
main.cpp \ main.cpp \
mainwindow.cpp mainwindow.cpp \
mydialog.cpp
HEADERS += \ HEADERS += \
mainwindow.h \ mainwindow.h \
mydialog.h \
serial_command.h serial_command.h
FORMS += \ FORMS += \

View File

@ -8,6 +8,9 @@ MainWindow::MainWindow(QWidget *parent)
m_serialPort = new QSerialPort(); m_serialPort = new QSerialPort();
ui->setupUi(this); ui->setupUi(this);
ui->lcdNumber_temp->setDigitCount(8);
initFigure();
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
@ -30,10 +33,116 @@ void MainWindow::msgBox(QWidget* ctx, QString content) {
} }
void MainWindow::receiveData() { void MainWindow::receiveData() {
QByteArray data = m_serialPort->readAll(); QByteArray msg = m_serialPort->readAll();
qDebug() << "\nreceived data:" << data.length() << data.toHex(); 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<QPointF> newPoints;
QVector<QPointF> 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() 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)); m_serialPort->write(&buff, sizeof(buff));
} }
void MainWindow::on_pushButton_temp_details_clicked()
{
myDialog *dialog = new myDialog();
dialog->myWidget->addWidget(m_chartView);
dialog->exec();
}

View File

@ -9,7 +9,20 @@
#include <QSerialPortInfo> #include <QSerialPortInfo>
#include <QDebug> #include <QDebug>
#include <QtCharts/QChartGlobal>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QtCharts/QChart>
#include <QtCharts/QValueAxis>
QT_BEGIN_NAMESPACE
class QLineSeries;
class QChart;
QT_END_NAMESPACE
QT_USE_NAMESPACE
#include <serial_command.h> #include <serial_command.h>
#include <mydialog.h>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui { namespace Ui {
@ -28,10 +41,10 @@ public:
void msgBox(QWidget* ctx, QString content); void msgBox(QWidget* ctx, QString content);
void initFigure(); void initFigure();
void processTempData(); void processTempData(uint data);
void processBodyData(); void processBodyData(uint data);
void processGasState(); void processGasState(uint data);
void processGasData(); void processGasData(uint data);
private slots: private slots:
void receiveData(); void receiveData();
@ -50,9 +63,25 @@ private slots:
void on_comboBox_gas_fetch_currentIndexChanged(int index); void on_comboBox_gas_fetch_currentIndexChanged(int index);
void on_pushButton_temp_details_clicked();
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
QSerialPort* m_serialPort; 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 #endif // MAINWINDOW_H

View File

@ -1260,16 +1260,22 @@
</size> </size>
</property> </property>
<property name="smallDecimalPoint"> <property name="smallDecimalPoint">
<bool>false</bool> <bool>true</bool>
</property> </property>
<property name="digitCount"> <property name="digitCount">
<number>6</number> <number>6</number>
</property> </property>
<property name="mode">
<enum>QLCDNumber::Mode::Dec</enum>
</property>
<property name="segmentStyle"> <property name="segmentStyle">
<enum>QLCDNumber::SegmentStyle::Filled</enum> <enum>QLCDNumber::SegmentStyle::Flat</enum>
</property> </property>
<property name="value" stdset="0"> <property name="value" stdset="0">
<double>30.125000000000000</double> <double>0.000000000000000</double>
</property>
<property name="intValue" stdset="0">
<number>0</number>
</property> </property>
</widget> </widget>
</item> </item>
@ -1280,7 +1286,7 @@
<item> <item>
<widget class="QLabel" name="label_max_temp"> <widget class="QLabel" name="label_max_temp">
<property name="text"> <property name="text">
<string>最高温度: 85C</string> <string>最高温度: -- ℃</string>
</property> </property>
</widget> </widget>
</item> </item>
@ -1290,7 +1296,7 @@
<string notr="true"/> <string notr="true"/>
</property> </property>
<property name="text"> <property name="text">
<string>最低温度: 29.5C</string> <string>最低温度: -- ℃</string>
</property> </property>
</widget> </widget>
</item> </item>
@ -1308,14 +1314,14 @@
<item> <item>
<widget class="QLabel" name="label_gas_current"> <widget class="QLabel" name="label_gas_current">
<property name="text"> <property name="text">
<string>当前气体浓度: 0.00122</string> <string>当前气体浓度: 0.00</string>
</property> </property>
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QLabel" name="label_gas_max"> <widget class="QLabel" name="label_gas_max">
<property name="text"> <property name="text">
<string>最高气体浓度: 0.001220</string> <string>最高气体浓度: 0.00</string>
</property> </property>
</widget> </widget>
</item> </item>
@ -1382,6 +1388,9 @@
</item> </item>
<item> <item>
<widget class="QComboBox" name="comboBox_baud"> <widget class="QComboBox" name="comboBox_baud">
<property name="currentIndex">
<number>1</number>
</property>
<item> <item>
<property name="text"> <property name="text">
<string>2400</string> <string>2400</string>
@ -1391,6 +1400,9 @@
<property name="text"> <property name="text">
<string>4800</string> <string>4800</string>
</property> </property>
<property name="icon">
<iconset theme="applications-development"/>
</property>
</item> </item>
<item> <item>
<property name="text"> <property name="text">

17
mydialog.cpp Normal file
View File

@ -0,0 +1,17 @@
#include "mydialog.h"
#include<QHBoxLayout>
#include<QVBoxLayout>
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()));
}

20
mydialog.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef MYDIALOG_H
#define MYDIALOG_H
#include <QDialog>
#include<QPushButton>
#include<QStackedWidget>
class myDialog : public QDialog
{
Q_OBJECT
public:
explicit myDialog(QWidget *parent = 0);
QPushButton* OKbtn;
QPushButton* Cancelbtn;
QStackedWidget *myWidget;
signals:
public slots:
};
#endif // MYDIALOG_H