从串行端口在Ruby on Rails应用程序挂起阅读 [英] Reading from serial port in a Ruby on Rails application hangs
问题描述
我使用了的SerialPort
宝石一>从我的Ruby on Rails的3.2应用串口读取。串行端口本身被用来从一个Arduino板写入数据。
创业板加入的Gemfile
。端口初始化中的 application.rb中
:
config.serial_port = SerialPort.new/ devttyACM0,9600
config.serial_port.read_timeout = 100
当我尝试从这个端口读取出现问题。
@sp =项目名::应用:: config.serial_port
@传播
应用程序挂起致命的。我试图从撬
会话进行阅读,一切都OK。如果我设置 read_timeout
到 0
,从撬
阅读也挂起。我已经尝试过 read_timeout
设置为更大的值没有结果。我应该怎么做才能使它工作?
更新:
我试图使用执行西纳特拉
框架同样的动作。它挂起了。
规定屈
要求的SerialPort得到'/'做
的read_data
结束做助手
高清的read_data
SP = SerialPort.new的/ dev / ttyACM0,9600
sp.read_timeout = 1500
T = sp.read.match!(/ \\ D +(\\ n | \\ R)+ /)[0] .gsub(/(\\ n | \\ R)* /,)抢救无
sp.close
ŧ
结束
结束
有同样的问题。伦敦定盘实际上很容易,问题是,你的Rails应用程序正在不断从串口读取新的价值观,并且只要它读取这些输入,它不会做任何事情,所以你的网站将永远不会被真正建立。
尝试,如果它的工作原理例如,如果你只是让它从串口读取的固定次数。
I'm using the serialport
gem to read from the serial port in my Ruby on Rails 3.2 application. The serial port itself is used to write data from an Arduino board.
The gem is added to Gemfile
. The port is initialized in application.rb
:
config.serial_port = SerialPort.new "/devttyACM0", 9600
config.serial_port.read_timeout = 100
The problem appears when I try to read from this port.
@sp = ProjectName::Application::config.serial_port
@sp.read
The application hangs deadly. I've tried to perform reading from a pry
session and everything was OK. If I set read_timeout
to 0
, reading from pry
also hangs. I already tried to set read_timeout
to bigger values with no result. What should I do to make it work?
Update:
I've tried to perform the same actions using sinatra
framework. It hangs too.
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
Had the same problem. The fix was actually easy, the problem is that your rails app is constantly reading new values from the serial port and as long its reading those inputs, it doesn't do anything else, so your site will never be actually built.
Try if it works if for example you simply let it read from the serial port a fixed number of times.
这篇关于从串行端口在Ruby on Rails应用程序挂起阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!