ui and serial port initialization

This commit is contained in:
Timothy Yin 2024-10-26 14:22:30 +08:00
parent 82af85f6f4
commit 3c60a069d9
6 changed files with 697 additions and 0 deletions

29
HelloWorld.pro Normal file
View File

@ -0,0 +1,29 @@
QT += core gui serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17 console
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
TRANSLATIONS += \
HelloWorld_zh_CN.ts
CONFIG += lrelease
CONFIG += embed_translations
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

3
HelloWorld_zh_CN.ts Normal file
View File

@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="zh_CN"></TS>

23
main.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "mainwindow.h"
#include <QApplication>
#include <QLocale>
#include <QTranslator>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTranslator translator;
const QStringList uiLanguages = QLocale::system().uiLanguages();
for (const QString &locale : uiLanguages) {
const QString baseName = "HelloWorld_" + QLocale(locale).name();
if (translator.load(":/i18n/" + baseName)) {
a.installTranslator(&translator);
break;
}
}
MainWindow w;
w.show();
return a.exec();
}

102
mainwindow.cpp Normal file
View File

@ -0,0 +1,102 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
m_serialPort = new QSerialPort();
ui->setupUi(this);
setWindowTitle("Serial Helper");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::receiveData() {
QByteArray data = m_serialPort->readAll();
qDebug() << "\nreceived data: " << data.length() << data.toHex();
}
void MainWindow::on_btn_serial_search_clicked()
{
QStringList m_serialPortNameList;
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
m_serialPortNameList << info.portName();
qDebug() << "Serial port discovered: " << info.portName();
}
ui->comboBox_serial->clear();
ui->comboBox_serial->addItems(m_serialPortNameList);
if (m_serialPortNameList.length() > 0) {
ui->comboBox_serial->setCurrentIndex(0);
}
}
void MainWindow::on_btn_serial_toggle_clicked()
{
if (ui->comboBox_serial->currentText() == "") {
QMessageBox::information(this, "", "请选择串口");
return;
}
if (m_serialPort->isOpen()) {
m_serialPort->clear();
m_serialPort->close();
qDebug() << "Serial port closed";
ui->btn_serial_toggle->setText("打开串口");
// enable serial port comboBoxes
ui->comboBox_serial->setDisabled(false);
ui->comboBox_baud->setDisabled(false);
ui->btn_serial_search->setDisabled(false);
} else {
m_serialPort->setPortName("/dev/" + ui->comboBox_serial->currentText());
if (!m_serialPort->open(QIODevice::ReadWrite)) {
QMessageBox::information(this, "", "串口打开失败");
qDebug() << "Serial open failed";
return;
}
qDebug() << "Serial open successful";
ui->btn_serial_toggle->setText("关闭串口");
// handle baud rate
QSerialPort::BaudRate t_BaudRate;
switch(ui->comboBox_baud->currentIndex()) {
case 0:
t_BaudRate = QSerialPort::Baud2400;
break;
case 1:
t_BaudRate = QSerialPort::Baud4800;
break;
case 2:
t_BaudRate = QSerialPort::Baud9600;
break;
case 3:
t_BaudRate = QSerialPort::Baud115200;
break;
default:
t_BaudRate = QSerialPort::Baud9600;
break;
}
// serial port configuration
m_serialPort->setBaudRate(t_BaudRate);
m_serialPort->setDataBits(QSerialPort::Data8);
m_serialPort->setFlowControl(QSerialPort::NoFlowControl);
m_serialPort->setParity(QSerialPort::NoParity);
m_serialPort->setStopBits(QSerialPort::OneStop);
connect(m_serialPort, SIGNAL(readyRead()), this, SLOT(receiveData()));
// disable serial port comboBoxes
ui->comboBox_serial->setDisabled(true);
ui->comboBox_baud->setDisabled(true);
ui->btn_serial_search->setDisabled(true);
}
}

62
mainwindow.h Normal file
View File

@ -0,0 +1,62 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMessageBox>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void receiveData();
void on_btn_serial_search_clicked();
void on_btn_serial_toggle_clicked();
void on_comboBox_serial_currentIndexChanged(int index);
void on_comboBox_baud_currentIndexChanged(int index);
private:
Ui::MainWindow *ui;
QSerialPort* m_serialPort;
};
#endif // MAINWINDOW_H
#define _DISPLAY_DATA_LENGTH_ 201
// Command code from MCU
enum MCU_COMMAND_ {
MCU_COMMAND_TEMP = 0x01,
MCU_COMMAND_BODY = 0x02,
MCU_COMMAND_GAS_STATE = 0x03,
MCU_COMMAND_GAS_DATA = 0x04,
};
// Body sensor status code
enum COMMAND_BODY {
MCU_COMMAND_BODY_YES = 0x01,
MCU_COMMAND_BODY_NO = 0x02,
};
// Gas sensor status code
enum {
MCU_COMMAND_GAS_STATE_SAFE = 0x01,
MCU_COMMAND_GAS_STATE_DANGER = 0x02,
};

