QSerialPort 类

提供访问串行端口的函数。 更多...

头: #include <QSerialPort>
qmake: QT += serialport
Since: Qt 5.1
继承: QIODevice

注意: 此类的所有函数 可重入 .

公共类型

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 }

特性

公共函数

QSerialPort (QObject * parent = nullptr)
QSerialPort (const QString & name , QObject * parent = nullptr)
QSerialPort (const QSerialPortInfo & serialPortInfo , QObject * parent = nullptr)
virtual ~QSerialPort ()
qint32 baudRate (QSerialPort::Directions directions = AllDirections) const
bool clear (QSerialPort::Directions directions = AllDirections)
void clearError ()
QSerialPort::DataBits dataBits () const
QSerialPort::SerialPortError error () const
QSerialPort::FlowControl flowControl () const
bool flush ()
QSerialPort::Handle handle () const
bool isBreakEnabled () const
bool isDataTerminalReady ()
bool isRequestToSend ()
QSerialPort::Parity parity () const
QSerialPort::PinoutSignals pinoutSignals ()
QString portName () const
qint64 readBufferSize () const
bool sendBreak (int duration = 0)
bool setBaudRate (qint32 baudRate , QSerialPort::Directions directions = AllDirections)
bool setBreakEnabled (bool set = true)
bool setDataBits (QSerialPort::DataBits dataBits )
bool setDataTerminalReady (bool set )
bool setFlowControl (QSerialPort::FlowControl flowControl )
bool setParity (QSerialPort::Parity parity )
void setPort (const QSerialPortInfo & serialPortInfo )
void setPortName (const QString & name )
void setReadBufferSize (qint64 size )
bool setRequestToSend (bool set )
bool setStopBits (QSerialPort::StopBits stopBits )
QSerialPort::StopBits stopBits () const

重实现公共函数

virtual bool atEnd () const override
virtual qint64 bytesAvailable () const override
virtual qint64 bytesToWrite () const override
virtual bool canReadLine () const override
virtual void close () override
virtual bool isSequential () const override
virtual bool open (QIODevice::OpenMode mode ) override
virtual bool waitForBytesWritten (int msecs = 30000) override
virtual bool waitForReadyRead (int msecs = 30000) override

信号

void baudRateChanged (qint32 baudRate , QSerialPort::Directions directions )
void breakEnabledChanged (bool set )
void dataBitsChanged (QSerialPort::DataBits dataBits )
void dataTerminalReadyChanged (bool set )
void errorOccurred (QSerialPort::SerialPortError error )
void flowControlChanged (QSerialPort::FlowControl flow )
void parityChanged (QSerialPort::Parity parity )
void requestToSendChanged (bool set )
void stopBitsChanged (QSerialPort::StopBits stopBits )

静态公共成员

const QMetaObject staticMetaObject

重实现保护函数

virtual qint64 readData (char * data , qint64 maxSize ) override
virtual qint64 readLineData (char * data , qint64 maxSize ) override
virtual qint64 writeData (const char * data , qint64 maxSize ) override

额外继承成员

详细描述

提供访问串行端口的函数。

可以获取有关可用串行端口的信息,使用 QSerialPortInfo helper class, which allows an enumeration of all the serial ports in the system. This is useful to obtain the correct name of the serial port you want to use. You can pass an object of the helper class as an argument to the setPort () 或 setPortName () methods to assign the desired serial device.

After setting the port, you can open it in read-only (r/o), write-only (w/o), or read-write (r/w) mode using the open () 方法。

注意: The serial port is always opened with exclusive access (that is, no other process or thread can access an already opened serial port).

使用 close () method to close the port and cancel the I/O operations.

Having successfully opened, QSerialPort tries to determine the current configuration of the port and initializes itself. You can reconfigure the port to the desired setting using the setBaudRate (), setDataBits (), setParity (), setStopBits (),和 setFlowControl () 方法。

There are a couple of properties to work with the pinout signals namely: QSerialPort::dataTerminalReady , QSerialPort::requestToSend . It is also possible to use the pinoutSignals () method to query the current pinout signals set.

