1、实现界面如下:
2、新建对话框窗口sDialog
3、设置ui格局如上图
4、将第三方类的文件加入项目中
5、将窗口配置文件的加入项目中
6、新建头文件如下:
#ifndef SDIALOG_H
#define SDIALOG_H
#include "qextSerialPort.h"
#include "win_qextserialport.h"
#include "comdialog.h"
#include <QDialog>
namespace Ui {
class sDialog;
}
class sDialog : public QDialog
{
Q_OBJECT
public:
explicit sDialog(QWidget *parent = 0);
~sDialog();
signals:
void qtWin();
void closeWin();
private:
Ui::sDialog *ui;
private:
QextSerialPort *myCom; //使用第三方类定义对象
comDialog *comWin; //使用自定义串口配置类对象
bool isOpen; //判断串口是否被打开
bool isLogin; //判断是否登录系统
bool isClose; //判断是否关闭
private slots:
void readMyCom(); //读数据槽函数
void sendData(); //发送数据槽函数
void comSetButton(); //串口配置槽函数
void disConnectMyComButton(); //断开连接槽函数
void scroll(); //textBrowser光标移动槽函数
protected:
void closeEvent(QCloseEvent *e);
};
#endif // SDIALOG_H
7、cpp源文件
#include "sdialog.h"
#include "ui_sdialog.h"
#include "qextserialport.h"
#include <QMessageBox>
#include <QCloseEvent>
sDialog::sDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::sDialog)
{
isLogin = false;
isOpen = false;
isClose = true;
ui->setupUi(this);
comWin = new comDialog;
ui->sendButton->setDefault(true);
ui->disConnectButton->setEnabled(false);
this->setWindowFlags(Qt::WindowCloseButtonHint);
this->setWindowFlags(Qt::WindowMaximizeButtonHint);
this->setWindowFlags(Qt::WindowMinMaxButtonsHint);
connect(ui->openButton,SIGNAL(clicked()),comWin,SLOT(exec())); //打开串口配置对话框
connect(ui->connectButton,SIGNAL(clicked()),this,SLOT(comSetButton())); //串口连接
connect(ui->sendButton,SIGNAL(clicked()),this,SLOT(sendData())); //发送命令
connect(ui->textBrowser,SIGNAL(textChanged()),this,SLOT(scroll())); //光标滚动
connect(ui->disConnectButton,SIGNAL(clicked()),this,SLOT(disConnectMyComButton())); //断开连接
connect(this,SIGNAL(qtWin()),this,SLOT(disConnectMyComButton())); //自定义信号断开连接
connect(this,SIGNAL(closeWin()),this,SLOT(accept()),Qt::QueuedConnection); //使用排队方式连接信号emit语句后的代码将在发出信号后立即被执行
}
sDialog::~sDialog()
{
delete ui;
}
//串口连接
void sDialog::comSetButton()
{
myCom = new QextSerialPort(comWin->comPort,QextSerialBase::EventDriven);
isOpen = myCom->open(QIODevice::ReadWrite);
if(isOpen == true)
{
QMessageBox::information(this,"infor",tr("open success!"));
}
else
{
QMessageBox::information(this,"infor","open failure!");
return;
}
ui->connectButton->setEnabled(false);
ui->openButton->setEnabled(false);
switch(comWin->comBaud)
{
case 0:
myCom->setBaudRate(BAUD300);
break;
case 1:
myCom->setBaudRate(BAUD600);
break;
case 2:
myCom->setBaudRate(BAUD1200);
break;
case 3:
myCom->setBaudRate(BAUD2400);
break;
case 4:
myCom->setBaudRate(BAUD9600);
break;
case 5:
myCom->setBaudRate(BAUD38400);
break;
case 6:
myCom->setBaudRate(BAUD57600);
break;
case 7:
myCom->setBaudRate(BAUD115200);
break;
case 8:
myCom->setBaudRate(BAUD128000);
break;
case 9:
myCom->setBaudRate(BAUD256000);
break;
}
switch(comWin->comStopBit)
{
case 0:
myCom->setStopBits(STOP_1);
break;
case 1:
myCom->setStopBits(STOP_1_5);
break;
case 2:
myCom->setStopBits(STOP_2);
break;
}
switch(comWin->comDataBit)
{
case 0:
myCom->setDataBits(DATA_5);
break;
case 1:
myCom->setDataBits(DATA_6);
break;
case 2:
myCom->setDataBits(DATA_7);
break;
case 3:
myCom->setDataBits(DATA_8);
break;
}
switch(comWin->comPariy)
{
case 0:
myCom->setParity(PAR_NONE);
break;
case 1:
myCom->setParity(PAR_ODD);
break;
case 2:
myCom->setParity(PAR_EVEN);
break;
case 3:
myCom->setParity(PAR_SPACE);
break;
case 4:
myCom->setParity(PAR_MARK);
break;
}
switch(comWin->comFlow)
{
case 0:
myCom->setFlowControl(FLOW_OFF);
break;
case 1:
myCom->setFlowControl(FLOW_HARDWARE);
break;
case 2:
myCom->setFlowControl(FLOW_XONXOFF);
break;
}
myCom->setTimeout(500);
//等待接收数据
connect(myCom,SIGNAL(readyRead()),this,SLOT(readMyCom()));
}
//按行读取串口数据
void sDialog::readMyCom()
{
char buf[512] = {0};
if(0 <<span style=" color:#c0c0c0;"> myCom->readLine(buf,sizeof(buf)))
{
if(strchr(buf,'@') != NULL) //判断是否登录
{
ui->disConnectButton->setEnabled(true);
if(isLogin)
{
QString str = "exit\n";
myCom->write(str.toAscii());
isLogin = false;
ui->disConnectButton->setEnabled(false);
myCom->close();
ui->openButton->setEnabled(true);
ui->connectButton->setEnabled(true);
}
}
if(!isClose)
{
emit closeWin();
return;
}
ui->textBrowser->insertPlainText(buf);
}
}
//发送数据
void sDialog::sendData()
{
if(isOpen)
{
QString str;
str = ui->msgEdit->text()+"\n";
myCom->write(str.toAscii());
Sleep(50);
ui->msgEdit->clear();
}
else
{
QMessageBox::information(this,"infor","can not open");
}
}
void sDialog::scroll()
{
ui->textBrowser->moveCursor(QTextCursor::End);
}
void sDialog::disConnectMyComButton()
{
if(!isClose && !isOpen)
{
emit closeWin();
return;
}
if(isOpen)
{
isLogin = true;
isOpen = false;
QString str = "\n";
myCom->write(str.toAscii());
}
else
{
QMessageBox::information(this,"infor","Has not open");
}
}
//重写closeEvent拦截关闭按钮
void sDialog::closeEvent(QCloseEvent *e)
{
if(QMessageBox::Yes == QMessageBox::information(this,"infor","Are you sure to close the window!",QMessageBox::Yes,QMessageBox::Cancel))
{
e->ignore();
isClose = false;
emit(qtWin()); //发送自定义信号清理打开的系统
}
else
{
e->ignore();
}
}