Loading...

Interfacing...

Arduino and Ruby

The SerialPort gem seems to do the job.

simplest example

Here's a simple example of how to read from the arduino (inefficiently) via ruby using the SerialPort gem. You may have to change the port_str param (or use an integer for that param as port number). See the readme within the ruby-serialport download.

  1. #simplest ruby program to read from arduino serial,
  2. #using the SerialPort gem
  3. #(http://rubygems.org/gems/serialport)
  4.  
  5. require "serialport"
  6.  
  7. #params for serial port
  8. port_str = "/dev/ttyUSB0"  #may be different for you
  9. baud_rate = 9600
  10. data_bits = 8
  11. stop_bits = 1
  12. parity = SerialPort::NONE
  13.  
  14. sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)
  15.  
  16. #just read forever
  17. while true do
  18.   printf("%c", sp.getc)
  19. end
  20.  
  21. sp.close                       #see note 1

  • note 1: As with the File api, you can instead use SerialPort.new() or .open() with a block to have the resource automatically closed for you (more robust).


Arduino rubygem

Arduino gem is a prototyping API for Arduino in Ruby. Helps prototype Arduino programs quickly from the computer, without the need to burn programs to the board frequently. It's a "burn once, write many programs" solution.


See also

others using the SerialPort gem: