我正在使用serialport
gem從我的Ruby on Rails 3.2應用程序中的串行端口讀取。串行端口本身用於從Arduino板上寫入數據。 該寶石被添加到Gemfile
。端口初始化在application.rb
:當我嘗試從該端口讀取出現從Ruby on Rails應用程序中的串行端口讀取
config.serial_port = SerialPort.new "/devttyACM0", 9600
config.serial_port.read_timeout = 100
的問題。
@sp = ProjectName::Application::config.serial_port
@sp.read
應用程序死機。我試圖從pry
進行閱讀並且一切正常。如果將read_timeout
設置爲0
,則從pry
中讀取也會掛起。我已經嘗試將read_timeout
設置爲更大的值,但沒有結果。我應該怎麼做才能使它工作?
更新:
我試着用sinatra
framework執行相同的操作。它也掛起。
require "sinatra"
require "serialport"
get '/' do
read_data
end
helpers do
def read_data
sp = SerialPort.new "/dev/ttyACM0", 9600
sp.read_timeout = 1500
t = sp.read.match(/\d+(\n|\r)+/)[0].gsub!(/(\n|\r)*/,"") rescue nil
sp.close
t
end
end