Once you know that the ports are ready to read or write, you can use the read () 或 write () methods. Alternatively the readLine () 和 readAll () convenience methods can also be invoked. If not all the data is read at once, the remaining data will be available for later as new incoming data is appended to the QSerialPort 's internal read buffer. You can limit the size of the read buffer using setReadBufferSize ().

QSerialPort provides a set of functions that suspend the calling thread until certain signals are emitted. These functions can be used to implement blocking serial ports:

见以下范例:

int numRead = 0, numReadTotal = 0;
char buffer[50];
for (;;) {
    numRead  = serial.read(buffer, 50);
    // Do whatever with the array
    numReadTotal += numRead;
    if (numRead == 0 && !serial.waitForReadyRead())
        break;
}
					

waitForReadyRead() 返回 false ,连接已关闭 (或出现错误)。

If an error occurs at any point in time, QSerialPort 将发射 errorOccurred () 信号。也可以调用 error () to find the type of error that occurred last.

Programming with a blocking serial port is radically different from programming with a non-blocking serial port. A blocking serial port does not require an event loop and typically leads to simpler code. However, in a GUI application, blocking serial port should only be used in non-GUI threads, to avoid freezing the user interface.

For more details about these approaches, refer to the 范例 applications.

The QSerialPort class can also be used with QTextStream and QDataStream 's stream operators (operator<<() and operator>>()). There is one issue to be aware of, though: make sure that enough data is available before attempting to read by using the operator>>() overloaded operator.

另请参阅 QSerialPortInfo .

成员类型文档编制

enum QSerialPort:: BaudRate

此枚举描述通信设备操作采用的波特率。

注意: 此枚举仅列出最常见标准波特率。

常量 描述
QSerialPort::Baud1200 1200 1200 波特。
QSerialPort::Baud2400 2400 2400 波特。
QSerialPort::Baud4800 4800 4800 波特。
QSerialPort::Baud9600 9600 9600 波特。
QSerialPort::Baud19200 19200 19200 波特。
QSerialPort::Baud38400 38400 38400 波特。
QSerialPort::Baud57600 57600 57600 波特。
QSerialPort::Baud115200 115200 115200 波特。
QSerialPort::UnknownBaud -1 未知波特。此值已过时。提供它是为使旧源代码能继续工作。强烈建议不要在新代码中使用它。

另请参阅 QSerialPort::baudRate .

enum QSerialPort:: DataBits

此枚举描述所用的数据位数。

常量 描述
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.

另请参阅 QSerialPort::dataBits .

enum QSerialPort:: Direction
flags QSerialPort:: Directions

此枚举描述数据传输的可能方向。

注意: 此枚举用于在某些操作系统 (例如:像 POSIX) 为每个方向分别设置设备的波特率。

常量 描述
QSerialPort::Input 1 输入方向。
QSerialPort::Output 2 输出方向。
QSerialPort::AllDirections Input | Output 同时在 2 方向。

Directions 类型是 typedef 对于 QFlags <Direction>。它存储 Direction 值的 OR 组合。

enum QSerialPort:: FlowControl

此枚举描述所使用的流控制。

常量 描述
QSerialPort::NoFlowControl 0 没有流控制。
QSerialPort::HardwareControl 1 硬件流控制 (RTS/CTS)。
QSerialPort::SoftwareControl 2 软件流控制 (XON/XOFF)。
QSerialPort::UnknownFlowControl -1 未知的流控制。此值已过时。提供它是为使旧源代码能继续工作。强烈建议不要在新代码中使用它。

另请参阅 QSerialPort::flowControl .

enum QSerialPort:: Parity

此枚举描述所使用的奇偶校验方案。

常量 描述
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.

另请参阅 QSerialPort::parity .

enum QSerialPort:: PinoutSignal
flags QSerialPort:: PinoutSignals

This enum describes the possible RS-232 pinout signals.

常量 描述
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).

The PinoutSignals type is a typedef for QFlags <PinoutSignal>. It stores an OR combination of PinoutSignal values.

另请参阅 pinoutSignals (), QSerialPort::dataTerminalReady ,和 QSerialPort::requestToSend .

enum QSerialPort:: SerialPortError

This enum describes the errors that may be contained by the QSerialPort::error 特性。

常量 描述
QSerialPort::NoError 0 没有出现错误。
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 发生无法识别的错误。

