summaryrefslogtreecommitdiff
path: root/src/serialport/qserialport_win.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/serialport/qserialport_win.cpp')
-rw-r--r--src/serialport/qserialport_win.cpp111
1 files changed, 23 insertions, 88 deletions
diff --git a/src/serialport/qserialport_win.cpp b/src/serialport/qserialport_win.cpp
index 87f9e7b..7ae40d9 100644
--- a/src/serialport/qserialport_win.cpp
+++ b/src/serialport/qserialport_win.cpp
@@ -40,12 +40,12 @@
****************************************************************************/
#include "qserialport_p.h"
+#include "qwinoverlappedionotifier_p.h"
#include <QtCore/qcoreevent.h>
#include <QtCore/qelapsedtimer.h>
#include <QtCore/qvector.h>
#include <QtCore/qtimer.h>
-#include <private/qwinoverlappedionotifier_p.h>
#include <algorithm>
#ifndef CTL_CODE
@@ -176,12 +176,9 @@ static inline void qt_set_flowcontrol(DCB *dcb, QSerialPort::FlowControl flowcon
bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
{
DWORD desiredAccess = 0;
- originalEventMask = 0;
- if (mode & QIODevice::ReadOnly) {
+ if (mode & QIODevice::ReadOnly)
desiredAccess |= GENERIC_READ;
- originalEventMask |= EV_RXCHAR;
- }
if (mode & QIODevice::WriteOnly)
desiredAccess |= GENERIC_WRITE;
@@ -193,7 +190,7 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
return false;
}
- if (initialize())
+ if (initialize(mode))
return true;
::CloseHandle(handle);
@@ -358,7 +355,7 @@ bool QSerialPortPrivate::waitForReadyRead(int msecs)
if (overlapped == &readCompletionOverlapped) {
const qint64 readBytesForOneReadOperation = qint64(buffer.size()) - currentReadBufferSize;
- if (readBytesForOneReadOperation == ReadChunkSize) {
+ if (readBytesForOneReadOperation == QSERIALPORT_BUFFERSIZE) {
currentReadBufferSize = buffer.size();
} else if (readBytesForOneReadOperation == 0) {
if (initialReadBufferSize != currentReadBufferSize)
@@ -484,11 +481,10 @@ bool QSerialPortPrivate::completeAsyncRead(qint64 bytesTransferred)
readStarted = false;
bool result = true;
- if (bytesTransferred == ReadChunkSize
+ if (bytesTransferred == QSERIALPORT_BUFFERSIZE
|| queuedBytesCount(QSerialPort::Input) > 0) {
result = startAsyncRead();
- } else if (readBufferMaxSize == 0
- || readBufferMaxSize > buffer.size()) {
+ } else {
result = startAsyncCommunication();
}
@@ -541,7 +537,7 @@ bool QSerialPortPrivate::startAsyncRead()
if (readStarted)
return true;
- qint64 bytesToRead = ReadChunkSize;
+ qint64 bytesToRead = QSERIALPORT_BUFFERSIZE;
if (readBufferMaxSize && bytesToRead > (readBufferMaxSize - buffer.size())) {
bytesToRead = readBufferMaxSize - buffer.size();
@@ -552,6 +548,8 @@ bool QSerialPortPrivate::startAsyncRead()
}
}
+ Q_ASSERT(int(bytesToRead) <= readChunkBuffer.size());
+
::ZeroMemory(&readCompletionOverlapped, sizeof(readCompletionOverlapped));
if (::ReadFile(handle, readChunkBuffer.data(), bytesToRead, nullptr, &readCompletionOverlapped)) {
readStarted = true;
@@ -658,7 +656,7 @@ qint64 QSerialPortPrivate::queuedBytesCount(QSerialPort::Direction direction) co
: ((direction == QSerialPort::Output) ? comstat.cbOutQue : -1);
}
-inline bool QSerialPortPrivate::initialize()
+inline bool QSerialPortPrivate::initialize(QIODevice::OpenMode mode)
{
Q_Q(QSerialPort);
@@ -691,20 +689,21 @@ inline bool QSerialPortPrivate::initialize()
return false;
}
- if (!::SetCommMask(handle, originalEventMask)) {
+ const DWORD eventMask = (mode & QIODevice::ReadOnly) ? EV_RXCHAR : 0;
+ if (!::SetCommMask(handle, eventMask)) {
setError(getSystemError());
return false;
}
+ if ((eventMask & EV_RXCHAR) && !startAsyncCommunication())
+ return false;
+
notifier = new QWinOverlappedIoNotifier(q);
QObjectPrivate::connect(notifier, &QWinOverlappedIoNotifier::notified,
this, &QSerialPortPrivate::_q_notified);
notifier->setHandle(handle);
notifier->setEnabled(true);
- if ((originalEventMask & EV_RXCHAR) && !startAsyncCommunication())
- return false;
-
return true;
}
@@ -785,80 +784,16 @@ QSerialPortErrorInfo QSerialPortPrivate::getSystemError(int systemErrorCode) con
}
// This table contains standard values of baud rates that
-// are defined in MSDN and/or in Win SDK file winbase.h
-
-static const QList<qint32> standardBaudRatePairList()
-{
-
- static const QList<qint32> standardBaudRatesTable = QList<qint32>()
-
- #ifdef CBR_110
- << CBR_110
- #endif
-
- #ifdef CBR_300
- << CBR_300
- #endif
-
- #ifdef CBR_600
- << CBR_600
- #endif
-
- #ifdef CBR_1200
- << CBR_1200
- #endif
-
- #ifdef CBR_2400
- << CBR_2400
- #endif
-
- #ifdef CBR_4800
- << CBR_4800
- #endif
-
- #ifdef CBR_9600
- << CBR_9600
- #endif
-
- #ifdef CBR_14400
- << CBR_14400
- #endif
-
- #ifdef CBR_19200
- << CBR_19200
- #endif
-
- #ifdef CBR_38400
- << CBR_38400
- #endif
-
- #ifdef CBR_56000
- << CBR_56000
- #endif
-
- #ifdef CBR_57600
- << CBR_57600
- #endif
-
- #ifdef CBR_115200
- << CBR_115200
- #endif
-
- #ifdef CBR_128000
- << CBR_128000
- #endif
-
- #ifdef CBR_256000
- << CBR_256000
- #endif
- ;
-
- return standardBaudRatesTable;
-};
-
+// are defined in file winbase.h
QList<qint32> QSerialPortPrivate::standardBaudRates()
{
- return standardBaudRatePairList();
+ static const QList<qint32> baudRates = {
+ CBR_110, CBR_300, CBR_600, CBR_1200, CBR_2400,
+ CBR_4800, CBR_9600, CBR_14400, CBR_19200, CBR_38400,
+ CBR_56000, CBR_57600, CBR_115200, CBR_128000, CBR_256000
+ };
+
+ return baudRates;
}
QSerialPort::Handle QSerialPort::handle() const