478
mainwindow.ui Normal file
View File

@ -0,0 +1,478 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>662</width>
<height>473</height>
</rect>
</property>
<property name="windowTitle">
<string>Serial Helper</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QWidget" name="horizontalLayoutWidget_10">
<property name="geometry">
<rect>
<x>50</x>
<y>360</y>
<width>561</width>
<height>41</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_11">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_12">
<item>
<widget class="QLabel" name="label_12">
<property name="text">
<string>人体感应情况</string>
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="widget_body_status" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>20</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background: rgb(143, 240, 164);</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_13">
<item>
<widget class="QLabel" name="label_13">
<property name="text">
<string>气体监测状态</string>
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="widget_gas_status" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>20</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background: rgb(143, 240, 164);</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QFrame" name="frame">
<property name="geometry">
<rect>
<x>40</x>
<y>90</y>
<width>231</width>
<height>261</height>
</rect>
</property>
<property name="frameShape">
<enum>QFrame::Shape::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Shadow::Raised</enum>
</property>
<widget class="QWidget" name="verticalLayoutWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>0</y>
<width>211</width>
<height>261</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>系统模式设置</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBox_sys_mode">
<item>
<property name="text">
<string>安防模式</string>
</property>
</item>
<item>
<property name="text">
<string>普通监测</string>
</property>
</item>
<item>
<property name="text">
<string>停止工作</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>采集周期设置</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBox_cycle">
<item>
<property name="text">
<string>500ms</string>
</property>
</item>
<item>
<property name="text">
<string>1s</string>
</property>
</item>
<item>
<property name="text">
<string>2s</string>
</property>
</item>
<item>
<property name="text">
<string>5s</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>温度采集设置</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBox_temp_fetch">
<item>
<property name="text">
<string>开始采集</string>
</property>
</item>
<item>
<property name="text">
<string>停止采集</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>人体感应设置</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBox_body_fetch">
<item>
<property name="text">
<string>LED灯告警</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<widget class="QLabel" name="label_6">
<property name="text">
<string>气体感应设置</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBox_gas_fetch">
<item>
<property name="text">
<string>采集状态</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<widget class="QFrame" name="frame_2">
<property name="geometry">
<rect>
<x>280</x>
<y>90</y>
<width>341</width>
<height>261</height>
</rect>
</property>
<property name="frameShape">
<enum>QFrame::Shape::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Shadow::Raised</enum>
</property>
<widget class="QWidget" name="verticalLayoutWidget_2">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>322</width>
<height>241</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_7">
<item>
<widget class="QLabel" name="label_7">
<property name="text">
<string>当前温度(C)</string>
</property>
</widget>
</item>
<item>
<widget class="QLCDNumber" name="lcdNumber_temp">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>50</height>
</size>
</property>
<property name="smallDecimalPoint">
<bool>false</bool>
</property>
<property name="digitCount">
<number>6</number>
</property>
<property name="segmentStyle">
<enum>QLCDNumber::SegmentStyle::Filled</enum>
</property>
<property name="value" stdset="0">
<double>30.125000000000000</double>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_8">
<item>
<widget class="QLabel" name="label_max_temp">
<property name="text">
<string>最高温度: 85C</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_min_temp">
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="text">
<string>最低温度: 29.5C</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QPushButton" name="pushButton_temp_details">
<property name="text">
<string>温度详细信息</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_9">
<item>
<widget class="QLabel" name="label_gas_current">
<property name="text">
<string>当前气体浓度: 0.00122</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_gas_max">
<property name="text">
<string>最高气体浓度: 0.001220</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">
<property name="text">
<string>气体监测详情</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<widget class="QFrame" name="frame_5">
<property name="geometry">
<rect>
<x>39</x>
<y>30</y>
<width>581</width>
<height>51</height>
</rect>
</property>
<property name="frameShape">
<enum>QFrame::Shape::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Shadow::Raised</enum>
</property>
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>561</width>
<height>32</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>12</number>
</property>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>串口配置</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBox_serial">
<property name="placeholderText">
<string>未发现串口</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBox_baud">
<item>
<property name="text">
<string>2400</string>
</property>
</item>
<item>
<property name="text">
<string>4800</string>
</property>
</item>
<item>
<property name="text">
<string>9600</string>
</property>
</item>
<item>
<property name="text">
<string>115200</string>
</property>
</item>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn_serial_search">
<property name="minimumSize">
<size>
<width>80</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>搜索串口</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn_serial_toggle">
<property name="text">
<string>打开串口</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>662</width>
<height>27</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>