diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/serialport/qserialport.cpp | 25 | ||||
-rw-r--r-- | src/serialport/qserialport_p.h | 2 | ||||
-rw-r--r-- | src/serialport/qserialport_unix.cpp | 223 | ||||
-rw-r--r-- | src/serialport/qserialport_win.cpp | 205 | ||||
-rw-r--r-- | src/serialport/qserialportinfo.cpp | 8 | ||||
-rw-r--r-- | src/serialport/qserialportinfo_win.cpp | 33 |
6 files changed, 265 insertions, 231 deletions
diff --git a/src/serialport/qserialport.cpp b/src/serialport/qserialport.cpp index 4f3fd046..02a9422b 100644 --- a/src/serialport/qserialport.cpp +++ b/src/serialport/qserialport.cpp @@ -109,7 +109,6 @@ QSerialPortPrivate::QSerialPortPrivate() , startAsyncWriteTimer(0) , originalEventMask(0) , triggeredEventMask(0) - , actualBytesToWrite(0) #elif defined(Q_OS_UNIX) , descriptor(-1) , readNotifier(0) @@ -515,10 +514,6 @@ void QSerialPort::setPort(const QSerialPortInfo &serialPortInfo) \li Removes the prefix "\\\\.\\" or "//./" from the system location and returns the remainder of the string. \row - \li Windows CE - \li Removes the suffix ":" from the system location - and returns the remainder of the string. - \row \li Unix, BSD \li Removes the prefix "/dev/" from the system location and returns the remainder of the string. @@ -568,15 +563,6 @@ bool QSerialPort::open(OpenMode mode) if (!d->open(mode)) return false; - if (!d->setBaudRate() - || !d->setDataBits(d->dataBits) - || !d->setParity(d->parity) - || !d->setStopBits(d->stopBits) - || !d->setFlowControl(d->flowControl)) { - d->close(); - return false; - } - QIODevice::open(mode); return true; } @@ -659,9 +645,9 @@ bool QSerialPort::settingsRestoredOnClose() const after that the opening of the port succeeds. \warning Setting the AllDirections flag is supported on all platforms. - Windows and Windows CE support only this mode. + Windows supports only this mode. - \warning Returns equal baud rate in any direction on Windows, Windows CE. + \warning Returns equal baud rate in any direction on Windows. The default value is Baud9600, i.e. 9600 bits per second. */ @@ -1261,11 +1247,8 @@ qint64 QSerialPort::bytesAvailable() const */ qint64 QSerialPort::bytesToWrite() const { - qint64 bytes = QIODevice::bytesToWrite(); -#ifdef Q_OS_WIN32 - bytes += d_func()->actualBytesToWrite; -#endif - return bytes; + Q_D(const QSerialPort); + return QIODevice::bytesToWrite() + d->writeBuffer.size(); } /*! diff --git a/src/serialport/qserialport_p.h b/src/serialport/qserialport_p.h index 923d5247..10fda340 100644 --- a/src/serialport/qserialport_p.h +++ b/src/serialport/qserialport_p.h @@ -204,6 +204,7 @@ public: COMMTIMEOUTS restoredCommTimeouts; HANDLE handle; QByteArray readChunkBuffer; + QByteArray writeChunkBuffer; bool communicationStarted; bool writeStarted; bool readStarted; @@ -214,7 +215,6 @@ public: OVERLAPPED writeCompletionOverlapped; DWORD originalEventMask; DWORD triggeredEventMask; - qint64 actualBytesToWrite; #elif defined(Q_OS_UNIX) diff --git a/src/serialport/qserialport_unix.cpp b/src/serialport/qserialport_unix.cpp index acc53c80..8e06adac 100644 --- a/src/serialport/qserialport_unix.cpp +++ b/src/serialport/qserialport_unix.cpp @@ -193,6 +193,120 @@ private: QSerialPortPrivate *dptr; }; +static inline void qt_set_common_props(termios *tio, QIODevice::OpenMode m) +{ +#ifdef Q_OS_SOLARIS + tio->c_iflag &= ~(IMAXBEL|IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); + tio->c_oflag &= ~OPOST; + tio->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); + tio->c_cflag &= ~(CSIZE|PARENB); + tio->c_cflag |= CS8; +#else + ::cfmakeraw(tio); +#endif + + tio->c_cflag |= CLOCAL; + tio->c_cc[VTIME] = 0; + tio->c_cc[VMIN] = 0; + + if (m & QIODevice::ReadOnly) + tio->c_cflag |= CREAD; +} + +static inline void qt_set_databits(termios *tio, QSerialPort::DataBits databits) +{ + tio->c_cflag &= ~CSIZE; + switch (databits) { + case QSerialPort::Data5: + tio->c_cflag |= CS5; + break; + case QSerialPort::Data6: + tio->c_cflag |= CS6; + break; + case QSerialPort::Data7: + tio->c_cflag |= CS7; + break; + case QSerialPort::Data8: + tio->c_cflag |= CS8; + break; + default: + tio->c_cflag |= CS8; + break; + } +} + +static inline void qt_set_parity(termios *tio, QSerialPort::Parity parity) +{ + tio->c_iflag &= ~(PARMRK | INPCK); + tio->c_iflag |= IGNPAR; + + switch (parity) { + +#ifdef CMSPAR + // Here Installation parity only for GNU/Linux where the macro CMSPAR. + case QSerialPort::SpaceParity: + tio->c_cflag &= ~PARODD; + tio->c_cflag |= PARENB | CMSPAR; + break; + case QSerialPort::MarkParity: + tio->c_cflag |= PARENB | CMSPAR | PARODD; + break; +#endif + case QSerialPort::NoParity: + tio->c_cflag &= ~PARENB; + break; + case QSerialPort::EvenParity: + tio->c_cflag &= ~PARODD; + tio->c_cflag |= PARENB; + break; + case QSerialPort::OddParity: + tio->c_cflag |= PARENB | PARODD; + break; + default: + tio->c_cflag |= PARENB; + tio->c_iflag |= PARMRK | INPCK; + tio->c_iflag &= ~IGNPAR; + break; + } +} + +static inline void qt_set_stopbits(termios *tio, QSerialPort::StopBits stopbits) +{ + switch (stopbits) { + case QSerialPort::OneStop: + tio->c_cflag &= ~CSTOPB; + break; + case QSerialPort::TwoStop: + tio->c_cflag |= CSTOPB; + break; + default: + tio->c_cflag &= ~CSTOPB; + break; + } +} + +static inline void qt_set_flowcontrol(termios *tio, QSerialPort::FlowControl flowcontrol) +{ + switch (flowcontrol) { + case QSerialPort::NoFlowControl: + tio->c_cflag &= ~CRTSCTS; + tio->c_iflag &= ~(IXON | IXOFF | IXANY); + break; + case QSerialPort::HardwareControl: + tio->c_cflag |= CRTSCTS; + tio->c_iflag &= ~(IXON | IXOFF | IXANY); + break; + case QSerialPort::SoftwareControl: + tio->c_cflag &= ~CRTSCTS; + tio->c_iflag |= IXON | IXOFF | IXANY; + break; + default: + tio->c_cflag &= ~CRTSCTS; + tio->c_iflag &= ~(IXON | IXOFF | IXANY); + break; + } +} + bool QSerialPortPrivate::open(QIODevice::OpenMode mode) { QString lockFilePath = serialPortLockFilePath(QSerialPortInfoPrivate::portNameFromSystemLocation(systemLocation)); @@ -605,24 +719,8 @@ bool QSerialPortPrivate::setDataBits(QSerialPort::DataBits dataBits) if (!getTermios(&tio)) return false; - tio.c_cflag &= ~CSIZE; - switch (dataBits) { - case QSerialPort::Data5: - tio.c_cflag |= CS5; - break; - case QSerialPort::Data6: - tio.c_cflag |= CS6; - break; - case QSerialPort::Data7: - tio.c_cflag |= CS7; - break; - case QSerialPort::Data8: - tio.c_cflag |= CS8; - break; - default: - tio.c_cflag |= CS8; - break; - } + qt_set_databits(&tio, dataBits); + return setTermios(&tio); } @@ -632,37 +730,7 @@ bool QSerialPortPrivate::setParity(QSerialPort::Parity parity) if (!getTermios(&tio)) return false; - tio.c_iflag &= ~(PARMRK | INPCK); - tio.c_iflag |= IGNPAR; - - switch (parity) { - -#ifdef CMSPAR - // Here Installation parity only for GNU/Linux where the macro CMSPAR. - case QSerialPort::SpaceParity: - tio.c_cflag &= ~PARODD; - tio.c_cflag |= PARENB | CMSPAR; - break; - case QSerialPort::MarkParity: - tio.c_cflag |= PARENB | CMSPAR | PARODD; - break; -#endif - case QSerialPort::NoParity: - tio.c_cflag &= ~PARENB; - break; - case QSerialPort::EvenParity: - tio.c_cflag &= ~PARODD; - tio.c_cflag |= PARENB; - break; - case QSerialPort::OddParity: - tio.c_cflag |= PARENB | PARODD; - break; - default: - tio.c_cflag |= PARENB; - tio.c_iflag |= PARMRK | INPCK; - tio.c_iflag &= ~IGNPAR; - break; - } + qt_set_parity(&tio, parity); return setTermios(&tio); } @@ -673,17 +741,8 @@ bool QSerialPortPrivate::setStopBits(QSerialPort::StopBits stopBits) if (!getTermios(&tio)) return false; - switch (stopBits) { - case QSerialPort::OneStop: - tio.c_cflag &= ~CSTOPB; - break; - case QSerialPort::TwoStop: - tio.c_cflag |= CSTOPB; - break; - default: - tio.c_cflag &= ~CSTOPB; - break; - } + qt_set_stopbits(&tio, stopBits); + return setTermios(&tio); } @@ -693,24 +752,8 @@ bool QSerialPortPrivate::setFlowControl(QSerialPort::FlowControl flowControl) if (!getTermios(&tio)) return false; - switch (flowControl) { - case QSerialPort::NoFlowControl: - tio.c_cflag &= ~CRTSCTS; - tio.c_iflag &= ~(IXON | IXOFF | IXANY); - break; - case QSerialPort::HardwareControl: - tio.c_cflag |= CRTSCTS; - tio.c_iflag &= ~(IXON | IXOFF | IXANY); - break; - case QSerialPort::SoftwareControl: - tio.c_cflag &= ~CRTSCTS; - tio.c_iflag |= IXON | IXOFF | IXANY; - break; - default: - tio.c_cflag &= ~CRTSCTS; - tio.c_iflag &= ~(IXON | IXOFF | IXANY); - break; - } + qt_set_flowcontrol(&tio, flowControl); + return setTermios(&tio); } @@ -825,25 +868,19 @@ inline bool QSerialPortPrivate::initialize(QIODevice::OpenMode mode) return false; restoredTermios = tio; -#ifdef Q_OS_SOLARIS - tio.c_iflag &= ~(IMAXBEL|IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); - tio.c_oflag &= ~OPOST; - tio.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); - tio.c_cflag &= ~(CSIZE|PARENB); - tio.c_cflag |= CS8; -#else - ::cfmakeraw(&tio); -#endif - tio.c_cflag |= CLOCAL; - tio.c_cc[VTIME] = 0; - tio.c_cc[VMIN] = 0; - if (mode & QIODevice::ReadOnly) - tio.c_cflag |= CREAD; + qt_set_common_props(&tio, mode); + qt_set_databits(&tio, dataBits); + qt_set_parity(&tio, parity); + qt_set_stopbits(&tio, stopBits); + qt_set_flowcontrol(&tio, flowControl); if (!setTermios(&tio)) return false; + if (!setBaudRate()) + return false; + if (mode & QIODevice::ReadOnly) setReadNotificationEnabled(true); @@ -852,7 +889,7 @@ inline bool QSerialPortPrivate::initialize(QIODevice::OpenMode mode) qint64 QSerialPortPrivate::writeData(const char *data, qint64 maxSize) { - ::memcpy(writeBuffer.reserve(maxSize), data, maxSize); + writeBuffer.append(data, maxSize); if (!writeBuffer.isEmpty() && !isWriteNotificationEnabled()) setWriteNotificationEnabled(true); return maxSize; diff --git a/src/serialport/qserialport_win.cpp b/src/serialport/qserialport_win.cpp index 7cbb81d4..9ab44487 100644 --- a/src/serialport/qserialport_win.cpp +++ b/src/serialport/qserialport_win.cpp @@ -81,6 +81,94 @@ QT_BEGIN_NAMESPACE +static inline void qt_set_common_props(DCB *dcb) +{ + dcb->fBinary = TRUE; + dcb->fAbortOnError = FALSE; + dcb->fNull = FALSE; + dcb->fErrorChar = FALSE; + + if (dcb->fDtrControl == DTR_CONTROL_HANDSHAKE) + dcb->fDtrControl = DTR_CONTROL_DISABLE; +} + +static inline void qt_set_baudrate(DCB *dcb, qint32 baudrate) +{ + dcb->BaudRate = baudrate; +} + +static inline void qt_set_databits(DCB *dcb, QSerialPort::DataBits databits) +{ + dcb->ByteSize = databits; +} + +static inline void qt_set_parity(DCB *dcb, QSerialPort::Parity parity) +{ + dcb->fParity = TRUE; + switch (parity) { + case QSerialPort::NoParity: + dcb->Parity = NOPARITY; + dcb->fParity = FALSE; + break; + case QSerialPort::OddParity: + dcb->Parity = ODDPARITY; + break; + case QSerialPort::EvenParity: + dcb->Parity = EVENPARITY; + break; + case QSerialPort::MarkParity: + dcb->Parity = MARKPARITY; + break; + case QSerialPort::SpaceParity: + dcb->Parity = SPACEPARITY; + break; + default: + dcb->Parity = NOPARITY; + dcb->fParity = FALSE; + break; + } +} + +static inline void qt_set_stopbits(DCB *dcb, QSerialPort::StopBits stopbits) +{ + switch (stopbits) { + case QSerialPort::OneStop: + dcb->StopBits = ONESTOPBIT; + break; + case QSerialPort::OneAndHalfStop: + dcb->StopBits = ONE5STOPBITS; + break; + case QSerialPort::TwoStop: + dcb->StopBits = TWOSTOPBITS; + break; + default: + dcb->StopBits = ONESTOPBIT; + break; + } +} + +static inline void qt_set_flowcontrol(DCB *dcb, QSerialPort::FlowControl flowcontrol) +{ + dcb->fInX = FALSE; + dcb->fOutX = FALSE; + dcb->fOutxCtsFlow = FALSE; + dcb->fRtsControl = RTS_CONTROL_DISABLE; + switch (flowcontrol) { + case QSerialPort::NoFlowControl: + break; + case QSerialPort::SoftwareControl: + dcb->fInX = TRUE; + dcb->fOutX = TRUE; + break; + case QSerialPort::HardwareControl: + dcb->fOutxCtsFlow = TRUE; + dcb->fRtsControl = RTS_CONTROL_HANDSHAKE; + break; + default: + break; + } +} + bool QSerialPortPrivate::open(QIODevice::OpenMode mode) { DWORD desiredAccess = 0; @@ -122,7 +210,6 @@ void QSerialPortPrivate::close() readStarted = false; writeStarted = false; writeBuffer.clear(); - actualBytesToWrite = 0; if (settingsRestoredOnClose) { ::SetCommState(handle, &restoredDcb); @@ -204,10 +291,8 @@ bool QSerialPortPrivate::clear(QSerialPort::Directions directions) DWORD flags = 0; if (directions & QSerialPort::Input) flags |= PURGE_RXABORT | PURGE_RXCLEAR; - if (directions & QSerialPort::Output) { + if (directions & QSerialPort::Output) flags |= PURGE_TXABORT | PURGE_TXCLEAR; - actualBytesToWrite = 0; - } if (!::PurgeComm(handle, flags)) { setError(getSystemError()); return false; @@ -281,7 +366,7 @@ bool QSerialPortPrivate::waitForReadyRead(int msecs) bool QSerialPortPrivate::waitForBytesWritten(int msecs) { - if (writeBuffer.isEmpty()) + if (writeBuffer.isEmpty() && writeChunkBuffer.isEmpty()) return false; if (!writeStarted && !_q_startAsyncWrite()) @@ -319,7 +404,8 @@ bool QSerialPortPrivate::setBaudRate(qint32 baudRate, QSerialPort::Directions di if (!getDcb(&dcb)) return false; - dcb.BaudRate = baudRate; + qt_set_baudrate(&dcb, baudRate); + return setDcb(&dcb); } @@ -329,7 +415,8 @@ bool QSerialPortPrivate::setDataBits(QSerialPort::DataBits dataBits) if (!getDcb(&dcb)) return false; - dcb.ByteSize = dataBits; + qt_set_databits(&dcb, dataBits); + return setDcb(&dcb); } @@ -339,29 +426,8 @@ bool QSerialPortPrivate::setParity(QSerialPort::Parity parity) if (!getDcb(&dcb)) return false; - dcb.fParity = TRUE; - switch (parity) { - case QSerialPort::NoParity: - dcb.Parity = NOPARITY; - dcb.fParity = FALSE; - break; - case QSerialPort::OddParity: - dcb.Parity = ODDPARITY; - break; - case QSerialPort::EvenParity: - dcb.Parity = EVENPARITY; - break; - case QSerialPort::MarkParity: - dcb.Parity = MARKPARITY; - break; - case QSerialPort::SpaceParity: - dcb.Parity = SPACEPARITY; - break; - default: - dcb.Parity = NOPARITY; - dcb.fParity = FALSE; - break; - } + qt_set_parity(&dcb, parity); + return setDcb(&dcb); } @@ -371,20 +437,8 @@ bool QSerialPortPrivate::setStopBits(QSerialPort::StopBits stopBits) if (!getDcb(&dcb)) return false; - switch (stopBits) { - case QSerialPort::OneStop: - dcb.StopBits = ONESTOPBIT; - break; - case QSerialPort::OneAndHalfStop: - dcb.StopBits = ONE5STOPBITS; - break; - case QSerialPort::TwoStop: - dcb.StopBits = TWOSTOPBITS; - break; - default: - dcb.StopBits = ONESTOPBIT; - break; - } + qt_set_stopbits(&dcb, stopBits); + return setDcb(&dcb); } @@ -394,24 +448,8 @@ bool QSerialPortPrivate::setFlowControl(QSerialPort::FlowControl flowControl) 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: - dcb.fInX = TRUE; - dcb.fOutX = TRUE; - break; - case QSerialPort::HardwareControl: - dcb.fOutxCtsFlow = TRUE; - dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; - break; - default: - break; - } + qt_set_flowcontrol(&dcb, flowControl); + return setDcb(&dcb); } @@ -431,10 +469,8 @@ bool QSerialPortPrivate::completeAsyncRead(qint64 bytesTransferred) readStarted = false; return false; } - if (bytesTransferred > 0) { - char *ptr = buffer.reserve(bytesTransferred); - ::memcpy(ptr, readChunkBuffer.constData(), bytesTransferred); - } + if (bytesTransferred > 0) + buffer.append(readChunkBuffer.constData(), bytesTransferred); readStarted = false; @@ -459,12 +495,13 @@ bool QSerialPortPrivate::completeAsyncWrite(qint64 bytesTransferred) if (writeStarted) { if (bytesTransferred == qint64(-1)) { + writeChunkBuffer.clear(); writeStarted = false; return false; - } else if (bytesTransferred > 0) { - writeBuffer.free(bytesTransferred); - emit q->bytesWritten(bytesTransferred); } + Q_ASSERT(bytesTransferred == writeChunkBuffer.size()); + writeChunkBuffer.clear(); + emit q->bytesWritten(bytesTransferred); writeStarted = false; } @@ -531,10 +568,10 @@ bool QSerialPortPrivate::_q_startAsyncWrite() if (writeBuffer.isEmpty() || writeStarted) return true; - const int writeBytes = writeBuffer.nextDataBlockSize(); + writeChunkBuffer = writeBuffer.read(); ::ZeroMemory(&writeCompletionOverlapped, sizeof(writeCompletionOverlapped)); - if (!::WriteFile(handle, writeBuffer.readPointer(), - writeBytes, nullptr, &writeCompletionOverlapped)) { + if (!::WriteFile(handle, writeChunkBuffer.constData(), + writeChunkBuffer.size(), nullptr, &writeCompletionOverlapped)) { QSerialPortErrorInfo error = getSystemError(); if (error.errorCode != QSerialPort::NoError) { @@ -545,7 +582,6 @@ bool QSerialPortPrivate::_q_startAsyncWrite() } } - actualBytesToWrite -= writeBytes; writeStarted = true; return true; } @@ -579,8 +615,7 @@ qint64 QSerialPortPrivate::writeData(const char *data, qint64 maxSize) { Q_Q(QSerialPort); - ::memcpy(writeBuffer.reserve(maxSize), data, maxSize); - actualBytesToWrite += maxSize; + writeBuffer.append(data, maxSize); if (!writeBuffer.isEmpty() && !writeStarted) { if (!startAsyncWriteTimer) { @@ -588,7 +623,8 @@ qint64 QSerialPortPrivate::writeData(const char *data, qint64 maxSize) QObjectPrivate::connect(startAsyncWriteTimer, &QTimer::timeout, this, &QSerialPortPrivate::_q_startAsyncWrite); startAsyncWriteTimer->setSingleShot(true); } - startAsyncWriteTimer->start(0); + if (!startAsyncWriteTimer->isActive()) + startAsyncWriteTimer->start(); } return maxSize; } @@ -623,17 +659,12 @@ inline bool QSerialPortPrivate::initialize() restoredDcb = dcb; - dcb.fBinary = TRUE; - dcb.fInX = FALSE; - dcb.fOutX = FALSE; - dcb.fAbortOnError = FALSE; - dcb.fNull = FALSE; - dcb.fErrorChar = FALSE; - - if (dcb.fDtrControl == DTR_CONTROL_HANDSHAKE) - dcb.fDtrControl = DTR_CONTROL_DISABLE; - - dcb.BaudRate = inputBaudRate; + qt_set_common_props(&dcb); + qt_set_baudrate(&dcb, inputBaudRate); + qt_set_databits(&dcb, dataBits); + qt_set_parity(&dcb, parity); + qt_set_stopbits(&dcb, stopBits); + qt_set_flowcontrol(&dcb, flowControl); if (!setDcb(&dcb)) return false; diff --git a/src/serialport/qserialportinfo.cpp b/src/serialport/qserialportinfo.cpp index 2b69966f..d978665d 100644 --- a/src/serialport/qserialportinfo.cpp +++ b/src/serialport/qserialportinfo.cpp @@ -87,14 +87,8 @@ QSerialPortInfo::QSerialPortInfo(const QSerialPortInfo &other) Constructs a QSerialPortInfo object from serial \a port. */ QSerialPortInfo::QSerialPortInfo(const QSerialPort &port) + : QSerialPortInfo(port.portName()) { - const auto infos = QSerialPortInfo::availablePorts(); - for (const QSerialPortInfo &info : infos) { - if (port.portName() == info.portName()) { - *this = info; - break; - } - } } /*! diff --git a/src/serialport/qserialportinfo_win.cpp b/src/serialport/qserialportinfo_win.cpp index 398666e5..1bab3e9c 100644 --- a/src/serialport/qserialportinfo_win.cpp +++ b/src/serialport/qserialportinfo_win.cpp @@ -187,23 +187,6 @@ static QString devicePortName(HDEVINFO deviceInfoSet, PSP_DEVINFO_DATA deviceInf return portName; } -class SerialPortNameEqualFunctor -{ -public: - explicit SerialPortNameEqualFunctor(const QString &serialPortName) - : m_serialPortName(serialPortName) - { - } - - bool operator() (const QSerialPortInfo &serialPortInfo) const - { - return serialPortInfo.portName() == m_serialPortName; - } - -private: - const QString &m_serialPortName; -}; - static QString deviceDescription(HDEVINFO deviceInfoSet, PSP_DEVINFO_DATA deviceInfoData) { @@ -288,6 +271,15 @@ static QString deviceSerialNumber(QString instanceIdentifier, return QString(); } +static bool anyOfPorts(const QList<QSerialPortInfo> &ports, const QString &portName) +{ + const auto end = ports.end(); + auto isPortNamesEquals = [&portName](const QSerialPortInfo &portInfo) { + return portInfo.portName() == portName; + }; + return std::find_if(ports.begin(), end, isPortNamesEquals) != end; +} + QList<QSerialPortInfo> QSerialPortInfo::availablePorts() { static const struct { @@ -318,10 +310,8 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts() if (portName.isEmpty() || portName.contains(QLatin1String("LPT"))) continue; - if (std::find_if(serialPortInfoList.begin(), serialPortInfoList.end(), - SerialPortNameEqualFunctor(portName)) != serialPortInfoList.end()) { + if (anyOfPorts(serialPortInfoList, portName)) continue; - } QSerialPortInfoPrivate priv; @@ -346,8 +336,7 @@ QList<QSerialPortInfo> QSerialPortInfo::availablePorts() const auto portNames = portNamesFromHardwareDeviceMap(); for (const QString &portName : portNames) { - if (std::find_if(serialPortInfoList.cbegin(), serialPortInfoList.cend(), - SerialPortNameEqualFunctor(portName)) == serialPortInfoList.cend()) { + if (!anyOfPorts(serialPortInfoList, portName)) { QSerialPortInfoPrivate priv; priv.portName = portName; priv.device = QSerialPortInfoPrivate::portNameToSystemLocation(portName); |