diff options
author | Frederik Gladhorn <frederik.gladhorn@digia.com> | 2014-08-12 13:09:58 +0200 |
---|---|---|
committer | Frederik Gladhorn <frederik.gladhorn@digia.com> | 2014-08-12 13:09:58 +0200 |
commit | c6027a9c0e6182bde7106bc97b927c544e76e155 (patch) | |
tree | cb0ee001e8204df9643e04ed4b8961b8eb6c68f5 /src | |
parent | 789c0ebcd4ed976eb124e2da1958a48b478751c0 (diff) | |
parent | 3c3b23b32c6d4a9067363f758aaf4ff9819b7fd8 (diff) |
Merge remote-tracking branch 'origin/5.3' into 5.4
Conflicts:
examples/serialport/examples.qdoc
Change-Id: If79f2f140e72f0a499f8e61e3897848f454a5042
Diffstat (limited to 'src')
-rw-r--r-- | src/serialport/qserialport.h | 2 | ||||
-rw-r--r-- | src/serialport/qserialport_unix.cpp | 79 | ||||
-rw-r--r-- | src/serialport/qserialport_unix_p.h | 1 | ||||
-rw-r--r-- | src/serialport/qserialport_win.cpp | 130 | ||||
-rw-r--r-- | src/serialport/qserialport_win_p.h | 1 | ||||
-rw-r--r-- | src/serialport/qserialport_wince.cpp | 83 | ||||
-rw-r--r-- | src/serialport/qserialport_wince_p.h | 1 | ||||
-rw-r--r-- | src/serialport/qserialportinfo_win.cpp | 26 |
8 files changed, 180 insertions, 143 deletions
diff --git a/src/serialport/qserialport.h b/src/serialport/qserialport.h index 45b9bbff..d0f65b5e 100644 --- a/src/serialport/qserialport.h +++ b/src/serialport/qserialport.h @@ -263,7 +263,9 @@ Q_SIGNALS: void parityChanged(QSerialPort::Parity parity); void stopBitsChanged(QSerialPort::StopBits stopBits); void flowControlChanged(QSerialPort::FlowControl flowControl); +#if QT_DEPRECATED_SINCE(5, 2) void dataErrorPolicyChanged(QSerialPort::DataErrorPolicy policy); +#endif void dataTerminalReadyChanged(bool set); void requestToSendChanged(bool set); void error(QSerialPort::SerialPortError serialPortError); diff --git a/src/serialport/qserialport_unix.cpp b/src/serialport/qserialport_unix.cpp index 29822d51..d555805c 100644 --- a/src/serialport/qserialport_unix.cpp +++ b/src/serialport/qserialport_unix.cpp @@ -204,39 +204,11 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode) return false; } -#ifdef TIOCEXCL - if (::ioctl(descriptor, TIOCEXCL) == -1) - q->setError(decodeSystemError()); -#endif - - if (::tcgetattr(descriptor, &restoredTermios) == -1) { - q->setError(decodeSystemError()); + if (!initialize(mode)) { + qt_safe_close(descriptor); return false; } - currentTermios = restoredTermios; -#ifdef Q_OS_SOLARIS - currentTermios.c_iflag &= ~(IMAXBEL|IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); - currentTermios.c_oflag &= ~OPOST; - currentTermios.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); - currentTermios.c_cflag &= ~(CSIZE|PARENB); - currentTermios.c_cflag |= CS8; -#else - ::cfmakeraw(¤tTermios); -#endif - currentTermios.c_cflag |= CLOCAL; - currentTermios.c_cc[VTIME] = 0; - currentTermios.c_cc[VMIN] = 0; - - if (mode & QIODevice::ReadOnly) - currentTermios.c_cflag |= CREAD; - - if (!updateTermios()) - return false; - - if ((flags & O_WRONLY) == 0) - setReadNotificationEnabled(true); - lockFileScopedPointer.swap(newLockFileScopedPointer); return true; @@ -365,12 +337,7 @@ bool QSerialPortPrivate::setRequestToSend(bool set) bool QSerialPortPrivate::flush() { - return completeAsyncWrite() -#ifndef Q_OS_ANDROID - && (::tcdrain(descriptor) != -1); -#else - && (::ioctl(descriptor, TCSBRK, 1) != -1); -#endif + return completeAsyncWrite(); } bool QSerialPortPrivate::clear(QSerialPort::Directions directions) @@ -869,6 +836,46 @@ bool QSerialPortPrivate::completeAsyncWrite() return startAsyncWrite(); } +inline bool QSerialPortPrivate::initialize(QIODevice::OpenMode mode) +{ + Q_Q(QSerialPort); + +#ifdef TIOCEXCL + if (::ioctl(descriptor, TIOCEXCL) == -1) + q->setError(decodeSystemError()); +#endif + + if (::tcgetattr(descriptor, &restoredTermios) == -1) { + q->setError(decodeSystemError()); + return false; + } + + currentTermios = restoredTermios; +#ifdef Q_OS_SOLARIS + currentTermios.c_iflag &= ~(IMAXBEL|IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); + currentTermios.c_oflag &= ~OPOST; + currentTermios.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); + currentTermios.c_cflag &= ~(CSIZE|PARENB); + currentTermios.c_cflag |= CS8; +#else + ::cfmakeraw(¤tTermios); +#endif + currentTermios.c_cflag |= CLOCAL; + currentTermios.c_cc[VTIME] = 0; + currentTermios.c_cc[VMIN] = 0; + + if (mode & QIODevice::ReadOnly) + currentTermios.c_cflag |= CREAD; + + if (!updateTermios()) + return false; + + if (mode & QIODevice::ReadOnly) + setReadNotificationEnabled(true); + + return true; +} + bool QSerialPortPrivate::updateTermios() { Q_Q(QSerialPort); diff --git a/src/serialport/qserialport_unix_p.h b/src/serialport/qserialport_unix_p.h index 64d1d9ec..ee3d82d5 100644 --- a/src/serialport/qserialport_unix_p.h +++ b/src/serialport/qserialport_unix_p.h @@ -153,6 +153,7 @@ public: QScopedPointer<QLockFile> lockFileScopedPointer; private: + bool initialize(QIODevice::OpenMode mode); bool updateTermios(); QSerialPort::SerialPortError setBaudRate_helper(qint32 baudRate, diff --git a/src/serialport/qserialport_win.cpp b/src/serialport/qserialport_win.cpp index 3432fc94..19f35b74 100644 --- a/src/serialport/qserialport_win.cpp +++ b/src/serialport/qserialport_win.cpp @@ -155,56 +155,11 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode) return false; } - ::ZeroMemory(&restoredDcb, sizeof(restoredDcb)); - restoredDcb.DCBlength = sizeof(restoredDcb); - - if (!::GetCommState(handle, &restoredDcb)) { - q->setError(decodeSystemError()); - return false; - } - - currentDcb = restoredDcb; - currentDcb.fBinary = TRUE; - currentDcb.fInX = FALSE; - currentDcb.fOutX = FALSE; - currentDcb.fAbortOnError = FALSE; - currentDcb.fNull = FALSE; - currentDcb.fErrorChar = FALSE; - - if (currentDcb.fDtrControl == DTR_CONTROL_HANDSHAKE) - currentDcb.fDtrControl = DTR_CONTROL_DISABLE; - - if (!updateDcb()) - return false; - - if (!::GetCommTimeouts(handle, &restoredCommTimeouts)) { - q->setError(decodeSystemError()); - return false; - } - - ::ZeroMemory(¤tCommTimeouts, sizeof(currentCommTimeouts)); - currentCommTimeouts.ReadIntervalTimeout = MAXDWORD; - - if (!updateCommTimeouts()) - return false; - - if (mode & QIODevice::ReadOnly) - readCompletionNotifier->setEnabled(true); - - if (mode & QIODevice::WriteOnly) - writeCompletionNotifier->setEnabled(true); - - if (!::SetCommMask(handle, originalEventMask)) { - q->setError(decodeSystemError()); - return false; - } - - if (!startAsyncCommunication()) - return false; - - communicationNotifier->setEnabled(true); + if (initialize(mode)) + return true; - return true; + ::CloseHandle(handle); + return false; } void QSerialPortPrivate::close() @@ -304,20 +259,7 @@ bool QSerialPortPrivate::setRequestToSend(bool set) bool QSerialPortPrivate::flush() { - Q_Q(QSerialPort); - - bool returnValue = true; - - if (!startAsyncWrite()) - returnValue = false; - - if (!::FlushFileBuffers(handle)) { - q->setError(decodeSystemError()); - returnValue = false; - } - - return returnValue; - + return startAsyncWrite(); } bool QSerialPortPrivate::clear(QSerialPort::Directions directions) @@ -449,8 +391,10 @@ bool QSerialPortPrivate::waitForBytesWritten(int msecs) return false; } - if (triggeredEvent == communicationOverlapped.hEvent - || triggeredEvent == readCompletionOverlapped.hEvent) { + if (triggeredEvent == communicationOverlapped.hEvent) { + if (!_q_completeAsyncCommunication()) + return false; + } else if (triggeredEvent == readCompletionOverlapped.hEvent) { if (!_q_completeAsyncRead()) return false; } else if (triggeredEvent == writeCompletionOverlapped.hEvent) { @@ -741,6 +685,62 @@ void QSerialPortPrivate::handleLineStatusErrors() } } +inline bool QSerialPortPrivate::initialize(QIODevice::OpenMode mode) +{ + Q_Q(QSerialPort); + + ::ZeroMemory(&restoredDcb, sizeof(restoredDcb)); + restoredDcb.DCBlength = sizeof(restoredDcb); + + if (!::GetCommState(handle, &restoredDcb)) { + q->setError(decodeSystemError()); + return false; + } + + currentDcb = restoredDcb; + currentDcb.fBinary = TRUE; + currentDcb.fInX = FALSE; + currentDcb.fOutX = FALSE; + currentDcb.fAbortOnError = FALSE; + currentDcb.fNull = FALSE; + currentDcb.fErrorChar = FALSE; + + if (currentDcb.fDtrControl == DTR_CONTROL_HANDSHAKE) + currentDcb.fDtrControl = DTR_CONTROL_DISABLE; + + if (!updateDcb()) + return false; + + if (!::GetCommTimeouts(handle, &restoredCommTimeouts)) { + q->setError(decodeSystemError()); + return false; + } + + ::ZeroMemory(¤tCommTimeouts, sizeof(currentCommTimeouts)); + currentCommTimeouts.ReadIntervalTimeout = MAXDWORD; + + if (!updateCommTimeouts()) + return false; + + if (mode & QIODevice::ReadOnly) + readCompletionNotifier->setEnabled(true); + + if (mode & QIODevice::WriteOnly) + writeCompletionNotifier->setEnabled(true); + + if (!::SetCommMask(handle, originalEventMask)) { + q->setError(decodeSystemError()); + return false; + } + + if (!startAsyncCommunication()) + return false; + + communicationNotifier->setEnabled(true); + + return true; +} + bool QSerialPortPrivate::updateDcb() { Q_Q(QSerialPort); diff --git a/src/serialport/qserialport_win_p.h b/src/serialport/qserialport_win_p.h index daf57884..a997315b 100644 --- a/src/serialport/qserialport_win_p.h +++ b/src/serialport/qserialport_win_p.h @@ -130,6 +130,7 @@ public: DWORD triggeredEventMask; private: + bool initialize(QIODevice::OpenMode mode); bool updateDcb(); bool updateCommTimeouts(); qint64 handleOverlappedResult(int direction, OVERLAPPED &overlapped); diff --git a/src/serialport/qserialport_wince.cpp b/src/serialport/qserialport_wince.cpp index 4a523ae6..c06e748c 100644 --- a/src/serialport/qserialport_wince.cpp +++ b/src/serialport/qserialport_wince.cpp @@ -219,43 +219,11 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode) return false; } - ::ZeroMemory(&restoredDcb, sizeof(restoredDcb)); - restoredDcb.DCBlength = sizeof(restoredDcb); - - if (!::GetCommState(handle, &restoredDcb)) { - q->setError(decodeSystemError()); - return false; - } - - currentDcb = restoredDcb; - currentDcb.fBinary = true; - currentDcb.fInX = false; - currentDcb.fOutX = false; - currentDcb.fAbortOnError = false; - currentDcb.fNull = false; - currentDcb.fErrorChar = false; - - if (currentDcb.fDtrControl == DTR_CONTROL_HANDSHAKE) - currentDcb.fDtrControl = DTR_CONTROL_DISABLE; - - if (!updateDcb()) - return false; - - if (!::GetCommTimeouts(handle, &restoredCommTimeouts)) { - q->setError(decodeSystemError()); - return false; - } - - ::memset(¤tCommTimeouts, 0, sizeof(currentCommTimeouts)); - currentCommTimeouts.ReadIntervalTimeout = MAXDWORD; - - if (!updateCommTimeouts()) - return false; - - eventNotifier = new CommEventNotifier(eventMask, this, q); - eventNotifier->start(); + if (initialize(eventMask)) + return true; - return true; + ::CloseHandle(handle); + return false; } void QSerialPortPrivate::close() @@ -646,6 +614,49 @@ void QSerialPortPrivate::processIoErrors(bool error) } } +inline bool QSerialPortPrivate::initialize(DWORD eventMask) +{ + Q_Q(QSerialPort); + + ::ZeroMemory(&restoredDcb, sizeof(restoredDcb)); + restoredDcb.DCBlength = sizeof(restoredDcb); + + if (!::GetCommState(handle, &restoredDcb)) { + q->setError(decodeSystemError()); + return false; + } + + currentDcb = restoredDcb; + currentDcb.fBinary = true; + currentDcb.fInX = false; + currentDcb.fOutX = false; + currentDcb.fAbortOnError = false; + currentDcb.fNull = false; + currentDcb.fErrorChar = false; + + if (currentDcb.fDtrControl == DTR_CONTROL_HANDSHAKE) + currentDcb.fDtrControl = DTR_CONTROL_DISABLE; + + if (!updateDcb()) + return false; + + if (!::GetCommTimeouts(handle, &restoredCommTimeouts)) { + q->setError(decodeSystemError()); + return false; + } + + ::memset(¤tCommTimeouts, 0, sizeof(currentCommTimeouts)); + currentCommTimeouts.ReadIntervalTimeout = MAXDWORD; + + if (!updateCommTimeouts()) + return false; + + eventNotifier = new CommEventNotifier(eventMask, this, q); + eventNotifier->start(); + + return true; +} + bool QSerialPortPrivate::updateDcb() { Q_Q(QSerialPort); diff --git a/src/serialport/qserialport_wince_p.h b/src/serialport/qserialport_wince_p.h index 11d05345..9697dc72 100644 --- a/src/serialport/qserialport_wince_p.h +++ b/src/serialport/qserialport_wince_p.h @@ -112,6 +112,7 @@ public: QMutex settingsChangeMutex; private: + bool initialize(DWORD eventMask); bool updateDcb(); bool updateCommTimeouts(); diff --git a/src/serialport/qserialportinfo_win.cpp b/src/serialport/qserialportinfo_win.cpp index dbef3141..8250d2a7 100644 --- a/src/serialport/qserialportinfo_win.cpp +++ b/src/serialport/qserialportinfo_win.cpp @@ -71,6 +71,15 @@ static inline const QList<GuidFlagsPair>& guidFlagsPairs() return guidFlagsPairList; } +static QString toStringAndTrimNullCharacter(const QByteArray &buffer) +{ + QString result = QString::fromWCharArray(reinterpret_cast<const wchar_t *>(buffer.constData()), + buffer.size() / sizeof(wchar_t)); + while (!result.isEmpty() && (result.at(result.size() - 1).unicode() == 0)) + result.chop(1); + return result; +} + static QStringList portNamesFromHardwareDeviceMap() { HKEY hKey = 0; @@ -80,7 +89,8 @@ static QStringList portNamesFromHardwareDeviceMap() QStringList result; DWORD index = 0; static const DWORD maximumValueNameInChars = 16383; - QByteArray outputValueName(maximumValueNameInChars * sizeof(wchar_t), 0); + QByteArray outputValueName; + outputValueName.resize(maximumValueNameInChars * sizeof(wchar_t)); QByteArray outputBuffer; DWORD requiredDataBytes = 0; forever { @@ -90,7 +100,7 @@ static QStringList portNamesFromHardwareDeviceMap() if (ret == ERROR_MORE_DATA) { outputBuffer.resize(requiredDataBytes); } else if (ret == ERROR_SUCCESS) { - result.append(QString::fromWCharArray(reinterpret_cast<const wchar_t *>(outputBuffer.constData()))); + result.append(toStringAndTrimNullCharacter(outputBuffer)); ++index; } else { break; @@ -120,7 +130,7 @@ static QString deviceRegistryProperty(HDEVINFO deviceInfoSet, } devicePropertyByteArray.resize(requiredSize); } - return QString::fromWCharArray(reinterpret_cast<const wchar_t *>(devicePropertyByteArray.constData())); + return toStringAndTrimNullCharacter(devicePropertyByteArray); } static QString deviceInstanceIdentifier(DEVINST deviceInstanceNumber) @@ -130,12 +140,13 @@ static QString deviceInstanceIdentifier(DEVINST deviceInstanceNumber) return QString(); // The size does not include the terminating null character. ++numberOfChars; - QByteArray outputBuffer(numberOfChars * sizeof(wchar_t), 0); + QByteArray outputBuffer; + outputBuffer.resize(numberOfChars * sizeof(wchar_t)); if (::CM_Get_Device_ID(deviceInstanceNumber, reinterpret_cast<wchar_t *>(outputBuffer.data()), outputBuffer.size(), 0) != CR_SUCCESS) { return QString(); } - return QString::fromWCharArray(reinterpret_cast<const wchar_t *>(outputBuffer.constData())); + return toStringAndTrimNullCharacter(outputBuffer); } static DEVINST parentDeviceInstanceNumber(DEVINST childDeviceInstanceNumber) @@ -176,12 +187,15 @@ static QString devicePortName(HDEVINFO deviceInfoSet, PSP_DEVINFO_DATA deviceInf continue; } else if (ret == ERROR_SUCCESS) { if (dataType == REG_SZ) - portName = QString::fromWCharArray((reinterpret_cast<const wchar_t *>(outputBuffer.constData()))); + portName = toStringAndTrimNullCharacter(outputBuffer); else if (dataType == REG_DWORD) portName = QStringLiteral("COM%1").arg(*(PDWORD(outputBuffer.constData()))); } break; } + + if (!portName.isEmpty()) + break; } ::RegCloseKey(key); return portName; |