|
14 | 14 | # GNU Affero General Public License for more details.
|
15 | 15 |
|
16 | 16 | # Modified by OpenC3, Inc.
|
17 |
| -# All changes Copyright 2022, OpenC3, Inc. |
| 17 | +# All changes Copyright 2024, OpenC3, Inc. |
18 | 18 | # All Rights Reserved
|
19 | 19 | #
|
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 |
21 | 21 | # if purchased from OpenC3, Inc.
|
22 | 22 |
|
23 | 23 | require 'fcntl'
|
@@ -82,12 +82,17 @@ def initialize(port_name = '/dev/ttyS0',
|
82 | 82 | tio.ospeed = baud_rate
|
83 | 83 | @handle.tcflush(Termios::TCIOFLUSH)
|
84 | 84 | @handle.tcsetattr(Termios::TCSANOW, tio)
|
| 85 | + |
| 86 | + @pipe_reader, @pipe_writer = IO.pipe |
| 87 | + @readers = [@handle, @pipe_reader] |
85 | 88 | end
|
86 | 89 |
|
87 | 90 | # (see SerialDriver#close)
|
88 | 91 | def close
|
89 | 92 | if @handle
|
90 | 93 | # Close the serial Port
|
| 94 | + @pipe_writer.write('.') |
| 95 | + @pipe_writer.close |
91 | 96 | @handle.close
|
92 | 97 | @handle = nil
|
93 | 98 | end
|
@@ -132,9 +137,19 @@ def read
|
132 | 137 | begin
|
133 | 138 | data = @handle.read_nonblock(65535)
|
134 | 139 | 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 |
138 | 153 | else
|
139 | 154 | raise Timeout::Error, "Read Timeout"
|
140 | 155 | end
|
|
0 commit comments