另请参阅 QSerialPort::error .

enum QSerialPort:: StopBits

This enum describes the number of stop bits used.

常量 描述
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::stopBits .

特性文档编制

baudRate : qint32

This property holds the data baud rate for the desired direction

若设置成功或在打开端口之前有设置,返回 true ;否则返回 false and sets an error code which can be obtained by accessing the value of the QSerialPort::error property. To set the baud rate, use the enumeration QSerialPort::BaudRate or any positive qint32 value.

注意: If the setting is set before opening the port, the actual serial port setting is done automatically in the QSerialPort::open () method right after that the opening of the port succeeds.

警告: 设置 AllDirections flag is supported on all platforms. Windows supports only this mode.

警告: Returns equal baud rate in any direction on Windows.

The default value is Baud9600, i.e. 9600 bits per second.

访问函数:

qint32 baudRate (QSerialPort::Directions directions = AllDirections) const
bool setBaudRate (qint32 baudRate , QSerialPort::Directions directions = AllDirections)

通知程序信号:

void baudRateChanged (qint32 baudRate , QSerialPort::Directions directions )

breakEnabled : bool

This property holds the state of the transmission line in break

返回 true 当成功时, false 否则。若标志为 true then the transmission line is in break state; otherwise is in non-break state.

注意: The serial port has to be open before trying to set or get this property; otherwise returns false and sets the NotOpenError error code. This is a bit unusual as opposed to the regular Qt property settings of a class. However, this is a special use case since the property is set through the interaction with the kernel and hardware. Hence, the two scenarios cannot be completely compared to each other.

该特性在 Qt 5.5 引入。

访问函数:

bool isBreakEnabled () const
bool setBreakEnabled (bool set = true)

通知程序信号:

void breakEnabledChanged (bool set )

dataBits : DataBits

This property holds the data bits in a frame

若设置成功或在打开端口之前有设置,返回 true ;否则返回 false and sets an error code which can be obtained by accessing the value of the QSerialPort::error 特性。

注意: If the setting is set before opening the port, the actual serial port setting is done automatically in the QSerialPort::open () method right after that the opening of the port succeeds.

The default value is Data8, i.e. 8 data bits.

访问函数:

QSerialPort::DataBits dataBits () const
bool setDataBits (QSerialPort::DataBits dataBits )

通知程序信号:

void dataBitsChanged (QSerialPort::DataBits dataBits )

dataTerminalReady : bool

This property holds the state (high or low) of the line signal DTR

返回 true 当成功时, false 否则。若标志为 true then the DTR signal is set to high; otherwise low.

注意: The serial port has to be open before trying to set or get this property; otherwise false is returned and the error code is set to NotOpenError .

访问函数:

bool isDataTerminalReady ()
bool setDataTerminalReady (bool set )

通知程序信号:

void dataTerminalReadyChanged (bool set )

另请参阅 pinoutSignals ().

error : SerialPortError

此特性保持串行端口的错误状态

I/O 设备状态返回错误代码。例如,若 open () 返回 false ,或读/写操作返回 -1 ,此特性可用于弄明白操作为什么失败的原因。

错误代码被设为默认 QSerialPort::NoError 在调用 clearError() 之后

访问函数:

QSerialPort::SerialPortError error () const
void clearError ()

flowControl : FlowControl

此属性保存期望的流控制模式

若设置成功或在打开端口之前有设置,返回 true ;否则返回 false and sets an error code which can be obtained by accessing the value of the QSerialPort::error 特性。

注意: If the setting is set before opening the port, the actual serial port setting is done automatically in the QSerialPort::open () method right after that the opening of the port succeeds.

默认值为 NoFlowControl , i.e. no flow control.

访问函数:

QSerialPort::FlowControl flowControl () const
bool setFlowControl (QSerialPort::FlowControl flowControl )

通知程序信号:

void flowControlChanged (QSerialPort::FlowControl flow )

parity : Parity

此特性保持奇偶校验模式

若设置成功或在打开端口之前有设置,返回 true ;否则返回 false and sets an error code which can be obtained by accessing the value of the QSerialPort::error 特性。

