Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 从Ruby on Rails应用程序中的串行端口读取时挂起_Ruby On Rails_Ruby_Ruby On Rails 3_Serial Port_Arduino - Fatal编程技术网

Ruby on rails 从Ruby on Rails应用程序中的串行端口读取时挂起

Ruby on rails 从Ruby on Rails应用程序中的串行端口读取时挂起,ruby-on-rails,ruby,ruby-on-rails-3,serial-port,arduino,Ruby On Rails,Ruby,Ruby On Rails 3,Serial Port,Arduino,我正在使用serialport从我的RubyonRails 3.2应用程序中的串行端口读取数据。串口本身用于从Arduino板写入数据。 gem被添加到Gemfile。端口在应用程序中初始化。rb: config.serial_port = SerialPort.new "/devttyACM0", 9600 config.serial_port.read_timeout = 100 当我尝试从此端口读取时出现问题 @sp = ProjectName::Application::config.

我正在使用
serialport
从我的RubyonRails 3.2应用程序中的串行端口读取数据。串口本身用于从Arduino板写入数据。 gem被添加到
Gemfile
。端口在
应用程序中初始化。rb

config.serial_port = SerialPort.new "/devttyACM0", 9600
config.serial_port.read_timeout = 100
当我尝试从此端口读取时出现问题

@sp = ProjectName::Application::config.serial_port
@sp.read
应用程序挂起。我尝试从
pry
会话执行读取,一切正常。如果我将
read\u timeout
设置为
0
,则从
pry
读取也会挂起。我已经尝试将
read\u timeout
设置为更大的值,但没有结果。我该怎么做才能让它工作

更新:

我尝试使用
sinatra
执行相同的操作。它也挂着

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

您能否使用以下简单脚本与设备交互:

require 'rubygems'
require 'serialport'
sp = SerialPort.new "/dev/ttyUSB0"
sp.write "AT\r"
while true do
   printf("%c", sp.getc)
 end

在这种情况下,我使用的是GSM调制解调器。可能您的设备正在等待某个命令。

也有同样的问题。修复其实很简单,问题是你的rails应用程序不断地从串口读取新的值,只要读取这些输入,它就不会做任何其他事情,所以你的网站永远不会真正建立起来

尝试一下,如果它工作,例如,你只是让它从串行端口读取一个固定的次数