# Let Ruby read the serial port

**URL:** https://rubytalk.org/t/let-ruby-read-the-serial-port/62438
**Category:** ruby-talk
**Created:** [16 April 2011 12:58 UTC](https://rubytalk.org/t/let-ruby-read-the-serial-port/62438 "2011-04-16T12:58:56Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Superpelican\_X](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Superpelican\_X](https://rubytalk.org/u/Superpelican_X)
#### Post date: [16 April 2011 12:58 UTC](https://rubytalk.org/t/let-ruby-read-the-serial-port/62438/1 "2011-04-16T12:58:56Z")

</div>

Hello everyone,

I would like to read a serial port. I'm going to connect want to connect  
my Arduino Uno microcontroller board to the computer via USB. It acts as  
a virtual serial port automatically. I can already read with the Arduino  
IDE' s serialterminal.

Processing can also read a serialport from a Arduino.

But now I would like to let Ruby read the virtual serialport and save  
the data in a variable. And then do a lots of stuff with it.

Is there a special library or gem for this? I already installed libusb  
for ruby.

Greetings,

Superpelican

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)
#### Post date: [16 April 2011 21:14 UTC](https://rubytalk.org/t/let-ruby-read-the-serial-port/62438/2 "2011-04-16T21:14:52Z")

</div>

Superpelican X. wrote in post #993185:

> Is there a special library or gem for this?

Which platform are you running under?

The ruby-serialport gem worked fine for me (under Linux) when I last  
tried it, although that was several years ago.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Josh\_Cheek](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@Josh\_Cheek](https://rubytalk.org/u/Josh_Cheek)
#### Post date: [16 April 2011 22:03 UTC](https://rubytalk.org/t/let-ruby-read-the-serial-port/62438/3 "2011-04-16T22:03:09Z")

</div>

Almost a year ago I used [serialport | RubyGems.org | your community gem host](http://rubygems.org/gems/serialport) successfully on  
my Mac to talk to USB and Bluetooth Arduinos.

> **···**
>
> On Sat, Apr 16, 2011 at 7:58 AM, Superpelican X. \<superpelican12@gmail.com\>wrote:
> 
> > Hello everyone,
> > 
> > I would like to read a serial port. I'm going to connect want to connect  
> > my Arduino Uno microcontroller board to the computer via USB. It acts as  
> > a virtual serial port automatically. I can already read with the Arduino  
> > IDE' s serialterminal.
> > 
> > Processing can also read a serialport from a Arduino.
> > 
> > But now I would like to let Ruby read the virtual serialport and save  
> > the data in a variable. And then do a lots of stuff with it.
> > 
> > Is there a special library or gem for this? I already installed libusb  
> > for ruby.
> > 
> > Greetings,
> > 
> > Superpelican
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Regis\_Aubarede](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@Regis\_Aubarede](https://rubytalk.org/u/Regis_Aubarede)
#### Post date: [18 April 2011 14:26 UTC](https://rubytalk.org/t/let-ruby-read-the-serial-port/62438/4 "2011-04-18T14:26:39Z")

</div>

I use IronRuby :

require "mscorlib"  
require "System.Windows.Forms"

def portConfigure(portName,bauds,bits,stop,parity)  
&nbsp;&nbsp;&nbsp;&nbsp;@components = System::ComponentModel::Container.new()  
&nbsp;&nbsp;&nbsp;&nbsp;@serialPort1 = System::IO::Ports::SerialPort.new(@components)  
&nbsp;&nbsp;&nbsp;&nbsp;portName ||= "COM3"  
&nbsp;&nbsp;&nbsp;&nbsp;bauds ||= "9600"  
&nbsp;&nbsp;&nbsp;&nbsp;bits ||= "8"  
&nbsp;&nbsp;&nbsp;&nbsp;stopBits ||= "1"  
&nbsp;&nbsp;&nbsp;&nbsp;parity ||= "none"  
&nbsp;&nbsp;&nbsp;&nbsp;#puts "Configure..."  
&nbsp;&nbsp;&nbsp;&nbsp;@serialPort1.DtrEnable = true  
&nbsp;&nbsp;&nbsp;&nbsp;@serialPort1.RtsEnable = true  
&nbsp;&nbsp;&nbsp;&nbsp;@serialPort1.PortName = portName  
&nbsp;&nbsp;&nbsp;&nbsp;@serialPort1.StopBits = stopBits.to\_i  
&nbsp;&nbsp;&nbsp;&nbsp;@serialPort1.BaudRate = bauds.to\_i  
&nbsp;&nbsp;&nbsp;&nbsp;@serialPort1.DataBits = bits.to\_i  
&nbsp;&nbsp;&nbsp;&nbsp;@serialPort1.Open()  
&nbsp;&nbsp;&nbsp;&nbsp;@serialPort1.DataReceived do |sender, e|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buff = @serialPort1.ReadExisting()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanner(buff)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def writeString(str)  
&nbsp;&nbsp;&nbsp;&nbsp;buf = System::Text::Encoding.GetEncoding("ASCII").GetBytes(str)  
&nbsp;&nbsp;&nbsp;&nbsp;size = buf.Length  
&nbsp;&nbsp;&nbsp;&nbsp;@serialPort1.Write(buf, 0, size)  
&nbsp;&nbsp;end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Andrew\_Mcelroy](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@Andrew\_Mcelroy](https://rubytalk.org/u/Andrew_Mcelroy)
#### Post date: [16 April 2011 21:38 UTC](https://rubytalk.org/t/let-ruby-read-the-serial-port/62438/5 "2011-04-16T21:38:33Z")

</div>

> Superpelican X. wrote in post #993185:
> 
> > Is there a special library or gem for this?
> 
> Which platform are you running under?
> 
> The ruby-serialport gem worked fine for me (under Linux) when I last  
> tried it, although that was several years ago.

Make sure you have the baud and port number correct.  
It's an often overlooked part of connecting to serial.

> **[GitHub - tenderlove/ruby-serialport: ruby-serialport is a Ruby library that...](https://github.com/tenderlove/ruby-serialport)**
>
> ruby-serialport is a Ruby library that provides a class for using RS-232 serial ports - GitHub - tenderlove/ruby-serialport: ruby-serialport is a Ruby library that provides a class for using RS-232...

This might help also.  
It also supports windows as well as linux (and theoretically mac)

Andrew McElroy

> **···**
>
> On Sat, Apr 16, 2011 at 4:14 PM, Brian Candler \<b.candler@pobox.com\> wrote:
> 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
