1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-22 03:34:53 +08:00

Fix issue with write() and a timeout of 0. (#137)

* Fix issue with write() and a timeout of 0.

* fix up style
This commit is contained in:
rhd 2017-01-20 17:34:49 -08:00 committed by William Woodall
parent 771e62c220
commit 827c4a784d
2 changed files with 8 additions and 1 deletions

View File

@ -450,6 +450,8 @@ public:
* reading or writing is complete, when a timeout occurs, or when an
* exception occurs.
*
* A timeout of 0 enables non-blocking mode.
*
* \param timeout A serial::Timeout struct containing the inter byte
* timeout, and the read and write timeout constants and multipliers.
*

View File

@ -617,12 +617,17 @@ Serial::SerialImpl::write (const uint8_t *data, size_t length)
total_timeout_ms += timeout_.write_timeout_multiplier * static_cast<long> (length);
MillisecondTimer total_timeout(total_timeout_ms);
bool first_iteration = true;
while (bytes_written < length) {
int64_t timeout_remaining_ms = total_timeout.remaining();
if (timeout_remaining_ms <= 0) {
// Only consider the timeout if it's not the first iteration of the loop
// otherwise a timeout of 0 won't be allowed through
if (!first_iteration && (timeout_remaining_ms <= 0)) {
// Timed out
break;
}
first_iteration = false;
timespec timeout(timespec_from_ms(timeout_remaining_ms));
FD_ZERO (&writefds);