QT学习笔记QserialPort类学习(一)

这篇博客记录一下QSerialPort类的学习成果,虽然用过多次了,但是从来没有仔细查看过官方的说明,所以这篇博客主要参考QT官方文档,给自己一个明确的,详细的使用方法,或许会发现点意外收获也说不定。

1、概述

QSerialPort类提供的是是一个接入串口的功能,它从QT的5.1版本之后才开始有,它继承自QIODevice。还有一个串口辅助的类QSerialPortInfo,它可以列举出系统里所有的串口,通过这个类,配合setPort和setPortName方法,我们就可以安排一下自己想用的串口了。下面是这类需要的头文件和工程文件的添加的qmake语句:

Header:

#include <QSerialPort>

qmake:

QT += serialport

官方文档上提到一个“All functions in this class are reentrant.”这个类里面所有的函数都是可重入的。这里的可重入意思是允许被递归调用的函数。这就意味着,这个类的函数正被调用尚未返回时,也可以直接或间接调用函数本身。一般的函数不能做到这样,只有重入函数才允许递归调用。

2、类中包含的公用类型

enum

BaudRate { Baud1200, Baud2400, Baud4800, Baud9600, ..., UnknownBaud }

enum

DataBits { Data5, Data6, Data7, Data8, UnknownDataBits }

enum

Direction { Input, Output, AllDirections }

flags

Directions

enum

FlowControl { NoFlowControl, HardwareControl, SoftwareControl, UnknownFlowControl }

enum

Parity { NoParity, EvenParity, OddParity, SpaceParity, MarkParity, UnknownParity }

enum

PinoutSignal { NoSignal, TransmittedDataSignal, ReceivedDataSignal, DataTerminalReadySignal, ..., SecondaryReceivedDataSignal }

flags

PinoutSignals

enum

SerialPortError { NoError, DeviceNotFoundError, PermissionError, OpenError, ..., UnknownError }

enum

StopBits { OneStop, OneAndHalfStop, TwoStop, UnknownStopBits }

 3、参数相关

*1、baudRate : qint32

这是设置串口波特率的。此项设置需要在串口打开之前,否则会回设置失败,返回一个错误,这个返回的错误值可以在QSerialPort::error里面看到。设置成功,返回一个true,然后在QSerialPort::open()运行的时候自动加载配置。要是想设置波特率,那就可以通过QSerialPort::BaudRate的枚举或者直接写一个qint32类型的数据。这个波特率的默认值是9600,也就是9600bits每秒。

跟这个参数相关的函数有这两个:

qint32

baudRate(Directions directions = AllDirections) const

bool

setBaudRate(qint32 baudRate, Directions directions = AllDirections)

相关的信号呢?

void

baudRateChanged(qint32 baudRate, QSerialPort::Directions directions)

*2、flowControl : FlowControl

这个是设置串口的流控的。跟波特率一样,要在打开串口之前设置。也可以在QSerialPort::error里面看到错误反馈。流控的默认配置是关闭的,默认值为NoFlowControl。

跟这个参数相关的函数有这两个:

FlowControl

flowControl() const

bool

setFlowControl(FlowControl flowControl)

相关的信号有:

void

flowControlChanged(QSerialPort::FlowControl flow)

*3、parity : Parity

这个参数是设置校验方式的,跟波特率一样,要在打开串口之前设置。默认是没有校验的,值是NoParity。

跟这个参数相关的函数有这两个:

Parity

parity() const

bool

setParity(Parity parity)

相关信号有:

void

parityChanged(QSerialPort::Parity parity)

*4、dataBits : DataBits

控制数据位的,设置方法跟波特率差不多,也是在串口打开之前设置。默认是8数据位,值是Data8。

跟这个参数相关的函数有两个:

DataBits

dataBits() const

bool

setDataBits(DataBits dataBits)

相关信号有:

void

dataBitsChanged(QSerialPort::DataBits dataBits)

*4、stopBits : StopBits

控制停止位的,设置方法同上。默认是1停止位,值是OneStop。

相关的函数有:

StopBits

stopBits() const

bool

setStopBits(StopBits stopBits)

相关信号有:

void

stopBitsChanged(QSerialPort::StopBits stopBits)

*5、breakEnabled : bool

