从 Ruby on Rails 应用程序中的串行端口读取挂起 [英] Reading from serial port in a Ruby on Rails application hangs

查看:24
本文介绍了从 Ruby on Rails 应用程序中的串行端口读取挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 serialport gem 从我的 Ruby on Rails 3.2 应用程序中的串行端口.串口本身用于从 Arduino 板写入数据.gem 被添加到 Gemfile.端口在application.rb中初始化:

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

应用程序死机.我尝试从 pry 会话中读取,一切正常.如果我将 read_timeout 设置为 0,从 pry 读取也会挂起.我已经尝试将 read_timeout 设置为更大的值但没有结果.我应该怎么做才能让它发挥作用?

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?

更新:

我尝试使用 sinatra framework 执行相同的操作.它也挂了.

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

推荐答案

遇到了同样的问题.修复实际上很简单,问题在于您的 rails 应用程序不断从串行端口读取新值,并且只要它读取这些输入,它就不会执行任何其他操作,因此您的站点将永远不会被实际构建.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