QSerialPort Class
QSerialPort 用于访问串口. More...
头文件: | #include <QSerialPort> |
qmake: | QT += serialport |
开始支持版本: | Qt 5.1 |
基类: | QIODevice |
Note: All functions in this class are reentrant.
公有类型
enum | BaudRate { Baud1200, Baud2400, Baud4800, Baud9600, Baud19200, …, 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, DataCarrierDetectSignal, …, SecondaryReceivedDataSignal } |
flags | PinoutSignals |
enum | SerialPortError { NoError, DeviceNotFoundError, PermissionError, OpenError, NotOpenError, …, UnknownError } |
enum | StopBits { OneStop, OneAndHalfStop, TwoStop, UnknownStopBits } |
属性
|
|
- 1 个属性继承自 QObject
公有函数
QSerialPort(const QSerialPortInfo &serialPortInfo, QObject *parent = nullptr) | |
QSerialPort(const QString &name, QObject *parent = nullptr) | |
QSerialPort(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) |
virtual bool | waitForReadyRead(int msecs = 30000) |
信号
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) |
重新实现的受保护函数
virtual qint64 | readData(char *data, qint64 maxSize) override |
virtual qint64 | readLineData(char *data, qint64 maxSize) override |
virtual qint64 | writeData(const char *data, qint64 maxSize) |
其他继承的成员
- 1 个公有槽函数继承自 QObject
- 1 个公有变量继承自 QObject
- 10 个静态公有成员继承自 QObject
- 5 个受保护的函数继承自 QIODevice
- 9 个受保护的函数继承自 QObject
- 2 个受保护的变量继承自 QObject
详细描述
你可以使用 QSerialPortInfo 帮助类来获取可用串口的信息, 枚举系统所有的串口. 当你想使用某个串口时, 使用这个类可以获得正确的串口名. 你可以把 QSerialPortInfo 类实例对象作为参数传递给 setPort() 或 setPortName() 方法, 指定串口设备.
串口设置完毕以后, 你可以调用 open() 函数以只读 (r/o), 只写 (w/o), 读写 (r/w) 方式打开串口.
注意: 串口总是以独占访问的方式打开, 即其它进程或线程无法访问已经打开的串口.
调用 close() 关闭串口或取消串口读写操作.
串口打开成功后, QSerialPort 会检查串口当前的配置并初始化. 你可以使用 setBaudRate(), setDataBits(), setParity(), setStopBits(), setFlowControl() 方法重新配置串口.
QSerialPort::dataTerminalReady, QSerialPort::requestToSend 属性设置串口引脚信号. 你还可以使用 pinoutSignals() 方法查询串口引脚信号.
串口准备好后, 你可以使用 read() 或 write() 函数读写串口. 你还可以使用 readLine() 和 readAll() 这些便捷函数读写串口. 如果数据没有一次读完, 剩余数据将可供稍后使用, 因为新的传入数据将附加到 QSerialPort 的内部读取缓冲区. 你可以使用 setReadBufferSize() 限制读缓冲区的大小.
QSerialPort 提供了一组函数来挂起调用线程, 直到发出某些信号为止. 这些函数可用于实现阻塞串口:
- waitForReadyRead() 会阻止调用, 直到有新数据可供读取.
- waitForBytesWritten() 会阻止调用, 直到将一个数据负载写入串行端口.
参考以下示例:
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
, 说明串口连接已经关闭或者有错误出现.
如果串口出现错误, QSerialPort 将会发出 errorOccurred() 信号. 你还可以调用 error() 查询串口最近一次出现错误的类型.
注意: 在 QSerialport 中, 并非所有的错误都是以操作系统平台无关的方式处理, 诸如帧, 奇偶校验, 终止条件这样的错误需要由应用程序代码处理, 可能需要使用设备描述符中的操作系统相关的 ioctls 和(或)解析串口数据流中的字节填充.
编程阻塞串口与编程非阻塞串口完全不同. 阻塞串口不需要事件循环, 通常会让编程更简单. 然而, 在图形用户界面程序中, 阻塞串口应该仅用于非图形用户界面线程, 这样做可以避免用户界面卡顿.
详见 example 应用程序.
QSerialPort 还可以与 QTextStream 和 QDataStream 的流运算符 (operator<<() 和 operator>>()) 一起使用. 有一点需要注意: 在使用流运算符 operator>>() 之前请确保串口缓冲区有足够多的数据可读.
另见 QSerialPortInfo.
Member Type Documentation
enum QSerialPort::BaudRate
This enum describes the baud rate which the communication device operates with.
Note: Only the most common standard baud rates are listed in this enum.
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. This value is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code. |
See also QSerialPort::baudRate.
enum QSerialPort::DataBits
This enum describes the number of data bits used.
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. |
See also QSerialPort::dataBits.
enum QSerialPort::Direction
flags QSerialPort::Directions
This enum describes the possible directions of the data transmission.
Note: This enumeration is used for setting the baud rate of the device separately for each direction on some operating systems (for example, POSIX-like).
Constant | Value | Description |
---|---|---|
QSerialPort::Input | 1 | Input direction. |
QSerialPort::Output | 2 | Output direction. |
QSerialPort::AllDirections | Input | Output | Simultaneously in two directions. |
The Directions type is a typedef for QFlags<Direction>. It stores an OR combination of Direction values.
enum QSerialPort::FlowControl
This enum describes the flow control used.
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. |
See also QSerialPort::flowControl.
enum QSerialPort::Parity
This enum describes the parity scheme used.
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. |
See also QSerialPort::parity.
enum QSerialPort::PinoutSignal
flags QSerialPort::PinoutSignals
This enum describes the possible RS-232 pinout signals.
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). |
The PinoutSignals type is a typedef for QFlags<PinoutSignal>. It stores an OR combination of PinoutSignal values.
See also pinoutSignals(), QSerialPort::dataTerminalReady, and QSerialPort::requestToSend.
enum QSerialPort::SerialPortError
This enum describes the errors that may be contained by the QSerialPort::error property.
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. |
See also QSerialPort::error.
enum QSerialPort::StopBits
This enum describes the number of stop bits used.
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. |
See also QSerialPort::stopBits.
Property Documentation
baudRate : qint32
This property holds the data baud rate for the desired direction
If the setting is successful or set before opening the port, returns true
; otherwise returns 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.
Note: 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.
Warning: Setting the AllDirections flag is supported on all platforms. Windows supports only this mode.
Warning: Returns equal baud rate in any direction on Windows.
The default value is Baud9600, i.e. 9600 bits per second.
Access functions:
qint32 | baudRate(QSerialPort::Directions directions = AllDirections) const |
bool | setBaudRate(qint32 baudRate, QSerialPort::Directions directions = AllDirections) |
Notifier signal:
void | baudRateChanged(qint32 baudRate, QSerialPort::Directions directions) |
breakEnabled : bool
This property holds the state of the transmission line in break
Returns true
on success, false
otherwise. If the flag is true
then the transmission line is in break state; otherwise is in non-break state.
Note: 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.
This property was introduced in Qt 5.5.
Access functions:
bool | isBreakEnabled() const |
bool | setBreakEnabled(bool set = true) |
Notifier signal:
void | breakEnabledChanged(bool set) |
dataBits : DataBits
This property holds the data bits in a frame
If the setting is successful or set before opening the port, returns true
; otherwise returns false
and sets an error code which can be obtained by accessing the value of the QSerialPort::error property.
Note: 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.
Access functions:
QSerialPort::DataBits | dataBits() const |
bool | setDataBits(QSerialPort::DataBits dataBits) |
Notifier signal:
void | dataBitsChanged(QSerialPort::DataBits dataBits) |
dataTerminalReady : bool
This property holds the state (high or low) of the line signal DTR
Returns true
on success, false
otherwise. If the flag is true
then the DTR signal is set to high; otherwise low.
Note: 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.
Access functions:
bool | isDataTerminalReady() |
bool | setDataTerminalReady(bool set) |
Notifier signal:
void | dataTerminalReadyChanged(bool set) |
See also pinoutSignals().
error : SerialPortError
This property holds the error status of the serial port
The I/O device status returns an error code. For example, if open() returns false
, or a read/write operation returns -1
, this property can be used to figure out the reason why the operation failed.
The error code is set to the default QSerialPort::NoError after a call to clearError()
Access functions:
QSerialPort::SerialPortError | error() const |
void | clearError() |
flowControl : FlowControl
This property holds the desired flow control mode
If the setting is successful or set before opening the port, returns true
; otherwise returns false
and sets an error code which can be obtained by accessing the value of the QSerialPort::error property.
Note: 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 NoFlowControl, i.e. no flow control.
Access functions:
QSerialPort::FlowControl | flowControl() const |
bool | setFlowControl(QSerialPort::FlowControl flowControl) |
Notifier signal:
void | flowControlChanged(QSerialPort::FlowControl flow) |
parity : Parity
This property holds the parity checking mode
If the setting is successful or set before opening the port, returns true
; otherwise returns false
and sets an error code which can be obtained by accessing the value of the QSerialPort::error property.
Note: 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 NoParity, i.e. no parity.
Access functions:
QSerialPort::Parity | parity() const |
bool | setParity(QSerialPort::Parity parity) |
Notifier signal:
void | parityChanged(QSerialPort::Parity parity) |
requestToSend : bool
This property holds the state (high or low) of the line signal RTS
Returns true
on success, false
otherwise. If the flag is true
then the RTS signal is set to high; otherwise low.
Note: 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.
Note: 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.
Access functions:
bool | isRequestToSend() |
bool | setRequestToSend(bool set) |
Notifier signal:
void | requestToSendChanged(bool set) |
See also pinoutSignals().
stopBits : StopBits
This property holds the number of stop bits in a frame
If the setting is successful or set before opening the port, returns true
; otherwise returns false
and sets an error code which can be obtained by accessing the value of the QSerialPort::error property.
Note: 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 OneStop, i.e. 1 stop bit.
Access functions:
QSerialPort::StopBits | stopBits() const |
bool | setStopBits(QSerialPort::StopBits stopBits) |
Notifier signal:
void | stopBitsChanged(QSerialPort::StopBits stopBits) |
Member Function Documentation
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(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() method.
QSerialPort::QSerialPort(QObject *parent = nullptr)
Constructs a new serial port object with the given parent.
[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.
Note: Notifier signal for property baudRate.
See also QSerialPort::baudRate.
[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.
Note: Notifier signal for property dataBits.
See also 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.
Note: Notifier signal for property dataTerminalReady.
See also QSerialPort::dataTerminalReady.
[signal]
void QSerialPort::errorOccurred(QSerialPort::SerialPortError error)
This signal is emitted when an error occurs in the serial port. The specified error describes the type of error that occurred.
This function was introduced in Qt 5.8.
See also 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.
Note: Notifier signal for property flowControl.
See also QSerialPort::flowControl.
[signal]
void QSerialPort::parityChanged(QSerialPort::Parity parity)
This signal is emitted after the parity checking mode has been changed. The new parity checking mode is passed as parity.
Note: Notifier signal for property parity.
See also QSerialPort::parity.
[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.
Note: Notifier signal for property requestToSend.
See also QSerialPort::requestToSend.
[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.
Note: Notifier signal for property stopBits.
See also QSerialPort::stopBits.
[virtual]
QSerialPort::~QSerialPort()
Closes the serial port, if necessary, and then destroys object.
[override virtual]
bool QSerialPort::atEnd() const
Reimplements: QIODevice::atEnd() const.
Returns true
if no more data is currently available for reading; otherwise returns 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); .... } }
See also bytesAvailable() and readyRead().
[override virtual]
qint64 QSerialPort::bytesAvailable() const
Reimplements: QIODevice::bytesAvailable() const.
Returns the number of incoming bytes that are waiting to be read.
See also bytesToWrite() and read().
[override virtual]
qint64 QSerialPort::bytesToWrite() const
Reimplements: QIODevice::bytesToWrite() const.
Returns the number of bytes that are waiting to be written. The bytes are written when control goes back to the event loop or when flush() is called.
See also bytesAvailable() and flush().
[override virtual]
bool QSerialPort::canReadLine() const
Reimplements: QIODevice::canReadLine() const.
Returns true
if a line of data can be read from the serial port; otherwise returns false
.
See also 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
; otherwise returns false
.
Note: The serial port has to be open before trying to clear any buffered data; otherwise returns false
and sets the NotOpenError error code.
[override virtual]
void QSerialPort::close()
Reimplements: QIODevice::close().
Note: The serial port has to be open before trying to close it; otherwise sets the NotOpenError error code.
See also QIODevice::close().
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
; otherwise returns 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() instead.
Note: The serial port has to be open before trying to flush any buffered data; otherwise returns false
and sets the NotOpenError error code.
See also write() and 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
.
Warning: This function is for expert use only; use it at your own risk. Furthermore, this function carries no compatibility promise between minor Qt releases.
This function was introduced in Qt 5.2.
[override virtual]
bool QSerialPort::isSequential() const
Reimplements: QIODevice::isSequential() const.
Always returns true
. The serial port is a sequential device.
[override virtual]
bool QSerialPort::open(QIODevice::OpenMode mode)
Reimplements: QIODevice::open(QIODevice::OpenMode mode).
Opens the serial port using OpenMode mode, and then returns true
if successful; otherwise returns false
and sets an error code which can be obtained by calling the error() method.
Note: The method returns false
if opening the port is successful, but could not set any of the port settings successfully. In that case, the port is closed automatically not to leave the port around with incorrect settings.
Warning: The mode has to be QIODevice::ReadOnly, QIODevice::WriteOnly, or QIODevice::ReadWrite. Other modes are unsupported.
See also QIODevice::OpenMode and setPort().
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.
Note: 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.
Note: The serial port has to be open before trying to get the pinout signals; otherwise returns NoSignal and sets the NotOpenError error code.
See also QSerialPort::dataTerminalReady and QSerialPort::requestToSend.
QString QSerialPort::portName() const
Returns the name set by 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:
Platform | Brief Description |
---|---|
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. |
See also setPortName(), setPort(), and 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() or readAll() methods.
A read buffer size of 0
(the default) means that the buffer has no size limit, ensuring that no data is lost.
See also setReadBufferSize() and read().
[override virtual protected]
qint64 QSerialPort::readData(char *data, qint64 maxSize)
Reimplements: QIODevice::readData(char *data, qint64 maxSize).
[override virtual protected]
qint64 QSerialPort::readLineData(char *data, qint64 maxSize)
Reimplements: QIODevice::readLineData(char *data, qint64 maxSize).
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
; otherwise returns false
.
If the duration is zero then zero bits are transmitted by at least 0.25
seconds, but no more than 0.5
seconds.
If the duration is non zero then zero bits are transmitted within a certain period of time depending on the implementation.
Note: The serial port has to be open before trying to send a break duration; otherwise returns false
and sets the NotOpenError error code.
See also setBreakEnabled().
void QSerialPort::setPort(const QSerialPortInfo &serialPortInfo)
Sets the port stored in the serial port info instance serialPortInfo.
See also portName() and QSerialPortInfo.
void QSerialPort::setPortName(const QString &name)
Sets the name of the serial port.
The name of the serial port can be passed as either a short name or the long system location if necessary.
See also portName() and QSerialPortInfo.
void QSerialPort::setReadBufferSize(qint64 size)
Sets the size of QSerialPort's internal read buffer to be size bytes.
If the buffer size is limited to a certain 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.
See also readBufferSize() and read().
[override virtual]
bool QSerialPort::waitForBytesWritten(int msecs = 30000)
Reimplements: QIODevice::waitForBytesWritten(int msecs).
This function blocks until at least one byte has been written to the serial port and the bytesWritten() signal has been emitted. The function will timeout after msecs milliseconds; the default timeout is 30000 milliseconds. If msecs is -1, this function will not time out.
The function returns true
if the bytesWritten() signal is emitted; otherwise it returns false
(if an error occurred or the operation timed out).
[override virtual]
bool QSerialPort::waitForReadyRead(int msecs = 30000)
Reimplements: QIODevice::waitForReadyRead(int msecs).
This function blocks until new data is available for reading and the readyRead() signal has been emitted. The function will timeout after msecs milliseconds; the default timeout is 30000 milliseconds. If msecs is -1, this function will not time out.
The function returns true
if the readyRead() signal is emitted and there is new data available for reading; otherwise it returns false
(if an error occurred or the operation timed out).
See also waitForBytesWritten().
[override virtual protected]
qint64 QSerialPort::writeData(const char *data, qint64 maxSize)
Reimplements: QIODevice::writeData(const char *data, qint64 maxSize).