Created
March 31, 2015 05:21
-
-
Save mqu/f32599117f1fa21c1b2c to your computer and use it in GitHub Desktop.
simple ruby example to read and write bytes to Arduino serial port.
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
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); | |
} |
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
# 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 | |
[email protected](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