diff options
author | Denis Shienkov <denis.shienkov@gmail.com> | 2015-09-06 16:46:07 +0300 |
---|---|---|
committer | Denis Shienkov <denis.shienkov@gmail.com> | 2015-09-07 13:11:50 +0000 |
commit | 42fd000309fe72f0209b6432144bc1f3e6b2add2 (patch) | |
tree | e0f1f50dea1adfb197d698515f17bf063ce0981e /src | |
parent | 3abd3ed07d2b30ecb2f8b9772ed5b3811bb9194b (diff) | |
download | qtserialport-42fd000309fe72f0209b6432144bc1f3e6b2add2.tar.gz |
Get rid of stored DCB structure
It is better to read the current DCB structure each
time before changing, than to have stored it locally.
Change-Id: I487931469112d657c12d99a74aa577921c042f35
Reviewed-by: Sergey Belyashov <Sergey.Belyashov@gmail.com>
Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/serialport/qserialport_p.h | 4 | ||||
-rw-r--r-- | src/serialport/qserialport_win.cpp | 132 |
2 files changed, 85 insertions, 51 deletions
diff --git a/src/serialport/qserialport_p.h b/src/serialport/qserialport_p.h index cf8cb79..e626723 100644 --- a/src/serialport/qserialport_p.h +++ b/src/serialport/qserialport_p.h @@ -208,7 +208,8 @@ public: #elif defined(Q_OS_WIN32) bool initialize(); - bool updateDcb(); + bool setDcb(DCB *dcb); + bool getDcb(DCB *dcb); bool updateCommTimeouts(); void handleLineStatusErrors(); OVERLAPPED *waitForNotified(int msecs); @@ -225,7 +226,6 @@ public: bool emulateErrorPolicy(); void emitReadyRead(); - DCB currentDcb; DCB restoredDcb; COMMTIMEOUTS currentCommTimeouts; COMMTIMEOUTS restoredCommTimeouts; diff --git a/src/serialport/qserialport_win.cpp b/src/serialport/qserialport_win.cpp index 1f228c7..540b169 100644 --- a/src/serialport/qserialport_win.cpp +++ b/src/serialport/qserialport_win.cpp @@ -181,8 +181,12 @@ bool QSerialPortPrivate::setDataTerminalReady(bool set) return false; } - currentDcb.fDtrControl = set ? DTR_CONTROL_ENABLE : DTR_CONTROL_DISABLE; - return true; + DCB dcb; + if (!getDcb(&dcb)) + return false; + + dcb.fDtrControl = set ? DTR_CONTROL_ENABLE : DTR_CONTROL_DISABLE; + return setDcb(&dcb); } bool QSerialPortPrivate::setRequestToSend(bool set) @@ -313,84 +317,105 @@ bool QSerialPortPrivate::setBaudRate(qint32 baudRate, QSerialPort::Directions di setError(QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError, QSerialPort::tr("Custom baud rate direction is unsupported"))); return false; } - currentDcb.BaudRate = baudRate; - return updateDcb(); + + DCB dcb; + if (!getDcb(&dcb)) + return false; + + dcb.BaudRate = baudRate; + return setDcb(&dcb); } bool QSerialPortPrivate::setDataBits(QSerialPort::DataBits dataBits) { - currentDcb.ByteSize = dataBits; - return updateDcb(); + DCB dcb; + if (!getDcb(&dcb)) + return false; + + dcb.ByteSize = dataBits; + return setDcb(&dcb); } bool QSerialPortPrivate::setParity(QSerialPort::Parity parity) { - currentDcb.fParity = TRUE; + DCB dcb; + if (!getDcb(&dcb)) + return false; + + dcb.fParity = TRUE; switch (parity) { case QSerialPort::NoParity: - currentDcb.Parity = NOPARITY; - currentDcb.fParity = FALSE; + dcb.Parity = NOPARITY; + dcb.fParity = FALSE; break; case QSerialPort::OddParity: - currentDcb.Parity = ODDPARITY; + dcb.Parity = ODDPARITY; break; case QSerialPort::EvenParity: - currentDcb.Parity = EVENPARITY; + dcb.Parity = EVENPARITY; break; case QSerialPort::MarkParity: - currentDcb.Parity = MARKPARITY; + dcb.Parity = MARKPARITY; break; case QSerialPort::SpaceParity: - currentDcb.Parity = SPACEPARITY; + dcb.Parity = SPACEPARITY; break; default: - currentDcb.Parity = NOPARITY; - currentDcb.fParity = FALSE; + dcb.Parity = NOPARITY; + dcb.fParity = FALSE; break; } - return updateDcb(); + return setDcb(&dcb); } bool QSerialPortPrivate::setStopBits(QSerialPort::StopBits stopBits) { + DCB dcb; + if (!getDcb(&dcb)) + return false; + switch (stopBits) { case QSerialPort::OneStop: - currentDcb.StopBits = ONESTOPBIT; + dcb.StopBits = ONESTOPBIT; break; case QSerialPort::OneAndHalfStop: - currentDcb.StopBits = ONE5STOPBITS; + dcb.StopBits = ONE5STOPBITS; break; case QSerialPort::TwoStop: - currentDcb.StopBits = TWOSTOPBITS; + dcb.StopBits = TWOSTOPBITS; break; default: - currentDcb.StopBits = ONESTOPBIT; + dcb.StopBits = ONESTOPBIT; break; } - return updateDcb(); + return setDcb(&dcb); } bool QSerialPortPrivate::setFlowControl(QSerialPort::FlowControl flowControl) { - currentDcb.fInX = FALSE; - currentDcb.fOutX = FALSE; - currentDcb.fOutxCtsFlow = FALSE; - currentDcb.fRtsControl = RTS_CONTROL_DISABLE; + DCB dcb; + if (!getDcb(&dcb)) + return false; + + dcb.fInX = FALSE; + dcb.fOutX = FALSE; + dcb.fOutxCtsFlow = FALSE; + dcb.fRtsControl = RTS_CONTROL_DISABLE; switch (flowControl) { case QSerialPort::NoFlowControl: break; case QSerialPort::SoftwareControl: - currentDcb.fInX = TRUE; - currentDcb.fOutX = TRUE; + dcb.fInX = TRUE; + dcb.fOutX = TRUE; break; case QSerialPort::HardwareControl: - currentDcb.fOutxCtsFlow = TRUE; - currentDcb.fRtsControl = RTS_CONTROL_HANDSHAKE; + dcb.fOutxCtsFlow = TRUE; + dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; break; default: break; } - return updateDcb(); + return setDcb(&dcb); } bool QSerialPortPrivate::setDataErrorPolicy(QSerialPort::DataErrorPolicy policy) @@ -638,28 +663,25 @@ inline bool QSerialPortPrivate::initialize() { Q_Q(QSerialPort); - ::ZeroMemory(&restoredDcb, sizeof(restoredDcb)); - restoredDcb.DCBlength = sizeof(restoredDcb); - - if (!::GetCommState(handle, &restoredDcb)) { - setError(getSystemError()); + DCB dcb; + if (!getDcb(&dcb)) return false; - } - currentDcb = restoredDcb; - currentDcb.fBinary = TRUE; - currentDcb.fInX = FALSE; - currentDcb.fOutX = FALSE; - currentDcb.fAbortOnError = FALSE; - currentDcb.fNull = FALSE; - currentDcb.fErrorChar = FALSE; + restoredDcb = dcb; - if (currentDcb.fDtrControl == DTR_CONTROL_HANDSHAKE) - currentDcb.fDtrControl = DTR_CONTROL_DISABLE; + dcb.fBinary = TRUE; + dcb.fInX = FALSE; + dcb.fOutX = FALSE; + dcb.fAbortOnError = FALSE; + dcb.fNull = FALSE; + dcb.fErrorChar = FALSE; - currentDcb.BaudRate = inputBaudRate; + if (dcb.fDtrControl == DTR_CONTROL_HANDSHAKE) + dcb.fDtrControl = DTR_CONTROL_DISABLE; - if (!updateDcb()) + dcb.BaudRate = inputBaudRate; + + if (!setDcb(&dcb)) return false; if (!::GetCommTimeouts(handle, &restoredCommTimeouts)) { @@ -690,9 +712,21 @@ inline bool QSerialPortPrivate::initialize() return true; } -bool QSerialPortPrivate::updateDcb() +bool QSerialPortPrivate::setDcb(DCB *dcb) { - if (!::SetCommState(handle, ¤tDcb)) { + if (!::SetCommState(handle, dcb)) { + setError(getSystemError()); + return false; + } + return true; +} + +bool QSerialPortPrivate::getDcb(DCB *dcb) +{ + ::ZeroMemory(dcb, sizeof(DCB)); + dcb->DCBlength = sizeof(DCB); + + if (!::GetCommState(handle, dcb)) { setError(getSystemError()); return false; } |