注意: If the setting is set before opening the port, the actual serial port setting is done automatically in the QSerialPort::open () method right after that the opening of the port succeeds.

默认值为 NoParity , i.e. no parity.

访问函数:

QSerialPort::Parity parity () const
bool setParity (QSerialPort::Parity parity )

通知程序信号:

void parityChanged (QSerialPort::Parity parity )

requestToSend : bool

This property holds the state (high or low) of the line signal RTS

返回 true 当成功时, false 否则。若标志为 true then the RTS signal is set to high; otherwise low.

注意: The serial port has to be open before trying to set or get this property; otherwise false is returned and the error code is set to NotOpenError .

注意: An attempt to control the RTS signal in the HardwareControl mode will fail with error code set to UnsupportedOperationError , because the signal is automatically controlled by the driver.

访问函数:

bool isRequestToSend ()
bool setRequestToSend (bool set )

通知程序信号:

void requestToSendChanged (bool set )

另请参阅 pinoutSignals ().

stopBits : StopBits

This property holds the number of stop bits in a frame

若设置成功或在打开端口之前有设置,返回 true ;否则返回 false and sets an error code which can be obtained by accessing the value of the QSerialPort::error 特性。

注意: If the setting is set before opening the port, the actual serial port setting is done automatically in the QSerialPort::open () method right after that the opening of the port succeeds.

默认值为 OneStop , i.e. 1 stop bit.

访问函数:

QSerialPort::StopBits stopBits () const
bool setStopBits (QSerialPort::StopBits stopBits )

通知程序信号:

void stopBitsChanged (QSerialPort::StopBits stopBits )

成员函数文档编制

QSerialPort:: QSerialPort ( QObject * parent = nullptr)

Constructs a new serial port object with the given parent .

QSerialPort:: QSerialPort (const QString & name , QObject * parent = nullptr)

Constructs a new serial port object with the given parent to represent the serial port with the specified name .

The name should have a specific format; see the setPort () 方法。

QSerialPort:: QSerialPort (const QSerialPortInfo & serialPortInfo , QObject * parent = nullptr)

Constructs a new serial port object with the given parent to represent the serial port with the specified helper class serialPortInfo .

[虚拟] QSerialPort:: ~QSerialPort ()

Closes the serial port, if necessary, and then destroys object.

[override virtual] bool QSerialPort:: atEnd () const

重实现自 QIODevice::atEnd ().

返回 true 若目前没有更多数据可供读取;否则返回 false .

This function is most commonly used when reading data from the serial port in a loop. For example:

// This slot is connected to QSerialPort::readyRead()
void QSerialPortClass::readyReadSlot()
{
    while (!port.atEnd()) {
        QByteArray data = port.read(100);
        ....
    }
}
					

另请参阅 bytesAvailable () 和 readyRead ().

[signal] void QSerialPort:: baudRateChanged ( qint32 baudRate , QSerialPort::Directions directions )

This signal is emitted after the baud rate has been changed. The new baud rate is passed as baudRate and directions as directions .

注意: 通知程序信号对于特性 baudRate .

另请参阅 QSerialPort::baudRate .

[override virtual] qint64 QSerialPort:: bytesAvailable () const

重实现自 QIODevice::bytesAvailable ().

返回等待读取的传入字节数。

另请参阅 bytesToWrite () 和 read ().

[override virtual] qint64 QSerialPort:: bytesToWrite () const

重实现自 QIODevice::bytesToWrite ().

返回等待写入的字节数。写入字节当控制回到事件循环或当 flush () 被调用。

另请参阅 bytesAvailable () 和 flush ().

[override virtual] bool QSerialPort:: canReadLine () const

重实现自 QIODevice::canReadLine ().

返回 true if a line of data can be read from the serial port; otherwise returns false .

另请参阅 readLine ().

bool QSerialPort:: clear ( QSerialPort::Directions directions = AllDirections)

Discards all characters from the output or input buffer, depending on given directions directions . This includes clearing the internal class buffers and the UART (driver) buffers. Also terminate pending read or write operations. If successful, returns true ;否则返回 false .

注意: The serial port has to be open before trying to clear any buffered data; otherwise returns false and sets the NotOpenError 错误代码。

[override virtual] void QSerialPort:: close ()

重实现自 QIODevice::close ().

