Skip to content

Commit 460e13e

Browse files
authored
Merge pull request #1166 from OpenC3/fix_posix_serial_driver
Fix Posix Serial Driver Not Unblocking Read on Close
2 parents 3c08059 + 1236092 commit 460e13e

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

openc3/lib/openc3/io/posix_serial_driver.rb

+20-5
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
# GNU Affero General Public License for more details.
1515

1616
# Modified by OpenC3, Inc.
17-
# All changes Copyright 2022, OpenC3, Inc.
17+
# All changes Copyright 2024, OpenC3, Inc.
1818
# All Rights Reserved
1919
#
20-
# This file may also be used under the terms of a commercial license
20+
# This file may also be used under the terms of a commercial license
2121
# if purchased from OpenC3, Inc.
2222

2323
require 'fcntl'
@@ -82,12 +82,17 @@ def initialize(port_name = '/dev/ttyS0',
8282
tio.ospeed = baud_rate
8383
@handle.tcflush(Termios::TCIOFLUSH)
8484
@handle.tcsetattr(Termios::TCSANOW, tio)
85+
86+
@pipe_reader, @pipe_writer = IO.pipe
87+
@readers = [@handle, @pipe_reader]
8588
end
8689

8790
# (see SerialDriver#close)
8891
def close
8992
if @handle
9093
# Close the serial Port
94+
@pipe_writer.write('.')
95+
@pipe_writer.close
9196
@handle.close
9297
@handle = nil
9398
end
@@ -132,9 +137,19 @@ def read
132137
begin
133138
data = @handle.read_nonblock(65535)
134139
rescue Errno::EAGAIN, Errno::EWOULDBLOCK
135-
result = IO.fast_select([@handle], nil, nil, @read_timeout)
136-
if result
137-
retry
140+
begin
141+
read_ready, _ = IO.fast_select(@readers, nil, nil, @read_timeout)
142+
rescue IOError
143+
@pipe_reader.close unless @pipe_reader.closed?
144+
return ""
145+
end
146+
if read_ready
147+
if read_ready.include?(@pipe_reader)
148+
@pipe_reader.close unless @pipe_reader.closed?
149+
return ""
150+
else
151+
retry
152+
end
138153
else
139154
raise Timeout::Error, "Read Timeout"
140155
end

0 commit comments

Comments
 (0)