Notes on Qt Serial Port

Keywords: Qt Qt5

Although RS232 interface is no longer available on most household PC s. However, RS232 serial port is still widely used in industry because of its simple operation and reliable communication. In previous versions of Qt, there was no official support for RS232 serial port, so it was inconvenient to write serial port program. Now, QtSerialPort module is provided in Qt5.1, which is convenient for programmers to develop applications of serial port quickly. This article briefly talks about the use of QtSerialPort module.

 

The current QtSerialPort module provides two C++ classes, QSerialPort and QSerialPortInfo.

The QSerialPort class provides various interfaces for manipulating serial ports.

QSerial PortInfo is an auxiliary class that can provide various information about the available serial ports in a computer.

Usage method

First, the use of QSerial PortInfo is introduced. Here is a simple example to illustrate all serial devices on a computer.

First, you need to add the following to the pro file:

QT += serialport

 

The Cpp document is as follows:

  1. #include <QCoreApplication>  
  2. #include <QDebug>  
  3.    
  4. #include <QSerialPort>  
  5. #include <QSerialPortInfo>  
  6.    
  7. int main(int argc, char *argv[])  
  8. {  
  9.     QCoreApplication a(argc, argv);  
  10.     foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())  
  11.     {  
  12.         qDebug() << "Name : " << info.portName();  
  13.         qDebug() << "Description : " << info.description();  
  14.         qDebug() << "Manufacturer: " << info.manufacturer();  
  15.         qDebug() << "Serial Number: " << info.serialNumber();  
  16.         qDebug() << "System Location: " << info.systemLocation();  
  17.     }  
  18.     return a.exec();  
  19. }  

The results on my computer are as follows:

  1. Name :  "COM1"  
  2. Description :  "Communication Port"  
  3. Manufacturer:  "(Standard port type)"  
  4. Serial Number:  ""  
  5. System Location:  "\\.\COM1"  
  6. Name :  "COM5"  
  7. Description :  "USB Serial Port"  
  8. Manufacturer:  "FTDI"  
  9. Serial Number:  "A400G3UXA"  
  10. System Location:  "\\.\COM5"  

Usually, we need to specify a program to use a certain serial port. At this time, we can not only use the serial name, because the serial name of the USB serial port may change every time it is inserted on different USB ports. At this time, you can use the serial number of the serial port, which is generally unique, such as the following code, first traverse all the serial ports, find the serial port we need, and then return.

  1. #include <QCoreApplication>  
  2. #include <QDebug>  
  3. #include <QSerialPort>  
  4. #include <QSerialPortInfo>  
  5.   
  6. int main(int argc, char *argv[])  
  7. {  
  8.     QCoreApplication a(argc, argv);  
  9.     QSerialPortInfo com_info;  
  10.     foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())  
  11.     {  
  12.         if( info.serialNumber() == "A400G3UXA" )  
  13.         {  
  14.             com_info = info;  
  15.             break;  
  16.         }  
  17.     }  
  18.     qDebug() << "Name : " << com_info.portName();  
  19.     qDebug() << "Description : " << com_info.description();  
  20.     qDebug() << "serialNumber: " << com_info.serialNumber();  
  21.     return a.exec();  
  22. }  

QSerialPort is responsible for specific serial port operations. After choosing the serial port, the serial port should be opened before setting the baud rate and other parameters. These parameters are set up and can be used. The most basic operations are read() and write(). It should be noted that both operations are non-blocking.

Another important signal that needs to be used is void QIODevice::readyRead()

Each time the serial port receives data, it sends out this signal. We need to define a slot in our program and connect it to this signal. In this way, every time new data arrives, we can read data in slot. At this time, we must read all the data in the serial buffer, which can be realized by readAll().

The following code snippet gives an example of setting up a serial port.

  1.     m_reader.setPort(info);  
  2.     if(m_reader.open(QIODevice::ReadWrite))  
  3.     {  
  4.         qDebug() << "m_reader.open(QIODevice::ReadWrite)";  
  5.         m_reader.setBaudRate(QSerialPort::Baud9600);  
  6.         m_reader.setParity(QSerialPort::NoParity);  
  7.         m_reader.setDataBits(QSerialPort::Data8);  
  8.         m_reader.setStopBits(QSerialPort::OneStop);  
  9.         m_reader.setFlowControl(QSerialPort::NoFlowControl);  
  10.   
  11.         m_reader.clearError();  
  12.         m_reader.clear();  
  13.         connect(&m_reader, SIGNAL(readyRead()), this, SLOT(readyReadSlot()));  
  14. }  
  15.   
  16.   
  17. void Dialog::readyReadSlot()  
  18. {  
  19.     qDebug() << "x";  
  20.     QByteArray arr = m_reader.readAll();  
  21.     do_something(arr);  
  22. }  

Posted by J@ystick_FI on Mon, 10 Jun 2019 12:54:23 -0700