注意: The serial port has to be open before trying to close it; otherwise sets the NotOpenError 错误代码。

另请参阅 QIODevice::close ().

[signal] void QSerialPort:: dataBitsChanged ( QSerialPort::DataBits dataBits )

This signal is emitted after the data bits in a frame has been changed. The new data bits in a frame is passed as dataBits .

注意: 通知程序信号对于特性 dataBits .

另请参阅 QSerialPort::dataBits .

[signal] void QSerialPort:: dataTerminalReadyChanged ( bool set )

This signal is emitted after the state (high or low) of the line signal DTR has been changed. The new the state (high or low) of the line signal DTR is passed as set .

注意: 通知程序信号对于特性 dataTerminalReady .

另请参阅 QSerialPort::dataTerminalReady .

[signal] void QSerialPort:: errorOccurred ( QSerialPort::SerialPortError error )

This signal is emitted when an error occurs in the serial port. The specified error 描述出现的错误类型。

该函数在 Qt 5.8 引入。

另请参阅 QSerialPort::error .

[signal] void QSerialPort:: flowControlChanged ( QSerialPort::FlowControl flow )

This signal is emitted after the flow control mode has been changed. The new flow control mode is passed as flow .

注意: 通知程序信号对于特性 flowControl .

另请参阅 QSerialPort::flowControl .

bool QSerialPort:: flush ()

This function writes as much as possible from the internal write buffer to the underlying serial port without blocking. If any data was written, this function returns true ;否则返回 false .

Call this function for sending the buffered data immediately to the serial port. The number of bytes successfully written depends on the operating system. In most cases, this function does not need to be called, because the QSerialPort class will start sending data automatically once control is returned to the event loop. In the absence of an event loop, call waitForBytesWritten () 代替。

注意: The serial port has to be open before trying to flush any buffered data; otherwise returns false and sets the NotOpenError 错误代码。

另请参阅 write () 和 waitForBytesWritten ().

QSerialPort::Handle QSerialPort:: handle () const

If the platform is supported and the serial port is open, returns the native serial port handle; otherwise returns -1 .

警告: This function is for expert use only; use it at your own risk. Furthermore, this function carries no compatibility promise between minor Qt releases.

该函数在 Qt 5.2 引入。

[override virtual] bool QSerialPort:: isSequential () const

重实现自 QIODevice::isSequential ().

始终返回 true 。串口是顺序设备。

[override virtual] bool QSerialPort:: open ( QIODevice::OpenMode mode )

重实现自 QIODevice::open ().

打开串口使用 OpenMode mode ,然后返回 true 若成功;否则返回 false 并设置可以获得的错误代码,通过调用 error () 方法。

注意: 方法返回 false 若打开端口成功,但无法成功设置任何端口设置。在这种情况下,端口将自动关闭,不要将端口留在不正确设置中。

警告: The mode 必须为 QIODevice::ReadOnly , QIODevice::WriteOnly ,或 QIODevice::ReadWrite 。不支持其它模式。

另请参阅 QIODevice::OpenMode and setPort ().

[signal] void QSerialPort:: parityChanged ( QSerialPort::Parity parity )

此信号被发射在奇偶校验模式有改变之后。传递新的奇偶校验模式按 parity .

注意: 通知程序信号对于特性 parity .

另请参阅 QSerialPort::parity .

QSerialPort::PinoutSignals QSerialPort:: pinoutSignals ()

Returns the state of the line signals in a bitmap format.

From this result, it is possible to allocate the state of the desired signal by applying a mask "AND", where the mask is the desired enumeration value from QSerialPort::PinoutSignals .

注意: This method performs a system call, thus ensuring that the line signal states are returned properly. This is necessary when the underlying operating systems cannot provide proper notifications about the changes.

注意: The serial port has to be open before trying to get the pinout signals; otherwise returns NoSignal and sets the NotOpenError 错误代码。

另请参阅 QSerialPort::dataTerminalReady and QSerialPort::requestToSend .

QString QSerialPort:: portName () const

返回名称设置通过 setPort () or passed to the QSerialPort constructor. This name is short, i.e. it is extracted and converted from the internal variable system location of the device. The conversion algorithm is platform specific:

