Simple Ruby Example To Read And Write Bytes To Arduino Serial Port.

Skip to content Search Gists Search Gists All gists Back to GitHub Sign in Sign up Sign in Sign up Dismiss alert {{ message }}

Instantly share code, notes, and snippets.

@mqu mqu/arduino-serial.ino Created March 31, 2015 05:21 Show Gist options
  • Star (3) You must be signed in to star a gist
  • Fork (0) You must be signed in to fork a gist
  • Embed Select an option
    • Embed Embed this gist in your website.
    • Share Copy sharable link for this gist.
    • Clone via HTTPS Clone using the web URL.

    No results found

    Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/mqu/f32599117f1fa21c1b2c.js"></script>
  • Save mqu/f32599117f1fa21c1b2c to your computer and use it in GitHub Desktop.
Code Revisions 1 Stars 3 Embed Select an option
  • Embed Embed this gist in your website.
  • Share Copy sharable link for this gist.
  • Clone via HTTPS Clone using the web URL.

No results found

Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/mqu/f32599117f1fa21c1b2c.js"></script> Save mqu/f32599117f1fa21c1b2c to your computer and use it in GitHub Desktop. Download ZIP simple ruby example to read and write bytes to Arduino serial port. Raw arduino-serial.ino This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters
uint8_t c;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(4800); // 8E2 : 8bits, 2 stops bits, even parity.
// Serial.begin(4800, SERIAL_8E2); // 8E2 : 8bits, 2 stops bits, even parity.
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
c='a';
}
void loop() {
loop2();
}
void loop1() {
Serial.write(c);
c++;
delay(1000);
}
void loop2() {
if (Serial.available() > 0) {
// read the incoming byte:
c = Serial.read();
// c++;
// Serial.println(c);
Serial.write(c);
}
}
void loop3() {
Serial.write(c);
c++;
delay(1000);
}
Raw arduino-serial.rb This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters
# link : http://playground.arduino.cc/interfacing/ruby
#simplest ruby program to read from arduino serial,
#using the SerialPort gem
#(http://rubygems.org/gems/serialport)
require "pp"
require "serialport"
class TTy
def initialize
# defaults params for arduino serial
baud_rate = 4800
data_bits = 8
stop_bits = 1
parity = SerialPort::NONE
# serial port
@sp=nil
@port=nil
end
def open port
@sp = SerialPort.new(port, @baud_rate, @data_bits, @stop_bits, @parity)
end
def shutdown reason
return if @sp==nil
return if reason==:int
printf("\nshutting down serial (%s)\n", reason)
# you may write something before closing tty
@sp.write(0x00)
@sp.flush()
printf("done\n")
end
def read
@sp.flush()
printf("# R : reading ...\n")
c=nil
while c==nil
c=@sp.read(1)
break if c != nil
end
printf("# R : 0x%02x\n", c.ord)
return c
# @sp.readByte()
end
def write c
@sp.putc(c)
@sp.flush()
printf("# W : 0x%02x\n", c.ord)
end
def flush
@sp.flush
end
end
# serial port should be connected to /dev/ttyUSB*
ports=Dir.glob("/dev/ttyUSB*")
if ports.size != 1
printf("did not found right /dev/ttyUSB* serial")
exit(1)
end
tty=TTy.new()
tty.open(ports[0])
at_exit { tty.shutdown :exit }
trap("INT") { tty.shutdown :int ; exit}
# reading thread
Thread.new do
d=0x16
pp d
while true do
sleep(0.01)
tty.write(d)
tty.flush()
# build a loop with a value (d) cycling from 0 to 255
d=d+1
d=d%255
end
end
#just read forever
while true do
while (true) do
c=tty.read()
sleep(0.01)
end
end
sleep 500
tty.shutdown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment You can’t perform that action at this time.

Tag » Arduino Write Byte To Port