Created
March 7, 2011 03:28
-
-
Save geetfun/858018 to your computer and use it in GitHub Desktop.
Simple Ruby program that uses serialport gem to connect with a serial-attached device
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
| require 'serialport' | |
| require 'highline/import' | |
| class HighLine | |
| public :get_character | |
| end | |
| class ThreadedTerminal | |
| class << self | |
| MODEM = '/dev/tty.usbmodem0000001' | |
| def start | |
| @sp = SerialPort.new(MODEM) | |
| @sp.read_timeout = 500 | |
| @output = [] | |
| @alive = true | |
| receiver_thread = Thread.new do | |
| while @alive do | |
| data = @sp.getc | |
| if not data.nil? | |
| @output << data | |
| end | |
| end | |
| end | |
| writer_thread = Thread.new do | |
| input = HighLine.new | |
| while @alive do | |
| while (c = input.get_character) != ?\e do | |
| if not c.nil? | |
| @output << c.chr | |
| end | |
| @sp.write(c.chr) | |
| end | |
| end | |
| end | |
| display_thread = Thread.new do | |
| while @alive do | |
| @output.each do |character| | |
| print "#{character}" | |
| @output = @output.drop(1) | |
| end | |
| end | |
| end | |
| [display_thread, receiver_thread, writer_thread].each(&:join) | |
| @sp.close | |
| end | |
| end | |
| end | |
| ThreadedTerminal.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment