Skip to main content
2 of 2
added 48 characters in body

Ruby and Arduino over Serial

I'm trying to send data over serial and pick it up in a ruby script. I'm using the serialport gem (https://rubygems.org/gems/serialport) as stated in the Arduino docs (http://playground.arduino.cc/Interfacing/Ruby).

My question is, I'm trying to print out some gyroscope data, particularly the x, y, and z. This is what my arduino sketch looks like:

void setup() {
  Serial.begin(9600);
  // ... do some gyro stuff here
}

void loop() {
  gyro.read();

  Serial.print((int)gyro.g.x);
  Serial.print(" ");
  Serial.print((int)gyro.g.y);
  Serial.print(" ");
  Serial.print((int)gyro.g.z);

  delay(500);
}

My ruby script is pretty straight forward as well:

require "serialport"
port_str = "/dev/tty.usbmodemfa141"
baud_rate = 9600
data_bits = 8
stop_bits = 1
parity = SerialPort::NONE

sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)
while true do
  message = sp.gets
  if message
    message.chomp!

    puts message
  end
end

However, the output that I get is choppy. What I mean by that is that the numbers don't come out as a single line as I intend:

52 9
5
250
 -28
 85
25
9 -5
1 69

Even if I replace it with "Hello World", it'll come out as:

He
llo
Wor
ld

or similar.

Any ideas why? Thanks in advance for any help.