Qt小白一枚,喜歡嵌入式應(yīng)用,以前都是使用別人的串口調(diào)試助手,學(xué)習(xí)了C++以后,自己也嘗試著做了簡單的串口調(diào)試助手,分享給大家吧,希望能幫助到大家,如果有錯誤,請大家指正。話不多說開干!
1.首先看一下我設(shè)計的界面(我這里比較簡單,大家可根據(jù)自己的需求進(jìn)行設(shè)計)
(界面設(shè)計的過程中,每一個控件的名稱最好進(jìn)行修改,便于后續(xù)控件太多不好區(qū)分,給控件命名的時候一定要就針對性,一下明白這個控件是干什么的。)
?2.首先看一下準(zhǔn)備工作。在pro文件中添加串口需要的文件
文章來源:http://www.zghlxwxcb.cn/news/detail-457143.html
?3.widget.h文件 (看看代碼一睹為快)文章來源地址http://www.zghlxwxcb.cn/news/detail-457143.html
#ifndef WIDGET_H
#define WIDGET_H
#include <QSerialPort>
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
QSerialPort *serialport;
explicit Widget(QWidget *parent = nullptr);
~Widget();
private slots:
//打開串口槽函數(shù)
void on_port_openBt_clicked();
//關(guān)閉串口槽函數(shù)
void on_port_closeBt_clicked();
//自定義的槽函數(shù)的聲明
void serialreadtext();
//數(shù)據(jù)發(fā)送槽函數(shù)
void on_data_sendBt_clicked();
//數(shù)據(jù)接受區(qū)和數(shù)據(jù)發(fā)送區(qū)清空槽函數(shù)
void on_rev_clearBt_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
4.widget.cpp文件
#include "widget.h"
#include "ui_widget.h"
#include<QSerialPortInfo>
#include <QMessageBox>
#include <QString>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{ serialport = new QSerialPort(this); //創(chuàng)建一個串口對象
ui->setupUi(this);
//這樣我們就獲取到 可用的串口名字了
QStringList m_serialPortName;
foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
{
m_serialPortName << info.portName();
}
//添加串口到conbox控件中
ui->port_comboBox->addItems(m_serialPortName);
//接收數(shù)據(jù)需要自己關(guān)聯(lián)槽函數(shù)
connect(serialport,SIGNAL(readyRead()),this , SLOT(serialreadtext()));
}
Widget::~Widget()
{
delete ui;
}
//讀數(shù)據(jù)
void Widget::serialreadtext()
{
QString buffer;
buffer=QString(serialport->readAll());
ui->revEdit->appendPlainText(buffer);
}
void Widget::on_port_openBt_clicked()
{
QSerialPort::BaudRate baudRate;
QSerialPort::DataBits dataBits;
QSerialPort::Parity parity;
QSerialPort::StopBits stopbits;
//波特率
if(ui->rate_comboBox->currentText()=="115200")
{
baudRate=QSerialPort::Baud115200;
}else if(ui->rate_comboBox->currentText()=="9600")
{
baudRate=QSerialPort::Baud9600;
}else {
baudRate=QSerialPort::Baud4800;
}
//數(shù)據(jù)位
if(ui->data_comboBox->currentText()=="8")
{
dataBits=QSerialPort::Data8;
}else if(ui->data_comboBox->currentText()=="7")
{
dataBits=QSerialPort::Data7;
}else if(ui->data_comboBox->currentText()=="6")
{
dataBits=QSerialPort::Data6;
}else if (ui->data_comboBox->currentText()=="5")
{
dataBits=QSerialPort::Data5;
}
//停止位
if(ui->stop_comboBox->currentText()=="1")
{
stopbits= QSerialPort::OneStop;
} else if(ui->stop_comboBox->currentText()=="2")
{
stopbits= QSerialPort::TwoStop;
}else if(ui->stop_comboBox->currentText()=="1.5")
{
stopbits= QSerialPort::OneAndHalfStop;
}
//校驗位
if(ui->cheak_comboBox->currentText()=="None")
{
parity=QSerialPort::NoParity;
}
//1.配置端口號
serialport->setPortName(ui->port_comboBox->currentText());
//2.配置波特率
serialport->setBaudRate(baudRate);
//設(shè)置數(shù)據(jù)位
serialport->setDataBits(dataBits);
//設(shè)置校驗位
serialport->setParity(parity);
//停止位
serialport->setStopBits(stopbits);
if(serialport->isOpen())//如果串口已經(jīng)打開了 先給他關(guān)閉了
{
serialport->clear();
serialport->close();
}
if(serialport->open(QIODevice::ReadWrite)==true)
{
QMessageBox::information(this ,"提示","串口打開成功");
}else
{
QMessageBox::critical(this ,"提示","串口打開失敗");
}
}
//關(guān)閉串口
void Widget::on_port_closeBt_clicked()
{
QMessageBox::information(this ,"提示","串口關(guān)閉成功");
serialport->close();
}
//發(fā)送數(shù)據(jù)
void Widget::on_data_sendBt_clicked()
{
serialport->write(ui->sendEdit->text().toLocal8Bit().data());
}
//清空接受區(qū)
void Widget::on_rev_clearBt_clicked()
{
ui->revEdit->clear();
}
5.好啦,完成上面的步驟,一個簡單的串口調(diào)試助手就完成了,大家快行動起來吧。
?
到了這里,關(guān)于使用QtCreator C++編寫串口調(diào)試助手的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!