diff options
author | Denis Shienkov <denis.shienkov@gmail.com> | 2014-01-23 15:32:13 +0400 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-02-02 10:34:54 +0100 |
commit | 1824ad0a6e97e293098c94a038abd7b2343bf572 (patch) | |
tree | 8d5fd731b449201e80be837a065d9c2fc0830383 | |
parent | df6baa1de99a65dd23586cb2fb308c3a24dd198e (diff) | |
download | qtserialport-1824ad0a6e97e293098c94a038abd7b2343bf572.tar.gz |
Optimize of writing sequence algorithm
* The writeSequenceStarted shall be set to true after a successful call
of WriteFile() and to be dropped to false in case of write completion.
* The method startAsyncWrite() shall not call WriteFile() if the
writeBuffer is empty or writing sequence is already started. Also now
there is no sense to check a validity of pointers on data in writeBuffer
before call of WriteFile() because writeBuffer can not be empty.
* It is also expedient to free the writeBuffer in _q_completeAsyncWrite()
method only if numberOfBytesTransferred is not zero.
Tested on Windows 8 with the on-board and the USB (PL2303) serial ports.
Change-Id: Ib70276705fcc3f2396bf7dd6cf6977b62382a64b
Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
-rw-r--r-- | src/serialport/qserialport_win.cpp | 42 |
1 files changed, 17 insertions, 25 deletions
diff --git a/src/serialport/qserialport_win.cpp b/src/serialport/qserialport_win.cpp index 6d288d1..17b3fb7 100644 --- a/src/serialport/qserialport_win.cpp +++ b/src/serialport/qserialport_win.cpp @@ -595,15 +595,13 @@ void QSerialPortPrivate::_q_completeAsyncWrite() q->setError(decodeSystemError()); } - writeBuffer.free(numberOfBytesTransferred); - - if (numberOfBytesTransferred > 0) + if (numberOfBytesTransferred > 0) { + writeBuffer.free(numberOfBytesTransferred); emit q->bytesWritten(numberOfBytesTransferred); + } - if (writeBuffer.isEmpty()) - writeSequenceStarted = false; - else - startAsyncWrite(); + writeSequenceStarted = false; + startAsyncWrite(); } bool QSerialPortPrivate::startAsyncCommunication() @@ -656,30 +654,24 @@ bool QSerialPortPrivate::startAsyncWrite() { Q_Q(QSerialPort); - qint64 nextSize = writeBuffer.nextDataBlockSize(); - const char *ptr = writeBuffer.readPointer(); - - // no more data to write - if (!ptr || nextSize == 0) + if (writeBuffer.isEmpty() || writeSequenceStarted) return true; - writeSequenceStarted = true; - initializeOverlappedStructure(writeCompletionOverlapped); - if (::WriteFile(descriptor, ptr, nextSize, NULL, &writeCompletionOverlapped)) - return true; - - QSerialPort::SerialPortError error = decodeSystemError(); - if (error != QSerialPort::NoError) { - writeSequenceStarted = false; - - if (error != QSerialPort::ResourceError) - error = QSerialPort::WriteError; + if (!::WriteFile(descriptor, writeBuffer.readPointer(), + writeBuffer.nextDataBlockSize(), + NULL, &writeCompletionOverlapped)) { - q->setError(error); - return false; + QSerialPort::SerialPortError error = decodeSystemError(); + if (error != QSerialPort::NoError) { + if (error != QSerialPort::ResourceError) + error = QSerialPort::WriteError; + q->setError(error); + return false; + } } + writeSequenceStarted = true; return true; } |