diff options
author | Chris Liechti <cliechti@gmx.net> | 2016-05-16 22:45:38 +0200 |
---|---|---|
committer | Chris Liechti <cliechti@gmx.net> | 2016-05-16 22:45:38 +0200 |
commit | 13949c66b68e029bff0da56f15137ea3231fde56 (patch) | |
tree | 09b5b6ef4cac3e631a82b9bbdfcf90e9e7830235 | |
parent | 42ab2b4b36b931a9e1aa946d78931e1e915a9c39 (diff) | |
download | pyserial-git-13949c66b68e029bff0da56f15137ea3231fde56.tar.gz |
posix: implement cancel_write
-rw-r--r-- | serial/serialposix.py | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/serial/serialposix.py b/serial/serialposix.py index 47f869a..79da9aa 100644 --- a/serial/serialposix.py +++ b/serial/serialposix.py @@ -270,6 +270,7 @@ class Serial(SerialBase, PlatformSpecific): raise self.reset_input_buffer() self.pipe_abort_read_r, self.pipe_abort_read_w = os.pipe() + self.pipe_abort_write_r, self.pipe_abort_write_w = os.pipe() def _reconfigure_port(self, force_update=False): """Set communication parameters on opened port.""" @@ -477,6 +478,9 @@ class Serial(SerialBase, PlatformSpecific): def cancel_read(self): os.write(self.pipe_abort_read_w, b"x") + def cancel_write(self): + os.write(self.pipe_abort_write_w, b"x") + def write(self, data): """Output the given byte string over the serial port.""" if not self.is_open: @@ -499,13 +503,19 @@ class Serial(SerialBase, PlatformSpecific): timeleft = timeout - time.time() if timeleft < 0: raise writeTimeoutError - _, ready, _ = select.select([], [self.fd], [], timeleft) + abort, ready, _ = select.select([self.pipe_abort_write_r], [self.fd], [], timeleft) + if abort: + os.read(self.pipe_abort_write_r, 1) + break if not ready: raise writeTimeoutError else: assert timeout is None # wait for write operation - _, ready, _ = select.select([], [self.fd], [], None) + abort, ready, _ = select.select([self.pipe_abort_write_r], [self.fd], [], None) + if abort: + os.read(self.pipe_abort_write_r, 1) + break if not ready: raise SerialException('write failed (select)') d = d[n:] |