summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2009-08-07 00:48:53 +0000
committercliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2009-08-07 00:48:53 +0000
commit65722c94a0ee7313660ae6cd7fd42a50115351ad (patch)
tree0cf8e6705f9bb633b501deb6b449392ab98b7e9a
parent3cf46d62e9e76529596d43496ec2050a55abef50 (diff)
downloadpyserial-git-65722c94a0ee7313660ae6cd7fd42a50115351ad.tar.gz
- read: improve error handling -> raise exception when device fails (e.g. unplugged USB serial convertors)
- write: raise SerialExceptions, not other exceptions from the system
-rw-r--r--pyserial/serial/serialposix.py13
1 files changed, 9 insertions, 4 deletions
diff --git a/pyserial/serial/serialposix.py b/pyserial/serial/serialposix.py
index 20f6347..53b6040 100644
--- a/pyserial/serial/serialposix.py
+++ b/pyserial/serial/serialposix.py
@@ -445,13 +445,18 @@ class PosixSerial(SerialBase):
until the requested number of bytes is read."""
if self.fd is None: raise portNotOpenError
read = bytearray()
+ poll = select.poll()
+ poll.register(self.fd, select.POLLIN|select.POLLERR|select.POLLHUP|select.POLLNVAL)
inp = None
if size > 0:
while len(read) < size:
# print "\tread(): size",size, "have", len(read) #debug
- ready,_,_ = select.select([self.fd], [], [], self._timeout)
- if not ready:
- break # timeout
+ # wait until device becomes ready to read (or something fails)
+ for fd, event in poll.poll(self._timeout):
+ if event & (select.POLLERR|select.POLLHUP|select.POLLNVAL):
+ raise SerialException('device reports error (poll)')
+ # we don't care if it is select.POLLIN or timeout, that's
+ # handled below
buf = os.read(self.fd, size - len(read))
read.extend(buf)
if ((self._timeout is not None and self._timeout >= 0) or
@@ -484,7 +489,7 @@ class PosixSerial(SerialBase):
t = t - n
except OSError, v:
if v.errno != errno.EAGAIN:
- raise
+ raise SerialException('write failed: %s' % (v,))
return len(data)
def flush(self):