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

1.
#-------------------------------------------------
#
# Project created by QtCreator 2017-11-08T10:55:55
#
#-------------------------------------------------

QT       += core gui
QT       += serialport

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = SerialPortTool
TEMPLATE = app


SOURCES += main.cpp\
        serialporttool.cpp

HEADERS  += serialporttool.h

FORMS    += 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_OBJECT

public:
    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.运行图



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值