diff --git a/include/serial/serial.h b/include/serial/serial.h index 82dc299..e25ec5c 100644 --- a/include/serial/serial.h +++ b/include/serial/serial.h @@ -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. * diff --git a/src/impl/unix.cc b/src/impl/unix.cc index 0ff251e..8a8f00d 100755 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -617,12 +617,18 @@ Serial::SerialImpl::write (const uint8_t *data, size_t length) total_timeout_ms += timeout_.write_timeout_multiplier * static_cast (length); MillisecondTimer total_timeout(total_timeout_ms); + bool first = 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 && (timeout_remaining_ms <= 0)) { // Timed out break; } + if( first ) { + first = false; + } timespec timeout(timespec_from_ms(timeout_remaining_ms)); FD_ZERO (&writefds);