A working serialport example

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

sp.close                       #see note 1

I tested the code abov e on a Raspberry Pi with an Arduino Nano version 3.0 connected to a PIR sensor. Here's whats I observed:

Motion detected!
Motion ended!
Motion detected!
Motion ended!
Motion detected!
Motion ended!
Motion detected!
Motion ended!
Motion detected!
Motion ended!

Resources

Tags:
Source:
1044hrs.txt
Published:
03-01-2013 10:44