Skip to content

Commit a572638

Browse files
committed
posix: fix PosixPollSerial with timeout=None and add cancel support
fixes pyserial#265
1 parent 797018b commit a572638

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

serial/serialposix.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -733,21 +733,28 @@ def read(self, size=1):
733733
if not self.is_open:
734734
raise portNotOpenError
735735
read = bytearray()
736+
timeout = Timeout(self._timeout)
736737
poll = select.poll()
737738
poll.register(self.fd, select.POLLIN | select.POLLERR | select.POLLHUP | select.POLLNVAL)
739+
poll.register(self.pipe_abort_read_r, select.POLLIN | select.POLLERR | select.POLLHUP | select.POLLNVAL)
738740
if size > 0:
739741
while len(read) < size:
740742
# print "\tread(): size",size, "have", len(read) #debug
741743
# wait until device becomes ready to read (or something fails)
742-
for fd, event in poll.poll(self._timeout * 1000):
744+
for fd, event in poll.poll(None if timeout.is_infinite else (timeout.time_left() * 1000)):
745+
if fd == self.pipe_abort_read_r:
746+
break
743747
if event & (select.POLLERR | select.POLLHUP | select.POLLNVAL):
744748
raise SerialException('device reports error (poll)')
745749
# we don't care if it is select.POLLIN or timeout, that's
746750
# handled below
751+
if fd == self.pipe_abort_read_r:
752+
os.read(self.pipe_abort_read_r, 1000)
753+
break
747754
buf = os.read(self.fd, size - len(read))
748755
read.extend(buf)
749-
if ((self._timeout is not None and self._timeout >= 0) or
750-
(self._inter_byte_timeout is not None and self._inter_byte_timeout > 0)) and not buf:
756+
if timeout.expired() \
757+
or (self._inter_byte_timeout is not None and self._inter_byte_timeout > 0) and not buf:
751758
break # early abort on timeout
752759
return bytes(read)
753760

0 commit comments

Comments
 (0)