Receiving a serial message from the Arduino

It's quite simple to receive a message from the arduino. Just listen for a message and wait.

require 'serialport'

sp = SerialPort.new "/dev/ttyUSB0", 9600
200.times {puts sp.read; puts '...'; sleep 0.2}

observ ed:

blink!
...
blink!
...

...
the LED was blinking


I've set the loop to 200 times because I want to test send messages repeatedly although in a working system I would use a while loop.

I also discovered that I can send messages to the Arduino in 1 IRB session and receive them in another. This makes sending and receiving messages straight forward since there is no need to write complicated background threads to listen for messages as well as send messages in 1 running script.

See also

Tags:
Source:
2000hrs.txt
Published:
22-04-2012 20:00