serial control

This commit is contained in:
Timothy Yin 2024-10-26 15:57:27 +08:00
parent 6658220e7e
commit 63fdba5a68
5 changed files with 233 additions and 32 deletions

View File

@ -13,7 +13,8 @@ SOURCES += \
mainwindow.cpp
HEADERS += \
mainwindow.h
mainwindow.h \
serial_command.h
FORMS += \
mainwindow.ui

View File

@ -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));
}

View File

@ -1,12 +1,16 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#define _DISPLAY_DATA_LENGTH_ 201
#include <QMainWindow>
#include <QMessageBox>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QDebug>
#include <serial_command.h>
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,
};

View File

@ -124,7 +124,7 @@
<widget class="QComboBox" name="comboBox_sys_mode">
<item>
<property name="text">
<string>安防模式</string>
<string>停止工作</string>
</property>
</item>
<item>
@ -134,7 +134,7 @@
</item>
<item>
<property name="text">
<string>停止工作</string>
<string>安防模式</string>
</property>
</item>
</widget>
@ -189,12 +189,12 @@
<widget class="QComboBox" name="comboBox_temp_fetch">
<item>
<property name="text">
<string>开始采集</string>
<string>停止采集</string>
</property>
</item>
<item>
<property name="text">
<string>停止采集</string>
<string>开始采集</string>
</property>
</item>
</widget>
@ -212,6 +212,16 @@
</item>
<item>
<widget class="QComboBox" name="comboBox_body_fetch">
<item>
<property name="text">
<string>停止</string>
</property>
</item>
<item>
<property name="text">
<string>蜂鸣器告警</string>
</property>
</item>
<item>
<property name="text">
<string>LED灯告警</string>
@ -232,11 +242,21 @@
</item>
<item>
<widget class="QComboBox" name="comboBox_gas_fetch">
<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>

52
serial_command.h Normal file
View File

@ -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