Archive for the ‘Programming’ Category

Ruby Serialport on Windows

Wednesday, January 27th, 2010

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

path=%path%;c:\ruby;

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

irb

which takes us to the ruby interpreter. From there,

require 'rubygems'
Gem::Platform.constants

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.

Ruby-on-Rails: Mongrel Configuration

Wednesday, October 7th, 2009

I actually forgot I had this site, until Alec Jacobson made a comment and reminded me of it’s existence  (oops).   Will attempt to remember to post more, but I’m just not the blogging sort of person.

So this post pertains to some work I actually did about a month ago, which would have made excellent material here, but (see paragraph above)….  At work we needed a way to keep track of production issues requiring engineering support.  I had already come across the most excellent Retrospectiva and had actually tried to set it up before but failed at the Apache/fastCGI integration step.  And so it was, a little over 1 year later, I tried again.  The initial setup went smoothly, but once again I found myself having difficulty integrating with Apache.

Retrospectiva up and running

This time I opted for Mongrel.  Unfortunately I couldn’t find a lot of information on configuring Mongrel, so it took a lot of trial and error.  Below, reconstructed from my notes, are the steps that I had to take to configure it and get it working.  Note that this transpired on Fedora Core 11.

  1. Follow the Retrospectiva installation instructions for setting up and installing gems.
    Note that the command for installing the mysql gem on Fedora is:

       gem install mysql -- --with-mysql-config=/usr/bin/mysql_config
  2. Install Mongrel per the Retrospectiva install guide
  3. Configure Mongrel to run as a cluster:
       mongrel_rails cluster::configure -e production -p 8000 -a 127.0.0.1 -N 3 -c /var/www/retrospectiva
  4. Check that the cluster works (you should be able to browse to http://127.0.0.1:8000 and see the website):
    mongrel_rails cluster::start
  5. This step is partially referenced from another blog.  Set up mongrel to start on boot:
       mkdir /etc/init.d/mongrel_cluster
       ln -s /usr/lib64/ruby/gems/1.8/gems/mongrel_cluster-X.Y.Z/resources/mongrel_cluster /etc/init.d/mongrel_cluster
       chmod +x /etc/init.d/mongrel_cluster

    Note that depending on your setup, you might need to use /usr/lib rather than /usr/lib64.

  6. Edit /etc/init.d/mongrel_cluster, set user=root (yes, I was bad.  You should use limited account here, but I didn’t feel like troubleshooting permission errors)
  7. Add the mongrel_cluster as a service:
       /sbin/chkconfig --level 345 mongrel_cluster on
  8. Finally, link your mongrel_cluster.yml from your app’s config directory to /etc/mongrel_cluster:
       mkdir -p /etc/mongrel_cluster
       ln -s /var/www/retrospectiva/config/mongrel_cluster.yml /etc/mongrel_cluster/retrospectiva.yml
  9. Mongrel should be starting on boot now. You can test by rebooting and issuing:
       /etc/init.d/mongrel_cluster start (or stop)
  10. Configure Apache, following these directions.  Note that Apache might not be able to pass off to Mongrel (check the error log).  It may be necessary to edit SELinux to allow Apache scripts to access the network (in my case I enabled all ports for tcp, using the seedit-gui program).
  11. Make sure Apache is starting on boot:
        chkconfig --list|grep http

    You get something like this:  httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off

    Now run:

        chkconfig httpd on

    This will set it up to start each time you boot.  Verify that it set correctly:

        chkconfig --list|grep http

    You should get:  httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

That’s all I have.  Note that this sets up your rails application as the sole Apache output.  Hosting other static content or web applications requires more configuration than I could figure out at this time.

Qt and Ruby on Windows and Mac

Saturday, June 20th, 2009

I use ruby at work (windows) from time to time to fill my lightweight scripting needs (breaking out visual studio 2005/C# to reformat a couple of hundred lines of text is a bit overkill).  If I have any user interaction (which I typically do not) I just prompt for input through the command line, but occasionally I’ve wanted to have an actual GUI.  I did a search and discovered the cross-platform GUI toolkit Qt and a Qt-Ruby binding so that one could call the other.

Qt on Windows

Some more searching revealed a pre-packaged install for windows.  I followed the directions in a handy guide. The install went almost perfectly, except that I already had MinGW installed.  I had the most recent version (I even ran the updater to make sure) but the version of win32api.h I had was lower than that required by the qt installer.  I checked the sourceforge repository and I did have the latest version listed.  I tried to complete the installation anyway, and when qt finished and ran it’s demo app, it was messed up (screen garbled).

During the process of installing Qt, it gives one the option to use the existing MinGW, or install one from the package.  During my second attempt, I opted to have the installer provide MinGW.  It downloaded a copy from trolltech (the  Qt maker) and installed over my existing copy of minGW (I have no clue if this has broken it yet; I haven’t used it since).  The installer completed, and the Qt (technically pronounced “Cute”, but I still say “Queue – Tee”) demo app ran correctly.  A quick test later and I had verified it working in ruby as well,

Qt on Mac

Installing Qt on the Mac is a tad bit different, both harder and easier.  I primarily followed this guide, but I had a few issues which will be documented here.  Note that the guide was made for Os X 10.4 Tiger.  The Ruby included in that version of the OS was broken, so the first step in the guide is installing a later version of Ruby and disabling the one that comes by default on the system.  This is not necessary for 10.5 Leopard.

The first thing one needs to do is download Qt and install it. I downloaded the Qt 4.5, LGPL/Free, Framework Only.  After that you need to download Mac Ports.

If you aren’t aware, OS X is based off of BSD.  Rather than using the Red Hat Linux RPM package managing system that most flavors of linux use, BSD used the ports system.  The ports sytem is basically directory tree off all the applications availible for BSD, organized by category (math, games, etc.).  Each application has its own folder, maintained by some person.  Inside the folder are pointers to all the files needed to install the program.  The person maintaining the folder is responsible for making sure the files are up to date and always compile.  Mac Ports is a Mac specific version of the ports sytem.  All the programs are maintained to compile and run on OS X. (Fink is a similar program that can help you get and install *nix programs).

We will be using Mac Ports to get cmake, a specialized makefile generator that is required to install the qt-ruby bindings (which you also need to download). I downloaded  qt4-qtruby-2.0.3, the latest version at this time.

Now it is time to install.  As per the guide:

Install Qt and Mac Ports.

Install cmake from the terminal:

cd ~/Desktop
sudo port install cmake   ## CMake is a cool build system

This is where I had to deviate from the guide.  When I called :

cmake .
make && sudo make install

cmake completed but building failed:

[ 38%] Building CXX object smoke/qtwebkit/CMakeFiles/smokeqtwebkit.dir/smokedata.o
[ 40%] Building CXX object smoke/qtwebkit/CMakeFiles/smokeqtwebkit.dir/x_1.o
i686-apple-darwin9-g++-4.0.1: /Users/Zack/Desktop/qt4-qtruby-2.0.3/smoke/qtwebkit/x_1.cpp: No such file or directory
i686-apple-darwin9-g++-4.0.1: no input files
make[2]: *** [smoke/qtwebkit/CMakeFiles/smokeqtwebkit.dir/x_1.o] Error 1
make[1]: *** [smoke/qtwebkit/CMakeFiles/smokeqtwebkit.dir/all] Error 2
make: *** [all] Error 2

I break out my Google-Foo(tm) and start searching for answers.  I track down a post on ruby forge giving me the answer:  Qt did not install with webkit support, so cmake could not find the qtwebkit cpp headers.  The easiest thing to do (for me at least) is just to disable qtwebkit support (the other option is to build and reinstall Qt from source).

cmake -DENABLE_QTWEBKIT_SMOKE=off -DENABLE_QTWEBKIT_RUBY=off .

I run build again and get a new error:

[100%] Building CXX object ruby/qttest/CMakeFiles/qttest.dir/qttesthandlers.o
Linking CXX shared module qttest.so
ld: library not found for -lsmokekde
collect2: ld returned 1 exit status
make[2]: *** [ruby/qttest/qttest.so] Error 1
make[1]: *** [ruby/qttest/CMakeFiles/qttest.dir/all] Error 2
make: *** [all] Error 2

The answer lies further up in the previous post.

cmake -DENABLE_QTWEBKIT_SMOKE=off -DENABLE_QTWEBKIT_RUBY=off -DENABLE_QTTEST=off -DENABLE_QTTEST_SMOKE=off . 

Finally, everything builds and installs.

Qt Ruby DemoQt Ruby Demo

You can find a nice little qt-ruby tutorial here: http://www.darshancomputing.com/qt4-qtruby-tutorial/