{"id":35,"date":"2010-01-27T10:02:52","date_gmt":"2010-01-27T16:02:52","guid":{"rendered":"http:\/\/curiosity.roguepenguin.net\/?p=35"},"modified":"2019-05-13T06:52:55","modified_gmt":"2019-05-13T12:52:55","slug":"ruby-serialport-on-windows","status":"publish","type":"post","link":"http:\/\/curiosity.roguepenguin.net\/?p=35","title":{"rendered":"Ruby Serialport on Windows"},"content":{"rendered":"<p>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&#8217;t have.  There is a <a href=\"http:\/\/rubyforge.org\/tracker\/index.php?func=detail&amp;aid=9924&amp;group_id=61&amp;atid=321\" target=\"_blank\" rel=\"noopener noreferrer\">pre-compiled<\/a> version (for windows) of the library on the <a href=\"http:\/\/rubyforge.org\/projects\/ruby-serialport\/\" target=\"_blank\" rel=\"noopener noreferrer\">ruby serial-port project<\/a> 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<\/p>\n<pre>path=%path%;c:\\ruby;<\/pre>\n<p>Once ruby has been added to the path, we can call programs like <em>irb<\/em> or <em>gem<\/em> from anywhere.  The next step is to update ruby gems to the latest version (1.3.5 as of the time of this writing).<\/p>\n<pre>gem update --system<\/pre>\n<p>After that is complete, you can optionally update the rest of the installed components<\/p>\n<pre>gem update<\/pre>\n<p>The next step is to download the pre-compiled ruby <a href=\"http:\/\/rubyforge.org\/tracker\/download.php\/61\/321\/9924\/1800\/ruby-serialport-0.6.0-mswin32-gem.zip\" target=\"_blank\" rel=\"noopener noreferrer\">serial-port gem<\/a>. 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. &nbsp;Now navigate back up a level to the rakefile and try raking it if you like (I&#8217;ll wait).  You probably got an error if the form &#8220;undefined method `manage_gems&#8217; for Gem:Module&#8221; <a href=\"http:\/\/curiosity.roguepenguin.net\/wp-content\/uploads\/2010\/01\/rake.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-31\" title=\"Rake Attmpt 1\" src=\"http:\/\/curiosity.roguepenguin.net\/wp-content\/uploads\/2010\/01\/rake.jpg\" alt=\"\" width=\"500\" height=\"253\" srcset=\"http:\/\/curiosity.roguepenguin.net\/wp-content\/uploads\/2010\/01\/rake.jpg 500w, http:\/\/curiosity.roguepenguin.net\/wp-content\/uploads\/2010\/01\/rake-300x151.jpg 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a> That&#8217;s because &#8216;<em>manage_gems<\/em>&#8216; has been removed from rubygems. A quick <a href=\"http:\/\/www.videc.at\/2009\/04\/30\/rubygems-undefined-method-manage_gems-for-gemmodule-nomethoderror\/\" target=\"_blank\" rel=\"noopener noreferrer\">search<\/a> shows that we just need to comment out the <em>manage gems<\/em> line and insert <em>require &#8216;rake\/gempackagetask&#8217;<\/em> instead.  When we make the change and run rake again, we get a different error, &#8220;uninitialized constant Gem::Platform::WIN32&#8221;  <a href=\"http:\/\/curiosity.roguepenguin.net\/wp-content\/uploads\/2010\/01\/rake2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-30\" title=\"Rake Attempt 2\" src=\"http:\/\/curiosity.roguepenguin.net\/wp-content\/uploads\/2010\/01\/rake2.jpg\" alt=\"\" width=\"500\" height=\"253\" srcset=\"http:\/\/curiosity.roguepenguin.net\/wp-content\/uploads\/2010\/01\/rake2.jpg 500w, http:\/\/curiosity.roguepenguin.net\/wp-content\/uploads\/2010\/01\/rake2-300x151.jpg 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a> Searching for the answer here didn&#8217;t yield good results (for me).  &nbsp;However, we can use ruby itself to look into Gem::Platform and see what constants are defined.  From the command line, type<\/p>\n<pre>irb<\/pre>\n<p>which takes us to the ruby interpreter.  From there,<\/p>\n<pre>require 'rubygems'\nGem::Platform.constants<\/pre>\n<p><a href=\"http:\/\/curiosity.roguepenguin.net\/wp-content\/uploads\/2010\/01\/gem_platform.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33\" title=\"Gem Platforms\" src=\"http:\/\/curiosity.roguepenguin.net\/wp-content\/uploads\/2010\/01\/gem_platform.jpg\" alt=\"\" width=\"500\" height=\"253\" srcset=\"http:\/\/curiosity.roguepenguin.net\/wp-content\/uploads\/2010\/01\/gem_platform.jpg 500w, http:\/\/curiosity.roguepenguin.net\/wp-content\/uploads\/2010\/01\/gem_platform-300x151.jpg 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a> 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 &#8220;win32api-blah-blah-blah.&#8221;  &nbsp;Comment out that line in the Rakefile, make a copy of it underneath, and then change WIN32 to CURRENT.  <a href=\"http:\/\/curiosity.roguepenguin.net\/wp-content\/uploads\/2010\/01\/rakefile.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-34\" title=\"Rakefile\" src=\"http:\/\/curiosity.roguepenguin.net\/wp-content\/uploads\/2010\/01\/rakefile.png\" alt=\"\" width=\"500\" height=\"448\" srcset=\"http:\/\/curiosity.roguepenguin.net\/wp-content\/uploads\/2010\/01\/rakefile.png 500w, http:\/\/curiosity.roguepenguin.net\/wp-content\/uploads\/2010\/01\/rakefile-300x268.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a> 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<\/p>\n<pre>gem install --local serialport-0.6.0-x86-mswin32-60.gem<\/pre>\n<p>The gem is installed  To test it, type &#8220;irb&#8221; to go to the ruby interpreter and then type<\/p>\n<pre>require 'serialport'<\/pre>\n<p>If the gem is properly installed, it command should return true.  You can now write scripts utilizing the serial port.  <a href=\"http:\/\/curiosity.roguepenguin.net\/wp-content\/uploads\/2010\/01\/test-serial.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-32\" title=\"Test Serial Port\" src=\"http:\/\/curiosity.roguepenguin.net\/wp-content\/uploads\/2010\/01\/test-serial.png\" alt=\"\" width=\"500\" height=\"473\" srcset=\"http:\/\/curiosity.roguepenguin.net\/wp-content\/uploads\/2010\/01\/test-serial.png 500w, http:\/\/curiosity.roguepenguin.net\/wp-content\/uploads\/2010\/01\/test-serial-300x283.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,7],"tags":[15,11],"class_list":["post-35","post","type-post","status-publish","format-standard","hentry","category-programming","category-ruby","tag-ruby","tag-serial-port"],"_links":{"self":[{"href":"http:\/\/curiosity.roguepenguin.net\/index.php?rest_route=\/wp\/v2\/posts\/35","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/curiosity.roguepenguin.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/curiosity.roguepenguin.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/curiosity.roguepenguin.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/curiosity.roguepenguin.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=35"}],"version-history":[{"count":4,"href":"http:\/\/curiosity.roguepenguin.net\/index.php?rest_route=\/wp\/v2\/posts\/35\/revisions"}],"predecessor-version":[{"id":62,"href":"http:\/\/curiosity.roguepenguin.net\/index.php?rest_route=\/wp\/v2\/posts\/35\/revisions\/62"}],"wp:attachment":[{"href":"http:\/\/curiosity.roguepenguin.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/curiosity.roguepenguin.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/curiosity.roguepenguin.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}