lib/openc3/io/posix_serial_driver.rb in openc3-5.15.1 vs lib/openc3/io/posix_serial_driver.rb in openc3-5.15.2

- old
+ new

@@ -12,14 +12,14 @@ # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # Modified by OpenC3, Inc. -# All changes Copyright 2022, OpenC3, Inc. +# All changes Copyright 2024, OpenC3, Inc. # All Rights Reserved # -# This file may also be used under the terms of a commercial license +# This file may also be used under the terms of a commercial license # if purchased from OpenC3, Inc. require 'fcntl' require 'termios' # Requires ruby-termios gem require 'timeout' # For Timeout::Error @@ -80,16 +80,21 @@ tio.cc[Termios::VMIN] = 1 tio.ispeed = baud_rate tio.ospeed = baud_rate @handle.tcflush(Termios::TCIOFLUSH) @handle.tcsetattr(Termios::TCSANOW, tio) + + @pipe_reader, @pipe_writer = IO.pipe + @readers = [@handle, @pipe_reader] end # (see SerialDriver#close) def close if @handle # Close the serial Port + @pipe_writer.write('.') + @pipe_writer.close @handle.close @handle = nil end end @@ -130,12 +135,22 @@ # (see SerialDriver#read) def read begin data = @handle.read_nonblock(65535) rescue Errno::EAGAIN, Errno::EWOULDBLOCK - result = IO.fast_select([@handle], nil, nil, @read_timeout) - if result - retry + begin + read_ready, _ = IO.fast_select(@readers, nil, nil, @read_timeout) + rescue IOError + @pipe_reader.close unless @pipe_reader.closed? + return "" + end + if read_ready + if read_ready.include?(@pipe_reader) + @pipe_reader.close unless @pipe_reader.closed? + return "" + else + retry + end else raise Timeout::Error, "Read Timeout" end end