Skip to content

Instantly share code, notes, and snippets.

@geetfun
Created March 7, 2011 03:28
Show Gist options
  • Select an option

  • Save geetfun/858018 to your computer and use it in GitHub Desktop.

Select an option

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
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