Class: Subduino::Serial
- Inherits:
-
Object
- Object
- Subduino::Serial
- Defined in:
- lib/subduino/serial.rb
Instance Attribute Summary collapse
-
#bauds ⇒ Object
readonly
Returns the value of attribute bauds.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#sp ⇒ Object
readonly
Returns the value of attribute sp.
Instance Method Summary collapse
- #find_port ⇒ Object
-
#initialize(bauds = 57600) ⇒ Serial
constructor
A new instance of Serial.
- #kill ⇒ Object
- #read ⇒ Object
- #up? ⇒ Boolean
- #write(msg) ⇒ Object
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
#bauds ⇒ Object (readonly)
Returns the value of attribute bauds.
4 5 6 |
# File 'lib/subduino/serial.rb', line 4 def bauds @bauds end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
4 5 6 |
# File 'lib/subduino/serial.rb', line 4 def port @port end |
#sp ⇒ Object (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_port ⇒ Object
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 |
#kill ⇒ Object
38 39 40 |
# File 'lib/subduino/serial.rb', line 38 def kill sp.close end |
#read ⇒ Object
21 22 23 |
# File 'lib/subduino/serial.rb', line 21 def read sp.getc end |
#up? ⇒ 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 |