maik> ruby analog_reader.rb /dev/tty.usbmodem24321
a0: 496
a0: 456
a0: 382
^Canalog_reader.rb:27:in `gets': Interrupt
from analog_reader.rb:27
Using Ruby to access an Arduino is a good choice because you can fully
concentrate on your application. All of the ugly details you have to deal with
in other programming languages are well hidden.
Python
Python is another dynamic programming language you can use to quickly
create Arduino clients. For programming a serial port, download and install
the pyserial library first.
5
There is a special installer for Windows, but usually
it’s sufficient to install it like this:
maik> python setup.py install
After you’ve installed pyserial, you can use it to create a client for our analog
reader sketch:
/SerialProgramming/Python/analog_reader.py
import sys
Line 1
import time
-
import serial
-
-
if len(sys.argv) != 2:
5
print "You have to pass the name of a serial port."
-
sys.exit(1)
-
-
serial_port = sys.argv[1]
-
arduino = serial.Serial(
10
serial_port,
-
9600,
-
serial.EIGHTBITS,
-
serial.PARITY_NONE,
-
serial.STOPBITS_ONE)
15
time.sleep(2)
-
while 1:
-
arduino.write("a0\n")
-
line = arduino.readline().rstrip()
-
print line
20
5.
http://sourceforge.net/projects/pyserial/files/
report erratum • discuss
Serial Communication Using Various Languages • 263
www.it-ebooks.info