class Smalrubot::TxRx::Serial

Constants

BAUD

Public Class Methods

new(options={}) click to toggle source
# File lib/smalrubot/tx_rx/serial.rb, line 8
def initialize(options={})
  @device = options[:device]
  @baud = options[:baud] || BAUD
  @first_write = true
end

Public Instance Methods

handshake() click to toggle source
Calls superclass method Smalrubot::TxRx::Base#handshake
# File lib/smalrubot/tx_rx/serial.rb, line 18
def handshake
  while tty_devices.length > 0
    begin
      if on_windows?
        io; sleep 3
      end

      return super
    rescue BoardNotFound
      @tty_devices.shift
      if @io
        @io.close
      end
      @io = nil
    end
  end
  raise BoardNotFound
end
io() click to toggle source
# File lib/smalrubot/tx_rx/serial.rb, line 14
def io
  @io ||= connect
end

Private Instance Methods

connect() click to toggle source
# File lib/smalrubot/tx_rx/serial.rb, line 39
def connect
  tty_devices.dup.each do |device|
    begin
      serial = ::Serial.new(device, @baud)
      Smalrubot.debug_log('found board: %s (%d)', device, @baud)
      return serial
    rescue Exception
      @tty_devices.shift
      Smalrubot.debug_log('could not access: %s', device)
      Smalrubot.show_backtrace($!)
    end
  end
  raise BoardNotFound
end
on_windows?() click to toggle source
# File lib/smalrubot/tx_rx/serial.rb, line 68
def on_windows?
  RUBY_PLATFORM.match /mswin|mingw/i
end
tty_devices() click to toggle source
# File lib/smalrubot/tx_rx/serial.rb, line 54
def tty_devices
  if !@tty_devices
    if @device
      @tty_devices = [@device]
    elsif on_windows?
      @tty_devices = (1..256).map { |n| "COM#{n}" }
    else
      @tty_devices =
        `ls /dev`.split("\n").grep(/usb|ACM/i).map{ |d| "/dev/#{d}" }
    end
  end
  @tty_devices
end