Ruby Serial Port
04/12/08 13:10 Filed in: Serial,
Ruby
Ruby is a great programming language for many
purposes. Whenever possible, I look for excuses to
use it rather than any other choice out there.
However, it's weak when it comes to device support
for embedded projects. That said, one package that
helps bridge the gap is the Ruby-serialport - it
provides the classes needed to access a system's
serial port from Ruby.
What is really nice is that it works very well with Mac OS X, even Leapord on an Intel Mac, even though it hasn't been updated since 8/27/2003.
You can download Ruby-serialport from http://rubyforge.org/frs/?group_id=61&release_id=308
There are two steps to installing it. First you need to compile the C code that comes with the download. Assuming you have the GNU compiler installed, it's as simple as this procedure:
ruby extconf.rb
make && sudo make install
Here's an example:
Then, to test it out, connect your serial device, identify the device port on your host system ("COM1:" for windows, for example, or "/dev/tty.KeyspanSerial1" on a *nix type box). Take a look at the example code here:
http://www.arduino.cc/playground/Interfacing/Ruby
Modify the port_str variable in the example to point to the device port on your system, adjust any baud rate or framing attributes, and run it with ruby.
I hooked up a KeySpan USB to Serial adapter to connect my Mac to a serial GPS and was able to see NMEA sentences flowing in through the ruby test application. Very nice, very handy.
What is really nice is that it works very well with Mac OS X, even Leapord on an Intel Mac, even though it hasn't been updated since 8/27/2003.
You can download Ruby-serialport from http://rubyforge.org/frs/?group_id=61&release_id=308
There are two steps to installing it. First you need to compile the C code that comes with the download. Assuming you have the GNU compiler installed, it's as simple as this procedure:
ruby extconf.rb
make && sudo make install
Here's an example:
Then, to test it out, connect your serial device, identify the device port on your host system ("COM1:" for windows, for example, or "/dev/tty.KeyspanSerial1" on a *nix type box). Take a look at the example code here:
http://www.arduino.cc/playground/Interfacing/Ruby
Modify the port_str variable in the example to point to the device port on your system, adjust any baud rate or framing attributes, and run it with ruby.
I hooked up a KeySpan USB to Serial adapter to connect my Mac to a serial GPS and was able to see NMEA sentences flowing in through the ruby test application. Very nice, very handy.