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:
* 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:
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: