基于Qt/C++的串口助手小程序

基于Qt/C++的串口助手小程序编写

因为教研室小项目需要用到嵌入式,以PC作为上位机串口通讯,在此做一个学习记录。

程序编写来自b站一个教程:https://www.bilibili.com/video/BV1EE4117728?p=5&t=1

 

思路:

实现功能:1.配置->串口号、波特率、停止位、奇偶校验、Hex显示等;    2.接收功能;   3.发送功能。

界面布局

 

代码主体:myserial.pro  serial.h  main.cpp  serial.cpp  serial.ui

/*----------------myserial.pro----------------------------------------*/

QT       += core gui
QT += serialport   #user  serial
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

DEFINES += QT_DEPRECATED_WARNINGS

SOURCES += \
    main.cpp \
    serial.cpp

HEADERS += \
    serial.h

FORMS += \
    serial.ui

RC_ICONS = system.ico   #user ICON

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

/*-------------------serial.h-------------------------------------*/

#ifndef SERIAL_H
#define SERIAL_H

#include <QMainWindow>

/*---------------------
 *  user
 * -------------------*/
#include <QSerialPort>

//debug
#include<QDebug>

QT_BEGIN_NAMESPACE
namespace Ui { class serial; }
QT_END_NAMESPACE

class serial : public QMainWindow
{
    Q_OBJECT

public:
    serial(QWidget *parent = nullptr);
    ~serial();


private slots:
    //button
    void btn_open_port(bool);
    void btn_close_port(bool);
    void btn_send_data(bool);

    //recieve data
    void receive_data();

private:
    Ui::serial *ui;
    /*------------funtion-----------------*/
    void system_init();
    /*------------variable-----------------*/
    QSerialPort global_port;

};
#endif // SERIAL_H
 

/*----------------serial.cpp----------------------------------------*/

#include "serial.h"
#include "ui_serial.h"

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

    //user
    system_init();
}

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


/*----------------------------------------------
 *     funtions
 * -------------------------------------------*/
void serial::system_init()
{
    //port config 默认配置(内置)
    global_port.setParity(QSerialPort::NoParity);
    global_port.setDataBits(QSerialPort::Data8);
    global_port.setStopBits(QSerialPort::OneStop);

    //connect
    connect(ui->btn_open,&QPushButton::clicked,this,&serial::btn_open_port);
    connect(ui->btn_close,&QPushButton::clicked,this,&serial::btn_close_port);
    connect(ui->btn_send,&QPushButton::clicked,this,&serial::btn_send_data);
    connect(&global_port,&QSerialPort::readyRead,this,&serial::receive_data);
}

//slots
/*---------------------open---------------------------*/
void serial::btn_open_port(bool)
{
    //qDebug()<< "ok";

    /*---------baud name-----------------------*/
    //qDebug()<<ui->cmb_port_name->currentIndex();//printf 0-7

    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------------------------------*/
    //qDebug()<<ui->cmb_baud_rate->currentIndex();
    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("串口已打开");         //串口状态显示
    //test
    //global_port.write("1");//测试输出情况
}

/*---------------------close---------------------------*/
void serial::btn_close_port(bool)
{
    global_port.close();                     //关闭串口
    ui->lab_status->setText("串口已关闭");        //串口状态显示
}

/*---------------------sending data---------------------------*/
void serial::btn_send_data(bool)
{
    QString data = ui->lineEdit->text();     //获取文本的方法;存入data中
    QByteArray array= data.toLatin1();       //QString---->QByteArray,即格式转换
    global_port.write(array);
}

/*--------------------recieve data-------------------------*/
void serial::receive_data()
{
    QByteArray array = global_port.readAll();     //Reads all remaining data from the device, and returns it as a byte array.
    qDebug()<<array;                              //控制台显示接收数据
    if(ui->checkBox->checkState() == Qt::Checked){
        ui->lint_send_data->insertPlainText(QString(array.toHex(' ').toUpper().append(' ')));  //Hex转换,同时设置大小写和空格间隔,读取接收数据
    }else{
        ui->lint_send_data->insertPlainText(QString(array));
    }
}

/*----------------ui->设计与实现----------------------*/

 

/*--------------------构建运行----------------------------------------------------------------*/

 

 

 

 

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页