view history edit print login register

Interfacing...

Arduino and Ruby

The Ruby/SerialPort library seems to do the job. I haven't seen a precompiled windows binary for this yet. I tested only on ubuntu linux.

On OS X it might be necessary to upgrade Ruby to build serialport. The stock version is 1.8.2, which has a bug that breaks serialport. I (HelgeG) installed ruby to 1.8.6 on my MiniMac (Intel-based, running OS X 10.4.9), and serialport built fine (it is necessary to install the OS X developer tools to build serialport).

The readme gives an idea of the comprehensiveness of the API.

simplest example

Here's a simple example of how to read from the arduino (inefficiently) via ruby using the Ruby/SerialPort library. 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.

#simplest ruby program to read from arduino serial, 
#using Ruby/SerialPort library
#(http://ruby-serialport.rubyforge.org)

require "serialport.so"

#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
  printf("%c", sp.getc)
end

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).

See also

others using Ruby/SerialPort library: