Qt小作业串口调试助手

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
/*----------------user------------------------*/
#include <QtSerialPort>
#include <QDebug>
/*----------------------------------------*/


QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private slots:
     /*------------------button---------------*/
    void btn_open_port(bool);
    void btn_send_data(bool);
    void btn_close_port();
    /*------------------receive data---------------*/
    void receive_data();

private:
    Ui::MainWindow *ui;
    /*------------------function---------------*/
    void system_init();//系统初始化
    /*------------------varirble--------------------*/
    QSerialPort global_port;//直接定义就不需要用new
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    /*----------------------------------------*/
    setWindowIcon(QIcon("system.ico"));
    system_init();
}

MainWindow::~MainWindow()
{
    delete ui;
}
/*--------------------function---------------------------*/
void  MainWindow::system_init()
{
    //port config
    global_port.setParity(QSerialPort::NoParity);
    global_port.setDataBits(QSerialPort::Data8);
    global_port.setStopBits(QSerialPort::OneStop);
/*--------------------function---------------------------*/
    connect(ui->btn_open,&QPushButton::clicked,this,&MainWindow::btn_open_port);
    connect(ui->btn_send,&QPushButton::clicked,this,&MainWindow::btn_send_data);
    connect(&global_port,&QSerialPort::readyRead,this,&MainWindow::receive_data);
    connect(ui->btn_close,&QPushButton::clicked,this,&MainWindow::btn_close_port);
}

/*-------------------slots---------------------*/
void MainWindow::btn_open_port(bool)
{
    /*-------------------port---------------------*/
    switch (ui->cmb_port_name->currentIndex()) {
    case 0:
        global_port.setPortName("COM1");
        break;
    case 1:
        global_port.setPortName("COM2");
        break;
    case 2:
        global_port.setPortName("COM3");
        break;
    case 3:
        global_port.setPortName("COM4");
        break;
    case 4:
        global_port.setPortName("COM5");
        break;
    case 5:
        global_port.setPortName("COM6");
        break;
    case 6:
        global_port.setPortName("COM7");
        break;
    default:
        global_port.setPortName("COM8");
        break;
    }
    /*-------------------baud rate---------------------*/
    switch (ui->cmb_baud_rate->currentIndex()) {
    case 0:
        global_port.setBaudRate(QSerialPort::Baud115200);
        break;
    case 1:
        global_port.setBaudRate(QSerialPort::Baud57600);
        break;
    case 2:
        global_port.setBaudRate(QSerialPort::Baud38400);
        break;
    case 3:
        global_port.setBaudRate(QSerialPort::Baud19200);
        break;
    case 4:
        global_port.setBaudRate(QSerialPort::Baud9600);
        break;
    case 5:
        global_port.setBaudRate(QSerialPort::Baud4800);
        break;
    case 6:
        global_port.setBaudRate(QSerialPort::Baud2400);
        break;
    default:
        global_port.setBaudRate(QSerialPort::Baud1200);
        break;
    }
    /*-------------------open---------------------*/
    global_port.open(QIODevice::ReadWrite);
    ui->lab_status->setText("Connected");
}

void MainWindow::btn_close_port()
{
    global_port.close();
    ui->lab_status->setText("Disconnected");
}
/*-------------------send data---------------------*/
void MainWindow::btn_send_data(bool)
{
    QString data = ui->lint_send_data->text();
    QByteArray array = data.toLatin1();//QString--->QByteArray
    global_port.write(array);
}
/*-------------------receive data---------------------*/
void MainWindow::receive_data()
{
   QByteArray array =  global_port.readAll();
   if(ui->checkBox->checkState() == Qt::Checked){
       ui->plainTextEdit->insertPlainText(QString(array.toHex(' ').append(' ')));//单引号为了解决连在一起,arg(" ")是在每个数据最后再加一个空格
   }
   else{
       ui->plainTextEdit->insertPlainText(QString(array));
   }
}

Qt小作业串口调试助手

上一篇:Qt框架下读取USB无驱摄像头视频流并作人脸识别


下一篇:QCustomPlot使用范例(一)