乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Qt編程之“串口助手”

       guitarhua 2014-08-28

      Qt編程之“串口助手”

      (2011-11-19 11:58:01)
      標(biāo)簽:

      串口助手

      雜談

      分類: Qt編程

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

      Qt編程之“串口助手”

      下面是該軟件mainwindow的相關(guān)源代碼,也是這個小軟件的精華部分,重要代碼行后面均有注釋,有祝大家理解:

      #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("草原神鷹串口助手"));//初始化主窗口的標(biāo)題
      
          ui->closeMyComBtn->setEnabled(false);//開始“關(guān)閉摁鈕”不可用
      
          ui->sendMsgBtn->setEnabled(false);////開始“發(fā)送數(shù)據(jù)摁鈕”不可用
      
          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();
      
          //讀取數(shù)據(jù)緩沖區(qū)中的所有數(shù)據(jù)給臨時變量temp
      
          ui->textBrowser->insertPlainText(temp);
      
          //將串口數(shù)據(jù)顯示在窗口的文本瀏覽器中
      
      }
      
      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"))//根據(jù)波特率窗口設(shè)置串口波特率
      
              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);
      
          //設(shè)置數(shù)據(jù)位
      
          if(ui->dataBitscomboBox->currentText()==tr("8"))
      
              myCom->setDataBits(DATA_8);
      
          else if(ui->dataBitscomboBox->currentText()==tr("7"))
      
              myCom->setDataBits(DATA_7);
      
          //設(shè)置奇偶校驗(yàn)位
      
          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);
      
          //設(shè)置停止位
      
          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);//設(shè)置為無數(shù)據(jù)流控制
      
          myCom->setTimeout(500);//延時
      
          connect(myCom,SIGNAL(readyRead()),this,SLOT(readMyCom()));
      
          //信號和槽函數(shù)連接,當(dāng)串口緩沖區(qū)有數(shù)據(jù)時,進(jìn)行讀串口操作
      
          ui->openMyComBtn->setEnabled(false);//打開串口后“打開串口摁鈕”不可用
      
          ui->closeMyComBtn->setEnabled(true);//打開串口后”關(guān)閉串口摁鈕“可用
      
          ui->sendMsgBtn->setEnabled(true);////打開串口后“發(fā)送數(shù)據(jù)摁鈕”可用
      
          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();//關(guān)閉串口
      
          ui->openMyComBtn->setEnabled(true);//關(guān)閉串口后“打開串口摁鈕”可用
      
          ui->sendMsgBtn->setEnabled(false);////關(guān)閉串口后“發(fā)送數(shù)據(jù)摁鈕”不可用
      
          ui->closeMyComBtn->setEnabled(false);//關(guān)閉串口后”關(guān)閉串口摁鈕“不可用
      
          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());
      
          //將行編輯器中文本轉(zhuǎn)化成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());//將文件中所有的內(nèi)容寫到行編輯器中
      
              curFile = QFileInfo(fileName).canonicalFilePath();
      
              ui->sendtextpathlineEdit->setText(curFile);
      
              ui->sendtextpathlineEdit->setVisible(true);//將文本設(shè)置為可見,此句話必須放在return true前,放在其后就見不到了,因?yàn)閞eturn語句返回了,此句執(zhí)行不到
      
              return true;
      
          }
      
      }
      
      void MainWindow::on_sendtextpushButton_clicked()
      
      {
      
         myCom->write(ui->sendtextkeylineEdit->text().toAscii());
      
          //將行編輯器中文本轉(zhuǎn)化成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();//將文本編輯器中的內(nèi)容以純文本的形式輸出到流對象中
      
          curFile = QFileInfo(fileName).canonicalFilePath();//獲取文件的標(biāo)準(zhǔn)路徑
      
           ui->savedatalineEdit->setText(curFile);
      
         // isSaved = true;
      
          return true;
      
      }
      
      void MainWindow::on_action_W_triggered()
      
      {
      
          QDialog *md = new QDialog(this);
      
          md->setWindowTitle(tr("關(guān)于作者"));
      
          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:lifeliang@hotmail.com"));
      
          mpic->setPixmap(QPixmap("C:/icon/liang.jpg"));
      
          md->show();
      
      }
      
      void MainWindow::on_action_H_triggered()
      
      {
      
          aboutu.show();
      

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多