summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2009-08-07 00:19:57 +0000
committercliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2009-08-07 00:19:57 +0000
commit3cf46d62e9e76529596d43496ec2050a55abef50 (patch)
tree3299ff372b31cc28d8abfe599425e1350fdcf71f
parente8f2b32235f33b4a81e00537d83ca9c92229b0dc (diff)
downloadpyserial-git-3cf46d62e9e76529596d43496ec2050a55abef50.tar.gz
improve write timeout (time applies to entire write, not to each internal loop)
-rw-r--r--pyserial/serial/serialposix.py21
1 files changed, 12 insertions, 9 deletions
diff --git a/pyserial/serial/serialposix.py b/pyserial/serial/serialposix.py
index a1f6856..20f6347 100644
--- a/pyserial/serial/serialposix.py
+++ b/pyserial/serial/serialposix.py
@@ -12,7 +12,7 @@
#
# references: http://www.easysw.com/~mike/serial/serial.html
-import sys, os, fcntl, termios, struct, select, errno
+import sys, os, fcntl, termios, struct, select, errno, time
from serialutil import *
# Do check the Python version as some constants have moved.
@@ -462,19 +462,22 @@ class PosixSerial(SerialBase):
def write(self, data):
"""Output the given string over the serial port."""
if self.fd is None: raise portNotOpenError
- if not isinstance(data, (bytes, bytearray)):
- raise TypeError('expected %s or bytearray, got %s' % (bytes, type(data)))
t = len(data)
d = data
+ if self._writeTimeout is not None and self._writeTimeout > 0:
+ timeout = time.time() + self._writeTimeout
+ else:
+ timeout = None
while t > 0:
try:
- if self._writeTimeout is not None and self._writeTimeout > 0:
- _, ready, _ = select.select([], [self.fd], [], self._writeTimeout)
- if not ready:
- raise writeTimeoutError
n = os.write(self.fd, d)
- if self._writeTimeout is not None and self._writeTimeout > 0:
- _, ready, _ = select.select([], [self.fd], [], self._writeTimeout)
+ if timeout:
+ # when timeout is set, use select to wait for being ready
+ # with the time left as timeout
+ timeleft = timeout - time.time()
+ if timeleft < 0:
+ raise writeTimeoutError
+ _, ready, _ = select.select([], [self.fd], [], timeleft)
if not ready:
raise writeTimeoutError
d = d[n:]