我已将最初的问题移至底部,因为它不再与问题完全相关。
我无法找到我可以这样要求的 serialport.so:
$ rvmree@global
$ irb
> require 'serialport.so'
=> true
(宝石列表返回 emtpy)
更新:
require 'serialport'
在 irb 会话中执行时,即使我卸载 gem,两者都需要返回 true。因此,似乎通过“require 'serialport'”需要另一个gem。我已经在我的系统中搜索了这个 gem 的任何其他版本,但没有结果。
如何确保需要正确的宝石?
更新:
[移除宝石列表]
当我卸载 rvm 全局命名空间中的所有 gem 时,我仍然可以要求 'serialport' 并实现。
现在我的 gem list 输出完全为空,并且 require 'serialport' 仍然从 irb 中返回 true。
我正在使用 rvm,我已经清空了全局 gem,以及我正在使用的 gemset 中的所有 gem。没有名称包含“serialport”的系统 gem,我搜索了将包含在 gem 目录中的文件,例如 serialport.o、serialport.so,但没有找到任何东西。
我对可能响应要求“串行端口”的内容感到茫然
require 'serialport.so'
也返回 true 和
sudo find / -name 'serialport.so' -print
不返回任何东西。
有任何想法吗?
原帖:
我正在使用串行端口(1.0.4)gem。
文档位于http://rubydoc.info/gems/serialport/1.0.4/
这是我的 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
运行 ruby implementation.rb 时,我得到:
implementation.rb:14:in `initialize': wrong number of arguments (5 for 2) (ArgumentError)
from implementation.rb:14:in `open'
from implementation.rb:14
这很奇怪,因为任何地方似乎都没有初始化方法(也许 ruby 在内部将 SerialPort::new 命名为初始化?)。
宝石的红宝石部分如下所示:
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
这是前几天的工作,我想不出有什么会改变的。
ruby-serialport gem (0.7.0) 也出现同样的错误,快速浏览一下这两个来源似乎完全相同。
示例实现位于http://www.sfc.wide.ad.jp/~mitsuya/4U/ruby-serialport-macosx.html
后一个 gem (ruby-serialport) 位于http://rubyforge.org/projects/ruby-serialport/(文档位于http://ruby-serialport.rubyforge.org/)
有任何想法吗?谢谢。