这个参数是设置传送状态的。如果返回true,那传送线路就是切断状态,否则就是非切断状态。这个参数的设置或者查询必须在串口打开之后,如果串口没打开,设置这个参数会报NotOpenError。这个参数不常用。

相关的函数:

bool

isBreakEnabled() const

bool

setBreakEnabled(bool set = true)

相关的信号:

void

breakEnabledChanged(bool set)

*6、dataTerminalReady : bool

数据终端就绪的参数,会保持一个状态(高或者低)。返回值是true代表成功,false代表失败。如果标志位是true,说明DTR(dataTerminalReady)信号被设置为高了,反之说明是低。设置或者查询这个参数的前提是串口已经打开了,否则会也会报NotOpenError的错误。

相关函数:

bool

isDataTerminalReady()

bool

setDataTerminalReady(bool set)

相关信号:

void

dataTerminalReadyChanged(bool set)

*7、error : SerialPortError

这个参数会记录串口的错误状态。I/O设备的状态出错时会返回错误码。这个参数可以用来查找错误原因。如果clearError()函数被调用了,那么错误码就会被设置为默认状态,也就是QSerialPort::NoError。

相关的函数有:

SerialPortError

error() const

void

clearError()

*8、requestToSend : bool

请求发送的参数。这个参数跟dataTerminalReady类似,也是保持一个高或者低的状态。设置成功则返回true,否则返回false。如果标志位是true,RTS(requestToSend)就是高,反之则是低。这里注意一点:如果在HardwareControl模式下,发送RTS信号会失败,错误提示是UnsupportedOperationError,这是因为此时的信号是由驱动自动控制的。

相关的函数:

bool

isRequestToSend()

bool

setRequestToSend(bool set)

相关的信号:

void

requestToSendChanged(bool set)

4、成员类型记录

*1、enum QSerialPort::BaudRate

下面的枚举是描述设备通信波特率的。这里只列出了一些比较常用的。

Constant

Value

Description

QSerialPort::Baud1200

1200

1200 baud.

QSerialPort::Baud2400

2400

2400 baud.

QSerialPort::Baud4800

4800

4800 baud.

QSerialPort::Baud9600

9600

9600 baud.

QSerialPort::Baud19200

19200

19200 baud.

QSerialPort::Baud38400

38400

38400 baud.

QSerialPort::Baud57600

57600

57600 baud.

QSerialPort::Baud115200

115200

115200 baud.

QSerialPort::UnknownBaud

-1

Unknown baud. 已经废弃,新版本不要用。

*2、enum QSerialPort::DataBits

下面的枚举是用来描述数据位的。

Constant

Value

Description

QSerialPort::Data5

5

The number of data bits in each character is 5. It is used for Baudot code. It generally only makes sense with older equipment such as teleprinters.

QSerialPort::Data6

6

The number of data bits in each character is 6. It is rarely used.

QSerialPort::Data7

7

The number of data bits in each character is 7. It is used for true ASCII. It generally only makes sense with older equipment such as teleprinters.

QSerialPort::Data8

8

The number of data bits in each character is 8. It is used for most kinds of data, as this size matches the size of a byte. It is almost universally used in newer applications.

QSerialPort::UnknownDataBits

-1

Unknown number of bits. This value is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.

*3、enum QSerialPort::Direction 和 flags QSerialPort::Directions

这里的枚举代表数据传输的方向,某些操作系统里设置波特率的时候它可以区分不同方向的波特率。(像OPSIX-like)

Constant

Value

Description

QSerialPort::Input

1

Input direction.

QSerialPort::Output

2

Output direction.

QSerialPort::AllDirections

Input | Output

Simultaneously in two directions.

方向的类型是QFlags<Direction>类型的typedef。它存储了这两种方向值通过“或”的组合。

*4、enum QSerialPort::FlowControl

这个枚举是对流控的描述。

Constant

Value

Description

QSerialPort::NoFlowControl

0

No flow control.

QSerialPort::HardwareControl

1

Hardware flow control (RTS/CTS).

QSerialPort::SoftwareControl

2

Software flow control (XON/XOFF).

QSerialPort::UnknownFlowControl

-1

Unknown flow control. This value is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.

*5、enum QSerialPort::Parity

这个枚举是校验方案的描述。

Constant

Value

Description

QSerialPort::NoParity

0

