Skip to content

Commit

Permalink
Merge pull request #1166 from OpenC3/fix_posix_serial_driver
Browse files Browse the repository at this point in the history
Fix Posix Serial Driver Not Unblocking Read on Close
  • Loading branch information
ryanmelt committed Mar 31, 2024
2 parents 3c08059 + 1236092 commit 460e13e
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions openc3/lib/openc3/io/posix_serial_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
# 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'
Expand Down Expand Up @@ -82,12 +82,17 @@ def initialize(port_name = '/dev/ttyS0',
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
Expand Down Expand Up @@ -132,9 +137,19 @@ 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
Expand Down

0 comments on commit 460e13e

Please sign in to comment.