QT5 学习记录 QSerialPort 类实现串口调试工具

来源:互联网 发布:我赢职场java视频 编辑:程序博客网 时间:2024/06/05 00:50
1.
#-------------------------------------------------## Project created by QtCreator 2017-11-08T10:55:55##-------------------------------------------------QT       += core guiQT       += serialportgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = SerialPortToolTEMPLATE = appSOURCES += main.cpp\        serialporttool.cppHEADERS  += serialporttool.hFORMS    += serialporttool.ui


2.serialporttool.h

#ifndef SERIALPORTTOOL_H#define SERIALPORTTOOL_H#include <QWidget>#include <QMainWindow>#include <QTextEdit>#include <QLabel>#include <QComboBox>#include <QPushButton>#include <QtSerialPort/QSerialPort>#include <QtSerialPort/QSerialPortInfo>#include <QHBoxLayout>#include <QVBoxLayout>#include <QGridLayout>namespace Ui {class SerialPortTool;}class SerialPortTool : public QWidget{    Q_OBJECTpublic:    explicit SerialPortTool(QWidget *parent = 0);    ~SerialPortTool();    //layout    QVBoxLayout * LeftLayout;    QGridLayout * RightLayout;    QHBoxLayout * MainLayout;    QTextEdit * RecvText;    QTextEdit * SendText;    //port    QSerialPort * serial ;    //port name    QLabel * PortNameLabel;    QComboBox * PortNameComboBox;    //baoud rate    QLabel * BaudRateLabel;    QComboBox * BaudRateComboBox;    //date bit    QLabel * DataBitLabel;    QComboBox * DataBitComboBox;    //check bit    QLabel * CheckLabel;    QComboBox * CheckComboBox;    //Stop bit    QLabel * StopLabel;    QComboBox * StopComboBox;    //open    QPushButton * openButton;    //send    QPushButton * SendButton;    //clear    QPushButton * ClearButton;private:    Ui::SerialPortTool *ui;private slots:    void openSlotFun();    void sendSlotFun();    void clearSlotFun();    void readUartSlotFun();};#endif // SERIALPORTTOOL_H


3.serialporttool.cpp

#include "serialporttool.h"#include "ui_serialporttool.h"#include <QDebug>#include <QString>#include <QDialog>#include <QMessageBox>SerialPortTool::SerialPortTool(QWidget *parent) : QWidget(parent), ui(new Ui::SerialPortTool){    ui->setupUi(this);    //布局    LeftLayout =new QVBoxLayout;    RightLayout = new QGridLayout;    MainLayout = new QHBoxLayout(this);    //发送框和接受框    RecvText = new QTextEdit;    SendText = new QTextEdit;    LeftLayout->addWidget(RecvText);    LeftLayout->addWidget(SendText);    LeftLayout->setStretchFactor(RecvText,3);    LeftLayout->setStretchFactor(SendText,1);    //添加端口    PortNameLabel = new QLabel(tr("端 口:"));    PortNameComboBox = new QComboBox;    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())    {     QSerialPort mSerialPort;     mSerialPort.setPort(info);     PortNameComboBox->addItem(mSerialPort.portName());    }    RightLayout->addWidget(PortNameLabel,0,0);    RightLayout->addWidget(PortNameComboBox,0,1);    //设置波特率    BaudRateLabel = new QLabel(tr("波特率:"));    BaudRateComboBox = new QComboBox;    BaudRateComboBox->addItem("115200");    BaudRateComboBox->addItem("9600");    RightLayout->addWidget(BaudRateLabel,1,0);    RightLayout->addWidget(BaudRateComboBox,1,1);    //数据位    DataBitLabel = new QLabel(tr("数据位:"));    DataBitComboBox = new QComboBox;    DataBitComboBox->addItem("8");    //DataBitComboBox->addItem("7");    RightLayout->addWidget(DataBitLabel,2,0);    RightLayout->addWidget(DataBitComboBox,2,1);    //校验位    CheckLabel = new QLabel("校验位:");    CheckComboBox = new QComboBox;    CheckComboBox->addItem("无");    RightLayout->addWidget(CheckLabel,3,0);    RightLayout->addWidget(CheckComboBox,3,1);    //停止位    StopLabel = new QLabel("停止位:");    StopComboBox = new QComboBox;    StopComboBox->addItem("1");    StopComboBox->addItem("2");    RightLayout->addWidget(StopLabel,4,0);    RightLayout->addWidget(StopComboBox,4,1);    //open    openButton = new QPushButton;    openButton->setText("open");    RightLayout->addWidget(openButton,5,0,1,2);    //clear    ClearButton = new QPushButton;    ClearButton->setText("clear");    RightLayout->addWidget(ClearButton,6,0,1,2);    //send    SendButton = new QPushButton;    SendButton->setText("send");    RightLayout->addWidget(SendButton,7,0,1,2);    MainLayout->addLayout(LeftLayout);    MainLayout->addLayout(RightLayout);    //SLOT    connect(openButton,SIGNAL(clicked()),this,SLOT(openSlotFun()));    connect(SendButton,SIGNAL(clicked()),this,SLOT(sendSlotFun()));    connect(ClearButton,SIGNAL(clicked()),this,SLOT(clearSlotFun()));}void SerialPortTool::openSlotFun(){    if(openButton->text() == tr("open"))    {        serial = new QSerialPort;        //port name        serial->setPortName(PortNameComboBox->currentText());        //open        serial->open(QIODevice::ReadWrite);        //baudrate        serial->setBaudRate(SerialPortTool::BaudRateComboBox->currentText().toInt());        //data bit        if(SerialPortTool::DataBitComboBox->currentText() == tr("8"))        {            serial->setDataBits(QSerialPort::Data8);        }        //check        if(SerialPortTool::CheckComboBox->currentText() == tr("无"))        {            serial->setParity(QSerialPort::NoParity);        }        //stop        switch(SerialPortTool::StopComboBox->currentText().toInt())        {        case 1:            serial->setStopBits(QSerialPort::OneStop);            break;        case 2:            serial->setStopBits(QSerialPort::TwoStop);            break;        default:            break;        }        //flowcontrol        serial->setFlowControl(QSerialPort::NoFlowControl);        //        connect(serial,SIGNAL(readyRead()),this,SLOT(readUartSlotFun()));        //        openButton->setText("close");    }    else    {        serial->clear();        serial->close();        serial->deleteLater();        openButton->setText("open");    }}void SerialPortTool::readUartSlotFun(){    QByteArray readData;    readData = serial->readAll();    qDebug()<<readData;    if(!readData.isEmpty())    {        //方法1,会换行        //RecvText->append(readData);        //方法2,不换行        RecvText->insertPlainText(readData);        RecvText->moveCursor(QTextCursor::End);    }    readData.clear();}void SerialPortTool::clearSlotFun(){    RecvText->clear();}void SerialPortTool::sendSlotFun(){    //不加判断的话,如果没打开串口直接点发送按钮程序会死    if(openButton->text() == tr("close"))    {        serial->write(SendText->toPlainText().toLatin1());    }    else    {        //没开串口,给个提示        QMessageBox::information(this,tr("info"),tr("Please open one serial port!"));    }}SerialPortTool::~SerialPortTool(){    delete ui;}


4.main.cpp

#include "serialporttool.h"#include <QApplication>int main(int argc, char *argv[]){    QApplication a(argc, argv);    SerialPortTool w;    w.setGeometry(300,200,600,400);    w.show();    return a.exec();}

5.运行图



原创粉丝点击