Ruby Serialport on Windows

At work, we design and build directional sensors (glorified digital compasses). I like to use Ruby for my scripting needs at work. Most of the the time this works well, but sometimes I need to talk to one of our sensors through the serial port, and this gets a little messy on the windows side of the street. Ruby has a decent serial-port library, but it typically needs to be installed from source with the Visual C++ 6.0 compiler, which I don’t have. There is a pre-compiled version (for windows) of the library on the ruby serial-port project page, but it was done some time ago, and needs a little help to install properly. Note that there is a support issue about getting it to install in VC++2003 that might be helpful to some, but this method covers getting it to work with no compiler installed. These instructions assume that the ruby 1.8.7 one-click installer (with Ruby gems configured) is setup on the system. The installer does not add ruby to the path. While not required, it does make life easier. To add ruby to the path type


Once ruby has been added to the path, we can call programs like irb or gem from anywhere. The next step is to update ruby gems to the latest version (1.3.5 as of the time of this writing).

gem update --system

After that is complete, you can optionally update the rest of the installed components

gem update

The next step is to download the pre-compiled ruby serial-port gem. Unzip the file to a new folder somewhere and from the command prompt change directory into it. You should see a Rakefile and two directories, lib and pkg. Before the library can be installed it needs to be raked, and before it can be raked the rakefile needs some tweaking. Before we do that, however, navigate into the pkg folder and rename the file there, serialport-0.6.0-mswin32.gem, to serialport-0.6.0-mswin32.gem.bak. When we rake, a new file will be created.  Now navigate back up a level to the rakefile and try raking it if you like (I’ll wait). You probably got an error if the form “undefined method `manage_gems’ for Gem:Module” That’s because ‘manage_gems‘ has been removed from rubygems. A quick search shows that we just need to comment out the manage gems line and insert require ‘rake/gempackagetask’ instead. When we make the change and run rake again, we get a different error, “uninitialized constant Gem::Platform::WIN32” Searching for the answer here didn’t yield good results (for me).  However, we can use ruby itself to look into Gem::Platform and see what constants are defined. From the command line, type


which takes us to the ruby interpreter. From there,

require 'rubygems'

We can see that our options are RUBY or CURRENT. If we were to trace it back far enough, CURRENT should point to something to the effect of “win32api-blah-blah-blah.”  Comment out that line in the Rakefile, make a copy of it underneath, and then change WIN32 to CURRENT. When we run rake this time, it completes without error. However, our gem is not installed quite yet. Navigate to the pkg directory from the command line, where you should see a new filed named serialport-0.6.0-x86-mswin32-60.gem. To install it, simply type

gem install --local serialport-0.6.0-x86-mswin32-60.gem

The gem is installed To test it, type “irb” to go to the ruby interpreter and then type

require 'serialport'

If the gem is properly installed, it command should return true. You can now write scripts utilizing the serial port.


  1. Posted May 10, 2010 at 8:59 pm | Permalink

    What is rakeback?

  2. Posted May 11, 2010 at 6:12 am | Permalink

    Rake is a ruby utility that executes little ruby mini-scripts. These mini-scripts are usually automatically generated and have a specialized format, but they are still ruby code. A rake task, for example, could add or modify tables in a database (ruby on rails does this a lot), or in this case, rake installs the serialport gem. The usage is rake filename.rb

  3. enun
    Posted November 10, 2011 at 8:57 am | Permalink

    hei, when i am write require ‘serialport’ then i get pop up like “the program cant start because msvcrt-ruby18.dll is missing from your computer. try reinstalling the program to fix the problem”

    my question is what should i do? i really need serial port through ruby

  4. Posted November 12, 2011 at 8:59 am | Permalink

    Do you actually have ruby 1.8.7 installed? At this point I think the default version is 1.9. The brief web search I did indicates that might be the problem. If not, search on the error. Results from Stack Overflow are almost always good.