開源的Qt串口助手:一學就會
第一部分:代碼托管: 此部分可能存在一些未知的bug,歡迎廣大網(wǎng)友指出,為了方便管理和學習,所有代碼均托管到gitee,鏈接地址:https://gitee.com/lumengcode/my-qt
第二部分效果展示:
實現(xiàn)的功能:
1.自動獲取計算機的端口號;
2.串口參數(shù)可更改:包括 波特率、數(shù)據(jù)位、停止位、校驗和等............
3.串口數(shù)據(jù)的發(fā)送和接收
4.支持十六進制數(shù)據(jù)的發(fā)送和接收
5.支持時間戳功能,方便文件的存儲查看
6.發(fā)送從窗口和接收窗口的清理
7.定時發(fā)送功能
............
簡單設置一下背景色,好看多了!
第二部分:代碼部分:
1.當我們的計算機的端口號發(fā)生改變時,串口助手要具備實時掃面本機的端口號的功能,具有實時獲取有效的串口信息,并將其刷新到下拉框中供我們選擇。 有些自己編寫的串口助手是沒有這個功能的,這里我給大家補充上去。
//使用foreach獲取有效的串口信息 foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { //這里相當于自動識別串口號之后添加到了cmb,如果要手動選擇可以用下面列表的方式添加進去 Serial.setPort(info); if(Serial.open(QIODevice::ReadWrite)) { //將串口號添加到cmb ui->comboBox_Port->addItem(info.portName()); //關(guān)閉串口等待人為(打開串口按鈕)打開 Serial.close(); } } 2.填充下拉框的波特率、數(shù)據(jù)位、停止位、效驗位….,初始化下拉框默認參數(shù),這個參數(shù)設置大部分的串口助手都會具備,因此不足為奇。 該有的功能個咱還是得有的。
// 填充波特率 QStringList Baud; Baud<<"1200"<<"2400"<<"4800"<<"9600"<<"38400"<<"115200"; ui->comboBox_Baud->addItems(Baud); // 填充數(shù)據(jù)位 QStringList DataBit; DataBit<<"5"<<"6"<<"7"<<"8"; ui->comboBox_DataBit->addItems(DataBit); // 填充停止位 QStringList StopBit; StopBit<<"1"<<"1.5"<<"2"; ui->comboBox_StopBit->addItems(StopBit); // 填充效驗位 QStringList CheckBit; CheckBit<<"奇效驗"<<"偶效驗"<<"無"; ui->comboBox_CheckBit->addItems(CheckBit); //初始化默認參數(shù) ui->comboBox_Baud->setCurrentIndex(3); //默認9600 ui->comboBox_DataBit->setCurrentIndex(3); //默認8bit Data ui->comboBox_StopBit->setCurrentIndex(0); //默認1bit Stop ui->comboBox_CheckBit->setCurrentIndex(2); //默認 無效驗 3.串口打開和關(guān)閉按鈕操作,這個就是打開串口按鈕和關(guān)閉按鈕的邏輯操作,成功打開串口后,相應的參數(shù)將會被設置。串口即可以用于數(shù)據(jù)的發(fā)送和接收了,這里也處理,打開失敗時的邏輯操作,可謂是“疏而不漏也!”。
//串口打開和關(guān)閉按鈕void MainWindow::on_pushButton_Open_clicked(){ //設置串口號;也就是說打開的是當前顯示的串口 if(ui->comboBox_Port->currentText().isEmpty()) { QMessageBox::information(this,"提示","沒有可用的串口"); return; } Serial.setPortName(ui->comboBox_Port->currentText()); if(ui->pushButton_Open->text() == "打開串口") { if(Serial.open(QIODevice::ReadWrite))//讀寫方式打開,成功后設置串口 { //設置波特率 Serial.setBaudRate(ui->comboBox_Baud->currentText().toInt()); //設置數(shù)據(jù)位 switch(ui->comboBox_DataBit->currentText().toInt()) { case 5: Serial.setDataBits(QSerialPort::Data5); break; case 6: Serial.setDataBits(QSerialPort::Data6); break; case 7: Serial.setDataBits(QSerialPort::Data7); break; case 8: Serial.setDataBits(QSerialPort::Data8); break; default: QMessageBox::information(this,"提示","數(shù)據(jù)位配置出錯"); return; break; } //設置校驗位 if (ui->comboBox_CheckBit->currentText() == "奇效驗") { Serial.setParity(QSerialPort::OddParity); } else if (ui->comboBox_CheckBit->currentText() == "偶效驗") { Serial.setParity(QSerialPort::EvenParity); } else if (ui->comboBox_CheckBit->currentText() == "無") { Serial.setParity(QSerialPort::NoParity); } //設置停止位 if (ui->comboBox_StopBit->currentText().toFloat() == 1) { Serial.setStopBits(QSerialPort::OneStop); } else if(ui->comboBox_StopBit->currentText().toFloat() == 1.5) { Serial.setStopBits(QSerialPort::OneAndHalfStop); } else if(ui->comboBox_StopBit->currentText().toFloat() == 2) { Serial.setStopBits(QSerialPort::TwoStop); } //設置流控制 Serial.setFlowControl(QSerialPort::NoFlowControl); ui->pushButton_Open->setText("關(guān)閉串口"); //建立串口接收的槽函數(shù) connect(&Serial,&QSerialPort::readyRead ,this,&MainWindow::ReadRecData); // timer0->start(100); } else//串口打開失敗 { QMessageBox::about(NULL, "提示", "打開出錯,串口被占用!"); return ; } } else if(ui->pushButton_Open->text() == "關(guān)閉串口") { Serial.close();//關(guān)串口 //timer0->stop(); ui->pushButton_Open->setText("打開串口"); }} 4. 串口接收數(shù)據(jù)函數(shù)(支持時間戳、HEX接收) 這個是很關(guān)鍵的地方了,要保證數(shù)據(jù)接收的完整性和實時性,可采用兩種接收數(shù)據(jù)的模式:定時器觸發(fā)和槽觸發(fā),定時器觸發(fā)我這里采用的是100ms的中斷接收,大家還可以調(diào)的更小一點。
void MainWindow::ReadRecData(){ QByteArray readData = Serial.readAll();//讀取串口數(shù)據(jù) QByteArray NewData; QString current_date; if(readData != NULL)//將讀到的數(shù)據(jù)顯示到數(shù)據(jù)接收區(qū) { if(HexRecvFlag) //判斷是否使用HEX { //判斷是否使用時間戳 if(EnableTimeFlag == 1) { current_date_time = QDateTime::currentDateTime(); current_date += "["; current_date += current_date_time.toString("yyyy-MM-dd hh:mm:ss"); current_date += "]收->"; ui->textEdit_Recv->append(current_date.toUtf8() + readData.toHex()); } else { ui->textEdit_Recv->append(readData.toHex()); } } else { //判斷是否使用時間戳 if(EnableTimeFlag == 1) { current_date_time = QDateTime::currentDateTime(); current_date += "["; current_date += current_date_time.toString("yyyy-MM-dd hh:mm:ss"); current_date += "]收->"; ui->textEdit_Recv->append(current_date.toUtf8() + readData); } else { ui->textEdit_Recv->append(readData); } } }} 5. 串口發(fā)送數(shù)據(jù)函數(shù)(支持時間戳、HEX接收) 這個是很關(guān)鍵的地方了,串口發(fā)送數(shù)據(jù)的方式就比較簡單了,直接將數(shù)據(jù)送入緩沖區(qū)
//發(fā)送數(shù)據(jù)void MainWindow::on_pushButton_Send_clicked(){ QString DataStr; QString NewData; QString current_date; DataStr = ui->textEdit_Send->toPlainText(); if(ui->pushButton_Open->text() == "打開串口") { QMessageBox::information(this,"提示","未打開串口"); return; } if(EnableTimeFlag == 1) { current_date_time = QDateTime::currentDateTime(); current_date += "["; current_date += current_date_time.toString("yyyy-MM-dd hh:mm:ss"); current_date += "]發(fā)->"; NewData = current_date + DataStr; } else { NewData = DataStr; } if(HexSendFlag) { Serial.write(DataStr.toUtf8().toHex());//寫入緩沖區(qū) } else { ui->textEdit_Recv->append(NewData.toUtf8()); } 6.清除接收和發(fā)送窗口數(shù)據(jù)函數(shù),為了方便調(diào)試和觀察,這里添加了清除接收和發(fā)送窗口數(shù)據(jù)函數(shù)的操作。
//清除接收窗口數(shù)據(jù)void MainWindow::on_pushButton_ClearRecv_clicked(){ ui->textEdit_Recv->clear();} //清除發(fā)送窗口數(shù)據(jù)void MainWindow::on_pushButton_2_clicked(){ ui->textEdit_Send->clear();} 7.使能時間戳,時間戳的主要目的在于通過一定的技術(shù)手段,對數(shù)據(jù)產(chǎn)生的時間進行認證,從而驗證這段數(shù)據(jù)在產(chǎn)生后是否經(jīng)過篡改。所以時間戳服務的提供者必須證明服務中使用的時間源是可信的,所提供的時間戳服務是安全的。
void MainWindow::on_checkBox_EnableTime_clicked(bool checked){ if(checked == true) { EnableTimeFlag = 1; } else { EnableTimeFlag = 0; }} 8. 使能定時發(fā)送,定時發(fā)送很香了,必須得有啊!
void MainWindow::on_checkBox_clicked(bool checked){ if(checked == true) { if(ui->pushButton_Open->text() == "打開串口") { QMessageBox::information(this,"提示","未打開串口"); ui->checkBox->setChecked(false); return; } quint32 stime= ui->lineEdit_STime->text().toInt(); timer_id1 = startTimer(stime); ui->lineEdit_STime->setEnabled(false); } else { killTimer(timer_id1); ui->lineEdit_STime->setEnabled(true); }} 9. 使能HEX 發(fā)送和接收按鈕,HEX那是標配,我只希望不要出bug,慢慢完善吧!
void MainWindow::on_checkBox_HexRecv_clicked(bool checked){ if(checked) { HexRecvFlag = 1; } else HexRecvFlag = 0;} void MainWindow::on_checkBox_HexSend_clicked(bool checked){ if(checked) HexSendFlag = 1; else HexSendFlag = 0;} 10. 定時器中斷函數(shù) 觸發(fā)接收串口數(shù)據(jù)的核心,沒它啥也干不了。
void MainWindow:: timerEvent(QTimerEvent *ev){ if(ev->timerId() == timer_id1) { on_pushButton_Send_clicked(); }} 串口助手部分最后的展示效果END 本文系21ic論壇藍V作者一路向北lm原創(chuàng)
免責聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺僅提供信息存儲服務。文章僅代表作者個人觀點,不代表本平臺立場,如有問題,請聯(lián)系我們,謝謝!