Simple Ruby Example To Read And Write Bytes To Arduino Serial Port.
Maybe your like
Skip to content Search Gists Search Gists All gists Back to GitHub Sign in Sign up Sign in Sign up You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }}
mqu/arduino-serial.ino Created March 31, 2015 05:21 Show Gist options
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
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.
Instantly share code, notes, and snippets.
- Download ZIP
- 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.
- 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); |
| } |
| # 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 |
Tag » Arduino Write Byte To Port
-
Writing An Output Byte Instead Of Single Pin - Arduino Forum
-
Serial.write() - Arduino Reference
-
Parallel I/O - E.g Reading Or Writing A Whole Byte, Not One Pin At A Time
-
Writing A Byte To A Processor Port - Project Guidance - Arduino Forum
-
Write Bytes To And Read Bytes From Arduino
-
Port Manipulation In Arduino Using Byte Value
-
Arduino And Port Manipulation : 9 Steps - Instructables
-
Atmega 8bit Port Read And Write - Arduino - Element14 Community
-
Serial.write() | Référence Du Langage Arduino En Français
-
Unable To Send Byte From Python Serial.write() To Arduino
-
Serial.write(int) Only Sends The First Byte - Electronics (Arduino, Etc.)
-
Write Byte To Arduino From Ruby Over Serial - ITecNote
-
4. Serial Communications - Arduino Cookbook [Book] - O'Reilly
-
Write Data To Serial Port - MATLAB Write - MathWorks