Reading from serial port in a Ruby on Rails application hangs

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

-------------Problems Reply------------

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.

Can you intereact with the device using a simple script, like this:

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

In this case I was using an GSM modem. Maybe your device is waiting for some command first.

Category:ruby on rails Views:10 Time:2012-04-15

Related post

  • Need assistance with serial port communications in Ruby 2008-10-03

    I need to do some communications over a serial port in Ruby. From my research, it appears that there aren't many modern libraries for serial communications and the newest material I can find is from 2006. Are there any gems that I'm not aware of? I u

  • Checking deployed port in ruby on rails application 2010-03-17

    Is there an elegant way to check which port you deployed a ruby on rails application using mongrel? I could not find a directive (i.e. such as #{RAILS_ROOT} which contains the root directory of the application) that I can use to perform a check. I ne

  • how to close specific serial port in C++ MFC smart device application? 2010-07-28

    how to close specific serial port in C++ MFC smart device application? how to check if a port is open? and how to close it? i am using this " ex: http://msdn.microsoft.com/en-us/library/ms881795.aspx --------------Solutions------------- To close hand

  • How can I use serial port in C# Silverlight 4.0 application? 2011-04-08

    How can I use serial port in a Silverlight 4.0 application? --------------Solutions------------- In Silverlight 4, this is only possible via COM, and only out of browser. Here is an article showing how it can be done. Usage while in-browser or direct

  • How do I published a Ruby on Rails application online? 2011-01-17

    I have a simple Ruby on Rails application that works through a localhost test (both using sqlite, or ruby mysql2 gem). I have a web server ready to upload my app online. I understand that I need to create a new mysql database, which is no problem, an

  • How to print (barcode) labels from a Ruby on Rails Application? 2011-04-03

    My first application I have developed in RoR is for some Kiosk Touchscreen PCs used in our stock. When the stock worker picked up some material he enters the quantity in a Form. Now I want to print a label containing: customer name, material descript

  • Which is the best web server for Ruby on Rails application on Ubuntu? 2011-09-12

    I tried it by installing Phusion passenger..Phusion passenger was succeessfully installed but while configuring it to Apache i'm facing a problem .... So can anybody tell me what is the best websever for ruby on rails applications to host them on Ubu

  • How do I send mail from my ruby on rails application? 2011-12-14

    How do i send mail on my ubuntu system. I need to send mail in my ruby on rails application but can't test, because mail isn't being sent. What do i need to do on my system. (now testing this app is making me realize why mail wasn't being sent throug

  • What is the best web server for Ruby on Rails application? 2009-02-23

    What is the best web server for ruby on rails application? Why? --------------Solutions------------- Passenger running on Apache. Easy to set up, low memory usage, and well supported. Both GitHub and 37 Signals use nginx + unicorn. https://github.com

  • says if i develop a Ruby on Rails application using Rails 2.3.2, will that usually be compatible with Passenger on my hosting company? 2009-05-24

    says if i develop a Ruby on Rails application using Rails 2.3.2, will that usually be compatible with Passenger on my hosting company? If i ssh to my hosting company and type rails -v, i get 2.2.2... so looks like they might be using Rails 2.2.2. So

  • Syntax Checking or "Compiling" a Ruby on Rails Application 2009-07-09

    I'm new to Ruby and recently ran into an issue comparing to values when creating a Ruby on Rails application. In a controller I had the following statement that always returned false: if (user.id != params[:id]) The problem was the user.id (which is

  • Where's the best place to define a constant in a Ruby on Rails application? 2009-07-10

    In a Ruby on Rails application, where is the best place to define a constant? I have an array of constant data that I need available across all the controllers in my application. --------------Solutions------------- Rails >= 3, the application is

  • Ruby on Rails application could not be started 2009-08-06

    I put my first Rails app online on Dreamhost, but I keep getting "Ruby on Rails application could not be started" (you can see it directly). Then running rake exiftool process seems to be killed by DH: sh: line 1: 10645 Killed convert "/tmp/stream200

  • Java: HTTP Post to create new "Ride" in a Ruby on Rails application 2009-11-05

    My question is very similar to http://stackoverflow.com/questions/695971/java-http-post-to-create-new-product-in-a-ruby-on-rails-application I have a form that I want to "post" to a form that looks like this Offer:<br /> <input id="ride_togi

  • Pass authentication between php and Ruby On Rails application 2010-05-04

    I have a simple Ruby on rails application that I want to integrate with an existing php website. I only want that users who's been authenticated by the php application would have access to my Ruby on Rails application (it should appear to the user as

  • How much memory should a Ruby on Rails application consume? 2010-06-04

    My Ruby on Rails application is consuming around 129 MB of memory.. is this normal? I have around 3,000 unique visitors a day, i have no complex queries... My users table has about 18k rows. --------------Solutions------------- 129MB doesn't seem too

  • Live chat in ruby on rails application 2010-07-26

    Can anyone suggest me the approach of implementing the live chat functionality in my ruby on rails application? Is there any plugin exists to implement this functionality? Thanks --------------Solutions------------- Yes, Juggernaut. Unfortunately it

  • Ruby on Rails application not regenerating stylesheets on deploy 2010-10-04

    I have a Ruby on Rails application that is not generating stylesheets properly. Sometimes SASS doesn't compile the SCSS into CSS and sometimes they concatenated CSS are not generated. I basically get an error writing the files, like this: Error Messa

  • Compile error in ruby on rails application (error is with rails form helpers) 2010-12-30

    I'm writing a ruby on rails application, and am getting a compile error for the following code <%= form_tag(search_path, :method => "get") do %> <%= label_tag(:q, "Search for:") %> <%= text_field_tag(:q) %> <%= submit_tag("Sea

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.191 (s). 11 q(s)