Qt編程之“串口助手”

http://blog.sina.com.cn/s/blog_7940d06d010100wh.html

首先展示一下我的效果圖,可以大致瞭解一下我這個串口助手的基本功能:
Qt編程之“串口助手”

Qt編程之“串口助手”

下面是該軟件mainwindow的相關源代碼,也是這個小軟件的精華部分,重要代碼行後面均有註釋,有祝大家理解:

#include "mainwindow.h"

#include "ui_mainwindow.h"
#include
#include
#include
#include
#include

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle(tr("草原神鷹串口助手"));//初始化主窗口的標題
ui->closeMyComBtn->setEnabled(false);//開始“關閉摁鈕”不可用
ui->sendMsgBtn->setEnabled(false);////開始“發送數據摁鈕”不可用
ui->choosetextpushButton->setEnabled(false);
ui->choose_positionpushButton->setEnabled(false);
ui->savedatalineEdit->setEnabled(false);
ui->pushButton->setEnabled(false);
ui->pushButton_2->setEnabled(false);
ui->sendtextpushButton->setEnabled(false);
ui->savedatalineEdit->setEnabled(false);
ui->sendtextkeylineEdit->setEnabled(false);
ui->sendtextpathlineEdit->setEnabled(false);
ui->sendMsglineEdit->setEnabled(false);
ui->textBrowser->setEnabled(false);
}



MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::readMyCom()
{
QByteArray temp = myCom->readAll();
//讀取數據緩衝區中的所有數據給臨時變量temp
ui->textBrowser->insertPlainText(temp);
//將串口數據顯示在窗口的文本瀏覽器中
}

void MainWindow::on_openMyComBtn_clicked()
{
QString portName = ui->portNamecomboBox->currentText();//獲取串口名
myCom = new Win_QextSerialPort(portName,QextSerialBase::EventDriven);
//定義串口對象
myCom->open(QIODevice::ReadWrite);//以讀寫方式打開串口
if(ui->baudRatecomboBox->currentText()==tr("110"))//根據波特率窗口設置串口波特率
myCom->setBaudRate(BAUD9600);
else if(ui->baudRatecomboBox->currentText()==tr("9600"))
myCom->setBaudRate(BAUD300);
else if(ui->baudRatecomboBox->currentText()==tr("600"))
myCom->setBaudRate(BAUD600);
else if(ui->baudRatecomboBox->currentText()==tr("1200"))
myCom->setBaudRate(BAUD1200);
else if(ui->baudRatecomboBox->currentText()==tr("2400"))
myCom->setBaudRate(BAUD2400);
else if(ui->baudRatecomboBox->currentText()==tr("4800"))
myCom->setBaudRate(BAUD4800);
else if(ui->baudRatecomboBox->currentText()==tr("300"))
myCom->setBaudRate(BAUD9600);
else if(ui->baudRatecomboBox->currentText()==tr("19200"))
myCom->setBaudRate(BAUD19200);
//設置數據位
if(ui->dataBitscomboBox->currentText()==tr("8"))
myCom->setDataBits(DATA_8);
else if(ui->dataBitscomboBox->currentText()==tr("7"))
myCom->setDataBits(DATA_7);
//設置奇偶校驗位
if(ui->paritycomboBox->currentText()==tr("無"))
myCom->setParity(PAR_NONE);
else if(ui->paritycomboBox->currentText()==tr("奇"))
myCom->setParity(PAR_ODD);
else if(ui->paritycomboBox->currentText()==tr("偶"))
myCom->setParity(PAR_EVEN);
//設置停止位
if(ui->stopBitscomboBox->currentText()==tr("1"))
myCom->setStopBits(STOP_1);
else if(ui->stopBitscomboBox->currentText()==tr("2"))
myCom->setStopBits(STOP_2);
myCom->setFlowControl(FLOW_OFF);//設置爲無數據流控制
myCom->setTimeout(500);//延時
connect(myCom,SIGNAL(readyRead()),this,SLOT(readMyCom()));
//信號和槽函數連接,當串口緩衝區有數據時,進行讀串口操作
ui->openMyComBtn->setEnabled(false);//打開串口後“打開串口摁鈕”不可用
ui->closeMyComBtn->setEnabled(true);//打開串口後”關閉串口摁鈕“可用
ui->sendMsgBtn->setEnabled(true);////打開串口後“發送數據摁鈕”可用
ui->baudRatecomboBox->setEnabled(false);
ui->portNamecomboBox->setEnabled(false);
ui->paritycomboBox->setEnabled(false);
ui->stopBitscomboBox->setEnabled(false);
ui->dataBitscomboBox->setEnabled(false);
ui->choosetextpushButton->setEnabled(true);
ui->choose_positionpushButton->setEnabled(true);
ui->savedatalineEdit->setEnabled(true);
ui->pushButton->setEnabled(true);
ui->pushButton_2->setEnabled(true);
ui->sendtextpushButton->setEnabled(true);
ui->savedatalineEdit->setEnabled(true);
ui->sendtextkeylineEdit->setEnabled(true);
ui->sendtextpathlineEdit->setEnabled(true);
ui->sendMsglineEdit->setEnabled(true);
ui->textBrowser->setEnabled(true);
}

