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.cpp109
1 files changed, 60 insertions, 49 deletions
diff --git a/src/serialport/qserialport_win.cpp b/src/serialport/qserialport_win.cpp
index a6e1df7..eccaca9 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(&currentCommTimeouts, 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()
@@ -743,6 +698,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(&currentCommTimeouts, 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);