Simple use method and example of qserialport module for QT serial communication development

Time:2021-7-28

I mainly use the serial port class here. The function is to open the serial port in read-write mode, click the send data button to send the data in the sending area to the buffer, and then display it in the receiving area. The interface is as follows: (the source code can be downloaded here)

Here, two classes provided by the qserialport module are used: qserialport class and qserialportinfo class. Qserialport class provides the operation of serial port and qserialportinfo class provides the acquisition of serial port information. The following is the main code, including the simple use of serial port class.

First, be sure to add the following to the. Pro file:QT += serialport

Serial port initialization is as follows:

/****************************Serial port initialization*****************************/ 
void MainWindow::initPort() 
{ 
 //Read serial port information 
 foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) 
 { 
  qDebug()<<"Name:"<<info.portName(); 
  qDebug()<<"Description:"<<info.description(); 
  qDebug()<<"Manufacturer:"<<info.manufacturer(); 
 
  //This is equivalent to automatically identifying the serial port number and adding it to CMB. If you want to manually select it, you can add it in the following list 
  QSerialPort serial; 
  serial.setPort(info); 
  if(serial.open(QIODevice::ReadWrite)) 
  { 
   //Add serial port number to CMB 
   ui->cmbPortName->addItem(info.portName()); 
   //Close the serial port and wait for it to be opened manually (open the serial port button) 
   serial.close(); 
  } 
 } 
 
 QStringList baudList;// Baud rate 
 QStringList parityList;// Check bit 
 QStringList dataBitsList;// Data bit 
 QStringList stopBitsList;// Stop bit 
 
 baudList<<"50"<<"75"<<"100"<<"134"<<"150"<<"200"<<"300" 
   <<"600"<<"1200"<<"1800"<<"2400"<<"4800"<<"9600" 
   <<"14400"<<"19200"<<"38400"<<"56000"<<"57600" 
   <<"76800"<<"115200"<<"128000"<<"256000"; 
 
 ui->cmbBaudRate->addItems(baudList); 
 ui->cmbBaudRate->setCurrentIndex(12); 
 
 Paritylist < < none < < odd < < even; 
 Paritylist < < "flag"; 
 Paritylist < < "space"; 
 
 ui->cmbParity->addItems(parityList); 
 ui->cmbParity->setCurrentIndex(0); 
 
 dataBitsList<<"5"<<"6"<<"7"<<"8"; 
 ui->cmbDataBits->addItems(dataBitsList); 
 ui->cmbDataBits->setCurrentIndex(3); 
 
 stopBitsList<<"1"; 
 stopBitsList<<"1.5"; 
 stopBitsList<<"2"; 
 
 ui->cmbStopBits->addItems(stopBitsList); 
 ui->cmbStopBits->setCurrentIndex(0); 
 
 //The set button can be pressed 
 ui->btnOpen->setCheckable(true); 
}

Here, the serial ports we need to use are added to the combox by traversing all serial ports. If you want to manually select, you can add the serial port name to the combox in the form of a list and select it when you use it.

The serial port settings are as follows:

/****************************Serial port settings******************************/ 
void MainWindow::on_btnOpen_clicked() 
{ 
 If (UI - > btnopen - > text() = = "open serial port") 
 { 
  my_serialport = new QSerialPort(this); 
 
  //Set serial port number 
  my_serialport->setPortName(ui->cmbPortName->currentText()); 
  //Open the serial port in read-write mode 
  if(my_serialport->open(QIODevice::ReadWrite)) 
  { 
   //Set baud rate 
   my_serialport->setBaudRate(ui->cmbBaudRate->currentText().toInt()); 
   //Set data bit 
   my_serialport->setDataBits(QSerialPort::Data8); 
   //Set check bit 
   my_serialport->setParity(QSerialPort::NoParity); 
   //Set flow control 
   my_serialport->setFlowControl(QSerialPort::NoFlowControl); 
   //Set stop bit 
   my_serialport->setStopBits(QSerialPort::OneStop); 
 
   //Read once per second 
   timer = new QTimer(this); 
   connect(timer, SIGNAL(timeout()), this, SLOT(readComDataSlot())); 
   timer->start(1000); 
 
   setNonSelectable(); 
  } 
  else 
  { 
   Qmessagebox:: about (null, "prompt", "serial port is not open!"); 
   return; 
  } 
 } 
 else 
 { 
  timer->stop(); 
  setSelectable(); 
  my_serialport->close(); 
 } 
}

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

Data sending and receiving are as follows:

/****************************Data transmission******************************/ 
void MainWindow::readComDataSlot() 
{ 
 //Read serial port data 
 QByteArray readComData = my_serialport->readAll(); 
 
 //The read data is displayed in te in the data receiving area 
 if(readComData != NULL) 
 { 
  ui->teReceiveData->append(readComData); 
 } 
 
 //Clear buffer 
 readComData.clear(); 
} 
 
void MainWindow::on_btnSend_clicked() 
{ 
 //Get data of sending area 
 QString sendData = ui->teSendData->toPlainText(); 
 QByteArray sendData_2 = sendData.toLatin1(); 
 
 //Write buffer 
 my_serialport->write(sendData_2); 
}

This paper briefly introduces the simple use methods and examples of QT serial communication module qserialport. For more detailed knowledge of QT serial communication module qserialport, please see the relevant links below