作者:古零零碎碎 | 来源:互联网 | 2023-07-17 17:49
1、下载qextserialport-1.2win-alpha.zip2、自定义串口配置类(comDialog:以备主界面调用)3、UI需要自己定义自定义类的头文件:
1、下载qextserialport-1.2win-alpha.zip
2、自定义串口配置类(comDialog:以备主界面调用)
3、UI需要自己定义
//自定义类的头文件:
////////////////////////////////////////////////////////////////////////
#ifndef COMDIALOG_H
#define COMDIALOG_H
#include
namespace Ui {
class comDialog;
}
class comDialog : public QDialog
{
Q_OBJECT
public:
explicit comDialog(QWidget *parent = 0);
~comDialog();
private:
Ui::comDialog *ui;
private:
void getCom(); //获取当前机器的可用端口函
private slots:
void clickCommitButton(); //确定按钮槽函数
void clickCancleButton(); //取消按钮槽函数
void clickBackButton(); //恢复默认函数
public:
QString comPort;
int comBaud;
int comStopBit;
int comDataBit;
int comPariy;
int comFlow;
};
#endif // COMDIALOG_H
//自定义类的源文件:
////////////////////////////////////////////////////////////////////////
#include "comdialog.h"
#include "ui_comdialog.h"
#include
comDialog::comDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::comDialog)
{
ui->setupUi(this);
getCom();
ui->commitButton->setDefault(true);
connect(ui->commitButton,SIGNAL(clicked()),this,SLOT(clickCommitButton()));
connect(ui->cancleButton,SIGNAL(clicked()),this,SLOT(clickCancleButton()));
connect(ui->recoverButton,SIGNAL(clicked()),this,SLOT(clickBackButton()));
}
comDialog::~comDialog()
{
delete ui;
}
void comDialog::getCom()
{
ui->portcomboBox->clear();
for(int portCount = 1;portCount<17;i++) //听说COM口最多16
{
QString strCom = "COM";
strCom += QString::number(portCount,10);
HANDLE Win_Handle =CreateFileA(strCom.toAscii(),GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if(Win_Handle != INVALID_HANDLE_VALUE)
{
ui->portcomboBox->addItem(strCom);
CloseHandle(Win_Handle);
}
}
}
//取消
void comDialog::clickCancleButton()
{
this->close();
}
//确定
void comDialog::clickCommitButton()
{
comPort = ui->portcomboBox->currentText();
comBaud = ui->baudcomboBox->currentIndex();
comStopBit = ui->stopBitcomboBox->currentIndex();
comDataBit = ui->dataBitcomboBox->currentIndex();
comPariy = ui->paritycomboBox->currentIndex();
comFlow = ui->flowCtrlcomboBox->currentIndex();
}
//恢复默认
void comDialog::clickBackButton()
{
ui->baudcomboBox->setCurrentIndex(7);
ui->stopBitcomboBox->setCurrentIndex(0);
ui->dataBitcomboBox->setCurrentIndex(3);
ui->paritycomboBox->setCurrentIndex(0);
ui->flowCtrlcomboBox->setCurrentIndex(0);
}
//效果图如下:
/////////////////////////////////////////////////////////////////////