Let Ruby read the serial port

S

Superpelican X.

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
 
B

Brian Candler

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.
 
A

andrew mcelroy

Superpelican X. wrote in post #993185:

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.
https://github.com/tenderlove/ruby-serialport
This might help also.
It also supports windows as well as linux (and theoretically mac)

Andrew McElroy
 
R

Regis d'Aubarede

I use IronRuby :

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



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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top