日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

串口调试工具开发

發(fā)布時(shí)間:2024/1/18 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 串口调试工具开发 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

剛學(xué)習(xí)Qt,搜查資料,根據(jù)學(xué)到的知識(shí),完成一個(gè)簡(jiǎn)單的串口調(diào)試工具~剛?cè)胄?#xff0c;諒解

最終效果圖,圖上功能都可實(shí)現(xiàn)

UI界面繪制:

代碼如下:

串口調(diào)試工具開發(fā)1.創(chuàng)建工程QWidget,根據(jù)串口調(diào)試工具繪制Ui界面 給每個(gè)控件設(shè)置特定名,方便調(diào)用2.在.pro文件添加代碼 QT += core gui serialport3.在.h文件頭添加代碼 #include <QSerialPort> #include <QSerialPortInfo>private:Ui::Widget *ui;QSerialPort * serialport;//串口端口QTimer *timSend;//創(chuàng)建定時(shí)器對(duì)象QString mTime;//拼接日志樣式對(duì)象QDateTime mDateTime;//獲取當(dāng)前時(shí)間QByteArray Sendtext;//發(fā)送緩存區(qū)QByteArray Receivetext;//接收緩存區(qū)long int SendByte=0;//發(fā)送字符數(shù)long int ReceByte=0;//接收字符數(shù)4.給串口號(hào)設(shè)置自動(dòng)獲取電腦中的串口//遍歷可用串口設(shè)備//創(chuàng)建一個(gè)QStringList 對(duì)象來存儲(chǔ)端口名QStringList serialPort_Name;//foreach遍歷方法foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts()){//端口名呈現(xiàn)格式serialPort_Name << info.portName();}//添加到控件串口顯示ui->PostName->addItems(serialPort_Name);*如果需要過濾無用串口,代碼如下//獲取串口列表int snum=0;//用下面的方法會(huì)過濾掉一些無用的串口QSerialPort temp_serial;foreach (const QSerialPortInfo &Info, QSerialPortInfo::availablePorts()) {temp_serial.setPort(Info);if(temp_serial.open(QIODevice::ReadWrite)){//如果串口是可以讀寫方式打開的ui->PostName->addItem(Info.portName());temp_serial.close();++snum;}}//默認(rèn)選中第0項(xiàng)if(snum>2){ui->PostName->setCurrentIndex(1);}ui->PostName->currentText();*如果需要對(duì)下拉框進(jìn)行自適應(yīng)設(shè)置,代碼如下//下拉列表自適應(yīng)/* 獲取最長字符串 */int maxlen = 0;for (int index = 0; index < ui->PostName->count(); index++){if (ui->PostName->itemText(index).length() > maxlen){maxlen = ui->PostName->itemText(index).length();}}/*獲取字體磅值轉(zhuǎn)換為像素值*/int fontsize = ui->PostName->font().pointSize();//獲取字體的磅值ui->PostName->setFixedWidth(fontsize * maxlen * 0.75);//設(shè)置像素值 ```接下來需聲明一個(gè)串口全局對(duì)象,在.h文件中 QSerialPort * serialport;//串口端口聲明后在.cpp文件中創(chuàng)建出來 serialport = new QSerialPort(this);//聲明后把對(duì)象創(chuàng)建出來給打開按鈕設(shè)置轉(zhuǎn)到槽.h文件自動(dòng)生成 private slots:void on_Btn_Open_clicked();//打開鍵按下/*打開按鈕配置*/ /*串口參數(shù)配置*/ void Widget::on_Btn_Open_clicked() {if (ui->Btn_Open->text()=="打開") {//設(shè)置串口名serialport->setPortName(ui->PostName->currentText());//設(shè)置波特率switch (ui->BaudRate->currentIndex()){case 0:serialport->setBaudRate(QSerialPort::Baud19200,QSerialPort::AllDirections);break;case 1:serialport->setBaudRate(QSerialPort::Baud38400,QSerialPort::AllDirections);break;case 2:serialport->setBaudRate(QSerialPort::Baud57600,QSerialPort::AllDirections);break;default:serialport->setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections);break;} /* 檢驗(yàn)位、數(shù)據(jù)位、停止位、流控制 與 波特率(switch)寫法一致 *///打開串口if(!serialport->open(QIODevice::ReadWrite)){QMessageBox::about(NULL,"提示","無法打開串口");return;}/*點(diǎn)擊后效果*///設(shè)置控件不可編輯ui->PostName->setEnabled(false);ui->BaudRate->setEnabled(false);ui->Parity->setEnabled(false);ui->DataBits->setEnabled(false);ui->StopBits->setEnabled(false);ui->FlowControl->setEnabled(false);//點(diǎn)擊打開按鈕后發(fā)送和循環(huán)發(fā)送可編輯ui->Btn_Send->setEnabled(true);ui->Cyclet_time->setEnabled(true);//點(diǎn)擊打開后顯示關(guān)閉ui->Btn_Open->setText("關(guān)閉");ui->Btn_Open->setStyleSheet("QPushButton{color:red;background:yellow}");/*點(diǎn)擊前效果*/與上方代碼相反,寫在else{}中 ```獲取時(shí)間戳/*獲取時(shí)間戳*/ 先在.h頭文件創(chuàng)建對(duì)象,如經(jīng)常使用的對(duì)象,都在頭文件創(chuàng)建,減少損耗資源 QString mTime; QDateTime mDateTime; .cpp文件寫對(duì)應(yīng)事件 if(ui->Re_Log->isChecked()){mDateTime = QDateTime::currentDateTime();//拼接日志樣式mTime = "[";mTime += mDateTime.toString("[yyyy-MM-dd hh:mm:ss.zzz]") ;mTime += "]:RECV ->" ;mTime += Receivetext;ui->Re_Edit->append(mTime);}添加數(shù)據(jù)換行問題,insertPlainText函數(shù)在原數(shù)據(jù)末尾直接添加,可以使用append函數(shù)替代換行添加if(ui->Re_Line->checkState()==Qt::Checked){ui->Re_Edit->append(Receivetext);}else{ui->Re_Edit->insertPlainText(Receivetext);} ```**串口接收代碼**/*串口接收*/ void Widget::on_SerialPort_readyRead(){int i,length;Receivetext = serialport->readAll();//接收字節(jié)并顯示ReceByte+=Receivetext.length();ui->Re_Byte->setText(QString::number(ReceByte));//十六進(jìn)制if(ui->Re_Hex->checkState()==Qt::Checked){//字符串轉(zhuǎn)十六進(jìn)制Receivetext = Receivetext.toHex().toUpper();length=Receivetext.length();for(i=0;i<=length/2;i++){//插入空格字符串Receivetext.insert((2+3*i),' ');}}//自動(dòng)換行if(ui->Re_Line->checkState()==Qt::Checked){ui->Re_Edit->append(Receivetext);}//日志顯示樣式else if(ui->Re_Log->isChecked()){//獲取當(dāng)前時(shí)間mDateTime = QDateTime::currentDateTime();//時(shí)間展示拼接樣式mTime = "[";mTime += mDateTime.toString("[yyyy-MM-dd hh:mm:ss.zzz]") ;mTime += "]:RECV ->" ;mTime += Receivetext;//append自動(dòng)換行ui->Re_Edit->append(mTime);}else{//原數(shù)據(jù)末尾添加(文本接收)ui->Re_Edit->insertPlainText(Receivetext);} }**串口發(fā)送代碼**void Widget::on_Btn_Send_clicked() {int i,length;Sendtext = ui->Se_Edit->toPlainText().toUtf8();//發(fā)送設(shè)置HEXif(ui->Se_Hex->checkState()==Qt::Checked){//轉(zhuǎn)換成十六進(jìn)制Sendtext = Sendtext.toHex().toUpper();length=Sendtext.length();for(i=0;i<=length/2;i++){//插入空格字符串Sendtext.insert((2+3*i),' ');}serialport->write(Sendtext);}else{serialport->write(Sendtext);}//發(fā)送字節(jié)顯示SendByte+=Sendtext.length();ui->Se_Byte->setText(QString::number(SendByte)); // ui->Se_Edit->moveCursor(QTextCursor::End);}定時(shí)器使用方法在.h頭文件中 //定義并初始化mTimerID int mTimerID= 0;/*定時(shí)器定時(shí)發(fā)送*///設(shè)置默認(rèn)值1000ms mTimerID = startTimer(500);在.cpp文件中 void Widget::timerEvent(QTimerEvent *event) {qDebug()<<"我是定時(shí)器!"; } void Widget::closeEvent(QCloseEvent *event) {if(mTimerID){killTimer(mTimerID);mTimerID = 0;} }**串口循環(huán)發(fā)送代碼**在.h頭文件 QTimer *timSend;//創(chuàng)建定時(shí)器對(duì)象在.cpp文件 //定時(shí)發(fā)送=定時(shí)器timSend=new QTimer;timSend->setInterval(1000);//設(shè)置默認(rèn)值1000ms//定時(shí)器信號(hào)連接發(fā)送信號(hào)connect(timSend,&QTimer::timeout,this,[=](){on_Btn_Send_clicked();});ui界面給循環(huán)發(fā)送checkbox轉(zhuǎn)到槽void on_Cyclet_time_stateChanged(int arg1);//循環(huán)發(fā)送void Widget::on_Cyclet_time_stateChanged(int arg1) {//獲取復(fù)選框狀態(tài),沒有選就為0,選擇了就為2if(arg1==0){timSend->stop();}else {//對(duì)輸入的值大小的限制,小于10會(huì)彈出對(duì)話框提示if(ui->Se_Line_2->text().toInt()>=10){timSend->start(ui->Se_Line_2->text().toInt());//設(shè)置時(shí)長,重新計(jì)數(shù)}else {ui->Cyclet_time->setCheckState(Qt::Unchecked);QMessageBox::information(this,"錯(cuò)誤提示","定時(shí)發(fā)送最小間隔為10ms!");}} }```清空按鈕在.h頭文件 void on_Btn_Reclean_clicked();//清空接收區(qū)域 void on_Btn_Seclean_clicked();//清空發(fā)送區(qū)域在.cpp文件 /*清空接收區(qū)域*/ void Widget::on_Btn_Reclean_clicked() {ui->Re_Edit->clear(); } /*清空發(fā)送區(qū)域*/ void Widget::on_Btn_Seclean_clicked() {ui->Se_Edit->clear(); }ASCII、HEX發(fā)送與接收在.h頭文件中 void on_Re_Hex_clicked();//點(diǎn)擊十六進(jìn)制接收 void on_Se_Ascll_clicked();//點(diǎn)擊ASCII發(fā)送在.cpp文件中 /*十六進(jìn)制接收*/ void Widget::on_Re_Hex_clicked() {ui->Re_Ascll->setCheckState(Qt::Unchecked);ui->Re_Hex->setCheckState(Qt::Checked); }/*ASCII發(fā)送*/ void Widget::on_Se_Ascll_clicked() {ui->Se_Ascll->setCheckState(Qt::Checked);ui->Se_Hex->setCheckState(Qt::Unchecked); }復(fù)位計(jì)數(shù)在.h頭文件中 void on_Btn_Reset_clicked();//復(fù)位計(jì)數(shù)在.cpp文件中 /*清除計(jì)數(shù)*/ void Widget::on_Btn_Reset_clicked() {SendByte = 0;ReceByte = 0;ui->Re_Byte->setText(QString::number(ReceByte));ui->Se_Byte->setText(QString::number(SendByte)); } ```

總結(jié)

以上是生活随笔為你收集整理的串口调试工具开发的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。