之前學了一下Qt,然后做了一個串口助手。功能測試穩定!初學者可以參考一下!
串口助手-精簡版-V3.14
作者:
付強
bearfq@126.com
時間:
2018年1月19日 10:50:53
實現功能:
1.串口收發
2.保存數據到本地
3.打開本地數據文件
4.調整波特率等參數
測試實現:
性能穩定
0.png (54.05 KB, 下載次數: 127)
下載附件
2018-3-4 02:03 上傳
源程序如下:
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- /*設置窗口圖標*/
- this->setWindowIcon(QIcon(":/logo256_256.ico"));
- /*激活參數選擇框*/
- activeComboBox(true);
- /*顯示所有可用端口*/
- showAllPort();
- /*設置接收文本框的光標一直保持在中心*/
- connect(ui->recvTextEdit, SIGNAL(textChanged()), ui->recvTextEdit, SLOT(centerCursor()));
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- }
- /*激活/失活參數選擇框*/
- void MainWindow::activeComboBox(bool status)
- {
- ui->serialPortComboBox->setEnabled(status);//端口下選框
- ui->BaudrateComboBox->setEnabled(status);//波特率下選框
- ui->dataBitComboBox->setEnabled(status);//數據位數
- ui->stopBitComboBox->setEnabled(status);//停止位
- ui->parityComboBox->setEnabled(status);//奇偶校驗
- ui->flushPortBtn->setEnabled(status);//刷新按鈕
- }
- //反轉runFlag標志,并返回標志狀態
- bool MainWindow::reverseRunFlag()
- {
- if (myThread->runFlag)
- {
- myThread->runFlag = false;
- }
- else
- {
- myThread->runFlag = true;
- }
- return myThread->runFlag;
- }
- /*顯示所有可用端口*/
- void MainWindow::showAllPort()
- {
- const auto infos = QSerialPortInfo::availablePorts();//返回可用的端口
- for (const QSerialPortInfo &info : infos)//將所有可用端口加入到下拉框中
- {
- ui->serialPortComboBox->addItem(info.portName());
- }
- }
- /*清除接收文本框中的信息*/
- void MainWindow::on_clearRecvBtn_clicked()
- {
- ui->recvTextEdit->clear();
- }
- /*清除發送文本框中的信息*/
- void MainWindow::on_clearSendBtn_clicked()
- {
- ui->sendTextEdit->clear();
- }
- /*點擊open按鈕*/
- void MainWindow::on_openBtn_clicked()
- {
- DBUG_OUT;
- if(!reverseRunFlag())//子線程在運行--->關閉子線程
- {
- /*參數設置combobox全部激活*/
- activeComboBox(true);
- /*修改子線程運行標志*/
- myThread->runFlag = false;
- // myThread->finished();
- }
- else//沒有運行子線程--->開啟子線程
- {
- /*參數設置combobox全部失活*/
- activeComboBox(false);
- /*創建線程*/
- myThread = new MyThread;
- /*傳遞參數給子線程*/
- myThread->portNameStr = ui->serialPortComboBox->currentText();
- myThread->baudRateStr = ui->BaudrateComboBox->currentText();
- myThread->parityStr = ui->parityComboBox->currentText();
- myThread->dataBitsStr = ui->dataBitComboBox->currentText();
- myThread->stopBitsStr = ui->stopBitComboBox->currentText();
- /*連接信號與槽(將接收的數據顯示在接收區中)*/
- connect(myThread, SIGNAL(output(QString)), this, SLOT(showData(QString)));
- //啟動線程--->執行線程的run函數
- myThread->start();
- }
- }
- /*顯示接收的數據*/
- void MainWindow::showData(QString data)
- {
- DBUG_OUT;
- ui->recvTextEdit->insertPlainText(data);
- }
- /*點擊發送按鈕,發送數據*/
- void MainWindow::on_sendBtn_clicked()
- {
- DBUG_OUT;
- //1.提取發送數據框文本
- myThread->sendData = ui->sendTextEdit->toPlainText();
- //2.發送標志置1
- myThread->sendFlag = true;
- }
- /*保存接收的數據到文件*/
- void MainWindow::on_saveRecvBtn_clicked()
- {
- DBUG_OUT;
- //1.彈出保存文件的對話框
- QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),"./recvdata",tr("text (*.txt *.cpp *.h)"));
- //2.創建新文件
- QFile file(fileName);
- file.open(QIODevice::ReadWrite | QIODevice::Truncate);
- //3.寫入文本到文件中
- //3.1獲取text中的文本
- QString text = ui->recvTextEdit->toPlainText();//轉成純文本
- //3.2將文本寫入文件中
- file.write(text.toStdString().c_str());//轉為標準字符串并返回其首地址
- //4.關閉文件
- file.close();
- }
- /*打開數據文件*/
- void MainWindow::on_openDataBtn_clicked()
- {
- DBUG_OUT;
- //1.彈出對話框:選擇文件,獲取文件名
- QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
- "./",
- tr("Text (*.txt *.cpp *.h)"));
- //2.讀文件
- //2.1打開文件
- QFile file(fileName);
- file.open(QIODevice::ReadOnly);
- //2.2讀文件
- char buf[1024] = {0};
- file.read(buf, sizeof(buf));
- //2.3關閉文件
- file.close();
- //3.在發送區中顯示文件信息
- QString text(buf);
- ui->sendTextEdit->insertPlainText(text);
- }
- /*刷新端口*/
- void MainWindow::on_flushPortBtn_clicked()
- {
- ui->serialPortComboBox->clear();//清除原有端口號
- showAllPort();//顯示現有端口號
- }
復制代碼
所有資料51hei提供下載:
串口助手精簡版.rar
(43.19 KB, 下載次數: 283)
2018-3-3 10:35 上傳
點擊文件名下載附件
源碼串口助手 下載積分: 黑幣 -5
|