QT makes a simple serial port assistant

1. Create a project

①Click New Project and select Qt Widgets Application

 

②Set the project name and save path

 

③Click Next, then select the Qwiget base class, and then click Finish

 

 

 

2. Programming

① Interface editing and layout

After editing the interface, remember to rename the control to facilitate subsequent code writing.

 

Double-click the corresponding box of ComboBOX to write the required value

 Click the data acceptance box and check read-only in the property bar

 

 ② Add serial port related modules in Serial.pro

③widget.h file 

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QSerialPort>
#include <QString>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

    QSerialPort *serialPort;//定义串口指针

private slots:

    /*手动连接槽函数*/
    void manual_serialPortReadyRead();

    /*以下为widget.ui文件中点击“转到槽”自动生成的函数*/
    void on_openBt_clicked();

    void on_closeBt_clicked();

    void on_sendBt_clicked();

    void on_clearBt_clicked();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

④widget.cpp file 

#include "widget.h"
#include "ui_widget.h"
#include "QSerialPortInfo"
#include <QSerialPort>
#include <QMessageBox>

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

    QStringList serialNamePort;

    serialPort = new QSerialPort(this);
    connect(serialPort,SIGNAL(readyRead()),this,SLOT(manual_serialPortReadyRead()));/*手动连接槽函数*/

    /*找出当前连接的串口并显示到serailCb*/
    foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
    {
        serialNamePort<<info.portName();
    }
    ui->serailCb->addItems(serialNamePort);

}

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

/*手动实现接收数据函数*/
void Widget::manual_serialPortReadyRead()
{
    QString buf;
    buf=QString(serialPort->readAll());
    ui->recvEdit->appendPlainText(buf);
}

/*打开串口*/
void Widget::on_openBt_clicked()
{
    /*串口初始化*/
    QSerialPort::BaudRate baudRate;
    QSerialPort::DataBits dataBits;
    QSerialPort::StopBits stopBits;
    QSerialPort::Parity checkBits;

    if(ui->baundrateCb->currentText()=="4800")
        baudRate=QSerialPort::Baud4800;
    else if(ui->baundrateCb->currentText()=="9600")
        baudRate=QSerialPort::Baud9600;
    else if(ui->baundrateCb->currentText()=="115200")
        baudRate=QSerialPort::Baud115200;

    if(ui->databitCb->currentText()=="5")
        dataBits=QSerialPort::Data5;
    else if(ui->databitCb->currentText()=="6")
        dataBits=QSerialPort::Data6;
    else if(ui->databitCb->currentText()=="7")
        dataBits=QSerialPort::Data7;
    else if(ui->databitCb->currentText()=="8")
        dataBits=QSerialPort::Data8;

    if(ui->stopbitCb->currentText()=="1")
        stopBits=QSerialPort::OneStop;
    else if(ui->stopbitCb->currentText()=="1.5")
        stopBits=QSerialPort::OneAndHalfStop;
    else if(ui->stopbitCb->currentText()=="2")
        stopBits=QSerialPort::TwoStop;

    if(ui->checkbitCb->currentText()=="none")
        checkBits=QSerialPort::NoParity;

    serialPort->setPortName(ui->serailCb->currentText());
    serialPort->setBaudRate(baudRate);
    serialPort->setDataBits(dataBits);
    serialPort->setStopBits(stopBits);
    serialPort->setParity(checkBits);

    if(serialPort->open(QIODevice::ReadWrite)==true)
        QMessageBox::information(this,"提示","成功");
    else
        QMessageBox::critical(this,"提示","失败");
}

/*关闭串口*/
void Widget::on_closeBt_clicked()
{
    serialPort->close();
}

/*发送数据*/
void Widget::on_sendBt_clicked()
{
    serialPort->write(ui->sendEdit->text().toLocal8Bit().data());
}

/*清空*/
void Widget::on_clearBt_clicked()
{
    ui->recvEdit->clear();
}

3. Run

Click CTRL+R to compile and run

Connect the development board to the computer, open the corresponding serial port, send commands and get feedback.

 4. Package the QT program into Windows software

①Change icon

Add the icon file in .ico format to the project folder

 Add this icon in Serial.pro

 Change the project to Release mode and recompile

 Packing operation

Create a Serial folder (no Chinese path), and copy the exe file of the Release version just produced (in the D:\likui1\code\Qt\build-Serail-Desktop_Qt_5_11_1_MinGW_32bit-Release\release directory) to this folder .

 Open the QT console, and then operate as shown below

 You can see that there are many more files in the folder, among which Serial.exe can be directly clicked to use

 

Guess you like

Origin blog.csdn.net/weixin_46183891/article/details/124368488