Initial use of QSerialPort, a QT5 serial port class

I mainly use the serial port class here. The function is to open the serial port read and write, click the Send Data but...
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. //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
  11. QSerialPort serial;
  12. serial.setPort(info);
  13. if(serial.open(QIODevice::ReadWrite))
  14. {
  15. //Add serial number to cmb
  16. ui->cmbPortName->addItem(info.portName());
  17. //Close the serial port and wait for it to be opened manually (open the serial button)
  18. serial.close();
  19. }
  20. }
  21. QStringList baudList;//baud rate
  22. QStringList parityList;//Check bits
  23. QStringList dataBitsList;//Data bits
  24. QStringList stopBitsList;//Stop Bit
  25. baudList<<"50"<<"75"<<"100"<<"134"<<"150"<<"200"<<"300"
  26. <<"600"<<"1200"<<"1800"<<"2400"<<"4800"<<"9600"
  27. <<"14400"<<"19200"<<"38400"<<"56000"<<"57600"
  28. <<"76800"<<"115200"<<"128000"<<"256000";
  29. ui->cmbBaudRate->addItems(baudList);
  30. ui->cmbBaudRate->setCurrentIndex(12);
  31. parityList<<"nothing"<<"odd"<<"even";
  32. parityList<<"sign";
  33. parityList<<"Spaces";
  34. ui->cmbParity->addItems(parityList);
  35. ui->cmbParity->setCurrentIndex(0);
  36. dataBitsList<<"5"<<"6"<<"7"<<"8";
  37. ui->cmbDataBits->addItems(dataBitsList);
  38. ui->cmbDataBits->setCurrentIndex(3);
  39. stopBitsList<<"1";
  40. stopBitsList<<"1.5";
  41. stopBitsList<<"2";
  42. ui->cmbStopBits->addItems(stopBitsList);
  43. ui->cmbStopBits->setCurrentIndex(0);
  44. //Settings button can be pressed
  45. ui->btnOpen->setCheckable(true);
  46. }

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

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. //Display read data in te of data receiving area
  7. if(readComData != NULL)
  8. {
  9. ui->teReceiveData->append(readComData);
  10. }
  11. //Clear Buffer
  12. readComData.clear();
  13. }
  14. void MainWindow::on_btnSend_clicked()
  15. {
  16. //Get data from the sending area
  17. QString sendData = ui->teSendData->toPlainText();
  18. QByteArray sendData_2 = sendData.toLatin1();
  19. //Write Buffer
  20. my_serialport->write(sendData_2);
  21. }

3 July 2020, 11:00 | Views: 4518

Add new comment

For adding a comment, please log in
or create account

0 comments