Ruby on rails and ardiuno's serial

Hi,

I am creating a ruby on rails application and i was wondering if its possible to have my application respond to serialdata from the USB/COM port of my Arduino (preferably by events).

If so, does anyone has an example on how to do this?

Thanks in advance

http://playground.arduino.cc/interfacing/ruby

-br

Thanks for the quick reply, however this does not shows by events. Maybe i have to poll it?

Gz,

Familiar with the EventMachine? check out these examples - https://github.com/eventmachine/eventmachine/wiki/Code-Snippets - Serial snippet is somewhere @75% of the page...

disclaimer - I'm no Ruby progger ..

Thanks for the quick reply, however this does not shows by events.

What events are you interested in? What does "does not shows by events" mean?

Maybe i have to poll it?

Yes.

robtillaart: Familiar with the EventMachine? check out these examples - https://github.com/eventmachine/eventmachine/wiki/Code-Snippets - Serial snippet is somewhere @75% of the page...

disclaimer - I'm no Ruby progger ..

No unfortunately i am not familiar with EventMachine, but i guess i can take a look into it ;) Do you have a link to some example using it in rails?

PaulS: What events are you interested in? What does "does not shows by events" mean?

I am interested in getting an event when serialdata is present and that be able to process it.

I am interested in getting an event when serialdata is present and that be able to process it.

On the Arduino or in Ruby?

Serial data arrives slowly to the Arduino. It does not arrive "all at once" in any way that could trigger an event.

PaulS:

I am interested in getting an event when serialdata is present and that be able to process it.

On the Arduino or in Ruby?

In ruby

Do you have a link to some example using it in rails?

robtillaart:

Do you have a link to some example using it in rails?

already found that example, however i can not get it working.

I get the following error

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension
.

    C:/Ruby193/bin/ruby.exe extconf.rb
checking for rb_trap_immediate in ruby.h,rubysig.h... no
checking for rb_thread_blocking_region()... yes
checking for inotify_init() in sys/inotify.h... no
checking for __NR_inotify_init in sys/syscall.h... no
checking for writev() in sys/uio.h... no
checking for rb_thread_check_ints()... yes
checking for rb_time_new()... yes
checking for windows.h... yes
checking for winsock.h... yes
checking for main() in -lkernel32... yes
checking for main() in -lrpcrt4... yes
checking for main() in -lgdi32... yes
checking for main() in -lssl... no
creating Makefile

make
generating rubyeventmachine-i386-mingw32.def
compiling binder.cpp
cc1plus.exe: warning: command line option "-Wdeclaration-after-statement" is val
id for C/ObjC but not for C++
cc1plus.exe: warning: command line option "-Wimplicit-function-declaration" is v
alid for C/ObjC but not for C++
In file included from binder.cpp:20:0:
project.h:26:0: warning: ignoring #pragma warning
In file included from binder.cpp:20:0:
project.h:85:0: warning: "FD_SETSIZE" redefined
<command-line>:0:0: note: this is the location of the previous definition
In file included from project.h:137:0,
                 from binder.cpp:20:
binder.h:35:34: warning: type qualifiers ignored on function return type
In file included from project.h:138:0,
                 from binder.cpp:20:
em.h:91:47: warning: type qualifiers ignored on function return type
em.h:92:76: warning: type qualifiers ignored on function return type
em.h:93:56: warning: type qualifiers ignored on function return type
em.h:95:57: warning: type qualifiers ignored on function return type
em.h:96:60: warning: type qualifiers ignored on function return type
em.h:97:58: warning: type qualifiers ignored on function return type
em.h:98:55: warning: type qualifiers ignored on function return type
em.h:99:36: warning: type qualifiers ignored on function return type
em.h:101:47: warning: type qualifiers ignored on function return type
em.h:106:42: warning: type qualifiers ignored on function return type
em.h:123:45: warning: type qualifiers ignored on function return type
em.h:132:36: warning: type qualifiers ignored on function return type
In file included from project.h:145:0,
                 from binder.cpp:20:
eventmachine.h:45:61: warning: type qualifiers ignored on function return type
eventmachine.h:46:112: warning: type qualifiers ignored on function return type
eventmachine.h:47:69: warning: type qualifiers ignored on function return type
eventmachine.h:49:73: warning: type qualifiers ignored on function return type
eventmachine.h:62:75: warning: type qualifiers ignored on function return type
eventmachine.h:63:74: warning: type qualifiers ignored on function return type
eventmachine.h:64:77: warning: type qualifiers ignored on function return type
eventmachine.h:65:41: warning: type qualifiers ignored on function return type
In file included from project.h:145:0,
                 from binder.cpp:20:
eventmachine.h:99:60: warning: type qualifiers ignored on function return type
eventmachine.h:100:58: warning: type qualifiers ignored on function return type
eventmachine.h:102:60: warning: type qualifiers ignored on function return type
eventmachine.h:105:41: warning: type qualifiers ignored on function return type
compiling cmain.cpp
cc1plus.exe: warning: command line option "-Wdeclaration-after-statement" is val
id for C/ObjC but not for C++
cc1plus.exe: warning: command line option "-Wimplicit-function-declaration" is v
alid for C/ObjC but not for C++
In file included from cmain.cpp:20:0:
project.h:26:0: warning: ignoring #pragma warning
In file included from cmain.cpp:20:0:
project.h:85:0: warning: "FD_SETSIZE" redefined
<command-line>:0:0: note: this is the location of the previous definition
In file included from project.h:137:0,
                 from cmain.cpp:20:
