communication from your application s logic. Have a look at the project s
examples to learn more about these features.
Ruby
Even dynamic languages such as Ruby give you instant access to your com-
puter s serial port and to an Arduino connected to it. But before that, you
need to install the serialport gem:
maik>
gem install serialport
Using it, you can connect to the Arduino in just 30 lines of code.
SerialProgramming/Ruby/analog_reader.rb
require
'rubygems'
Line 1
require
'serialport'
-
-
if
ARGV.size != 1
-
puts
"You have to pass the name of a serial port."
5
exit 1
-
end
-
-
port_name = ARGV[0]
-
baud_rate = 9600
10
data_bits = 8
-
stop_bits = 1
-
parity
= SerialPort::NONE
-
-
arduino = SerialPort.new(
15
port_name,
-
baud_rate,
-
data_bits,
-
stop_bits,
-
parity
20
)
-
-
sleep 2
-
while
true
-
arduino.write
"a0\n"
25
sleep 0.01
-
line = arduino.gets.chomp
-
puts line
-
end
-
We create a new
SerialPort
After we sleep for two seconds, we start a loop and call
object. To get the result back from the Arduino, we call
the result to the console. Here you can see the program in action:
Appendix 3. Advanced Serial Programming
object in line 15, passing it all the usual parameters.
www.it-ebooks.info
262
write
on the
SerialPort
gets
, and then we print
report erratum
discuss
Need help?
Do you have a question about the Arduino Uno and is the answer not in the manual?