Introduction to QtSerialPort
The QtSerialPort module is an additional part of the Qt5 library and provides a unified interface for hardware and virtual serial ports. Note: This module also supports Qt4.
Due to its simplicity and reliability, serial ports are still widely used in industries such as embedded systems and robots. With the QtSerialPort module, developers can greatly shorten the cycle of developing applications related to serial ports. The QtSerialPort module was originally used by a third-party library QSerialDevice (2.0 Branch). Now, QtSerialPort has been moved to the https://codereview.qt-project.org/code library.
Features
Currently, this module API only includes two classes: Serial and SerialPortInfo.
[SerialPort]
SerialPort is the basic class of this module. It provides a series of basic methods and attributes to access serial port resources.
The SerialPort class supports the following operating systems:
The support for the Symbian platform is only partly due to the lack of developers, because Nokia has decided to give up the platform and Qt5 does not include support for the platform.
[SerialPortInfo]
SerialPortInfo is a help class. It provides information about available serial ports on the system.
The SerialPortInfo class supports the following operating systems:
The support for the Symbian platform is only partly due to the lack of developers, because Nokia has decided to give up the platform and Qt5 does not include support for the platform.
Source code
Currently, source code related to QtSerialPort can be viewed through the browser: https://qt.gitorious.org/qt/qtserialport
You can also download: git clone git: // gitorious.org/qt/qtserialport.git through git.
Compile and install
Note: It is best to select shadow build, because the build directory is separated from the source code directory, so that you can keep the source code directory clean.
Compilation steps:
1. Configure the compiling environment
* Install Perl
* Make sure that the environment variables are correctly set: Qt4/Qt5; compiler; Perl
* Create a compilation Directory: Same as the source code directory
/
|-/Serialport-src
|-/Serialport-build
2. Perl is only required for Qt5. It is not required for Qt4. 3. Use the following recommended steps to compile and install the QtSerialPort library in Qt4/Qt5.
Cd serialport-build
Qmake ../serialport-src/qtserialport. pro
Make [or 'nmake' for MSVC compiler, or 'mingw32-make' for MinGW compiler]
Make install [or 'nmake install' for MSVC compiler, or 'mingw32-make install' for MinGW compiler]
Note: On * nix systems, you may need the permissions of Super Users to install them:
Sudo make install
Usage
QtSerialPort provides the corresponding library files. We can use these libraries when writing our own applications. The usage is as follows:
Qt4
CONFIG + = serialport
Qt5
QT + = serialport
Then, you can include the corresponding header file in the project:
...
# Include <QtSerialPort/QSerialPort>
# Include <QtSerialPort/QSerialPortInfo>
...
Sample Code
#include <QtCore/QCoreApplication>#include <QtCore/QDebug> #include <QtSerialPort/QSerialPort>#include <QtSerialPort/QSerialPortInfo> QT_USE_NAMESPACE int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); // Example use QSerialPortInfo foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { qDebug() << "Name : " << info.portName(); qDebug() << "Description : " << info.description(); qDebug() << "Manufacturer: " << info.manufacturer(); // Example use QSerialPort QSerialPort serial; serial.setPort(info); if (serial.open(QIODevice::ReadWrite)) serial.close(); } return a.exec();}
Note: CONFIG + = serialport must be the first or second line in the. pro file.
Actual Operation 1. Download the Code:
2. Compile:
3. installation:
After sudo make install, Qt5 environment variables are set on my system:
The header file is installed in:/opt/Qt5.0.1/5.0.1/gcc/include/QtSerialPort.
The library file is installed in:/opt/Qt5.0.1/5.0.1/gcc/lib
4. Test and run the built-in examples:
4. Use the QSerialPort Library (1) to create a Qt Console application
Modify the. pro file and add the serialport library as follows:
(2) modify the main function
Add the corresponding header file of the QtSerialPort library and implement simple code as follows:
#include <QCoreApplication>#include <QtCore/QDebug>#include "QSerialPort"#include "QSerialPortInfo"int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); // Example use QSerialPortInfo foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { qDebug() << "Name : " << info.portName(); qDebug() << "Description : " << info.description(); qDebug() << "Manufacturer: " << info.manufacturer(); // Example use QSerialPort QSerialPort serial; serial.setPort(info); if (serial.open(QIODevice::ReadWrite)) serial.close(); } return a.exec();}(3) Compile and run the following code: