191 lines
6.7 KiB
C++
191 lines
6.7 KiB
C++
|
#include "widget.h"
|
||
|
#include "ui_widget.h"
|
||
|
|
||
|
Widget::Widget(QWidget *parent)
|
||
|
: QWidget(parent)
|
||
|
, ui(new Ui::Widget)
|
||
|
{
|
||
|
ui->setupUi(this);
|
||
|
// 启动时刷新串口
|
||
|
QStringList serialNamePort;
|
||
|
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
|
||
|
{
|
||
|
serialNamePort << info.portName() + "|" + info.description();
|
||
|
}
|
||
|
ui -> SerialPort_comboBox -> clear();
|
||
|
ui -> SerialPort_comboBox -> addItems(serialNamePort);
|
||
|
|
||
|
Serial_port = new QSerialPort(this);
|
||
|
}
|
||
|
|
||
|
Widget::~Widget()
|
||
|
{
|
||
|
delete ui;
|
||
|
}
|
||
|
|
||
|
void Widget::closeEvent(QCloseEvent *event)
|
||
|
{
|
||
|
// 退出提醒弹窗
|
||
|
QMessageBox *Close_msgBox = new QMessageBox(QMessageBox::Question, "提示", "是否退出", QMessageBox::Yes | QMessageBox::No, this, Qt::WindowStaysOnTopHint);
|
||
|
Close_msgBox -> setDefaultButton(QMessageBox::No);
|
||
|
Close_msgBox -> button(QMessageBox::No) -> setText(u8"不退出");
|
||
|
Close_msgBox -> button(QMessageBox::Yes) -> setText(u8"退出");
|
||
|
int val = Close_msgBox -> exec();
|
||
|
if(val == QMessageBox::No)
|
||
|
{
|
||
|
event -> ignore();
|
||
|
}
|
||
|
if(val == QMessageBox::Yes)
|
||
|
{
|
||
|
event -> accept();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//串口刷新
|
||
|
void Widget::on_SerialPort_refresh_pushButton_clicked()
|
||
|
{
|
||
|
QStringList serialNamePort;
|
||
|
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
|
||
|
{
|
||
|
serialNamePort << info.portName() + "|" + info.description();
|
||
|
}
|
||
|
ui -> SerialPort_comboBox -> clear();
|
||
|
ui -> SerialPort_comboBox -> addItems(serialNamePort);
|
||
|
}
|
||
|
|
||
|
//打开关闭按钮状态
|
||
|
//1将打开按钮
|
||
|
//0将关闭按钮
|
||
|
int8_t open_button_status = 1;
|
||
|
void Widget::on_Open_Serial_pushButton_clicked()
|
||
|
{
|
||
|
if(open_button_status == 1)
|
||
|
{
|
||
|
QSerialPort::BaudRate Baudrate = QSerialPort::Baud9600;
|
||
|
QSerialPort::DataBits Databits = QSerialPort::Data8;
|
||
|
QSerialPort::Parity Parity = QSerialPort::NoParity;
|
||
|
QSerialPort::StopBits Stopbits = QSerialPort::OneStop;
|
||
|
|
||
|
QComboBox *SerialPortbox = ui -> SerialPort_comboBox;
|
||
|
QComboBox *Baudbox = ui -> BaudRate_comboBox;
|
||
|
QComboBox *Databitsbox = ui -> DataBits_comboBox;
|
||
|
QComboBox *Checkbitsbox = ui -> CheckBits_comboBox;
|
||
|
QComboBox *Stopbitsbox = ui -> StopBits_comboBox;
|
||
|
|
||
|
//波特率
|
||
|
if(Baudbox -> currentText() == "4800"){
|
||
|
Baudrate = QSerialPort::Baud4800;
|
||
|
}
|
||
|
else if(Baudbox -> currentText() == "9600"){
|
||
|
Baudrate = QSerialPort::Baud9600;
|
||
|
}
|
||
|
else if(Baudbox -> currentText() == "19200"){
|
||
|
Baudrate = QSerialPort::Baud19200;
|
||
|
}
|
||
|
else if(Baudbox -> currentText() == "38400"){
|
||
|
Baudrate = QSerialPort::Baud38400;
|
||
|
}
|
||
|
else if(Baudbox -> currentText() == "57600"){
|
||
|
Baudrate = QSerialPort::Baud57600;
|
||
|
}
|
||
|
else if(Baudbox -> currentText() == "115200"){
|
||
|
Baudrate = QSerialPort::Baud115200;
|
||
|
}
|
||
|
else {
|
||
|
QMessageBox::warning(this, "错误", "波特率未设置");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
//数据位
|
||
|
if(Databitsbox -> currentText() == "6"){
|
||
|
Databits = QSerialPort::Data6;
|
||
|
}
|
||
|
else if(Databitsbox -> currentText() == "7"){
|
||
|
Databits = QSerialPort::Data7;
|
||
|
}
|
||
|
else if(Databitsbox -> currentText() == "8"){
|
||
|
Databits = QSerialPort::Data8;
|
||
|
}
|
||
|
else {
|
||
|
QMessageBox::warning(this, "错误", "数据位未设置");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
//校验位
|
||
|
if(Checkbitsbox -> currentText() == "无"){
|
||
|
Parity = QSerialPort::NoParity;
|
||
|
}
|
||
|
else if(Checkbitsbox -> currentText() == "奇"){
|
||
|
Parity = QSerialPort::OddParity;
|
||
|
}
|
||
|
else if(Checkbitsbox -> currentText() == "偶"){
|
||
|
Parity = QSerialPort::EvenParity;
|
||
|
}
|
||
|
else {
|
||
|
QMessageBox::warning(this, "错误", "校验位未设置");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
//停止位
|
||
|
if(Stopbitsbox -> currentText() == "1"){
|
||
|
Stopbits = QSerialPort::OneStop;
|
||
|
}
|
||
|
else if(Stopbitsbox -> currentText() == "1.5"){
|
||
|
Stopbits = QSerialPort::OneAndHalfStop;
|
||
|
}
|
||
|
else if(Stopbitsbox -> currentText() == "2"){
|
||
|
Stopbits = QSerialPort::TwoStop;
|
||
|
}
|
||
|
else {
|
||
|
QMessageBox::warning(this, "错误", "停止位未设置");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
//设置参数
|
||
|
Serial_port -> setPort((const QSerialPortInfo)(SerialPortbox -> currentText().section('|', 0, 0)));
|
||
|
Serial_port -> setBaudRate(Baudrate);
|
||
|
Serial_port -> setDataBits(Databits);
|
||
|
Serial_port -> setParity(Parity);
|
||
|
Serial_port -> setStopBits(Stopbits);
|
||
|
Serial_port -> setReadBufferSize(4096);
|
||
|
if(Serial_port -> open(QIODevice::ReadWrite) == true){
|
||
|
// 打开后将按钮变为关闭按钮,绿色指示标志
|
||
|
ui -> Open_Serial_pushButton -> setText("关闭串口");
|
||
|
ui -> serial_status_label -> setStyleSheet("QLabel { background-color :rgb(0, 144, 0); border-radius: 10px;}");
|
||
|
//打开串口后禁止配置
|
||
|
ui -> SerialPort_comboBox -> setEnabled(false);
|
||
|
ui -> BaudRate_comboBox -> setEnabled(false);
|
||
|
ui -> DataBits_comboBox -> setEnabled(false);
|
||
|
ui -> CheckBits_comboBox -> setEnabled(false);
|
||
|
ui -> StopBits_comboBox -> setEnabled(false);
|
||
|
ui -> SerialPort_refresh_pushButton -> setEnabled(false);
|
||
|
ui -> BaudRate_label -> setEnabled(false);
|
||
|
ui -> DataBits_label -> setEnabled(false);
|
||
|
ui -> CheckBits_label -> setEnabled(false);
|
||
|
ui -> StopBits_label -> setEnabled(false);
|
||
|
}
|
||
|
else{
|
||
|
QMessageBox::critical(this, "提示", "串口打开失败");
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
//将按钮变为打开按钮,红色指示标志
|
||
|
ui -> Open_Serial_pushButton ->setText("打开串口");
|
||
|
ui -> serial_status_label ->setStyleSheet("QLabel { background-color :rgb(184, 0, 0); border-radius: 10px;}");
|
||
|
Serial_port -> close();
|
||
|
//关闭串口后允许配置
|
||
|
ui -> SerialPort_comboBox -> setEnabled(true);
|
||
|
ui -> BaudRate_comboBox -> setEnabled(true);
|
||
|
ui -> DataBits_comboBox -> setEnabled(true);
|
||
|
ui -> CheckBits_comboBox -> setEnabled(true);
|
||
|
ui -> StopBits_comboBox -> setEnabled(true);
|
||
|
ui -> SerialPort_refresh_pushButton -> setEnabled(true);
|
||
|
ui -> BaudRate_label -> setEnabled(true);
|
||
|
ui -> DataBits_label -> setEnabled(true);
|
||
|
ui -> CheckBits_label -> setEnabled(true);
|
||
|
ui -> StopBits_label -> setEnabled(true);
|
||
|
}
|
||
|
open_button_status = !open_button_status;
|
||
|
}
|