|
QGroundControl
Ground Control Station for MAVLink Drones
|
Provides functions to access serial ports. More...
#include <qserialport.h>
Inheritance diagram for QSerialPort:
Collaboration diagram for QSerialPort:Signals | |
| void | baudRateChanged (qint32 baudRate, QSerialPort::Directions directions) |
| void | dataBitsChanged (QSerialPort::DataBits dataBits) |
| void | parityChanged (QSerialPort::Parity parity) |
| void | stopBitsChanged (QSerialPort::StopBits stopBits) |
| void | flowControlChanged (QSerialPort::FlowControl flowControl) |
| void | dataTerminalReadyChanged (bool set) |
| void | requestToSendChanged (bool set) |
| void | errorOccurred (QSerialPort::SerialPortError error) |
| void | breakEnabledChanged (bool set) |
Public Member Functions | |
| QSerialPort (QObject *parent=nullptr) | |
| QSerialPort (const QString &name, QObject *parent=nullptr) | |
| QSerialPort (const QSerialPortInfo &info, QObject *parent=nullptr) | |
| virtual | ~QSerialPort () |
| void | setPortName (const QString &name) |
| QString | portName () const |
| void | setPort (const QSerialPortInfo &info) |
| bool | open (OpenMode mode) override |
| void | close () override |
| bool | setBaudRate (qint32 baudRate, Directions directions=AllDirections) |
| qint32 | baudRate (Directions directions=AllDirections) const |
| the data baud rate for the desired direction | |
| bool | setDataBits (DataBits dataBits) |
| DataBits | dataBits () const |
| the data bits in a frame | |
| QBindable< DataBits > | bindableDataBits () |
| bool | setParity (Parity parity) |
| Parity | parity () const |
| the parity checking mode | |
| QBindable< Parity > | bindableParity () |
| bool | setStopBits (StopBits stopBits) |
| StopBits | stopBits () const |
| the number of stop bits in a frame | |
| QBindable< StopBits > | bindableStopBits () |
| bool | setFlowControl (FlowControl flowControl) |
| FlowControl | flowControl () const |
| the desired flow control mode | |
| QBindable< FlowControl > | bindableFlowControl () |
| bool | setDataTerminalReady (bool set) |
| bool | isDataTerminalReady () |
| bool | setRequestToSend (bool set) |
| bool | isRequestToSend () |
| PinoutSignals | pinoutSignals () |
| bool | flush () |
| bool | clear (Directions directions=AllDirections) |
| SerialPortError | error () const |
| the error status of the serial port | |
| void | clearError () |
| QBindable< SerialPortError > | bindableError () const |
| qint64 | readBufferSize () const |
| void | setReadBufferSize (qint64 size) |
| bool | isSequential () const override |
| qint64 | bytesAvailable () const override |
| qint64 | bytesToWrite () const override |
| bool | canReadLine () const override |
| bool | waitForReadyRead (int msecs=30000) override |
| bool | waitForBytesWritten (int msecs=30000) override |
| bool | setBreakEnabled (bool set=true) |
| bool | isBreakEnabled () const |
| QBindable< bool > | bindableIsBreakEnabled () |
| Handle | handle () const |
Protected Member Functions | |
| qint64 | readData (char *data, qint64 maxSize) override |
| qint64 | readLineData (char *data, qint64 maxSize) override |
| qint64 | writeData (const char *data, qint64 maxSize) override |
Provides functions to access serial ports.
\reentrant
\inmodule QtSerialPort
You can get information about the available serial ports using the 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() or 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() method.
Use the 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(), and setFlowControl() methods.
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() or write() methods. Alternatively the readLine() and 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:
\list
\endlist
See the following example:
If \l{QIODevice::}{waitForReadyRead()} returns false, the connection has been closed or an error has occurred.
If an error occurs at any point in time, QSerialPort will emit the errorOccurred() signal. You can also call 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 \l {Qt Serial Port Examples}{example} 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.
Definition at line 16 of file qserialport.h.
This enum describes the baud rate which the communication device operates with.
\value Baud1200 1200 baud. \value Baud2400 2400 baud. \value Baud4800 4800 baud. \value Baud9600 9600 baud. \value Baud19200 19200 baud. \value Baud38400 38400 baud. \value Baud57600 57600 baud. \value Baud115200 115200 baud.
| Enumerator | |
|---|---|
| Baud1200 | |
| Baud2400 | |
| Baud4800 | |
| Baud9600 | |
| Baud19200 | |
| Baud38400 | |
| Baud57600 | |
| Baud115200 | |
Definition at line 46 of file qserialport.h.
This enum describes the number of data bits used.
\value Data5 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. \value Data6 The number of data bits in each character is 6. It is rarely used. \value Data7 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. \value Data8 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.
| Enumerator | |
|---|---|
| Data5 | |
| Data6 | |
| Data7 | |
| Data8 | |
Definition at line 59 of file qserialport.h.
This enum describes the possible directions of the data transmission.
\value Input Input direction. \value Output Output direction. \value AllDirections Simultaneously in two directions.
| Enumerator | |
|---|---|
| Input | |
| Output | |
| AllDirections | |
Definition at line 37 of file qserialport.h.
This enum describes the flow control used.
\value NoFlowControl No flow control. \value HardwareControl Hardware flow control (RTS/CTS). \value SoftwareControl Software flow control (XON/XOFF).
| Enumerator | |
|---|---|
| NoFlowControl | |
| HardwareControl | |
| SoftwareControl | |
Definition at line 86 of file qserialport.h.
| enum QSerialPort::Parity |
This enum describes the parity scheme used.
\value NoParity No parity bit it sent. This is the most common parity setting. Error detection is handled by the communication protocol. \value EvenParity The number of 1 bits in each character, including the parity bit, is always even. \value OddParity 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. \value SpaceParity Space parity. The parity bit is sent in the space signal condition. It does not provide error detection information. \value MarkParity Mark parity. The parity bit is always set to the mark signal condition (logical 1). It does not provide error detection information.
| Enumerator | |
|---|---|
| NoParity | |
| EvenParity | |
| OddParity | |
| SpaceParity | |
| MarkParity | |
Definition at line 68 of file qserialport.h.
This enum describes the possible RS-232 pinout signals.
\value NoSignal No line active \value DataTerminalReadySignal DTR (Data Terminal Ready). \value DataCarrierDetectSignal DCD (Data Carrier Detect). \value DataSetReadySignal DSR (Data Set Ready). \value RingIndicatorSignal RNG (Ring Indicator). \value RequestToSendSignal RTS (Request To Send). \value ClearToSendSignal CTS (Clear To Send). \value SecondaryTransmittedDataSignal STD (Secondary Transmitted Data). \value SecondaryReceivedDataSignal SRD (Secondary Received Data).
Definition at line 94 of file qserialport.h.
This enum describes the errors that may be contained by the QSerialPort::error property.
\value NoError No error occurred.
\value DeviceNotFoundError An error occurred while attempting to open an non-existing device.
\value PermissionError 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.
\value OpenError An error occurred while attempting to open an already opened device in this object.
\value NotOpenError 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.
\value WriteError An I/O error occurred while writing the data.
\value ReadError An I/O error occurred while reading the data.
\value ResourceError An I/O error occurred when a resource becomes unavailable, e.g. when the device is unexpectedly removed from the system.
\value UnsupportedOperationError The requested device operation is not supported or prohibited by the running operating system.
\value TimeoutError A timeout error occurred. This value was introduced in QtSerialPort 5.2.
\value UnknownError An unidentified error occurred.
| Enumerator | |
|---|---|
| NoError | |
| DeviceNotFoundError | |
| PermissionError | |
| OpenError | |
| WriteError | |
| ReadError | |
| ResourceError | |
| UnsupportedOperationError | |
| UnknownError | |
| TimeoutError | |
| NotOpenError | |
Definition at line 109 of file qserialport.h.
This enum describes the number of stop bits used.
\value OneStop 1 stop bit. \value OneAndHalfStop 1.5 stop bits. This is only for the Windows platform. \value TwoStop 2 stop bits.
| Enumerator | |
|---|---|
| OneStop | |
| OneAndHalfStop | |
| TwoStop | |
Definition at line 78 of file qserialport.h.
|
explicit |
Constructs a new serial port object with the given parent.
Definition at line 332 of file qserialport.cpp.
|
explicit |
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.
Definition at line 342 of file qserialport.cpp.
References setPortName().
|
explicit |
Constructs a new serial port object with the given parent to represent the serial port with the specified helper class serialPortInfo.
Definition at line 352 of file qserialport.cpp.
References setPort().
|
virtual |
Closes the serial port, if necessary, and then destroys object.
Definition at line 361 of file qserialport.cpp.
References close().
Referenced by setBaudRate().
| qint32 QSerialPort::baudRate | ( | Directions | directions = AllDirections | ) | const |
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.
The default value is Baud9600, i.e. 9600 bits per second.
Definition at line 530 of file qserialport.cpp.
References AllDirections, and Input.
Referenced by setBaudRate().
|
signal |
| QBindable< QSerialPort::DataBits > QSerialPort::bindableDataBits | ( | ) |
Definition at line 583 of file qserialport.cpp.
| QBindable< QSerialPort::SerialPortError > QSerialPort::bindableError | ( | ) | const |
Definition at line 962 of file qserialport.cpp.
| QBindable< QSerialPort::FlowControl > QSerialPort::bindableFlowControl | ( | ) |
Definition at line 733 of file qserialport.cpp.
| QBindable< bool > QSerialPort::bindableIsBreakEnabled | ( | ) |
Definition at line 1159 of file qserialport.cpp.
| QBindable< QSerialPort::Parity > QSerialPort::bindableParity | ( | ) |
Definition at line 633 of file qserialport.cpp.
| QBindable< QSerialPort::StopBits > QSerialPort::bindableStopBits | ( | ) |
Definition at line 683 of file qserialport.cpp.
|
signal |
Referenced by setBreakEnabled().
|
override |
\reimp
Returns the number of incoming bytes that are waiting to be read.
Definition at line 1034 of file qserialport.cpp.
Referenced by GPSProvider::callback().
|
override |
\reimp
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.
Definition at line 1048 of file qserialport.cpp.
|
override |
\reimp
Returns true if a line of data can be read from the serial port; otherwise returns false.
Definition at line 1062 of file qserialport.cpp.
| bool QSerialPort::clear | ( | 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.
false and sets the NotOpenError error code. Definition at line 918 of file qserialport.cpp.
References Input, NotOpenError, and Output.
| void QSerialPort::clearError | ( | ) |
|
override |
\reimp
Definition at line 468 of file qserialport.cpp.
References NotOpenError.
Referenced by Bootloader::close(), SerialWorker::disconnectFromPort(), and ~QSerialPort().
| QSerialPort::DataBits QSerialPort::dataBits | ( | ) | const |
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.
The default value is Data8, i.e. 8 data bits.
Definition at line 577 of file qserialport.cpp.
Referenced by setDataBits().
|
signal |
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.
Referenced by setDataBits().
|
signal |
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.
Referenced by setDataTerminalReady().
| QSerialPort::SerialPortError QSerialPort::error | ( | ) | const |
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()
Definition at line 950 of file qserialport.cpp.
Referenced by SerialWorker::connectToPort().
|
signal |
This signal is emitted when an error occurs in the serial port. The specified error describes the type of error that occurred.
Referenced by SerialWorker::setupPort().
| QSerialPort::FlowControl QSerialPort::flowControl | ( | ) | const |
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.
The default value is NoFlowControl, i.e. no flow control.
Definition at line 727 of file qserialport.cpp.
Referenced by setFlowControl().
|
signal |
This signal is emitted after the flow control mode has been changed. The new flow control mode is passed as flow.
Referenced by setFlowControl().
| 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.
false and sets the NotOpenError error code.Definition at line 896 of file qserialport.cpp.
References NotOpenError.
Referenced by Bootloader::reboot().
| 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.
Definition at line 597 of file qserialport_android.cpp.
| bool QSerialPort::isBreakEnabled | ( | ) | const |
Definition at line 1153 of file qserialport.cpp.
| bool QSerialPort::isDataTerminalReady | ( | ) |
Definition at line 778 of file qserialport.cpp.
References DataTerminalReadySignal.
Referenced by setDataTerminalReady().
| bool QSerialPort::isRequestToSend | ( | ) |
Definition at line 834 of file qserialport.cpp.
References RequestToSendSignal.
Referenced by setRequestToSend().
|
override |
\reimp
Always returns true. The serial port is a sequential device.
Definition at line 1022 of file qserialport.cpp.
|
override |
\reimp
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.
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.Definition at line 436 of file qserialport.cpp.
References clearError(), OpenError, and UnsupportedOperationError.
Referenced by SerialWorker::connectToPort(), and Bootloader::open().
| QSerialPort::Parity QSerialPort::parity | ( | ) | const |
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.
The default value is NoParity, i.e. no parity.
Definition at line 627 of file qserialport.cpp.
Referenced by setParity().
|
signal |
This signal is emitted after the parity checking mode has been changed. The new parity checking mode is passed as parity.
Referenced by setParity().
| 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.
Definition at line 866 of file qserialport.cpp.
References NoSignal, and NotOpenError.
| 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: \table \header
Definition at line 414 of file qserialport.cpp.
References QSerialPortInfoPrivate::portNameFromSystemLocation().
Referenced by SerialWorker::connectToPort(), and SerialWorker::disconnectFromPort().
| 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.
Definition at line 987 of file qserialport.cpp.
|
overrideprotected |
\reimp
\omit This function does not really read anything, as we use QIODevicePrivate's buffer. The buffer will be read inside of QIODevice before this method will be called. \endomit
Definition at line 1173 of file qserialport.cpp.
|
overrideprotected |
\reimp
Definition at line 1193 of file qserialport.cpp.
|
signal |
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.
Referenced by setRequestToSend().
| bool QSerialPort::setBaudRate | ( | qint32 | baudRate, |
| Directions | directions = AllDirections |
||
| ) |
Definition at line 502 of file qserialport.cpp.
References baudRate(), Input, Output, and ~QSerialPort().
Referenced by GPSProvider::callback(), Bootloader::getBoardInfo(), Bootloader::initFlashSequence(), and Bootloader::open().
| bool QSerialPort::setBreakEnabled | ( | bool | set = true | ) |
Definition at line 1132 of file qserialport.cpp.
References breakEnabledChanged(), and NotOpenError.
| bool QSerialPort::setDataBits | ( | DataBits | dataBits | ) |
Definition at line 561 of file qserialport.cpp.
References dataBits(), and dataBitsChanged().
Referenced by Bootloader::open().
| bool QSerialPort::setDataTerminalReady | ( | bool | set | ) |
Definition at line 760 of file qserialport.cpp.
References dataTerminalReadyChanged(), isDataTerminalReady(), and NotOpenError.
| bool QSerialPort::setFlowControl | ( | FlowControl | flowControl | ) |
Definition at line 711 of file qserialport.cpp.
References flowControl(), and flowControlChanged().
Referenced by Bootloader::open().
| bool QSerialPort::setParity | ( | Parity | parity | ) |
Definition at line 611 of file qserialport.cpp.
References parity(), and parityChanged().
Referenced by Bootloader::open().
| void QSerialPort::setPort | ( | const QSerialPortInfo & | serialPortInfo | ) |
Sets the port stored in the serial port info instance serialPortInfo.
Definition at line 387 of file qserialport.cpp.
References QSerialPortInfo::systemLocation().
Referenced by QSerialPort().
| 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.
Definition at line 376 of file qserialport.cpp.
References QSerialPortInfoPrivate::portNameToSystemLocation().
Referenced by SerialWorker::connectToPort(), Bootloader::open(), and QSerialPort().
| 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.
Definition at line 1009 of file qserialport.cpp.
| bool QSerialPort::setRequestToSend | ( | bool | set | ) |
Definition at line 811 of file qserialport.cpp.
References HardwareControl, isRequestToSend(), NotOpenError, requestToSendChanged(), and UnsupportedOperationError.
| bool QSerialPort::setStopBits | ( | StopBits | stopBits | ) |
Definition at line 661 of file qserialport.cpp.
References stopBits(), and stopBitsChanged().
Referenced by Bootloader::open().
| QSerialPort::StopBits QSerialPort::stopBits | ( | ) | const |
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.
The default value is OneStop, i.e. 1 stop bit.
Definition at line 677 of file qserialport.cpp.
Referenced by setStopBits().
|
signal |
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.
Referenced by setStopBits().
|
override |
\reimp
This function blocks until at least one byte has been written to the serial port and the \l{QIODevice::}{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).
Definition at line 1110 of file qserialport.cpp.
Referenced by GPSProvider::callback().
|
override |
\reimp
This function blocks until new data is available for reading and the \l{QIODevice::}{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).
Definition at line 1081 of file qserialport.cpp.
Referenced by GPSProvider::callback(), Bootloader::getBoardInfo(), and Bootloader::initFlashSequence().
|
overrideprotected |
\reimp
Definition at line 1201 of file qserialport.cpp.