No parity bit it sent. This is the most common parity setting. Error detection is handled by the communication protocol.

QSerialPort::EvenParity

2

The number of 1 bits in each character, including the parity bit, is always even.

QSerialPort::OddParity

3

The number of 1 bits in each character, including the parity bit, is always odd. It ensures that at least one state transition occurs in each character.

QSerialPort::SpaceParity

4

Space parity. The parity bit is sent in the space signal condition. It does not provide error detection information.

QSerialPort::MarkParity

5

Mark parity. The parity bit is always set to the mark signal condition (logical 1). It does not provide error detection information.

QSerialPort::UnknownParity

-1

Unknown parity. This value is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.

*6、enum QSerialPort::PinoutSignal 和 flags QSerialPort::PinoutSignals

这个枚举描述了RS-232引脚输出信号的可能情况。

Constant

Value

Description

QSerialPort::NoSignal

0x00

No line active

QSerialPort::TransmittedDataSignal

0x01

TxD (Transmitted Data). This value is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.

QSerialPort::ReceivedDataSignal

0x02

RxD (Received Data). This value is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.

QSerialPort::DataTerminalReadySignal

0x04

DTR (Data Terminal Ready).

QSerialPort::DataCarrierDetectSignal

0x08

DCD (Data Carrier Detect).

QSerialPort::DataSetReadySignal

0x10

DSR (Data Set Ready).

QSerialPort::RingIndicatorSignal

0x20

RNG (Ring Indicator).

QSerialPort::RequestToSendSignal

0x40

RTS (Request To Send).

QSerialPort::ClearToSendSignal

0x80

CTS (Clear To Send).

QSerialPort::SecondaryTransmittedDataSignal

0x100

STD (Secondary Transmitted Data).

QSerialPort::SecondaryReceivedDataSignal

0x200

SRD (Secondary Received Data).

这里的引脚输出信号类型是QFlags<PinoutSignal>的Typedef。它保存了这些引脚输出信号值“或”的组合。

*7、enum QSerialPort::SerialPortError

这个枚举描述了QSerialPort::error参数可能包含的错误情况。

Constant

Value

Description

QSerialPort::NoError

0

No error occurred.

QSerialPort::DeviceNotFoundError

1

An error occurred while attempting to open an non-existing device.

QSerialPort::PermissionError

2

An error occurred while attempting to open an already opened device by another process or a user not having enough permission and credentials to open.

QSerialPort::OpenError

3

An error occurred while attempting to open an already opened device in this object.

QSerialPort::NotOpenError

13

This error occurs when an operation is executed that can only be successfully performed if the device is open. This value was introduced in QtSerialPort 5.2.

QSerialPort::ParityError

4

Parity error detected by the hardware while reading data. This value is obsolete. We strongly advise against using it in new code.

QSerialPort::FramingError

5

Framing error detected by the hardware while reading data. This value is obsolete. We strongly advise against using it in new code.

QSerialPort::BreakConditionError

6

Break condition detected by the hardware on the input line. This value is obsolete. We strongly advise against using it in new code.

QSerialPort::WriteError

7

An I/O error occurred while writing the data.

QSerialPort::ReadError

8

An I/O error occurred while reading the data.

QSerialPort::ResourceError

9

An I/O error occurred when a resource becomes unavailable, e.g. when the device is unexpectedly removed from the system.

QSerialPort::UnsupportedOperationError

10

The requested device operation is not supported or prohibited by the running operating system.

QSerialPort::TimeoutError

12

A timeout error occurred. This value was introduced in QtSerialPort 5.2.

QSerialPort::UnknownError

11

An unidentified error occurred.

*8、enum QSerialPort::StopBits

这个枚举描述的是停止位的数量。

Constant

Value

Description

QSerialPort::OneStop

1

1 stop bit.

QSerialPort::OneAndHalfStop

3

1.5 stop bits. This is only for the Windows platform.

QSerialPort::TwoStop

2

2 stop bits.

QSerialPort::UnknownStopBits

-1

Unknown number of stop bits. This value is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.

下一篇要总结一下QSerialPort类的成员函数。这一篇先写这么多

发布了11 篇原创文章 · 获赞 9 · 访问量 638

猜你喜欢

转载自blog.csdn.net/yimuta9538/article/details/103901439
今日推荐