串口調(diào)試助手是一款用于串口調(diào)試的工具,目前網(wǎng)上存在很多個(gè)版本,功能都差不多,但穩(wěn)定性有好有壞,如果只用于一般的串口調(diào)試,這些工具夠用了,如果想開發(fā)一款適于自己的串口調(diào)試助手,本文也許可以幫你。本文中的調(diào)試助手用QT開發(fā),QT的開發(fā)環(huán)境不在這里詳述了,不會安裝的可以在網(wǎng)上找找類似的博文吧,以下僅供參考。
第一部分:代碼托管:此部分可能存在一些未知的bug,歡迎廣大網(wǎng)友指出。
第二部分:效果展示:
實(shí)現(xiàn)的功能:
1.自動(dòng)獲取計(jì)算機(jī)的端口號;
2.串口參數(shù)可更改:包括 波特率、數(shù)據(jù)位、停止位、校驗(yàn)和等。
3.串口數(shù)據(jù)的發(fā)送和接收
4.支持十六進(jìn)制數(shù)據(jù)的發(fā)送和接收
5.支持時(shí)間戳功能,方便文件的存儲查看
6.發(fā)送從窗口和接收窗口的清理
7.定時(shí)發(fā)送功能
簡單設(shè)置一下背景色,好看多了!
第二部分:代碼部分:
1.當(dāng)我們的計(jì)算機(jī)的端口號發(fā)生改變時(shí),串口助手要具備實(shí)時(shí)掃面本機(jī)的端口號的功能,具有實(shí)時(shí)獲取有效的串口信息,并將其刷新到下拉框中供我們選擇。有些自己編寫的串口助手是沒有這個(gè)功能的,這里我給大家補(bǔ)充上去。
//使用foreach獲取有效的串口信息 foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { //這里相當(dāng)于自動(dòng)識別串口號之后添加到了cmb,如果要手動(dòng)選擇可以用下面列表的方式添加進(jìn)去 Serial.setPort(info); if(Serial.open(QIODevice::ReadWrite)) { //將串口號添加到cmb ui-》comboBox_Port-》addItem(info.portName()); //關(guān)閉串口等待人為(打開串口按鈕)打開 Serial.close(); } }
2.填充下拉框的波特率、數(shù)據(jù)位、停止位、效驗(yàn)位…。,初始化下拉框默認(rèn)參數(shù),這個(gè)參數(shù)設(shè)置大部分的串口助手都會具備,因此不足為奇。該有的功能個(gè)咱還是得有的。
// 填充波特率 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); // 填充效驗(yàn)位 QStringList CheckBit; CheckBit《《“奇效驗(yàn)”《《“偶效驗(yàn)”《《“無”; ui-》comboBox_CheckBit-》addItems(CheckBit);
//初始化默認(rèn)參數(shù) ui-》comboBox_Baud-》setCurrentIndex(3); //默認(rèn)9600 ui-》comboBox_DataBit-》setCurrentIndex(3); //默認(rèn)8bit Data ui-》comboBox_StopBit-》setCurrentIndex(0); //默認(rèn)1bit Stop ui-》comboBox_CheckBit-》setCurrentIndex(2); //默認(rèn) 無效驗(yàn)
3.串口打開和關(guān)閉按鈕操作,這個(gè)就是打開串口按鈕和關(guān)閉按鈕的邏輯操作,成功打開串口后,相應(yīng)的參數(shù)將會被設(shè)置。串口即可以用于數(shù)據(jù)的發(fā)送和接收了,這里也處理,打開失敗時(shí)的邏輯操作,可謂是“疏而不漏也!”。
//串口打開和關(guān)閉按鈕void MainWindow::on_pushButton_Open_clicked(){ //設(shè)置串口號;也就是說打開的是當(dāng)前顯示的串口 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))//讀寫方式打開,成功后設(shè)置串口 { //設(shè)置波特率 Serial.setBaudRate(ui-》comboBox_Baud-》currentText().toInt());
//設(shè)置數(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ù)位配置出錯(cuò)”); return; break; }
//設(shè)置校驗(yàn)位 if (ui-》comboBox_CheckBit-》currentText() == “奇效驗(yàn)”) { Serial.setParity(QSerialPort::OddParity); } else if (ui-》comboBox_CheckBit-》currentText() == “偶效驗(yàn)”) { Serial.setParity(QSerialPort::EvenParity); } else if (ui-》comboBox_CheckBit-》currentText() == “無”) { Serial.setParity(QSerialPort::NoParity); }
//設(shè)置停止位 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); }
//設(shè)置流控制 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, “提示”, “打開出錯(cuò),串口被占用!”); return ; } } else if(ui-》pushButton_Open-》text() == “關(guān)閉串口”) { Serial.close();//關(guān)串口 //timer0-》stop(); ui-》pushButton_Open-》setText(“打開串口”); }}
4. 串口接收數(shù)據(jù)函數(shù)(支持時(shí)間戳、HEX接收) 這個(gè)是很關(guān)鍵的地方了,要保證數(shù)據(jù)接收的完整性和實(shí)時(shí)性,可采用兩種接收數(shù)據(jù)的模式:定時(shí)器觸發(fā)和槽觸發(fā),定時(shí)器觸發(fā)我這里采用的是100ms的中斷接收,大家還可以調(diào)的更小一點(diǎn)。
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 { //判斷是否使用時(shí)間戳 if(EnableTimeFlag == 1) { current_date_time = QDateTime::currentDateTime(); current_date += “[”; current_date += current_date_time.toString(“yyyy-MM-dd hhss”); current_date += “]收-》”; ui-》textEdit_Recv-》append(current_date.toUtf8() + readData.toHex()); } else { ui-》textEdit_Recv-》append(readData.toHex()); }
} else { //判斷是否使用時(shí)間戳 if(EnableTimeFlag == 1) { current_date_time = QDateTime::currentDateTime(); current_date += “[”; current_date += current_date_time.toString(“yyyy-MM-dd hhss”); current_date += “]收-》”; ui-》textEdit_Recv-》append(current_date.toUtf8() + readData); } else { ui-》textEdit_Recv-》append(readData); } }
}}
5. 串口發(fā)送數(shù)據(jù)函數(shù)(支持時(shí)間戳、HEX接收) 這個(gè)是很關(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 hhss”); 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í)間戳的主要目的在于通過一定的技術(shù)手段,對數(shù)據(jù)產(chǎn)生的時(shí)間進(jìn)行認(rèn)證,從而驗(yàn)證這段數(shù)據(jù)在產(chǎn)生后是否經(jīng)過篡改。所以時(shí)間戳服務(wù)的提供者必須證明服務(wù)中使用的時(shí)間源是可信的,所提供的時(shí)間戳服務(wù)是安全的。
void MainWindow::on_checkBox_EnableTime_clicked(bool checked){ if(checked == true) { EnableTimeFlag = 1; } else { EnableTimeFlag = 0; }}
8. 使能定時(shí)發(fā)送,定時(shí)發(fā)送很香了,必須得有?。?/p>
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那是標(biāo)配,我只希望不要出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í)器中斷函數(shù) 觸發(fā)接收串口數(shù)據(jù)的核心,沒它啥也干不了。
void MainWindow:: timerEvent(QTimerEvent *ev){ if(ev-》timerId() == timer_id1) { on_pushButton_Send_clicked(); }}
串口助手部分最后的展示效果
原文標(biāo)題:開源的Qt串口助手:一學(xué)就會
文章出處:【微信公眾號:嵌入式ARM】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
責(zé)任編輯:haq
-
串口
+關(guān)注
關(guān)注
14文章
1559瀏覽量
77124 -
Qt
+關(guān)注
關(guān)注
1文章
309瀏覽量
38086
原文標(biāo)題:開源的Qt串口助手:一學(xué)就會
文章出處:【微信號:gh_c472c2199c88,微信公眾號:嵌入式微處理器】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
無線串口模塊—配置軟件以及串口助手的使用
![無線<b class='flag-5'>串口</b>模塊—配置軟件以及<b class='flag-5'>串口</b><b class='flag-5'>助手</b>的使用](https://file1.elecfans.com//web2/M00/09/B0/wKgZomcQ4hKAGY4eAACxVw_cFSs319.jpg)
示波器高壓探頭使用方法
探針頭型使用方法有哪些
TensorFlow的定義和使用方法
淺談錫膏的儲存及使用方法
可編程電源使用方法
4月全志芯片開源項(xiàng)目分享合輯
超好用的紙飛機(jī)串口調(diào)試助手
![超好用的紙飛機(jī)<b class='flag-5'>串口</b>調(diào)試<b class='flag-5'>助手</b>](https://file1.elecfans.com/web2/M00/DE/22/wKgZomYtrdKAQA3EAABTUg92Wvk565.png)
評論