diff --git a/HelloWorld.pro b/HelloWorld.pro new file mode 100644 index 0000000..8980c54 --- /dev/null +++ b/HelloWorld.pro @@ -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 diff --git a/HelloWorld_zh_CN.ts b/HelloWorld_zh_CN.ts new file mode 100644 index 0000000..630fd35 --- /dev/null +++ b/HelloWorld_zh_CN.ts @@ -0,0 +1,3 @@ + + + diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..a30848a --- /dev/null +++ b/main.cpp @@ -0,0 +1,23 @@ +#include "mainwindow.h" + +#include +#include +#include + +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(); +} diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000..64161cd --- /dev/null +++ b/mainwindow.cpp @@ -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); + } +} + diff --git a/mainwindow.h b/mainwindow.h new file mode 100644 index 0000000..4c2a929 --- /dev/null +++ b/mainwindow.h @@ -0,0 +1,62 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include +#include +#include +#include + +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, +}; diff --git a/mainwindow.ui b/mainwindow.ui new file mode 100644 index 0000000..1f3e796 --- /dev/null +++ b/mainwindow.ui @@ -0,0 +1,478 @@ + + + MainWindow + + + + 0 + 0 + 662 + 473 + + + + Serial Helper + + + + + + 50 + 360 + 561 + 41 + + + + + + + + + 人体感应情况 + + + + + + + + 0 + 0 + + + + + 0 + 20 + + + + background: rgb(143, 240, 164); + + + + + + + + + + + 气体监测状态 + + + + + + + + 0 + 0 + + + + + 0 + 20 + + + + background: rgb(143, 240, 164); + + + + + + + + + + + 40 + 90 + 231 + 261 + + + + QFrame::Shape::StyledPanel + + + QFrame::Shadow::Raised + + + + + 10 + 0 + 211 + 261 + + + + + + + + + 系统模式设置 + + + + + + + + 安防模式 + + + + + 普通监测 + + + + + 停止工作 + + + + + + + + + + + + 采集周期设置 + + + + + + + + 500ms + + + + + 1s + + + + + 2s + + + + + 5s + + + + + + + + + + + + 温度采集设置 + + + + + + + + 开始采集 + + + + + 停止采集 + + + + + + + + + + + + 人体感应设置 + + + + + + + + LED灯告警 + + + + + + + + + + + + 气体感应设置 + + + + + + + + 采集状态 + + + + + + + + + + + + + 280 + 90 + 341 + 261 + + + + QFrame::Shape::StyledPanel + + + QFrame::Shadow::Raised + + + + + 10 + 10 + 322 + 241 + + + + + + + + + 当前温度(C) + + + + + + + + 0 + 0 + + + + + 0 + 50 + + + + false + + + 6 + + + QLCDNumber::SegmentStyle::Filled + + + 30.125000000000000 + + + + + + + + + + + 最高温度: 85C + + + + + + + + + + 最低温度: 29.5C + + + + + + + + + 温度详细信息 + + + + + + + + + 当前气体浓度: 0.00122 + + + + + + + 最高气体浓度: 0.001220 + + + + + + + + + + + + + + 气体监测详情 + + + + + + + + + + + + 39 + 30 + 581 + 51 + + + + QFrame::Shape::StyledPanel + + + QFrame::Shadow::Raised + + + + + 10 + 10 + 561 + 32 + + + + + 12 + + + + + 串口配置 + + + + + + + 未发现串口 + + + + + + + + 2400 + + + + + 4800 + + + + + 9600 + + + + + 115200 + + + + + + + + + 80 + 0 + + + + 搜索串口 + + + + + + + 打开串口 + + + + + + + + + + + 0 + 0 + 662 + 27 + + + + + + + +