public
Description: A simple serial port library for Ruby
Homepage: http://github.com/Floppy/rb232
Clone URL: git://github.com/Floppy/rb232.git
rb232 / examples / listen.rb
100644 47 lines (36 sloc) 1.087 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env ruby
 
dir = File.dirname(__FILE__) + '/../lib'
$LOAD_PATH << dir unless $LOAD_PATH.include?(dir)
 
require 'rubygems'
require 'rb232'
require 'rb232/text_protocol'
 
class SimpleClient
  def update(message)
    puts("Message received:")
    puts(message)
  end
end
 
require 'optparse'
 
# Command-line options - just baud rate and separator for now
options = {:port => '/dev/ttyS0', :baud_rate => 9600, :separator => "\n"}
OptionParser.new do |opts|
  opts.on("-p", "--serial_port SERIAL_PORT", "serial port") do |p|
    options[:port] = p
  end
  opts.on("-b", "--baud_rate BAUD_RATE", "baud rate") do |b|
    options[:baud_rate] = b.to_i
  end
  opts.on("-s", "--separator SEPARATOR", "message separator character") do |s|
    options[:separator] = s
  end
end.parse!
 
port = RB232::Port.new(options[:port], :baud_rate => options[:baud_rate].to_i)
 
protocol = RB232::TextProtocol.new(port, options[:separator])
client = SimpleClient.new
protocol.add_observer(client)
 
# Start receiving messags
protocol.start
 
# Wait a while
sleep(30)
 
# OK, finish now
protocol.stop