平台 简要描述
Windows Removes the prefix "\\.\" or "//./" from the system location and returns the remainder of the string.
Unix, BSD Removes the prefix "/dev/" from the system location and returns the remainder of the string.

另请参阅 setPortName (), setPort (),和 QSerialPortInfo::portName ().

qint64 QSerialPort:: readBufferSize () const

Returns the size of the internal read buffer. This limits the amount of data that the client can receive before calling the read () 或 readAll () 方法。

读取缓冲尺寸为 0 (默认) 意味着缓冲没有大小限制,确保数据不丢失。

另请参阅 setReadBufferSize () 和 read ().

[override virtual protected] qint64 QSerialPort:: readData ( char * data , qint64 maxSize )

重实现自 QIODevice::readData ().

[override virtual protected] qint64 QSerialPort:: readLineData ( char * data , qint64 maxSize )

重实现自 QIODevice::readLineData ().

[signal] void QSerialPort:: requestToSendChanged ( bool set )

This signal is emitted after the state (high or low) of the line signal RTS has been changed. The new the state (high or low) of the line signal RTS is passed as set .

注意: 通知程序信号对于特性 requestToSend .

另请参阅 QSerialPort::requestToSend .

bool QSerialPort:: sendBreak ( int duration = 0)

Sends a continuous stream of zero bits during a specified period of time duration in msec if the terminal is using asynchronous serial data. If successful, returns true ;否则返回 false .

If the duration is zero then zero bits are transmitted by at least 0.25 seconds, but no more than 0.5 秒。

If the duration is non zero then zero bits are transmitted within a certain period of time depending on the implementation.

注意: The serial port has to be open before trying to send a break duration; otherwise returns false and sets the NotOpenError 错误代码。

另请参阅 setBreakEnabled ().

void QSerialPort:: setPort (const QSerialPortInfo & serialPortInfo )

设置端口存储在串口信息实例 serialPortInfo .

另请参阅 portName () 和 QSerialPortInfo .

void QSerialPort:: setPortName (const QString & name )

设置 name 为串口。

The name of the serial port can be passed as either a short name or the long system location if necessary.

另请参阅 portName () 和 QSerialPortInfo .

void QSerialPort:: setReadBufferSize ( qint64 size )

设置尺寸为 QSerialPort 的内部读取缓冲到 size 字节。

若缓冲尺寸被限制到某个大小, QSerialPort will not buffer more than this size of data. The special case of a buffer size of 0 means that the read buffer is unlimited and all incoming data is buffered. This is the default.

This option is useful if the data is only read at certain points in time (for instance in a real-time streaming application) or if the serial port should be protected against receiving too much data, which may eventually cause the application to run out of memory.

另请参阅 readBufferSize () 和 read ().

[signal] void QSerialPort:: stopBitsChanged ( QSerialPort::StopBits stopBits )

This signal is emitted after the number of stop bits in a frame has been changed. The new number of stop bits in a frame is passed as stopBits .

注意: 通知程序信号对于特性 stopBits .

另请参阅 QSerialPort::stopBits .

[override virtual] bool QSerialPort:: waitForBytesWritten ( int msecs = 30000)

重实现自 QIODevice::waitForBytesWritten ().

This function blocks until at least one byte has been written to the serial port and the bytesWritten() 信号已被发射。函数将超时花费 msecs milliseconds; the default timeout is 30000 milliseconds. If msecs 是 -1,此函数不会超时。

函数返回 true bytesWritten () 信号被发射;否则,返回 false (若发生错误或操作超时)。

[override virtual] bool QSerialPort:: waitForReadyRead ( int msecs = 30000)

重实现自 QIODevice::waitForReadyRead ().

此函数阻塞,直到有新数据可供读取和 readyRead() 信号已被发射。函数将超时花费 msecs milliseconds; the default timeout is 30000 milliseconds. If msecs 是 -1,此函数不会超时。

函数返回 true readyRead () 信号被发射且有新的数据可供读取;否则它返回 false (若发生错误或操作超时)。

另请参阅 waitForBytesWritten ().

[override virtual protected] qint64 QSerialPort:: writeData (const char * data , qint64 maxSize )

重实现自 QIODevice::writeData ().