Class: Calypso::SerialMonitor
- Inherits:
-
Object
- Object
- Calypso::SerialMonitor
- Defined in:
- lib/calypso/serialmonitor.rb
Overview
Calypso’s serial controller
Constant Summary collapse
- CALYPSO_EXIT =
Calypso application exit token.
"calypso_exit".freeze
Instance Attribute Summary collapse
-
#baud ⇒ Fixnum
readonly
Serial baud rate.
-
#data ⇒ Array<String>
readonly
Array of data read from the serial port.
-
#databits ⇒ Fixnum
readonly
Number of data bits per byte.
-
#parity ⇒ Symbol
readonly
Connection parity.
-
#portname ⇒ String
readonly
Path to the serial device.
-
#stopbits ⇒ Fixnum
readonly
Number of stop bits.
Instance Method Summary collapse
-
#initialize(port, baud = 9600, databits = 8, stopbits = 1, parity = SerialPort::NONE) ⇒ SerialMonitor
constructor
Create a new serial port controller.
-
#monitor ⇒ Boolean
Monitor the serial port.
Constructor Details
#initialize(port, baud = 9600, databits = 8, stopbits = 1, parity = SerialPort::NONE) ⇒ SerialMonitor
Create a new serial port controller.
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/calypso/serialmonitor.rb', line 49 def initialize(port, baud = 9600, databits = 8, stopbits = 1, parity = SerialPort::NONE) @port = SerialPort.new(port, baud, databits, stopbits, parity) @portname = port @baud = baud @databits = databits @stopbits = stopbits @parity = parity @data = nil @mutex = Mutex.new end |
Instance Attribute Details
#baud ⇒ Fixnum (readonly)
Returns Serial baud rate.
29 30 31 |
# File 'lib/calypso/serialmonitor.rb', line 29 def baud @baud end |
#data ⇒ Array<String> (readonly)
Returns Array of data read from the serial port.
37 38 39 |
# File 'lib/calypso/serialmonitor.rb', line 37 def data @data end |
#databits ⇒ Fixnum (readonly)
Returns Number of data bits per byte.
31 32 33 |
# File 'lib/calypso/serialmonitor.rb', line 31 def databits @databits end |
#parity ⇒ Symbol (readonly)
Returns Connection parity.
35 36 37 |
# File 'lib/calypso/serialmonitor.rb', line 35 def parity @parity end |
#portname ⇒ String (readonly)
Returns Path to the serial device.
27 28 29 |
# File 'lib/calypso/serialmonitor.rb', line 27 def portname @portname end |
#stopbits ⇒ Fixnum (readonly)
Returns Number of stop bits.
33 34 35 |
# File 'lib/calypso/serialmonitor.rb', line 33 def stopbits @stopbits end |
Instance Method Details
#monitor ⇒ Boolean
Monitor the serial port.
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 |
# File 'lib/calypso/serialmonitor.rb', line 63 def monitor running = true ary = [] manual_stop = false thread = Thread.new do while running do begin input = gets puts input if input.nil? @mutex.synchronize {running = false} manual_stop = true Thread.stop break end input.chomp! @port.write input @port.flush rescue Exception => e puts e. exit end end end while (data = @port.gets.chomp) do if data.eql? CALYPSO_EXIT then Thread.kill thread ary.push data break end puts data ary.push data break unless running end thread.join unless manual_stop @port.close @data = ary manual_stop end |