Joke Collection Website - Blessing messages - How to read serial data in QT

How to read serial data in QT

First, download files.

File download address:

You can also download what I uploaded to the network disk:

Second, the file content introduction

1. The downloaded file is qextserialport-1.2win-alpha, and the contents after decompression and opening are as follows.

(Click on the picture to see a clear big picture)

The following are introduced respectively:

The file content in the (1)doc folder is a simple description of the QextSerialPort class and QextBaseType, and we can open them with notepad program.

(2) There are several sample programs in the sample folder. You can look at its source code, but if you want to run them, there seem to be many problems.

(3) The 3)html folder is the use document of the QextSerialPort class.

(4) and then the rest of the file. Among them, qextserialenumerator.cpp and qextserialenumerator.h files are specified.

The QextSerialEnumerator class is used to obtain the serial port information available on the platform. However, this class doesn't seem to be very useful, and it is not our focus, so the following.

I won't introduce it again.

(5)QextSerialBase. CPP and qextserialbase.h files define a qextserialbase.

Class, win_qextserialport.cpp and win_qextserialport.h files define a win_qextserialport.

Class, posix_qextserialport.cpp and posix_qextserialport.h files define a.

Posix_QextSerialPort class, qextserialport.cpp and qextserialport.h files define a.

The QextSerialPort class. This QextSerialPort class is the one we mentioned above. It is a subclass of all these classes and the highest abstraction. It shields the platform function.

So it can be used on any platform.

2. Introduction to several courses.

The following are diagrams of these classes.

We can see that they are all inherited from the QIODevice class, so we can also use some functions of this class directly. There is also a QextBaseType class in the figure, which is actually just a label.

There is no specific content in Zhihu, it is used to represent Win_QextSerialPort or Posix_QextSerialPort.

Because conditional compilation is used in the QextSerialPort class, the QextSerialPort class can be downloaded from the.

The Win_QextSerialPort class can also inherit from the Posix_QextSerialPort class, so it is represented by QextBaseType. At this moment

We can see it in the qextserialport.h file. In addition, the QextSerialPort class is actually just for the convenience of cross-platform compilation of programs, and it can be used for different

On the platform, different classes are compiled and inherited according to different conditions. So it's just an abstraction, providing several constructors, with no specific content. In the qextserialport.h file.

Conditional compilation content is as follows:

#ifdef_TTY_POSIX_

# include“POSIX _ qextserialport . h“

# define qextbasetypeposix _ qextserialport

# Otherwise

# contains "win_qextserialport.h"

# define QextBaseTypeWin_QextSerialPort

#endif

So, in fact, we don't need to use this class, just use Win_QextSerialPort or Posix_QextSerialPort. Of course, if

You want to use this class to realize that the same source program can be compiled and run directly under Windows and Linux, so please make sure to add it here under Linux.

#define _TTY_POSIX_。 In order to make the program clearer, we don't use this class here, so we don't introduce it here.

The QextSerialBase class inherits from the QIODevice class, which provides some variables and functions necessary for operating serial ports, while

Win_QextSerialPort and Posix_QextSerialPort inherit from QextSerialBase.

Class, Win_QextSerialPort class adds some functions of operating serial port under Windows platform, and Posix_QextSerialPort class is added.

Some functions of operating serial port under Linux platform. Therefore, we use the Win_QextSerialPort class in Windows and use it in Linux.

Posix_QextSerialPort class.

3. The 3.QextSerialBase class also involves an enumeration variable QueryMode.

It has two values, Polling and EventDriven.

. QueryMode refers to the way of reading serial port, which we call query mode, polling mode and event-driven mode.

The event-driven way is to use events to handle serial port reading. Once the data arrives, it will send out readyRead () signal, which we can correlate to read the serial data. In the event-driven mode, the reading and writing of the serial port is asynchronous, and calling the reading and writing function will return immediately without freezing the calling thread.

However, the polling method is different. The reading and writing functions are executed synchronously, so the signal cannot work in this mode and some functions cannot be realized. However, the overhead of this model is very small. We need to set our own timer to read serial data.

The above two modes are supported under Windows, but only polling mode is supported under Linux.

Third, summarize.

Having said so much here, the last thing to say is that we use this class to write serial programs in Qt, and only need to use four files according to different platforms.