feat: serial communication with mcu and temperature charts
This commit is contained in:
parent
3f60026f3c
commit
3eebd8fe52
@ -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 += \
|
||||
|
121
mainwindow.cpp
121
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<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()
|
||||
{
|
||||
@ -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();
|
||||
}
|
||||
|
||||
|
37
mainwindow.h
37
mainwindow.h
@ -9,7 +9,20 @@
|
||||
#include <QSerialPortInfo>
|
||||
#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 <mydialog.h>
|
||||
|
||||
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
|
||||
|
@ -1260,16 +1260,22 @@
|
||||
</size>
|
||||
</property>
|
||||
<property name="smallDecimalPoint">
|
||||
<bool>false</bool>
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="digitCount">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="mode">
|
||||
<enum>QLCDNumber::Mode::Dec</enum>
|
||||
</property>
|
||||
<property name="segmentStyle">
|
||||
<enum>QLCDNumber::SegmentStyle::Filled</enum>
|
||||
<enum>QLCDNumber::SegmentStyle::Flat</enum>
|
||||
</property>
|
||||
<property name="value" stdset="0">
|
||||
<double>30.125000000000000</double>
|
||||
<double>0.000000000000000</double>
|
||||
</property>
|
||||
<property name="intValue" stdset="0">
|
||||
<number>0</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
@ -1280,7 +1286,7 @@
|
||||
<item>
|
||||
<widget class="QLabel" name="label_max_temp">
|
||||
<property name="text">
|
||||
<string>最高温度: 85C</string>
|
||||
<string>最高温度: -- ℃</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
@ -1290,7 +1296,7 @@
|
||||
<string notr="true"/>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>最低温度: 29.5C</string>
|
||||
<string>最低温度: -- ℃</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
@ -1308,14 +1314,14 @@
|
||||
<item>
|
||||
<widget class="QLabel" name="label_gas_current">
|
||||
<property name="text">
|
||||
<string>当前气体浓度: 0.00122</string>
|
||||
<string>当前气体浓度: 0.00</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="label_gas_max">
|
||||
<property name="text">
|
||||
<string>最高气体浓度: 0.001220</string>
|
||||
<string>最高气体浓度: 0.00</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
@ -1382,6 +1388,9 @@
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QComboBox" name="comboBox_baud">
|
||||
<property name="currentIndex">
|
||||
<number>1</number>
|
||||
</property>
|
||||
<item>
|
||||
<property name="text">
|
||||
<string>2400</string>
|
||||
@ -1391,6 +1400,9 @@
|
||||
<property name="text">
|
||||
<string>4800</string>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset theme="applications-development"/>
|
||||
</property>
|
||||
</item>
|
||||
<item>
|
||||
<property name="text">
|
||||
|
17
mydialog.cpp
Normal file
17
mydialog.cpp
Normal 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
20
mydialog.h
Normal 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
|
Loading…
Reference in New Issue
Block a user