summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaszlo Papp <lpapp@kde.org>2014-03-02 14:35:37 +0000
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-03-06 10:47:32 +0100
commit7c737e0edcb93585856c65890ef34e5c5a28ee6b (patch)
tree8daf3a17a6d2b8696b82ba48ca6a9f03badeca43
parent903c050fa4fdd2124f9f0709520b75a557080a92 (diff)
downloadqtserialport-7c737e0edcb93585856c65890ef34e5c5a28ee6b.tar.gz
Make it possible to set the port values before opening
This patch also changes the behavior of the open method. We do not use port detection anymore for good. It has been a broken concept, and it is very unlikely that anyone has ever relied on it. If anyone had done that, they would be in trouble anyway, getting noisy warnings needlessly and all that. The other option was also considered to keep this behavior optionally as the default, but that would complicate the API too much without not much gain. The default port settings are now also the sane 9600,8,N,1, and no flow control. Also please note that the serial port is closed automatically in the open method if any of the settings fails. Several subsequent changes can follow this one, namely: * Changing the examples to show this usage. * Add a QSerialPortSettings data structure as it seems to be common currently in client codes. * Add the QML plugin with only thin wrapper declarative classes and all that. * Deprecate the restore settings on close API. * Remove the internal current and restored data representations. * Add further error values to the API for the fine tuning of the error reporting when there are setting errors on open. * Add proper test coverage. In short, this change opens the gate up for many interesting subsequent changes that would be useful for the module overall. In general, this change makes it possible to reach the feature parity with QextSerialPort so that we are not much worse anymore. We could do the same that QextSerialPort could do many years ago, already. Task-number: QTBUG-33774 Change-Id: I8527672f8b0bb2581fa7054ccffda66ab7fa4a85 Reviewed-by: Sergey Belyashov <Sergey.Belyashov@gmail.com>
-rw-r--r--src/serialport/qserialport.cpp194
-rw-r--r--src/serialport/qserialport_symbian.cpp84
-rw-r--r--src/serialport/qserialport_symbian_p.h2
-rw-r--r--src/serialport/qserialport_unix.cpp104
-rw-r--r--src/serialport/qserialport_unix_p.h2
-rw-r--r--src/serialport/qserialport_win.cpp80
-rw-r--r--src/serialport/qserialport_win_p.h2
-rw-r--r--src/serialport/qserialport_wince.cpp1
8 files changed, 99 insertions, 370 deletions
diff --git a/src/serialport/qserialport.cpp b/src/serialport/qserialport.cpp
index cfb30bc..040b7ef 100644
--- a/src/serialport/qserialport.cpp
+++ b/src/serialport/qserialport.cpp
@@ -68,8 +68,8 @@ QSerialPortPrivateData::QSerialPortPrivateData(QSerialPort *q)
, readBuffer(SERIALPORT_BUFFERSIZE)
, writeBuffer(SERIALPORT_BUFFERSIZE)
, error(QSerialPort::NoError)
- , inputBaudRate(0)
- , outputBaudRate(0)
+ , inputBaudRate(9600)
+ , outputBaudRate(9600)
, dataBits(QSerialPort::Data8)
, parity(QSerialPort::NoParity)
, stopBits(QSerialPort::OneStop)
@@ -499,9 +499,9 @@ QString QSerialPort::portName() const
successful; otherwise returns false and sets an error code which can be
obtained by calling the error() method.
- \note This method has to be called before setting certain serial port
- parameters. See each property documentation for the details of when it is
- necessary.
+ \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 \a mode has to be QIODevice::ReadOnly, QIODevice::WriteOnly,
or QIODevice::ReadWrite. Other modes are unsupported.
@@ -530,8 +530,16 @@ bool QSerialPort::open(OpenMode mode)
QIODevice::open(mode);
- d->dataTerminalReady = isDataTerminalReady();
- d->requestToSend = isRequestToSend();
+ if (!d->setBaudRate()
+ || !d->setDataBits(d->dataBits)
+ || !d->setParity(d->parity)
+ || !d->setStopBits(d->stopBits)
+ || !d->setFlowControl(d->flowControl)
+ || !d->setDataTerminalReady(d->dataTerminalReady)
+ || !d->setRequestToSend(d->requestToSend)) {
+ close();
+ return false;
+ }
return true;
}
@@ -596,35 +604,29 @@ bool QSerialPort::settingsRestoredOnClose() const
\property QSerialPort::baudRate
\brief the data baud rate for the desired direction
- If the setting is successful, 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.
+ 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 The serial port has to be open before trying to set 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.
+ \note If the setting is set before opening the port, the actual serial port
+ setting is done automatically in the \l{QSerialPort::open()} method right
+ after that the opening of the port succeeds.
\warning Setting the AllDirections flag is only supported on
the Windows, Windows CE, and Symbian platforms.
\warning Returns equal baud rate in any direction on Windows, Windows CE, and
Symbian.
+
+ The default value is Baud9600, i.e. 9600 bits per second.
*/
bool QSerialPort::setBaudRate(qint32 baudRate, Directions directions)
{
Q_D(QSerialPort);
- if (!isOpen()) {
- setError(QSerialPort::NotOpenError);
- qWarning("%s: device not open", Q_FUNC_INFO);
- return false;
- }
-
- if (d->setBaudRate(baudRate, directions)) {
+ if (!isOpen() || d->setBaudRate(baudRate, directions)) {
if (directions & QSerialPort::Input) {
if (d->inputBaudRate != baudRate)
d->inputBaudRate = baudRate;
@@ -670,28 +672,21 @@ qint32 QSerialPort::baudRate(Directions directions) const
\property QSerialPort::dataBits
\brief the data bits in a frame
- If the setting is successful, returns true; otherwise returns false and sets
- an error code which can be obtained by accessing the value of the
- QSerialPort::error property.
+ 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 The serial port has to be open before trying to set 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.
+ \note If the setting is set before opening the port, the actual serial port
+ setting is done automatically in the \l{QSerialPort::open()} method right
+ after that the opening of the port succeeds.
+
+ The default value is Data8, i.e. 8 data bits.
*/
bool QSerialPort::setDataBits(DataBits dataBits)
{
Q_D(QSerialPort);
- if (!isOpen()) {
- setError(QSerialPort::NotOpenError);
- qWarning("%s: device not open", Q_FUNC_INFO);
- return false;
- }
-
- if (d->setDataBits(dataBits)) {
+ if (!isOpen() || d->setDataBits(dataBits)) {
if (d->dataBits != dataBits) {
d->dataBits = dataBits;
emit dataBitsChanged(d->dataBits);
@@ -722,28 +717,21 @@ QSerialPort::DataBits QSerialPort::dataBits() const
\property QSerialPort::parity
\brief the parity checking mode
- If the setting is successful, returns true; otherwise returns false and sets
- an error code which can be obtained by accessing the value of the
- QSerialPort::error property.
+ 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 The serial port has to be open before trying to set 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.
+ \note If the setting is set before opening the port, the actual serial port
+ setting is done automatically in the \l{QSerialPort::open()} method right
+ after that the opening of the port succeeds.
+
+ The default value is NoParity, i.e. no parity.
*/
bool QSerialPort::setParity(Parity parity)
{
Q_D(QSerialPort);
- if (!isOpen()) {
- setError(QSerialPort::NotOpenError);
- qWarning("%s: device not open", Q_FUNC_INFO);
- return false;
- }
-
- if (d->setParity(parity)) {
+ if (!isOpen() || d->setParity(parity)) {
if (d->parity != parity) {
d->parity = parity;
emit parityChanged(d->parity);
@@ -773,28 +761,21 @@ QSerialPort::Parity QSerialPort::parity() const
\property QSerialPort::stopBits
\brief the number of stop bits in a frame
- If the setting is successful, returns true; otherwise returns false and
- sets an error code which can be obtained by accessing the value of the
- QSerialPort::error property.
+ 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 The serial port has to be open before trying to set 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.
+ \note If the setting is set before opening the port, the actual serial port
+ setting is done automatically in the \l{QSerialPort::open()} method right
+ after that the opening of the port succeeds.
+
+ The default value is OneStop, i.e. 1 stop bit.
*/
bool QSerialPort::setStopBits(StopBits stopBits)
{
Q_D(QSerialPort);
- if (!isOpen()) {
- setError(QSerialPort::NotOpenError);
- qWarning("%s: device not open", Q_FUNC_INFO);
- return false;
- }
-
- if (d->setStopBits(stopBits)) {
+ if (!isOpen() || d->setStopBits(stopBits)) {
if (d->stopBits != stopBits) {
d->stopBits = stopBits;
emit stopBitsChanged(d->stopBits);
@@ -824,28 +805,21 @@ QSerialPort::StopBits QSerialPort::stopBits() const
\property QSerialPort::flowControl
\brief the desired flow control mode
- If the setting is successful, returns true; otherwise returns false and sets
- an error code which can be obtained by accessing the value of the
- QSerialPort::error property.
+ 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 The serial port has to be open before trying to set 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.
+ \note If the setting is set before opening the port, the actual serial port
+ setting is done automatically in the \l{QSerialPort::open()} method right
+ after that the opening of the port succeeds.
+
+ The default value is NoFlowControl, i.e. no flow control.
*/
bool QSerialPort::setFlowControl(FlowControl flowControl)
{
Q_D(QSerialPort);
- if (!isOpen()) {
- setError(QSerialPort::NotOpenError);
- qWarning("%s: device not open", Q_FUNC_INFO);
- return false;
- }
-
- if (d->setFlowControl(flowControl)) {
+ if (!isOpen() || d->setFlowControl(flowControl)) {
if (d->flowControl != flowControl) {
d->flowControl = flowControl;
emit flowControlChanged(d->flowControl);
@@ -875,15 +849,13 @@ QSerialPort::FlowControl QSerialPort::flowControl() const
\property QSerialPort::dataTerminalReady
\brief the state (high or low) of the line signal DTR
- If the setting is successful, returns true; otherwise returns false.
- If the flag is true then the DTR signal is set to high; otherwise low.
+ If the setting is successful or set before opening the port, returns true;
+ otherwise returns false. 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 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.
+ \note If the setting is set before opening the port, the actual serial port
+ setting is done automatically in the \l{QSerialPort::open()} method right
+ after that the opening of the port succeeds.
\sa pinoutSignals()
*/
@@ -891,13 +863,7 @@ bool QSerialPort::setDataTerminalReady(bool set)
{
Q_D(QSerialPort);
- if (!isOpen()) {
- setError(QSerialPort::NotOpenError);
- qWarning("%s: device not open", Q_FUNC_INFO);
- return false;
- }
-
- bool retval = d->setDataTerminalReady(set);
+ bool retval = !isOpen() || d->setDataTerminalReady(set);
if (retval && (d->dataTerminalReady != set)) {
d->dataTerminalReady = set;
emit dataTerminalReadyChanged(set);
@@ -926,15 +892,13 @@ bool QSerialPort::isDataTerminalReady()
\property QSerialPort::requestToSend
\brief the state (high or low) of the line signal RTS
- If the setting is successful, returns true; otherwise returns false.
- If the flag is true then the RTS signal is set to high; otherwise low.
+ If the setting is successful or set before opening the port, returns true;
+ otherwise returns false. 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 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.
+ \note If the setting is set before opening the port, the actual serial port
+ setting is done automatically in the \l{QSerialPort::open()} method right
+ after that the opening of the port succeeds.
\sa pinoutSignals()
*/
@@ -942,13 +906,7 @@ bool QSerialPort::setRequestToSend(bool set)
{
Q_D(QSerialPort);
- if (!isOpen()) {
- setError(QSerialPort::NotOpenError);
- qWarning("%s: device not open", Q_FUNC_INFO);
- return false;
- }
-
- bool retval = d->setRequestToSend(set);
+ bool retval = !isOpen() || d->setRequestToSend(set);
if (retval && (d->requestToSend != set)) {
d->requestToSend = set;
emit requestToSendChanged(set);
diff --git a/src/serialport/qserialport_symbian.cpp b/src/serialport/qserialport_symbian.cpp
index 7b8d5fd..48c0f16 100644
--- a/src/serialport/qserialport_symbian.cpp
+++ b/src/serialport/qserialport_symbian.cpp
@@ -149,7 +149,6 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
return false;
}
- detectDefaultSettings();
return true;
}
@@ -254,6 +253,11 @@ bool QSerialPortPrivate::waitForBytesWritten(int msec)
return false;
}
+bool QSerialPortPrivate::setBaudRate()
+{
+ return setBaudRate(inputBaudRate, QSerialPort::AllDirections);
+}
+
bool QSerialPortPrivate::setBaudRate(qint32 baudRate, QSerialPort::Directions directions)
{
Q_Q(QSerialPort);
@@ -389,84 +393,6 @@ bool QSerialPortPrivate::updateCommConfig()
return true;
}
-void QSerialPortPrivate::detectDefaultSettings()
-{
- // Detect baud rate.
- inputBaudRate = baudRateFromSetting(currentSettings().iRate);
- outputBaudRate = inputBaudRate;
-
- // Detect databits.
- switch (currentSettings().iDataBits) {
- case EData5:
- dataBits = QSerialPort::Data5;
- break;
- case EData6:
- dataBits = QSerialPort::Data6;
- break;
- case EData7:
- dataBits = QSerialPort::Data7;
- break;
- case EData8:
- dataBits = QSerialPort::Data8;
- break;
- default:
- qWarning("%s: Unexpected data bits settings", Q_FUNC_INFO);
- dataBits = QSerialPort::Data8;
- break;
- }
-
- // Detect parity.
- switch (currentSettings().iParity) {
- case EParityNone:
- parity = QSerialPort::NoParity;
- break;
- case EParityEven:
- parity = QSerialPort::EvenParity;
- break;
- case EParityOdd:
- parity = QSerialPort::OddParity;
- break;
- case EParityMark:
- parity = QSerialPort::MarkParity;
- break;
- case EParitySpace:
- parity = QSerialPort::SpaceParity;
- break;
- default:
- qWarning("%s: Unexpected parity settings", Q_FUNC_INFO);
- parity = QSerialPort::NoParity;
- break;
- }
-
- // Detect stopbits.
- switch (currentSettings().iStopBits) {
- case EStop1:
- stopBits = QSerialPort::OneStop;
- break;
- case EStop2:
- stopBits = QSerialPort::TwoStop;
- break;
- default:
- qWarning("%s: Unexpected stop bits settings", Q_FUNC_INFO);
- stopBits = QSerialPort::OneStop;
- break;
- }
-
- // Detect flow control.
- if ((currentSettings().iHandshake & (KConfigObeyXoff | KConfigSendXoff))
- == (KConfigObeyXoff | KConfigSendXoff))
- flowControl = QSerialPort::SoftwareControl;
- else if ((currentSettings().iHandshake & (KConfigObeyCTS | KConfigFreeRTS))
- == (KConfigObeyCTS | KConfigFreeRTS))
- flowControl = QSerialPort::HardwareControl;
- else if (currentSettings().iHandshake & KConfigFailDSR)
- flowControl = QSerialPort::NoFlowControl;
- else {
- qWarning("%s: Unexpected flow control settings", Q_FUNC_INFO);
- flowControl = QSerialPort::NoFlowControl;
- }
-}
-
QSerialPort::SerialPortError QSerialPortPrivate::decodeSystemError() const
{
QSerialPort::SerialPortError error;
diff --git a/src/serialport/qserialport_symbian_p.h b/src/serialport/qserialport_symbian_p.h
index 5d95ca9..258a811 100644
--- a/src/serialport/qserialport_symbian_p.h
+++ b/src/serialport/qserialport_symbian_p.h
@@ -74,6 +74,7 @@ public:
bool waitForReadyRead(int msec);
bool waitForBytesWritten(int msec);
+ bool setBaudRate();
bool setBaudRate(qint32 baudRate, QSerialPort::Directions directions);
bool setDataBits(QSerialPort::DataBits dataBits);
bool setParity(QSerialPort::Parity parity);
@@ -101,7 +102,6 @@ public:
private:
bool updateCommConfig();
- void detectDefaultSettings();
QSerialPort::SerialPortError decodeSystemError() const;
bool waitForReadOrWrite(bool *selectForRead, bool *selectForWrite,
diff --git a/src/serialport/qserialport_unix.cpp b/src/serialport/qserialport_unix.cpp
index 3ab7514..0f999bb 100644
--- a/src/serialport/qserialport_unix.cpp
+++ b/src/serialport/qserialport_unix.cpp
@@ -265,7 +265,6 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
if ((flags & O_WRONLY) == 0)
setReadNotificationEnabled(true);
- detectDefaultSettings();
return true;
}
@@ -520,6 +519,16 @@ bool QSerialPortPrivate::waitForBytesWritten(int msecs)
return false;
}
+bool QSerialPortPrivate::setBaudRate()
+{
+ if (!setBaudRate(inputBaudRate, QSerialPort::Input)
+ && !setBaudRate(outputBaudRate, QSerialPort::Output)) {
+ return false;
+ }
+
+ return true;
+}
+
bool QSerialPortPrivate::setBaudRate(qint32 baudRate, QSerialPort::Directions directions)
{
Q_Q(QSerialPort);
@@ -862,99 +871,6 @@ bool QSerialPortPrivate::updateTermios()
return true;
}
-void QSerialPortPrivate::detectDefaultSettings()
-{
- // Detect baud rate.
- const speed_t inputUnixBaudRate = ::cfgetispeed(&currentTermios);
- const speed_t outputUnixBaudRate = ::cfgetospeed(&currentTermios);
- bool isCustomBaudRateCurrentSet = false;
-
-#ifdef Q_OS_LINUX
- // try detect the ability to support custom baud rate
- isCustomBaudRateSupported = ::ioctl(descriptor, TIOCGSERIAL, &currentSerialInfo) != -1
- && ::ioctl(descriptor, TIOCSSERIAL, &currentSerialInfo) != -1;
-
- if (isCustomBaudRateSupported) {
- restoredSerialInfo = currentSerialInfo;
-
- // assume that the baud rate is a custom
- isCustomBaudRateCurrentSet = inputUnixBaudRate == B38400 && outputUnixBaudRate == B38400;
-
- if (isCustomBaudRateCurrentSet) {
- if ((currentSerialInfo.flags & ASYNC_SPD_CUST)
- && currentSerialInfo.custom_divisor > 0) {
-
- // yes, speed is really custom
- inputBaudRate = currentSerialInfo.baud_base / currentSerialInfo.custom_divisor;
- outputBaudRate = inputBaudRate;
- } else {
- // no, we were wrong and the speed is a standard 38400 baud
- isCustomBaudRateCurrentSet = false;
- }
- }
- }
-#else
- // other *nix
-#endif
- if (!isCustomBaudRateSupported || !isCustomBaudRateCurrentSet) {
- inputBaudRate = QSerialPortPrivate::baudRateFromSetting(inputUnixBaudRate);
- outputBaudRate = QSerialPortPrivate::baudRateFromSetting(outputUnixBaudRate);
- }
-
- // Detect databits.
- switch (currentTermios.c_cflag & CSIZE) {
- case CS5:
- dataBits = QSerialPort::Data5;
- break;
- case CS6:
- dataBits = QSerialPort::Data6;
- break;
- case CS7:
- dataBits = QSerialPort::Data7;
- break;
- case CS8:
- dataBits = QSerialPort::Data8;
- break;
- default:
- qWarning("%s: Unexpected data bits settings", Q_FUNC_INFO);
- dataBits = QSerialPort::Data8;
- break;
- }
-
- // Detect parity.
-#ifdef CMSPAR
- if (currentTermios.c_cflag & CMSPAR) {
- parity = currentTermios.c_cflag & PARODD ?
- QSerialPort::MarkParity : QSerialPort::SpaceParity;
- } else {
-#endif
- if (currentTermios.c_cflag & PARENB) {
- parity = currentTermios.c_cflag & PARODD ?
- QSerialPort::OddParity : QSerialPort::EvenParity;
- } else {
- parity = QSerialPort::NoParity;
- }
-#ifdef CMSPAR
- }
-#endif
-
- // Detect stopbits.
- stopBits = currentTermios.c_cflag & CSTOPB ?
- QSerialPort::TwoStop : QSerialPort::OneStop;
-
- // Detect flow control.
- if ((!(currentTermios.c_cflag & CRTSCTS)) && (!(currentTermios.c_iflag & (IXON | IXOFF | IXANY))))
- flowControl = QSerialPort::NoFlowControl;
- else if ((!(currentTermios.c_cflag & CRTSCTS)) && (currentTermios.c_iflag & (IXON | IXOFF | IXANY)))
- flowControl = QSerialPort::SoftwareControl;
- else if ((currentTermios.c_cflag & CRTSCTS) && (!(currentTermios.c_iflag & (IXON | IXOFF | IXANY))))
- flowControl = QSerialPort::HardwareControl;
- else {
- qWarning("%s: Unexpected flow control settings", Q_FUNC_INFO);
- flowControl = QSerialPort::NoFlowControl;
- }
-}
-
QSerialPort::SerialPortError QSerialPortPrivate::decodeSystemError() const
{
QSerialPort::SerialPortError error;
diff --git a/src/serialport/qserialport_unix_p.h b/src/serialport/qserialport_unix_p.h
index 566ce52..97a88c0 100644
--- a/src/serialport/qserialport_unix_p.h
+++ b/src/serialport/qserialport_unix_p.h
@@ -113,6 +113,7 @@ public:
bool waitForReadyRead(int msecs);
bool waitForBytesWritten(int msecs);
+ bool setBaudRate();
bool setBaudRate(qint32 baudRate, QSerialPort::Directions directions);
bool setDataBits(QSerialPort::DataBits dataBits);
bool setParity(QSerialPort::Parity parity);
@@ -161,7 +162,6 @@ public:
private:
bool updateTermios();
- void detectDefaultSettings();
QSerialPort::SerialPortError decodeSystemError() const;
bool isReadNotificationEnabled() const;
diff --git a/src/serialport/qserialport_win.cpp b/src/serialport/qserialport_win.cpp
index abf13b3..2c15e94 100644
--- a/src/serialport/qserialport_win.cpp
+++ b/src/serialport/qserialport_win.cpp
@@ -208,7 +208,6 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
communicationNotifier->setEnabled(true);
- detectDefaultSettings();
return true;
}
@@ -461,6 +460,11 @@ bool QSerialPortPrivate::waitForBytesWritten(int msecs)
#endif // #ifndef Q_OS_WINCE
+bool QSerialPortPrivate::setBaudRate()
+{
+ return setBaudRate(inputBaudRate, QSerialPort::AllDirections);
+}
+
bool QSerialPortPrivate::setBaudRate(qint32 baudRate, QSerialPort::Directions directions)
{
Q_Q(QSerialPort);
@@ -793,80 +797,6 @@ bool QSerialPortPrivate::updateCommTimeouts()
#endif // #ifndef Q_OS_WINCE
-void QSerialPortPrivate::detectDefaultSettings()
-{
- // Detect baud rate.
- inputBaudRate = quint32(currentDcb.BaudRate);
- outputBaudRate = inputBaudRate;
-
- // Detect databits.
- switch (currentDcb.ByteSize) {
- case 5:
- dataBits = QSerialPort::Data5;
- break;
- case 6:
- dataBits = QSerialPort::Data6;
- break;
- case 7:
- dataBits = QSerialPort::Data7;
- break;
- case 8:
- dataBits = QSerialPort::Data8;
- break;
- default:
- qWarning("%s: Unexpected data bits settings", Q_FUNC_INFO);
- dataBits = QSerialPort::Data8;
- break;
- }
-
- // Detect parity.
- if ((currentDcb.Parity == NOPARITY) && !currentDcb.fParity)
- parity = QSerialPort::NoParity;
- else if ((currentDcb.Parity == SPACEPARITY) && currentDcb.fParity)
- parity = QSerialPort::SpaceParity;
- else if ((currentDcb.Parity == MARKPARITY) && currentDcb.fParity)
- parity = QSerialPort::MarkParity;
- else if ((currentDcb.Parity == EVENPARITY) && currentDcb.fParity)
- parity = QSerialPort::EvenParity;
- else if ((currentDcb.Parity == ODDPARITY) && currentDcb.fParity)
- parity = QSerialPort::OddParity;
- else {
- qWarning("%s: Unexpected parity settings", Q_FUNC_INFO);
- parity = QSerialPort::NoParity;
- }
-
- // Detect stopbits.
- switch (currentDcb.StopBits) {
- case ONESTOPBIT:
- stopBits = QSerialPort::OneStop;
- break;
- case ONE5STOPBITS:
- stopBits = QSerialPort::OneAndHalfStop;
- break;
- case TWOSTOPBITS:
- stopBits = QSerialPort::TwoStop;
- break;
- default:
- qWarning("%s: Unexpected stop bits settings", Q_FUNC_INFO);
- stopBits = QSerialPort::OneStop;
- break;
- }
-
- // Detect flow control.
- if (!currentDcb.fOutxCtsFlow && (currentDcb.fRtsControl == RTS_CONTROL_DISABLE)
- && !currentDcb.fInX && !currentDcb.fOutX) {
- flowControl = QSerialPort::NoFlowControl;
- } else if (!currentDcb.fOutxCtsFlow && (currentDcb.fRtsControl == RTS_CONTROL_DISABLE)
- && currentDcb.fInX && currentDcb.fOutX) {
- flowControl = QSerialPort::SoftwareControl;
- } else if (currentDcb.fOutxCtsFlow && (currentDcb.fRtsControl == RTS_CONTROL_HANDSHAKE)
- && !currentDcb.fInX && !currentDcb.fOutX) {
- flowControl = QSerialPort::HardwareControl;
- } else {
- flowControl = QSerialPort::NoFlowControl;
- }
-}
-
QSerialPort::SerialPortError QSerialPortPrivate::decodeSystemError() const
{
QSerialPort::SerialPortError error;
diff --git a/src/serialport/qserialport_win_p.h b/src/serialport/qserialport_win_p.h
index ad5f267..dd6ae0e 100644
--- a/src/serialport/qserialport_win_p.h
+++ b/src/serialport/qserialport_win_p.h
@@ -83,6 +83,7 @@ public:
bool waitForReadyRead(int msec);
bool waitForBytesWritten(int msec);
+ bool setBaudRate();
bool setBaudRate(qint32 baudRate, QSerialPort::Directions directions);
bool setDataBits(QSerialPort::DataBits dataBits);
bool setParity(QSerialPort::Parity parity);
@@ -144,7 +145,6 @@ private:
bool updateDcb();
bool updateCommTimeouts();
- void detectDefaultSettings();
#ifndef Q_OS_WINCE
bool waitAnyEvent(int msecs, bool *timedOut, HANDLE *triggeredEvent);
diff --git a/src/serialport/qserialport_wince.cpp b/src/serialport/qserialport_wince.cpp
index b987c80..f5b17a2 100644
--- a/src/serialport/qserialport_wince.cpp
+++ b/src/serialport/qserialport_wince.cpp
@@ -224,7 +224,6 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
eventNotifier = new CommEventNotifier(eventMask, this, q);
eventNotifier->start();
- detectDefaultSettings();
return true;
}