Search code examples
crubyrubygemsserial-portbarcode-scanner

Wrong gem being required with require 'serialport'


I've moved my original question to the bottom as its no longer fully relavent to the problem.

I am unable to locate serialport.so which I am able to require like this:

$ rvmree@global
$ irb
> require 'serialport.so'
=> true

(gem list returns emtpy)

Update:

require 'serialport'

when executed in an irb session, both requires return true even when I uninstall the gem. So, it appears another gem is being required via "require 'serialport'". I've searched my system for any other versions of this gem without result.

How can I ensure the correct gem is being required?

Update:

[Removed list of gems]

When I uninstall all gems in rvm global namespace, I can still require 'serialport' and get true.

Now my output of gem list is completely empty and require 'serialport' still returns true from within irb.

I'm using rvm, I've emptied the global gems, and all gems in the gemset I'm using. There are no system gems with a name containing 'serialport', I've searched for files which would be included in the gem directory such as serialport.o, serialport.so and didn't find anything.

I'm at a loss for what could possibly be responding to require 'serialport'

require 'serialport.so'

also returns true and

sudo find / -name 'serialport.so' -print

doesn't return anything.

Any ideas?

Original Post:

I'm using the serialport (1.0.4) gem.

Documentation is found at http://rubydoc.info/gems/serialport/1.0.4/

Here is my implementation.rb:

require 'rubygems'
require 'serialport'
port_str = "/dev/cu.usbserial" # Serialport mount point
baud_rate = 9600
data_bits = 8
stop_bits = 1
parity = 0

sp = SerialPort.new(port_str, data_bits, stop_bits, baud_rate, parity)

while barcode = sp.gets do
  puts barcode
end

sp.close


When running ruby implementation.rb, I get:

implementation.rb:14:in `initialize': wrong number of arguments (5 for 2) (ArgumentError)
from implementation.rb:14:in `open'
from implementation.rb:14

This is weird as there doesn't appear to be an initialize method anywhere (maybe ruby is internally naming SerialPort::new as initialize?).

The ruby part of the gem looks like:

require 'serialport.so'

class SerialPort
   private_class_method(:create)

   # Creates a serial port object.
   #
   # <tt>port</tt> may be a port number
   # or the file name of a defice.
   # The number is portable; so 0 is mapped to "COM1" on Windows,
   # "/dev/ttyS0" on Linux, "/dev/cuaa0" on Mac OS X, etc.
   #
   # <tt>params</tt> can be used to configure the serial port.
   # See SerialPort#set_modem_params for details
   def SerialPort::new(port, *params)
      sp = create(port)
      begin
         sp.set_modem_params(*params) # Calls C extension
      rescue
         sp.close
         raise
      end
      return sp
   end
  # SerialPort::open removed as its the same thing as new() with a block
end

This was working the other day and I can't think of anything which would of changed.

I also get the same error with ruby-serialport gem (0.7.0) which appears to be exactly the same from a quick glance at both sources.

A sample implementation is found at http://www.sfc.wide.ad.jp/~mitsuya/4U/ruby-serialport-macosx.html

The latter gem (ruby-serialport) is found at http://rubyforge.org/projects/ruby-serialport/ (documentation at http://ruby-serialport.rubyforge.org/)

Any ideas? Thanks.


Solution

  • Looking at $LOAD_PATH from within irb, I started searching through them for anything serialport related.

    Before long, I found ~/.rvm/rubies/ree-1.8.7-2010.02/lib/ruby/site_ruby/1.8/i686-darwin10.6.0/serialport.bundle. After deleting it, I tried require 'serialport' and got the expected LoadError: no such file to load -- serialport as I had previously uninstalled the serialport gem for debugging this problem.

    After gem install serialport, my code works again as expected.

    Had I been thinking clearly, I would have done this in the first place and avoided the headache. I hope this helps anyone with a similar problem to debug it more quickly.