From 63fdba5a68495d5484945034eb60dd05a8825d5f Mon Sep 17 00:00:00 2001 From: HoshinoSuzumi Date: Sat, 26 Oct 2024 15:57:27 +0800 Subject: [PATCH] serial control --- Serialicon.pro | 3 +- mainwindow.cpp | 144 +++++++++++++++++++++++++++++++++++++++++++++-- mainwindow.h | 38 ++++++------- mainwindow.ui | 28 +++++++-- serial_command.h | 52 +++++++++++++++++ 5 files changed, 233 insertions(+), 32 deletions(-) create mode 100644 serial_command.h diff --git a/Serialicon.pro b/Serialicon.pro index 35f73a5..b0c17cd 100644 --- a/Serialicon.pro +++ b/Serialicon.pro @@ -13,7 +13,8 @@ SOURCES += \ mainwindow.cpp HEADERS += \ - mainwindow.h + mainwindow.h \ + serial_command.h FORMS += \ mainwindow.ui diff --git a/mainwindow.cpp b/mainwindow.cpp index d083a3f..a3028d4 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -12,12 +12,26 @@ MainWindow::MainWindow(QWidget *parent) MainWindow::~MainWindow() { + if (m_serialPort->isOpen()) { + m_serialPort->clear(); + m_serialPort->close(); + qDebug() << "exiting, clear and close serial port..."; + } + delete m_serialPort; delete ui; } +void MainWindow::msgBox(QWidget* ctx, QString title, QString content) { + QMessageBox::information(ctx, title, content); +} + +void MainWindow::msgBox(QWidget* ctx, QString content) { + msgBox(ctx, "title", content); +} + void MainWindow::receiveData() { QByteArray data = m_serialPort->readAll(); - qDebug() << "\nreceived data: " << data.length() << data.toHex(); + qDebug() << "\nreceived data:" << data.length() << data.toHex(); } @@ -27,7 +41,7 @@ void MainWindow::on_btn_serial_search_clicked() foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { m_serialPortNameList << info.portName(); - qDebug() << "Serial port discovered: " << info.portName(); + qDebug() << "Serial port discovered:" << info.portName(); } ui->comboBox_serial->clear(); @@ -41,7 +55,7 @@ void MainWindow::on_btn_serial_search_clicked() void MainWindow::on_btn_serial_toggle_clicked() { if (ui->comboBox_serial->currentText() == "") { - QMessageBox::information(this, "", "请选择串口"); + msgBox(this, "请选择串口"); return; } if (m_serialPort->isOpen()) { @@ -57,7 +71,7 @@ void MainWindow::on_btn_serial_toggle_clicked() } else { m_serialPort->setPortName("/dev/" + ui->comboBox_serial->currentText()); if (!m_serialPort->open(QIODevice::ReadWrite)) { - QMessageBox::information(this, "", "串口打开失败"); + msgBox(this, "串口打开失败", "串口被占用或不存在"); qDebug() << "Serial open failed"; return; } @@ -66,7 +80,7 @@ void MainWindow::on_btn_serial_toggle_clicked() // handle baud rate QSerialPort::BaudRate t_BaudRate; - switch(ui->comboBox_baud->currentIndex()) { + switch (ui->comboBox_baud->currentIndex()) { case 0: t_BaudRate = QSerialPort::Baud2400; break; @@ -99,3 +113,123 @@ void MainWindow::on_btn_serial_toggle_clicked() } } + +void MainWindow::on_comboBox_sys_mode_currentIndexChanged(int index) +{ + static char buff = 0xff; + switch (index) { + case 0: + buff = CTRL_CODE_SYSMODE_Disable; + break; + case 1: + buff = CTRL_CODE_SYSMODE_Telemetry; + break; + case 2: + buff = CTRL_CODE_SYSMODE_Security; + break; + } + + if (!m_serialPort->isOpen()) { + msgBox(this, "未连接设备"); + return; + } + + m_serialPort->write(&buff, sizeof(buff)); +} + + +void MainWindow::on_comboBox_cycle_currentIndexChanged(int index) +{ + static char buff = 0xff; + switch (index) { + case 0: + buff = CTRL_CODE_CYCLE_500ms; + break; + case 1: + buff = CTRL_CODE_CYCLE_1s; + break; + case 2: + buff = CTRL_CODE_CYCLE_2s; + break; + case 3: + buff = CTRL_CODE_CYCLE_5s; + break; + } + + if (!m_serialPort->isOpen()) { + msgBox(this, "未连接设备"); + return; + } + + m_serialPort->write(&buff, sizeof(buff)); +} + + +void MainWindow::on_comboBox_temp_fetch_currentIndexChanged(int index) +{ + static char buff = 0xff; + switch (index) { + case 0: + buff = CTRL_CODE_TEMP_Disable; + break; + case 1: + buff = CTRL_CODE_TEMP_Enable; + break; + } + + if (!m_serialPort->isOpen()) { + msgBox(this, "未连接设备"); + return; + } + + m_serialPort->write(&buff, sizeof(buff)); +} + + +void MainWindow::on_comboBox_body_fetch_currentIndexChanged(int index) +{ + static char buff = 0xff; + switch (index) { + case 0: + buff = CTRL_CODE_BODY_Disable; + break; + case 1: + buff = CTRL_CODE_BODY_Buzzer; + break; + case 2: + buff = CTRL_CODE_BODY_Light; + break; + } + + if (!m_serialPort->isOpen()) { + msgBox(this, "未连接设备"); + return; + } + + m_serialPort->write(&buff, sizeof(buff)); +} + + +void MainWindow::on_comboBox_gas_fetch_currentIndexChanged(int index) +{ + static char buff = 0xff; + switch (index) { + case 0: + buff = CTRL_CODE_GAS_Disable; + break; + case 1: + buff = CTRL_CODE_GAS_State; + break; + case 2: + buff = CTRL_CODE_GAS_Data; + break; + } + + if (!m_serialPort->isOpen()) { + msgBox(this, "未连接设备"); + return; + } + + m_serialPort->write(&buff, sizeof(buff)); +} + diff --git a/mainwindow.h b/mainwindow.h index 19f9541..2cb4099 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -1,12 +1,16 @@ #ifndef MAINWINDOW_H #define MAINWINDOW_H +#define _DISPLAY_DATA_LENGTH_ 201 + #include #include #include #include #include +#include + QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; @@ -20,6 +24,8 @@ class MainWindow : public QMainWindow public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); + void msgBox(QWidget* ctx, QString title, QString content); + void msgBox(QWidget* ctx, QString content); private slots: void receiveData(); @@ -28,31 +34,19 @@ private slots: void on_btn_serial_toggle_clicked(); + void on_comboBox_sys_mode_currentIndexChanged(int index); + + void on_comboBox_cycle_currentIndexChanged(int index); + + void on_comboBox_temp_fetch_currentIndexChanged(int index); + + void on_comboBox_body_fetch_currentIndexChanged(int index); + + void on_comboBox_gas_fetch_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 index a04c93b..da53098 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -124,7 +124,7 @@ - 安防模式 + 停止工作 @@ -134,7 +134,7 @@ - 停止工作 + 安防模式 @@ -189,12 +189,12 @@ - 开始采集 + 停止采集 - 停止采集 + 开始采集 @@ -212,6 +212,16 @@ + + + 停止 + + + + + 蜂鸣器告警 + + LED灯告警 @@ -232,11 +242,21 @@ + + + 停止 + + 采集状态 + + + 采集数据 + + diff --git a/serial_command.h b/serial_command.h new file mode 100644 index 0000000..0290316 --- /dev/null +++ b/serial_command.h @@ -0,0 +1,52 @@ +#ifndef SERIAL_COMMAND_H +#define SERIAL_COMMAND_H + +// Command send to MCU +enum CTRL_CODE_ { + // System mode + CTRL_CODE_SYSMODE_Disable = 0x01, + CTRL_CODE_SYSMODE_Telemetry = 0x02, + CTRL_CODE_SYSMODE_Security = 0x03, + + // Tmperature telemetry + CTRL_CODE_TEMP_Disable = 0x10, + CTRL_CODE_TEMP_Enable = 0x11, + + // Body alarming + CTRL_CODE_BODY_Disable = 0x21, + CTRL_CODE_BODY_Buzzer = 0x22, + CTRL_CODE_BODY_Light = 0x23, + + // Gas telemetry + CTRL_CODE_GAS_Disable = 0x31, + CTRL_CODE_GAS_State = 0x32, + CTRL_CODE_GAS_Data = 0x33, + + // Collection cycle + CTRL_CODE_CYCLE_500ms = 0x41, + CTRL_CODE_CYCLE_1s = 0x42, + CTRL_CODE_CYCLE_2s = 0x43, + CTRL_CODE_CYCLE_5s = 0x44 +}; + +// Command code from MCU +enum MCU_CMD_ { + MCU_CMD_TEMP = 0x01, + MCU_CMD_BODY = 0x02, + MCU_CMD_GAS_STATE = 0x03, + MCU_CMD_GAS_DATA = 0x04, +}; + +// Body sensor status code +enum COMMAND_BODY { + MCU_CMD_BODY_YES = 0x01, + MCU_CMD_BODY_NO = 0x02, +}; + +// Gas sensor status code +enum { + MCU_CMD_GAS_STATE_NORMAL = 0x01, + MCU_CMD_GAS_STATE_ALARMING = 0x02, +}; + +#endif // SERIAL_COMMAND_H