void MainWindow::on_closeMyComBtn_clicked()
{
myCom->close();//關閉串口
ui->openMyComBtn->setEnabled(true);//關閉串口後“打開串口摁鈕”可用
ui->sendMsgBtn->setEnabled(false);////關閉串口後“發送數據摁鈕”不可用
ui->closeMyComBtn->setEnabled(false);//關閉串口後”關閉串口摁鈕“不可用
ui->baudRatecomboBox->setEnabled(true);
ui->portNamecomboBox->setEnabled(true);
ui->paritycomboBox->setEnabled(true);
ui->stopBitscomboBox->setEnabled(true);
ui->dataBitscomboBox->setEnabled(true);
ui->choosetextpushButton->setEnabled(false);
ui->pushButton_2->setEnabled(false);
ui->sendtextpushButton->setEnabled(false);
ui->savedatalineEdit->setEnabled(false);
ui->sendtextkeylineEdit->setEnabled(false);
ui->sendtextpathlineEdit->setEnabled(false);
ui->sendMsglineEdit->setEnabled(false);

}

void MainWindow::on_sendMsgBtn_clicked()
{
myCom->write(ui->sendMsglineEdit->text().toAscii());
//將行編輯器中文本轉化成ascii碼形式寫入串口
}

bool MainWindow::on_choosetextpushButton_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this);
if(!fileName.isEmpty())
{
QFile file(fileName);
if(!file.open(QFile::ReadOnly|QFile::Text)) //以只讀方式打開文件,如果打開失敗則返回,彈出對話框
{
QMessageBox::warning(this,tr("讀取文件"),tr("無法讀取文件 %1:\n%2.").arg(fileName).arg(file.errorString()));
return false;//%1和%2表示後面兩個arg的值
}
QTextStream in(&file);//新建流對象,指向選定文件
ui->sendtextkeylineEdit->setText(in.readAll());//將文件中所有的內容寫到行編輯器中
curFile = QFileInfo(fileName).canonicalFilePath();
ui->sendtextpathlineEdit->setText(curFile);
ui->sendtextpathlineEdit->setVisible(true);//將文本設置爲可見,此句話必須放在return true前,放在其後就見不到了,因爲return語句返回了,此句執行不到
return true;
}
}

void MainWindow::on_sendtextpushButton_clicked()
{
myCom->write(ui->sendtextkeylineEdit->text().toAscii());
//將行編輯器中文本轉化成ascii碼形式寫入串口
}

void MainWindow::on_pushButton_clicked()
{
ui->textBrowser->clear();
}

void MainWindow::on_pushButton_2_clicked()
{
ui->sendMsglineEdit->clear();
}

void MainWindow::on_choose_positionpushButton_clicked()
{
QString fileName = QFileDialog::getSaveFileName(this,tr("另存爲"),curFile);//獲取文件名
if(!fileName.isEmpty())//文件名不爲空,則保存文件
{
saveFile(fileName);
}
}

bool MainWindow::saveFile(const QString& fileName)//存儲文件
{
QFile file(fileName);
if(!file.open(QFile::WriteOnly|QFile::Text))
//以只寫方式打開文件,如果打開失敗則返回,彈出對話框
{
QMessageBox::warning(this,tr("保存文件"),tr("無法保存文件 %1:\n%2").arg(fileName).arg(file.errorString()));
return false;//%1和%2表示後面兩個arg的值
}
QTextStream out(&file);//新建流對象,指向選定文件
out<<ui->textBrowser->toPlainText();//將文本編輯器中的內容以純文本的形式輸出到流對象中
curFile = QFileInfo(fileName).canonicalFilePath();//獲取文件的標準路徑
ui->savedatalineEdit->setText(curFile);
// isSaved = true;
return true;

}

void MainWindow::on_action_W_triggered()
{
QDialog *md = new QDialog(this);
md->setWindowTitle(tr("關於作者"));
QLabel *tell = new QLabel(md);
QLabel *mpic = new QLabel(md);
QVBoxLayout* layout = new QVBoxLayout(md);//新建一個垂直佈局管理器,並將行編輯器和按鈕加入
layout->addWidget(tell);
layout->addWidget(mpic);
tell->setText(tr("羅曉亮,microliang,喜歡編程,尤愛Qt編程。\nQQ:995846665,Email:[email protected]"));
mpic->setPixmap(QPixmap("C:/icon/liang.jpg"));
md->show();
}

void MainWindow::on_action_H_triggered()
{
aboutu.show();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章