Serial port reading in a Ruby on Rails application

I am using serialport gem to read from the serial port in a Ruby on Rails 3.2 application. The serial port itself is used to write data from the Arduino board. Pearls are added to Gemfile. The port is initialized to application.rb:

config.serial_port = SerialPort.new "/devttyACM0", 9600
config.serial_port.read_timeout = 100

The problem occurs when I try to read from this port.

@sp = ProjectName::Application::config.serial_port
@sp.read

The application freezes. I tried to read from the session pryand everything was fine. If I set read_timeoutto 0, reading from also freezes pry. I already tried to set read_timeoutto large values ​​with no result. What to do to make it work?

Update:

I tried to do the same things using sinatra framework . He is hanging 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
+5
2

. , , rails , , , .

, , , , .

+2

script, :

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

GSM-. , .

0

All Articles