Sha256: e7f6433bde79f8fa6398f947a4cfdfa5f7c82c58661ce204b254f838f4f95aa8

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

module Lorraine
  
  class SerialConnection < Lorraine::Connection
    
    require "serialport"
    
    attr_accessor :port
    
    def initialize
      super
      
      #simplest ruby program to read from arduino serial, 
      #using the SerialPort gem
      #(http://rubygems.org/gems/serialport)


      #params for serial port
      # port_str = port  # may be different for you
      baud_rate = 115200
      # data_bits = 8
      # stop_bits = 1
      # parity = SerialPort::NONE

      self.port = SerialPort.new Lorraine::SerialConnection.first_available_port, baud_rate#, data_bits, stop_bits, parity
      
    end
    
    def self.first_available_port
      p = Dir.glob("/dev/tty.usb*").first
      raise "No available ports." unless p
      p
    end
    
    def write_binary_string(binary_string)
      self.port.write binary_string
    end
    
    def write_message(msg)
      puts "Writing serial message: #{msg}"
      write_binary_string msg.to_binary
    end
    
    def read_line
      m = self.port.gets
      puts "Lorraine::SerialConnection Message: #{m}"
      m
    end
    
    def sever!
      super
      self.port.close
    end
    
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lorraine-0.0.7 lib/lorraine/serial_connection.rb
lorraine-0.0.6 lib/lorraine/serial_connection.rb