From 65722c94a0ee7313660ae6cd7fd42a50115351ad Mon Sep 17 00:00:00 2001 From: cliechti Date: Fri, 7 Aug 2009 00:48:53 +0000 Subject: - read: improve error handling -> raise exception when device fails (e.g. unplugged USB serial convertors) - write: raise SerialExceptions, not other exceptions from the system --- pyserial/serial/serialposix.py | 13 +++++++++---- 1 file 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): -- cgit v1.2.1