当前位置:网站首页>Qt development -- compilation of serial port assistant

Qt development -- compilation of serial port assistant

2022-06-09 08:59:00 Lee1989 Qi

One 、 Create a project

Create a normal widget Project engineering , Control for code research . Create a compilation test project . You can compile it for the next step .

Two 、 A serial port UI Interface design

1、 choice ui Control

  • The serial port receives the data display interface control of the lower computer
     Insert picture description here
  • Select container 、 Baud rate 、 Serial number 、 Controls for multiple selection containers such as data bits
     Insert picture description here
  • Upper computer serial port to send data control
     Insert picture description here
  • Make some advertisements , Add your own elements , For example, the author's telephone number and other personal information
     Insert picture description here
  • Finally, after the layout, the effect is as follows :

 Insert picture description here


2、 Set up UI Default display

  • Containers 、 Baud rate 、 Serial number 、 Controls for multiple selection containers such as data bits
     Insert picture description here
  • Modify the property name of the control , Easy to write code for debugging
     Insert picture description here

3、 Add serial port identification code

#include <qserialportinfo.h>


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

    foreach(const QSerialPortInfo &info ,QSerialPortInfo::availablePorts()){
    
        serialNamePort<<info.portName();
    }
    ui->Serial->addItems(serialNamePort);
}

The effect is as follows :


3、 ... and 、 Realize the logic function of the button :


When opening the serial port button, you need to initialize the serial port , That is, a series of parameters for initializing the serial port , The following is the initialization code .

    QSerialPort::BaudRate baudrate;
    QSerialPort::DataBits databits;
    QSerialPort::StopBits stopbits;
    QSerialPort::Parity parity;

    if(ui->bote->currentText() == "1200"){
    
        baudrate = QSerialPort::Baud1200;
        }else if(ui->bote->currentText() == "4800"){
    
           baudrate = QSerialPort::Baud4800;
        }else if(ui->bote->currentText() == "9600"){
    
           baudrate = QSerialPort::Baud9600;
        }else if(ui->bote->currentText() == "38400"){
    
           baudrate = QSerialPort::Baud38400;
        }else if(ui->bote->currentText() == "115200"){
    
           baudrate = QSerialPort::Baud115200;
        }


    if(ui->shujubit->currentText() == "5"){
    
        databits = QSerialPort::Data5;
        }else if(ui->shujubit->currentText() == "6"){
    
           databits = QSerialPort::Data6;
        }else if(ui->shujubit->currentText() == "7"){
    
           databits = QSerialPort::Data7;
        }else if(ui->shujubit->currentText() == "8"){
    
           databits = QSerialPort::Data8;
        }

    if(ui->closebit->currentText() == "1"){
    
        stopbits = QSerialPort::OneStop;
        }else if(ui->closebit->currentText() == "1.5"){
    
           stopbits = QSerialPort::OneAndHalfStop;
        }else if(ui->closebit->currentText() == "2"){
    
           stopbits = QSerialPort::TwoStop;
        }

    if(ui->jiaoyanwei->currentText() == "None"){
    
        parity = QSerialPort::NoParity;
        }else if(ui->jiaoyanwei->currentText() == "Odd"){
    
           parity = QSerialPort::OddParity;
        }else if(ui->jiaoyanwei->currentText() == "Even"){
    
           parity = QSerialPort::EvenParity;
        }

Then set corresponding parameters

        serilaPort->setPortName(ui->Serial->currentText());
        serilaPort->setBaudRate(baudrate);
        serilaPort->setDataBits(databits);
        serilaPort->setStopBits(stopbits);
        serilaPort->setParity(parity);

Add button event , Pop up the prompt box .

        if(serilaPort->open(QIODevice::ReadWrite) == true){
    
            QMessageBox::information(this," Tips "," success ");
        }else {
    

            QMessageBox::critical(this," Tips "," Failure ");
        }

Write slot function for serial port display

Learning output :

Tips : Here is the total number of learning plans

for example :

  • Technical notes 2 All over
  • CSDN Technology blogs 3 piece
  • Habitual vlog video 1 individual
原网站

版权声明
本文为[Lee1989 Qi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090845276441.html