diff options
author | Robert Smallshire <robert@smallshire.org.uk> | 2016-03-25 21:18:38 +0100 |
---|---|---|
committer | Robert Smallshire <robert@smallshire.org.uk> | 2016-03-26 09:12:07 +0100 |
commit | 325a73818bfd8bf576ea3c770908b42a3f12347b (patch) | |
tree | dce208bec0faf11ac6d9a17f2161594e330b6176 | |
parent | 1dc14a0b1a7a235463a6da763417f22a80c9f01b (diff) | |
download | pyserial-git-325a73818bfd8bf576ea3c770908b42a3f12347b.tar.gz |
Makes serialposix.Serial.write() respect write_timeout=0 to be non-blocking.
-rw-r--r-- | serial/serialposix.py | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/serial/serialposix.py b/serial/serialposix.py index 70fb241..c99dc0e 100644 --- a/serial/serialposix.py +++ b/serial/serialposix.py @@ -521,14 +521,17 @@ class Serial(SerialBase, PlatformSpecific): raise portNotOpenError d = to_bytes(data) tx_len = len(d) - if self._write_timeout is not None and self._write_timeout > 0: - timeout = time.time() + self._write_timeout - else: - timeout = None + timeout = self._write_timeout + if timeout and timeout > 0: # Avoid comparing None with zero + timeout += time.time() while tx_len > 0: try: n = os.write(self.fd, d) - if timeout: + if timeout == 0: + # Zero timeout indicates non-blocking - simply return the + # number of bytes of data actually written + return n + elif timeout and timeout > 0: # Avoid comparing None with zero # when timeout is set, use select to wait for being ready # with the time left as timeout timeleft = timeout - time.time() @@ -538,6 +541,7 @@ class Serial(SerialBase, PlatformSpecific): if not ready: raise writeTimeoutError else: + assert timeout is None # wait for write operation _, ready, _ = select.select([], [self.fd], [], None) if not ready: |