Access Serial Ports through Ruby
Ruby is a great programming language for many purposes, 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
which, as its name implies, provides the classes needed to access a system's serial port from Ruby. Ruby SerialPort works very well with Mac OS X, Leopard on an Intel or PPC Mac (I've tested both), even though it hasn't been updated since 8/27/2003. Versions of Ruby SerialPort are said to work fine on Windows and Linux, though I've only tested it on Mac OS X. 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 (for Mac OS X, just make sure your XCode tools DVD is installed), it's as simple as this procedure: ruby extconf.rb make && sudo make install Here's an example of building Ruby SerialPort on OS X Leopard:
ï¿¼ Then, to test it out, connect your serial device, identify the device port on your host system ("COM1:" for windows or "/dev/tty.KeyspanSerial1" for example on a *nix type box). Next take a look at the example code here that uses Ruby SerialPort to talk to an Arduino
(another piece of open source hardware I've mentioned in the past): 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. On my workbench, I have multiple serial GPS's, a Nokia LCD that's driven by serial, several LANtronix devices that translate serial to IP data and I'm sure if I dug through the archeology on my bench, I'd find other examples. Part of the reason I was looking for a Ruby serial solution was that I am working on a serial multiplexor to combine up to 5 serial data streams into one so microcontrollers with one serial port can still talk to many serial devices. I wrote the firmware for a Propeller ( a cool 32-bit 8-way multi-processor microcontroller - and a topic for another post sometime)
to multiplex the data from devices like GPSs. I used Ruby SerialPort and Ruby code on my Mac to parse the multiplexed packets in and out of the propeller. As a basis for some of the firmware, I used the FullDuplexSerial.spin code
that's open source (MIT).
Here's a picture of my prototype serial multiplexor - the Propeller board is the blue board and some test serial devices are connected to it:
FYI - Because serial data devices are still so ubiquitous (in the form of RS232, TTL, USB, SPI, I2C), an excellent book on serial port software and hardware development is:
"Serial Port Complete: COM Ports, USB Virtual COM Ports, and Ports for Embedded Systems (Complete Guides series)" (Jan Axelson)
Long live serial. And thanks to the Ruby SerialPort project for making a cool add-on to Ruby.
This work is licensed under a Creative Commons Attribution 3.0 Unported License