Serial port communication Arduino with Ruby on linux mint

1. gem install serialport
2. if you don't have permission for dialout, do following:
$ sudo adduser Username dialout
$ sudo chmod a+rw /dev/ttyUSB0
In order to know where your arduino is plugged, run the following:
$ ll /dev/tty* | grep USB
and result will be the port plugged with arduino

3. run following script to test it:
    #simplest ruby program to read from arduino serial,
    #using the SerialPort gem
    #(http://rubygems.org/gems/serialport)
    
    require "serialport"
    
    #params for serial port
    port_str = "/dev/ttyUSB0"  #may be different for you
    baud_rate = 9600
    data_bits = 8
    stop_bits = 1
    parity = SerialPort::NONE
    
    sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)
    
    #just read forever
    while true do
       while (i = sp.gets.chomp) do       # see note 2
          puts i
          #puts i.class #String
        end
    end
    
    sp.close                       #see note 1

Comments