Class: Subduino::Serial

Inherits:
Object
  • Object
show all
Defined in:
lib/subduino/serial.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bauds = 57600) ⇒ Serial

Returns a new instance of Serial.



6
7
8
9
10
11
12
# File 'lib/subduino/serial.rb', line 6

def initialize(bauds = 57600)
  @bauds = bauds
  @port = find_port
  @sp ||= SerialPort.new(@port, bauds) #, DATA_BITS, DATA_STOP, parity)
  # @sp.read_timeout  = 10
  # @sp.write_timeout = 10
end

Instance Attribute Details

#baudsObject (readonly)

Returns the value of attribute bauds.



4
5
6
# File 'lib/subduino/serial.rb', line 4

def bauds
  @bauds
end

#portObject (readonly)

Returns the value of attribute port.



4
5
6
# File 'lib/subduino/serial.rb', line 4

def port
  @port
end

#spObject (readonly)

Returns the value of attribute sp.



4
5
6
# File 'lib/subduino/serial.rb', line 4

def sp
  @sp
end

Instance Method Details

#find_portObject



14
15
16
17
18
19
# File 'lib/subduino/serial.rb', line 14

def find_port
  prefix = RUBY_PLATFORM =~ /darwin/ ? ".usbmodem" : "USB"
  port =  Dir.glob("/dev/tty#{prefix}*").first
  raise "Can't find serial port" unless port
  port
end

#killObject



38
39
40
# File 'lib/subduino/serial.rb', line 38

def kill
  sp.close
end

#readObject



21
22
23
# File 'lib/subduino/serial.rb', line 21

def read
  sp.getc
end

#up?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/subduino/serial.rb', line 34

def up?
  sp
end

#write(msg) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/subduino/serial.rb', line 25

def write msg
  # txt += "\n" unless txt =~ /^\\n/
  txt = msg.gsub("\r|\n", "")
  Log.debug "=> Sending #{txt.inspect}"
  sp.puts txt
  # msg.to_s.chomp.split("").each do |char|
  # @sp.putc char
end