提供訪問串口的函數。 更多...
| 頭: | #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 幫手類,允許枚舉係統中的所有串口。這很有用,為獲得要使用的串口的正確名稱。可以將幫手類對象作為自變量傳遞給 setPort () 或 setPortName () 方法來賦值期望的串行設備。
設置後,可以按 r/o (隻讀)、w/o (隻讀) 或 r/w (讀寫) 方式打開端口使用 open () 方法。
注意: 串口的打開是始終采用獨占訪問 (也就是說,沒有其它進程或綫程,可以訪問已經打開的串口)。
使用 close () 方法關閉端口和取消 I/O 操作。
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 () 方法。
有幾個特性可以工作於引腳信號,也就是: QSerialPort::dataTerminalReady , QSerialPort::requestToSend 。也是可能的,使用 pinoutSignals () 方法以查詢當前引腳信號設置。
一旦知道端口讀寫已就緒,就可以使用 read () 或 write () 方法。另外的 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 () 以查找最後發生的錯誤類型。
采用阻塞串口編程,完全不同於采用非阻塞串口編程。阻塞串口不要求事件循環,且通常導緻更簡單代碼。不管怎樣,在 GUI 應用程序中,阻塞串口隻應用於非 GUI 綫程,以避免凍結用戶界麵。
這些方式的更多有關細節,參考 範例 應用程序。
The QSerialPort class can also be used with QTextStream and QDataStream 的流運算符 (operator<<() 和 operator>>())。不過,有一個問題要意識到:確保足夠的數據可用,先於嘗試讀取通過使用 operator>>() 重載運算符。
另請參閱 QSerialPortInfo .
此枚舉描述通信設備操作采用的波特率。
注意: 此枚舉僅列齣最常見標準波特率。
| 常量 | 值 | 描述 |
|---|---|---|
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 .
此枚舉描述所用的數據位數。
| 常量 | 值 | 描述 |
|---|---|---|
QSerialPort::Data5
|
5
|
每個字符的數據位數為 5。用於 Baudot (波特) 碼。一般隻對舊設備 (譬如:電傳打字機) 有意義。 |
QSerialPort::Data6
|
6
|
每個字符的數據位數為 6。很少使用。 |
QSerialPort::Data7
|
7
|
每個字符的數據位數為 7。用於真正的 ASCII。一般隻對舊設備 (譬如:電傳打字機) 有意義。 |
QSerialPort::Data8
|
8
|
每個字符的數據位數為 8。用於大多數種類的數據,因為這的大小匹配字節大小。它幾乎普遍用於較新應用程序。 |
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 .
此枚舉描述數據傳輸的可能方嚮。
注意: 此枚舉用於在某些操作係統 (例如:像 POSIX) 為每個方嚮分彆設置設備的波特率。
| 常量 | 值 | 描述 |
|---|---|---|
QSerialPort::Input
|
1
|
輸入方嚮。 |
QSerialPort::Output
|
2
|
輸齣方嚮。 |
QSerialPort::AllDirections
|
Input | Output
|
同時在 2 方嚮。 |
Directions 類型是 typedef 對於 QFlags <Direction>。它存儲 Direction 值的 OR 組閤。
此枚舉描述所使用的流控製。
| 常量 | 值 | 描述 |
|---|---|---|
QSerialPort::NoFlowControl
|
0
|
沒有流控製。 |
QSerialPort::HardwareControl
|
1
|
硬件流控製 (RTS/CTS)。 |
QSerialPort::SoftwareControl
|
2
|
軟件流控製 (XON/XOFF)。 |
QSerialPort::UnknownFlowControl
|
-1
|
未知的流控製。此值已過時。提供它是為使舊源代碼能繼續工作。強烈建議不要在新代碼中使用它。 |
另請參閱 QSerialPort::flowControl .
此枚舉描述所使用的奇偶校驗方案。
| 常量 | 值 | 描述 |
|---|---|---|
QSerialPort::NoParity
|
0
|
不發送奇偶校驗位。這是最常見的奇偶校驗設置。錯誤檢測由通信協議處理。 |
QSerialPort::EvenParity
|
2
|
每個字符 (包括奇偶校驗位) 1 位數始終偶數。 |
QSerialPort::OddParity
|
3
|
每個字符 (包括奇偶校驗位) 1 位數始終奇數。 |
QSerialPort::SpaceParity
|
4
|
空間奇偶校驗。奇偶校驗位是在空間信號條件下發送的。它不提供錯誤檢測信息。 |
QSerialPort::MarkParity
|
5
|
標記奇偶校驗。奇偶校驗位始終被設為標記信號條件 (邏輯 1)。它不提供錯誤檢測信息。 |
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 .
此枚舉描述可能的 RS-232 引腳信號。
| 常量 | 值 | 描述 |
|---|---|---|
QSerialPort::NoSignal
|
0x00
|
無綫路活動 |
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 (數據終端就緒)。 |
QSerialPort::DataCarrierDetectSignal
|
0x08
|
DCD (數據載波檢測)。 |
QSerialPort::DataSetReadySignal
|
0x10
|
DSR (數據集就緒)。 |
QSerialPort::RingIndicatorSignal
|
0x20
|
RNG (環形指示器)。 |
QSerialPort::RequestToSendSignal
|
0x40
|
RTS (請求發送)。 |
QSerialPort::ClearToSendSignal
|
0x80
|
CTS (清零發送)。 |
QSerialPort::SecondaryTransmittedDataSignal
|
0x100
|
STD (二次傳輸數據)。 |
QSerialPort::SecondaryReceivedDataSignal
|
0x200
|
SRD (二次接收數據)。 |
PinoutSignals 類型是 typedef 對於 QFlags <PinoutSignal>。它存儲 PinoutSignal 值的 OR (或) 組閤。
另請參閱 pinoutSignals (), QSerialPort::dataTerminalReady ,和 QSerialPort::requestToSend .
此枚舉描述可能包含的錯誤通過 QSerialPort::error 特性。
| 常量 | 值 | 描述 |
|---|---|---|
QSerialPort::NoError
|
0
|
沒有齣現錯誤。 |
QSerialPort::DeviceNotFoundError
|
1
|
齣現錯誤,當試圖打開不存在的設備時。 |
QSerialPort::PermissionError
|
2
|
齣現錯誤,當試圖通過另一進程打開已經打開的設備時 (或用戶沒有足夠權限和證書來打開)。 |
QSerialPort::OpenError
|
3
|
齣現錯誤,當試圖打開在此對象中已經打開的設備時。 |
QSerialPort::NotOpenError
|
13
|
齣現這種錯誤,當隻可以成功履行要執行操作時,若設備是打開的。該值的引入是在 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
|
齣現 I/O 錯誤,當寫入數據時。 |
QSerialPort::ReadError
|
8
|
齣現 I/O 錯誤,當讀取數據時。 |
QSerialPort::ResourceError
|
9
|
齣現 I/O 錯誤,當資源變為不可用時 (如:當從係統意外移除設備時)。 |
QSerialPort::UnsupportedOperationError
|
10
|
請求的設備操作不支持 (或被在運行操作係統禁止)。 |
QSerialPort::TimeoutError
|
12
|
齣現超時錯誤。該值的引入是在 QtSerialPort 5.2. |
QSerialPort::UnknownError
|
11
|
發生無法識彆的錯誤。 |
另請參閱 QSerialPort::error .
此枚舉描述使用的停止位數。
| 常量 | 值 | 描述 |
|---|---|---|
QSerialPort::OneStop
|
1
|
1 停止位。 |
QSerialPort::OneAndHalfStop
|
3
|
1.5 停止位。這僅用於 Windows 平颱。 |
QSerialPort::TwoStop
|
2
|
2 停止位。 |
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 .
此特性保持期望方嚮的數據波特率
若設置成功 (或在打開端口前有設置),返迴
true
;否則返迴
false
並設置錯誤代碼,可以獲得通過訪問值
QSerialPort::error
特性。要設置波特率,使用枚舉
QSerialPort::BaudRate
或任何正 qint32 值。
注意: 若設置是在打開端口前設置的,實際串口設置的自動完成是在 QSerialPort::open () 方法,在那之後,端口打開成功。
警告: 設置 AllDirections 標誌在所有平颱都支持。Windows 僅支持此模式。
警告: 在 Windows,在任何方嚮返迴相等波特率。
默認值為 Baud9600 (即 9600 比特/秒)。
訪問函數:
| qint32 | baudRate (QSerialPort::Directions directions = AllDirections) const |
| bool | setBaudRate (qint32 baudRate , QSerialPort::Directions directions = AllDirections) |
通知程序信號:
| void | baudRateChanged (qint32 baudRate , QSerialPort::Directions directions ) |
此特性保持斷路時的傳輸綫狀態
返迴
true
當成功時,
false
否則。若標誌為
true
那麼傳輸綫處於斷路狀態;否則處於非斷路狀態。
注意:
串口必須打開,在試著設置 (或獲取) 此特性前;否則返迴
false
和設置
NotOpenError
錯誤代碼。這有點與眾不同,而不是如常規類的 Qt 特性設置。不管怎樣,這是特殊用例,由於特性是透過與內核和硬件的交互來設置的。因此,這 2 種情景無法完全相互比較。
該特性在 Qt 5.5 引入。
訪問函數:
| bool | isBreakEnabled () const |
| bool | setBreakEnabled (bool set = true) |
通知程序信號:
| void | breakEnabledChanged (bool set ) |
此特性保持幀中的數據位
若設置成功 (或在打開端口前有設置),返迴
true
;否則返迴
false
並設置錯誤代碼,可以獲得通過訪問值
QSerialPort::error
特性。
注意: 若設置是在打開端口前設置的,實際串口設置的自動完成是在 QSerialPort::open () 方法,在那之後,端口打開成功。
默認值為 Data8 (即:8 數據位)。
訪問函數:
| QSerialPort::DataBits | dataBits () const |
| bool | setDataBits (QSerialPort::DataBits dataBits ) |
通知程序信號:
| void | dataBitsChanged (QSerialPort::DataBits dataBits ) |
此特性保持綫路信號 DTR (數據終端就緒) 的狀態 (高或低)
返迴
true
當成功時,
false
否則。若標誌為
true
那麼 DTR (數據終端就緒) 信號被設為高;否則低。
注意:
串口必須打開,在試著設置 (或獲取) 此特性前;否則
false
被返迴,且錯誤代碼被設為
NotOpenError
.
訪問函數:
| bool | isDataTerminalReady () |
| bool | setDataTerminalReady (bool set ) |
通知程序信號:
| void | dataTerminalReadyChanged (bool set ) |
另請參閱 pinoutSignals ().
此特性保持串口的錯誤狀態
I/O 設備狀態返迴錯誤代碼。例如,若
open
() 返迴
false
,或讀/寫操作返迴
-1
,此特性可用於弄明白操作為什麼失敗的原因。
錯誤代碼被設為默認 QSerialPort::NoError 在調用 clearError() 之後
訪問函數:
| QSerialPort::SerialPortError | error () const |
| void | clearError () |
此屬性保存期望的流控製模式
若設置成功 (或在打開端口前有設置),返迴
true
;否則返迴
false
並設置錯誤代碼,可以獲得通過訪問值
QSerialPort::error
特性。
注意: 若設置是在打開端口前設置的,實際串口設置的自動完成是在 QSerialPort::open () 方法,在那之後,端口打開成功。
默認值為 NoFlowControl (即:沒有流控製)。
訪問函數:
| QSerialPort::FlowControl | flowControl () const |
| bool | setFlowControl (QSerialPort::FlowControl flowControl ) |
通知程序信號:
| void | flowControlChanged (QSerialPort::FlowControl flow ) |
此特性保持奇偶校驗模式
若設置成功 (或在打開端口前有設置),返迴
true
;否則返迴
false
並設置錯誤代碼,可以獲得通過訪問值
QSerialPort::error
特性。
注意: 若設置是在打開端口前設置的,實際串口設置的自動完成是在 QSerialPort::open () 方法,在那之後,端口打開成功。
默認值為 NoParity (即:沒有奇偶校驗)。
訪問函數:
| QSerialPort::Parity | parity () const |
| bool | setParity (QSerialPort::Parity parity ) |
通知程序信號:
| void | parityChanged (QSerialPort::Parity parity ) |
此特性保持綫路信號 RTS (請求發送) 的狀態 (高或低)
返迴
true
當成功時,
false
否則。若標誌為
true
那麼 RTS (請求發送) 信號被設為高;否則 low。
注意:
串口必須打開,在試著設置 (或獲取) 此特性前;否則
false
被返迴,且錯誤代碼被設為
NotOpenError
.
注意: 試圖控製 RTS (請求發送) 信號在 HardwareControl 模式會失敗,具有錯誤碼被設為 UnsupportedOperationError ,因為信號是由驅動程序自動控製的。
訪問函數:
| bool | isRequestToSend () |
| bool | setRequestToSend (bool set ) |
通知程序信號:
| void | requestToSendChanged (bool set ) |
另請參閱 pinoutSignals ().
此特性保持幀中的停止位數
若設置成功 (或在打開端口前有設置),返迴
true
;否則返迴
false
並設置錯誤代碼,可以獲得通過訪問值
QSerialPort::error
特性。
注意: 若設置是在打開端口前設置的,實際串口設置的自動完成是在 QSerialPort::open () 方法,在那之後,端口打開成功。
默認值為 OneStop (即:1 停止位)。
訪問函數:
| QSerialPort::StopBits | stopBits () const |
| bool | setStopBits (QSerialPort::StopBits stopBits ) |
通知程序信號:
| void | stopBitsChanged (QSerialPort::StopBits stopBits ) |
構造新的串口對象采用給定 parent .
構造新的串口對象采用給定 parent 來錶示串口采用指定的 name .
名稱應該有特定格式;見 setPort () 方法。
構造新的串口對象采用給定 parent 來錶示串口采用指定幫手類 serialPortInfo .
[虛擬]
QSerialPort::
~QSerialPort
()
關閉串口,若有必要,然後銷毀對象。
[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
)
此信號發射,在波特率改變後。新波特率被傳遞作為 baudRate 和方嚮作為 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
若可以從串口讀取 1 行數據;否則返迴
false
.
另請參閱 readLine ().
丟棄輸齣 (或輸入) 緩衝中的所有字符,從屬給定方嚮
directions
。這包括清零內部類緩衝和 UART (驅動程序) 緩衝。還會終止掛起的讀取,或寫入操作。若成功,返迴
true
;否則返迴
false
.
注意:
串口必須打開,在試著清零任何緩衝數據前;否則返迴
false
和設置
NotOpenError
錯誤代碼。
[override virtual]
void
QSerialPort::
close
()
重實現自 QIODevice::close ().
注意: 串口必須打開,在試著關閉它前;否則設置 NotOpenError 錯誤代碼。
另請參閱 QIODevice::close ().
[signal]
void
QSerialPort::
dataBitsChanged
(
QSerialPort::DataBits
dataBits
)
此信號發射,在幀中的數據位改變後。幀中的新數據位被傳遞作為 dataBits .
注意: 通知程序信號對於特性 dataBits .
另請參閱 QSerialPort::dataBits .
[signal]
void
QSerialPort::
dataTerminalReadyChanged
(
bool
set
)
此信號發射,在綫路信號 DTR (數據終端就緒) 狀態 (高或低) 改變後。綫路信號 DTR 的新狀態(高或低)被傳遞作為 set .
注意: 通知程序信號對於特性 dataTerminalReady .
另請參閱 QSerialPort::dataTerminalReady .
[signal]
void
QSerialPort::
errorOccurred
(
QSerialPort::SerialPortError
error
)
此信號發射,當串口齣現錯誤時。指定的 error 描述齣現的錯誤類型。
該函數在 Qt 5.8 引入。
另請參閱 QSerialPort::error .
[signal]
void
QSerialPort::
flowControlChanged
(
QSerialPort::FlowControl
flow
)
此信號發射,在流控製模式已改變後。新的流控方式被傳遞作為 flow .
注意: 通知程序信號對於特性 flowControl .
另請參閱 QSerialPort::flowControl .
此函數盡可能多地把內部寫入緩衝,不阻塞寫入到底層串口。若有寫入任何數據,此函數返迴
true
;否則返迴
false
.
調用此函數把緩衝數據,立即發送給串口。成功寫入的字節數,從屬 OS (操作係統)。在大多數情況下,不需要調用此函數,因為 QSerialPort 自會開始發送數據,一旦把控製返迴給事件循環。若缺乏事件循環,調用 waitForBytesWritten () 代替。
注意:
串口必須打開,在試著刷新任何緩衝數據前;否則返迴
false
和設置
NotOpenError
錯誤代碼。
另請參閱 write () 和 waitForBytesWritten ().
若平颱支持且串口是打開的,返迴本機串口句柄;否則返迴
-1
.
警告: 此函數僅供專傢使用;使用它風險自負。此外,此函數沒有攜帶兼容性承諾,在次要 Qt 發行之間。
該函數在 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 .
以位圖格式返迴綫路信號的狀態。
從此結果,通過應用掩碼 AND 來分配期望信號狀態是可能的,若掩碼是期望的枚舉值來自 QSerialPort::PinoutSignals .
注意: 此方法履行係統調用,從而確保正確返迴綫路信號狀態。這是必要的,當底層操作係統無法提供改變有關的適當通知時。
注意: 串口必須打開,在試著獲取引腳信號前;否則返迴 NoSignal 和設置 NotOpenError 錯誤代碼。
另請參閱 QSerialPort::dataTerminalReady and QSerialPort::requestToSend .
返迴的名稱設置通過 setPort () 或被傳遞給 QSerialPort 構造函數。此名稱很短 (即:它是從設備的內部變量係統位置提取和轉換的)。轉換算法平颱特定:
| 平颱 | 簡要描述 |
|---|---|
| Windows | 移除前綴 \\.\ 或 //./ 從係統位置,並返迴剩餘字符串。 |
| Unix, BSD | 移除前綴 /dev/ 從係統位置,並返迴剩餘字符串。 |
另請參閱 setPortName (), setPort (),和 QSerialPortInfo::portName ().
返迴內部讀取緩衝的大小。這限製客戶端可以接收的數據量,先於調用 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
)
此信號發射,在綫路信號 RTS (請求發送) 的狀態 (高或低) 已改變後。綫路信號 RTS (請求發送) 的新狀態 (高或低) 被傳遞作為 set .
注意: 通知程序信號對於特性 requestToSend .
另請參閱 QSerialPort::requestToSend .
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
和設置
NotOpenError
錯誤代碼。
另請參閱 setBreakEnabled ().
設置端口存儲在串口信息實例 serialPortInfo .
另請參閱 portName () 和 QSerialPortInfo .
設置 name 為串口。
串口的名稱可以按短名稱 (或長係統位置) 傳遞,若有必要。
另請參閱 portName () 和 QSerialPortInfo .
設置大小為 QSerialPort 的內部讀取緩衝到 size 字節。
若緩衝尺寸被限製到某個大小,
QSerialPort
緩衝數據就不會超過此大小。緩衝大小的特殊情況
0
意味著讀取緩衝不受限製,且緩衝所有傳入數據。這是默認。
此選項是有用的,若僅在某個時間點讀取數據 (例如:在實時流應用程序中),或者,若應該保護串口,防止接收太多數據 (可能最終導緻應用程序內存不足)。
另請參閱 readBufferSize () 和 read ().
[signal]
void
QSerialPort::
stopBitsChanged
(
QSerialPort::StopBits
stopBits
)
此信號發射,在幀中的停止位數改變後。新幀的停止位數的傳遞是按 stopBits .
注意: 通知程序信號對於特性 stopBits .
另請參閱 QSerialPort::stopBits .
[override virtual]
bool
QSerialPort::
waitForBytesWritten
(
int
msecs
= 30000)
重實現自 QIODevice::waitForBytesWritten ().
此函數阻塞,直到至少 1 字節被寫入串口和 bytesWritten() 信號已被發射。函數將超時花費 msecs 毫秒;默認超時為 30000 毫秒。若 msecs 是 -1,此函數不會超時。
函數返迴
true
若
bytesWritten
() 信號被發射;否則返迴
false
(若發生錯誤或操作超時)。
[override virtual]
bool
QSerialPort::
waitForReadyRead
(
int
msecs
= 30000)
重實現自 QIODevice::waitForReadyRead ().
此函數阻塞,直到有新數據可供讀取和 readyRead() 信號已被發射。函數將超時花費 msecs 毫秒;默認超時為 30000 毫秒。若 msecs 是 -1,此函數不會超時。
函數返迴
true
若
readyRead
() 信號被發射且有新的數據可供讀取;否則它返迴
false
(若發生錯誤或操作超時)。
另請參閱 waitForBytesWritten ().
[override virtual protected]
qint64
QSerialPort::
writeData
(const
char
*
data
,
qint64
maxSize
)
重實現自 QIODevice::writeData ().