Class: Calypso::SerialMonitor

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(port, baud = 9600, databits = 8, stopbits = 1, parity = SerialPort::NONE) ⇒ SerialMonitor

Create a new serial port controller.

Parameters:

  • port (String)

    Path to the serial device.

  • baud (Fixnum) (defaults to: 9600)

    Serial baud rate.

  • databits (Fixnum) (defaults to: 8)

    Number of data bits per byte.

  • stopbits (Fixnum) (defaults to: 1)

    Number of stop bits.

  • parity (Symbol) (defaults to: SerialPort::NONE)

    Connection parity.



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

#baudFixnum (readonly)

Returns Serial baud rate.

Returns:

  • (Fixnum)

    Serial baud rate.



29
30
31
# File 'lib/calypso/serialmonitor.rb', line 29

def baud
  @baud
end

#dataArray<String> (readonly)

Returns Array of data read from the serial port.

Returns:

  • (Array<String>)

    Array of data read from the serial port.



37
38
39
# File 'lib/calypso/serialmonitor.rb', line 37

def data
  @data
end

#databitsFixnum (readonly)

Returns Number of data bits per byte.

Returns:

  • (Fixnum)

    Number of data bits per byte.



31
32
33
# File 'lib/calypso/serialmonitor.rb', line 31

def databits
  @databits
end

#paritySymbol (readonly)

Returns Connection parity.

Returns:

  • (Symbol)

    Connection parity.



35
36
37
# File 'lib/calypso/serialmonitor.rb', line 35

def parity
  @parity
end

#portnameString (readonly)

Returns Path to the serial device.

Returns:

  • (String)

    Path to the serial device.



27
28
29
# File 'lib/calypso/serialmonitor.rb', line 27

def portname
  @portname
end

#stopbitsFixnum (readonly)

Returns Number of stop bits.

Returns:

  • (Fixnum)

    Number of stop bits.



33
34
35
# File 'lib/calypso/serialmonitor.rb', line 33

def stopbits
  @stopbits
end

Instance Method Details

#monitorBoolean

Monitor the serial port.

Returns:

  • (Boolean)

    Whether or not the application was manually stopped.



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.message
        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