Initial use of QSerialPort, a QT5 serial port class

Posted by msgcrap on Fri, 03 Jul 2020 16:57:37 +0200

I mainly use the serial port class here. The function is to open the serial port read and write, click the Send Data button to send the data of the sending area to the buffer, and then display it in the receiving area. The interface is as follows: (Source code can be in Here Download)

                                                             


This uses two classes provided by the QSerialPort module: the QSerialPort class and the QSerialPortInfo class, the QSerialPort class provides operations on the serial port, and the QSerialPortInfo class provides access to the serial port information.The following is the main code, which contains a simple use of the serial class.


First, be sure to add in the.pro file: QT += serialport


The serial port is initialized as follows:

  1. /****************************Serial port initialization*****************************************/  
  2. void MainWindow::initPort()  
  3. {  
  4.     //Read Serial Port Information  
  5.     foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())  
  6.     {  
  7.         qDebug()<<"Name:"<<info.portName();  
  8.         qDebug()<<"Description:"<<info.description();  
  9.         qDebug()<<"Manufacturer:"<<info.manufacturer();  
  10.   
  11.         //This is equivalent to adding cmb after recognizing serial number automatically, if you want to select manually you can add it in the following list  
  12.         QSerialPort serial;  
  13.         serial.setPort(info);  
  14.         if(serial.open(QIODevice::ReadWrite))  
  15.         {  
  16.             //Add serial number to cmb  
  17.             ui->cmbPortName->addItem(info.portName());  
  18.             //Close the serial port and wait for it to be opened manually (open the serial button)  
  19.             serial.close();  
  20.         }  
  21.     }  
  22.   
  23.     QStringList baudList;//baud rate  
  24.     QStringList parityList;//Check bits  
  25.     QStringList dataBitsList;//Data bits  
  26.     QStringList stopBitsList;//Stop Bit  
  27.   
  28.     baudList<<"50"<<"75"<<"100"<<"134"<<"150"<<"200"<<"300"  
  29.            <<"600"<<"1200"<<"1800"<<"2400"<<"4800"<<"9600"  
  30.           <<"14400"<<"19200"<<"38400"<<"56000"<<"57600"  
  31.          <<"76800"<<"115200"<<"128000"<<"256000";  
  32.   
  33.     ui->cmbBaudRate->addItems(baudList);  
  34.     ui->cmbBaudRate->setCurrentIndex(12);  
  35.   
  36.     parityList<<"nothing"<<"odd"<<"even";  
  37.     parityList<<"sign";  
  38.     parityList<<"Spaces";  
  39.   
  40.     ui->cmbParity->addItems(parityList);  
  41.     ui->cmbParity->setCurrentIndex(0);  
  42.   
  43.     dataBitsList<<"5"<<"6"<<"7"<<"8";  
  44.     ui->cmbDataBits->addItems(dataBitsList);  
  45.     ui->cmbDataBits->setCurrentIndex(3);  
  46.   
  47.     stopBitsList<<"1";  
  48.     stopBitsList<<"1.5";  
  49.     stopBitsList<<"2";  
  50.   
  51.     ui->cmbStopBits->addItems(stopBitsList);  
  52.     ui->cmbStopBits->setCurrentIndex(0);  
  53.   
  54.     //Settings button can be pressed  
  55.     ui->btnOpen->setCheckable(true);  
  56. }  

* Here we add the serial port we need to use to the combox by traversing all the serial ports. If you want to select manually, you can add the serial port name to the combox by listing it, and then select it when you use it.


The serial port settings are as follows:

  1. /****************************Serial Port Settings******************************/  
  2. void MainWindow::on_btnOpen_clicked()  
  3. {  
  4.     if(ui->btnOpen->text() == "Open Serial Port")  
  5.     {  
  6.         my_serialport = new QSerialPort(this);  
  7.   
  8.         //Set serial number  
  9.         my_serialport->setPortName(ui->cmbPortName->currentText());  
  10.         //Open Serial Port Read-Write  
  11.         if(my_serialport->open(QIODevice::ReadWrite))  
  12.         {  
  13.             //set baud rate  
  14.             my_serialport->setBaudRate(ui->cmbBaudRate->currentText().toInt());  
  15.             //set data bit  
  16.             my_serialport->setDataBits(QSerialPort::Data8);  
  17.             //Set Check Bit  
  18.             my_serialport->setParity(QSerialPort::NoParity);  
  19.             //set flow control  
  20.             my_serialport->setFlowControl(QSerialPort::NoFlowControl);  
  21.             //Set stop bits  
  22.             my_serialport->setStopBits(QSerialPort::OneStop);  
  23.   
  24.             //Read once per second  
  25.             timer = new QTimer(this);  
  26.             connect(timer, SIGNAL(timeout()), this, SLOT(readComDataSlot()));  
  27.             timer->start(1000);  
  28.   
  29.             setNonSelectable();  
  30.         }  
  31.         else  
  32.         {  
  33.             QMessageBox::about(NULL, "Tips""Serial port not open!");  
  34.             return;  
  35.         }  
  36.     }  
  37.     else  
  38.     {  
  39.         timer->stop();  
  40.         setSelectable();  
  41.         my_serialport->close();  
  42.     }  
  43. }  

This is the setting of the serial port. You need to open the serial port before you can set the parameters of the serial port.Once the parameters are set, you can read and write the data through the read() and write() functions. I use a 1-second timer here to read the data from the buffer.


The data is sent and received as follows:

  1. /****************************Data Transfer************************************/  
  2. void MainWindow::readComDataSlot()  
  3. {  
  4.     //Reading Serial Port Data  
  5.     QByteArray readComData = my_serialport->readAll();  
  6.   
  7.     //Display read data in te of data receiving area  
  8.     if(readComData != NULL)  
  9.     {  
  10.         ui->teReceiveData->append(readComData);  
  11.     }  
  12.   
  13.     //Clear Buffer  
  14.     readComData.clear();  
  15. }  
  16.   
  17. void MainWindow::on_btnSend_clicked()  
  18. {  
  19.     //Get data from the sending area  
  20.     QString sendData = ui->teSendData->toPlainText();  
  21.     QByteArray sendData_2 = sendData.toLatin1();  
  22.   
  23.     //Write Buffer  
  24.     my_serialport->write(sendData_2);  
  25. }  

Topics: Qt