Class: OpenC3::SerialStream
- Defined in:
- lib/openc3/streams/serial_stream.rb
Overview
Stream that reads and writes to serial ports by using SerialDriver.
Instance Method Summary collapse
-
#connect ⇒ Object
Connect the stream.
-
#connected? ⇒ Boolean
Whether the serial stream is connected to the serial port.
-
#disconnect ⇒ Object
Disconnect by closing the serial ports.
-
#initialize(write_port_name, read_port_name, baud_rate, parity, stop_bits, write_timeout, read_timeout, flow_control = :NONE, data_bits = 8) ⇒ SerialStream
constructor
A new instance of SerialStream.
-
#read ⇒ String
Returns a binary string of data from the serial port.
-
#read_nonblock ⇒ String
Returns a binary string of data from the serial port without blocking.
- #write(data) ⇒ Object
Constructor Details
#initialize(write_port_name, read_port_name, baud_rate, parity, stop_bits, write_timeout, read_timeout, flow_control = :NONE, data_bits = 8) ⇒ SerialStream
Returns a new instance of SerialStream.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/openc3/streams/serial_stream.rb', line 50 def initialize(write_port_name, read_port_name, baud_rate, parity, stop_bits, write_timeout, read_timeout, flow_control = :NONE, data_bits = 8) super() # The SerialDriver class will validate the parameters @write_port_name = ConfigParser.handle_nil(write_port_name) @read_port_name = ConfigParser.handle_nil(read_port_name) @baud_rate = Integer(baud_rate) @parity = parity @stop_bits = stop_bits.to_i @write_timeout = ConfigParser.handle_nil(write_timeout) if @write_timeout @write_timeout = @write_timeout.to_f else Logger.instance.warn("Warning: To avoid interface lock, write_timeout can not be nil. Setting to 10 seconds.") @write_timeout = 10.0 end @read_timeout = ConfigParser.handle_nil(read_timeout) @read_timeout = @read_timeout.to_f if @read_timeout @flow_control = flow_control.to_s.intern @data_bits = data_bits.to_i if @write_port_name @write_serial_port = SerialDriver.new(@write_port_name, @baud_rate, @parity, @stop_bits, @write_timeout, @read_timeout, @flow_control, @data_bits) else @write_serial_port = nil end if @read_port_name if @read_port_name == @write_port_name @read_serial_port = @write_serial_port else @read_serial_port = SerialDriver.new(@read_port_name, @baud_rate, @parity, @stop_bits, @write_timeout, @read_timeout, @flow_control, @data_bits) end else @read_serial_port = nil end if @read_serial_port.nil? && @write_serial_port.nil? raise "Either a write port or read port must be given" end # We 'connect' when we create the stream @connected = true # Mutex on write is needed to protect from commands coming in from more # than one tool @write_mutex = Mutex.new end |
Instance Method Details
#connect ⇒ Object
Connect the stream
145 146 147 |
# File 'lib/openc3/streams/serial_stream.rb', line 145 def connect # N/A - Serial streams 'connect' on creation end |
#connected? ⇒ Boolean
Returns Whether the serial stream is connected to the serial port.
151 152 153 |
# File 'lib/openc3/streams/serial_stream.rb', line 151 def connected? @connected end |
#disconnect ⇒ Object
Disconnect by closing the serial ports
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/openc3/streams/serial_stream.rb', line 156 def disconnect if @connected begin @write_serial_port.close if @write_serial_port && !@write_serial_port.closed? rescue IOError # Ignore end begin @read_serial_port.close if @read_serial_port && !@read_serial_port.closed? rescue IOError # Ignore end @connected = false end end |
#read ⇒ String
Returns a binary string of data from the serial port
120 121 122 123 124 125 |
# File 'lib/openc3/streams/serial_stream.rb', line 120 def read raise "Attempt to read from write only stream" unless @read_serial_port # No read mutex is needed because reads happen serially @read_serial_port.read end |
#read_nonblock ⇒ String
Returns a binary string of data from the serial port without blocking
128 129 130 131 132 133 |
# File 'lib/openc3/streams/serial_stream.rb', line 128 def read_nonblock raise "Attempt to read from write only stream" unless @read_serial_port # No read mutex is needed because reads happen serially @read_serial_port.read_nonblock end |
#write(data) ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/openc3/streams/serial_stream.rb', line 136 def write(data) raise "Attempt to write to read only stream" unless @write_serial_port @write_mutex.synchronize do @write_serial_port.write(data) end end |