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 mainwindow.cpp
HEADERS += \ HEADERS += \
mainwindow.h mainwindow.h \
serial_command.h
FORMS += \ FORMS += \
mainwindow.ui mainwindow.ui

View File

@ -12,12 +12,26 @@ MainWindow::MainWindow(QWidget *parent)
MainWindow::~MainWindow() MainWindow::~MainWindow()
{ {
if (m_serialPort->isOpen()) {
m_serialPort->clear();
m_serialPort->close();
qDebug() << "exiting, clear and close serial port...";
}
delete m_serialPort;
delete ui; 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() { void MainWindow::receiveData() {
QByteArray data = m_serialPort->readAll(); 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()) { foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
m_serialPortNameList << info.portName(); m_serialPortNameList << info.portName();
qDebug() << "Serial port discovered: " << info.portName(); qDebug() << "Serial port discovered:" << info.portName();
} }
ui->comboBox_serial->clear(); ui->comboBox_serial->clear();
@ -41,7 +55,7 @@ void MainWindow::on_btn_serial_search_clicked()
void MainWindow::on_btn_serial_toggle_clicked() void MainWindow::on_btn_serial_toggle_clicked()
{ {
if (ui->comboBox_serial->currentText() == "") { if (ui->comboBox_serial->currentText() == "") {
QMessageBox::information(this, "", "请选择串口"); msgBox(this, "请选择串口");
return; return;
} }
if (m_serialPort->isOpen()) { if (m_serialPort->isOpen()) {
@ -57,7 +71,7 @@ void MainWindow::on_btn_serial_toggle_clicked()
} else { } else {
m_serialPort->setPortName("/dev/" + ui->comboBox_serial->currentText()); m_serialPort->setPortName("/dev/" + ui->comboBox_serial->currentText());
if (!m_serialPort->open(QIODevice::ReadWrite)) { if (!m_serialPort->open(QIODevice::ReadWrite)) {
QMessageBox::information(this, "", "串口打开失败"); msgBox(this, "串口打开失败", "串口被占用或不存在");
qDebug() << "Serial open failed"; qDebug() << "Serial open failed";
return; return;
} }
@ -66,7 +80,7 @@ void MainWindow::on_btn_serial_toggle_clicked()
// handle baud rate // handle baud rate
QSerialPort::BaudRate t_BaudRate; QSerialPort::BaudRate t_BaudRate;
switch(ui->comboBox_baud->currentIndex()) { switch (ui->comboBox_baud->currentIndex()) {
case 0: case 0:
t_BaudRate = QSerialPort::Baud2400; t_BaudRate = QSerialPort::Baud2400;
break; 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 #ifndef MAINWINDOW_H
#define MAINWINDOW_H #define MAINWINDOW_H
#define _DISPLAY_DATA_LENGTH_ 201
#include <QMainWindow> #include <QMainWindow>
#include <QMessageBox> #include <QMessageBox>
#include <QSerialPort> #include <QSerialPort>
#include <QSerialPortInfo> #include <QSerialPortInfo>
#include <QDebug> #include <QDebug>
#include <serial_command.h>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui { namespace Ui {
class MainWindow; class MainWindow;
@ -20,6 +24,8 @@ class MainWindow : public QMainWindow
public: public:
MainWindow(QWidget *parent = nullptr); MainWindow(QWidget *parent = nullptr);
~MainWindow(); ~MainWindow();
void msgBox(QWidget* ctx, QString title, QString content);
void msgBox(QWidget* ctx, QString content);
private slots: private slots:
void receiveData(); void receiveData();
@ -28,31 +34,19 @@ private slots:
void on_btn_serial_toggle_clicked(); 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: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
QSerialPort* m_serialPort; QSerialPort* m_serialPort;
}; };
#endif // MAINWINDOW_H #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"> <widget class="QComboBox" name="comboBox_sys_mode">
<item> <item>
<property name="text"> <property name="text">
<string>安防模式</string> <string>停止工作</string>
</property> </property>
</item> </item>
<item> <item>
@ -134,7 +134,7 @@
</item> </item>
<item> <item>
<property name="text"> <property name="text">
<string>停止工作</string> <string>安防模式</string>
</property> </property>
</item> </item>
</widget> </widget>
@ -189,12 +189,12 @@
<widget class="QComboBox" name="comboBox_temp_fetch"> <widget class="QComboBox" name="comboBox_temp_fetch">
<item> <item>
<property name="text"> <property name="text">
<string>开始采集</string> <string>停止采集</string>
</property> </property>
</item> </item>
<item> <item>
<property name="text"> <property name="text">
<string>停止采集</string> <string>开始采集</string>
</property> </property>
</item> </item>
</widget> </widget>
@ -212,6 +212,16 @@
</item> </item>
<item> <item>
<widget class="QComboBox" name="comboBox_body_fetch"> <widget class="QComboBox" name="comboBox_body_fetch">
<item>
<property name="text">
<string>停止</string>
</property>
</item>
<item>
<property name="text">
<string>蜂鸣器告警</string>
</property>
</item>
<item> <item>
<property name="text"> <property name="text">
<string>LED灯告警</string> <string>LED灯告警</string>
@ -232,11 +242,21 @@
</item> </item>
<item> <item>
<widget class="QComboBox" name="comboBox_gas_fetch"> <widget class="QComboBox" name="comboBox_gas_fetch">
<item>
<property name="text">
<string>停止</string>
</property>
</item>
<item> <item>
<property name="text"> <property name="text">
<string>采集状态</string> <string>采集状态</string>
</property> </property>
</item> </item>
<item>
<property name="text">
<string>采集数据</string>
</property>
</item>
</widget> </widget>
</item> </item>
</layout> </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