QT5.11串口调试助手

pro

#-------------------------------------------------
#
# Project created by QtCreator 2018-10-31T21:14:36
#
#-------------------------------------------------

QT       += core gui
QT       += serialport

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = uart
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES += \
        main.cpp \
        widget.cpp

HEADERS += \
        widget.h

FORMS += \
        widget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();
private slots:

    void on_OpenButton_clicked();

    void on_OutputText_Dispaly();

    void on_Send_clicked();

private:
    Ui::Widget *ui;
    QSerialPort *serial;
};

#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    //查找可用的串口
    foreach (const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
    {
        QSerialPort serial;
        serial.setPort(info);
        if(serial.open(QIODevice::ReadWrite))
        {
            ui->PortBox->addItem(serial.portName());
            serial.close();
        }
    }
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_OpenButton_clicked()
{
    if(ui->OpenButton->text() == tr("Open"))
    {
        serial = new QSerialPort;
        serial->setPortName(ui->PortBox->currentText());
        serial->open(QIODevice::ReadWrite);

        if(ui->BaudBox->currentText() == tr("9600"))
        {
            serial->setBaudRate(QSerialPort::Baud9600);
        }
        else if(ui->BaudBox->currentText() == tr("19200"))
        {
            serial->setBaudRate(QSerialPort::Baud19200);
        }
        else if(ui->BaudBox->currentText() == tr("38400"))
        {
            serial->setBaudRate(QSerialPort::Baud38400);
        }
        else if(ui->BaudBox->currentText() == tr("115200"))
        {
            serial->setBaudRate(QSerialPort::Baud115200);
        }

        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);

        ui->PortBox->setEnabled(false);
        ui->BaudBox->setEnabled(false);
        ui->OpenButton->setText("Close");

        //连接信号槽 当串口缓冲区有数据时,进行读串口操作
        QObject::connect(serial,SIGNAL(readyRead()),this,SLOT(on_OutputText_Dispaly()));
    }
    else
    {
        serial->clear();
        serial->close();
        serial->deleteLater();

        ui->PortBox->setEnabled(true);
        ui->BaudBox->setEnabled(true);
        ui->OpenButton->setText("Open");
    }
}

void Widget::on_Send_clicked()
{
    serial->write(ui->SendEdit->toPlainText().toLatin1());
}

void Widget::on_OutputText_Dispaly()
{
    QByteArray buf;
    buf = serial->readAll();
    if(!buf.isEmpty())
    {
        QString str = ui->OutputText->toPlainText();
        str += tr(buf);
        ui->OutputText->clear();
        ui->OutputText->append(str);
    }
    buf.clear();
}

main.cpp

#include "widget.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

widget.ui



 Widget
 
  
   
    0
    0
    478
    282
   
  
  
   Widget
  
  
   
    
     
      串口设置
     
    
   
   
    
     
      
     
     
      
     
    
   
   
    
     
      
       
        串口
       
      
     
     
      
     
     
      
       
        波特率
       
      
     
     
      
       
        
         9600
        
       
       
        
         19200
        
       
       
        
         38400
        
       
       
        
         115200
        
       
      
     
     
      
       
        数据位
       
      
     
     
      
       
        
         8
        
       
       
        
         7
        
       
       
        
         6
        
       
       
        
         5
        
       
      
     
     
      
       
        校验位
       
      
     
     
      
       
        
         None
        
       
      
     
     
      
       
        停止位
       
      
     
     
      
       
        
         1
        
       
       
        
         2
        
       
      
     
     
      
       
        流控
       
      
     
     
      
       
        
         None
        
       
      
     
     
      
       
        Qt::Vertical
       
       
        
         20
         40
        
       
      
     
     
      
       
        Qt::Vertical
       
       
        
         20
         40
        
       
      
     
     
      
       
        Qt::Vertical
       
       
        
         20
         40
        
       
      
     
     
      
       
        Qt::Vertical
       
       
        
         20
         40
        
       
      
     
     
      
       
        Open
       
      
     
     
      
       
        Send
       
      
     
    
   
  
 
 
 
 

 

你可能感兴趣的:(QT5.11串口调试助手)