Class: Denko::Connection::Serial

Inherits:
Base
  • Object
show all
Defined in:
lib/denko/connection/serial.rb

Constant Summary collapse

BAUD =
115200

Constants included from Handshake

Handshake::HANDSHAKE_TIMEOUT, Handshake::HANDSHAKE_TRIES

Instance Method Summary collapse

Methods inherited from Base

inherited, #stop

Methods included from Handshake

#handshake

Constructor Details

#initialize(options = {}) ⇒ Serial

Returns a new instance of Serial.



8
9
10
11
12
13
14
# File 'lib/denko/connection/serial.rb', line 8

def initialize(options={})
  @device = options[:device]
  @baud = options[:baud] || BAUD
  @rx_buffer = ""
  @rx_line = ""
  @rx_escaped = false
end

Instance Method Details

#_readObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/denko/connection/serial.rb', line 24

def _read
  # A native USB serial packet can be up to 64 bytes.
  @rx_buffer << io.read(64) if @rx_buffer.empty?

  while @rx_buffer.length > 0
    # Take a single character off the RX buffer.
    char = @rx_buffer[0]
    @rx_buffer = @rx_buffer[1..-1]

    if @rx_escaped
      @rx_line << char
      @rx_escaped = false
    else
      if (char == "\n")
        line = @rx_line
        @rx_line = ""
        return line
      elsif (char == "\\")
        @rx_escaped = true
      else
        @rx_line << char
        @rx_escaped = false
      end
    end
  end

  # Return nil if line wasn't returned and entire buffer parsed.
  return nil
end

#_write(message) ⇒ Object



20
21
22
# File 'lib/denko/connection/serial.rb', line 20

def _write(message)
  io.write(message)
end

#to_sObject



16
17
18
# File 'lib/denko/connection/serial.rb', line 16

def to_s
  "#{@device} @ #{@baud} baud"
end