binder.h:35:34: warning: type qualifiers ignored on function return type
In file included from project.h:138:0,
                 from cmain.cpp:20:
em.h:91:47: warning: type qualifiers ignored on function return type
em.h:92:76: warning: type qualifiers ignored on function return type
em.h:93:56: warning: type qualifiers ignored on function return type
em.h:95:57: warning: type qualifiers ignored on function return type
em.h:96:60: warning: type qualifiers ignored on function return type
em.h:97:58: warning: type qualifiers ignored on function return type
em.h:98:55: warning: type qualifiers ignored on function return type
em.h:99:36: warning: type qualifiers ignored on function return type
em.h:101:47: warning: type qualifiers ignored on function return type
em.h:106:42: warning: type qualifiers ignored on function return type
em.h:123:45: warning: type qualifiers ignored on function return type
em.h:132:36: warning: type qualifiers ignored on function return type
In file included from project.h:145:0,
                 from cmain.cpp:20:
eventmachine.h:45:61: warning: type qualifiers ignored on function return type
eventmachine.h:46:112: warning: type qualifiers ignored on function return type
eventmachine.h:47:69: warning: type qualifiers ignored on function return type
eventmachine.h:49:73: warning: type qualifiers ignored on function return type
eventmachine.h:62:75: warning: type qualifiers ignored on function return type
eventmachine.h:63:74: warning: type qualifiers ignored on function return type
eventmachine.h:64:77: warning: type qualifiers ignored on function return type
eventmachine.h:65:41: warning: type qualifiers ignored on function return type
In file included from project.h:145:0,
                 from cmain.cpp:20:
eventmachine.h:99:60: warning: type qualifiers ignored on function return type
eventmachine.h:100:58: warning: type qualifiers ignored on function return type
eventmachine.h:102:60: warning: type qualifiers ignored on function return type
eventmachine.h:105:41: warning: type qualifiers ignored on function return type
cmain.cpp:98:71: warning: type qualifiers ignored on function return type
cmain.cpp:109:122: warning: type qualifiers ignored on function return type
cmain.cpp:119:79: warning: type qualifiers ignored on function return type
cmain.cpp:129:83: warning: type qualifiers ignored on function return type
cmain.cpp:260:85: warning: type qualifiers ignored on function return type
cmain.cpp:270:84: warning: type qualifiers ignored on function return type
cmain.cpp:280:88: warning: type qualifiers ignored on function return type
cmain.cpp:290:51: warning: type qualifiers ignored on function return type
cmain.cpp:300:70: warning: type qualifiers ignored on function return type
cmain.cpp:320:55: warning: type qualifiers ignored on function return type
cmain.cpp:546:70: warning: type qualifiers ignored on function return type
cmain.cpp:669:68: warning: type qualifiers ignored on function return type
cmain.cpp: In function 'int evma_send_file_data_to_connection(long unsigned int,
 const char*)':
cmain.cpp:752:6: error: 'fstati64' was not declared in this scope
make: *** [cmain.o] Error 1


Gem files will remain installed in C:/Ruby193/lib/ruby/gems/1.9.1/gems/eventmach
ine-0.12.10 for inspection.
Results logged to C:/Ruby193/lib/ruby/gems/1.9.1/gems/eventmachine-0.12.10/ext/g
em_make.out
An error occurred while installing eventmachine (0.12.10), and Bundler cannot
continue.
Make sure that `gem install eventmachine -v '0.12.10'` succeeds before bundling.

P.S working on windows 7

my disclaimer form post #3 applies here ... Sorry ;)

Apologies if you already know ... that type of Rubygem error message usually means that it can't compile part of the gem code usually because some piece of software from the compile chain is missing. It is possible that the gem won't work on Windows so do a bit of Googling to check that before wasting a lot of time.

It's not clear from your post what Gem you are having difficulty with.

Reading serial data with Ruby and Rails is no different from doing it with any other language. The simplest solution might be to run code in a separate thread that regularly checks the serial port. When something happens in the thread it can call a method elsewhere (I think). Ruby threads are pretty straightforward so it would be easy to test this.

I got it working may not be the cleanest code but it works:

Thread.new{ serial_read }

def serial_read
  sp = SerialPort.new "COM4", 115200
  while true   
    puts sp.readline("\n")
    # here you can handle the input from the serialport 
  end
end

im using serialport gem

Thanks for help :)

Good to hear it.

You might want to read up about closing the serial port when your program finishes. I'm not sure if that happens automatically with the Serialport gem but, if not, you can sometimes get problems trying to reopen the serial port because the PC thinks it's already in use.

...R

Yea i am having these issues at the moment, anyone knows how i can make sure the thread shutsdown cleanly when the Rails server is stopped?

Yea i am having these issues at the moment, anyone knows how i can make sure the thread shutsdown cleanly when the Rails server is stopped?

What does this have to do with the Arduino? Take your Ruby questions elsewhere!

Perhaps you could have a variable (maybe it needs a global variable) that the server can change before it shuts down and which the Thread will read from time to time and close the Serial port when the variable changes. There might then be another variable (or another quantity in the same one) that would let the server know that the Thread had closed the Serial port.

Another possibility might be to open the port in the server but use it in the Thread. Then the server could close it anytime it liked.

...R

Dickvdsteen: Yea i am having these issues at the moment, anyone knows how i can make sure the thread shutsdown cleanly when the Rails server is stopped?