Module: Serialbar::Listener
- Defined in:
- lib/serialbar/listener.rb
Instance Method Summary collapse
-
#listener? ⇒ Boolean
:nodoc: Testing method for class inclusion.
-
#method_missing(id, *args) ⇒ Object
Using method missing to check for the inclusion of the subclass implementation of parse.
-
#parse_missing? ⇒ Boolean
:nodoc: Testing method for presence of parse.
-
#poll(send, lines = 1) ⇒ Object
Poll device by sending string .
-
#poll_every_n_minutes(send, lines = 1, n = 1) ⇒ Object
Poll device with a timer could use clever missing_method stuff here.
-
#poll_every_n_seconds(send, lines = 1, n = 1) ⇒ Object
Poll device with a timer could use clever missing_method stuff here.
- #port_initialized? ⇒ Boolean
-
#run ⇒ Object
Trigger listening on setup serial port.
-
#serial_port ⇒ Object
Direct access to the serialport serial port object.
-
#setup(port, baud = 9600, data_bits = 8, stop_bits = 1, parity = 1) ⇒ Object
Setup the serial port.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(id, *args) ⇒ Object
Using method missing to check for the inclusion of the subclass implementation of parse
97 98 99 100 101 102 103 |
# File 'lib/serialbar/listener.rb', line 97 def method_missing(id, *args) #:nodoc: if id.to_s.eql?("parse") raise ::Exceptions::NoParseMethodError, "Parse method not implemented" else raise NoMethodError end end |
Instance Method Details
#listener? ⇒ Boolean
:nodoc: Testing method for class inclusion
105 106 107 |
# File 'lib/serialbar/listener.rb', line 105 def listener? #:nodoc: Testing method for class inclusion return true #testing method end |
#parse_missing? ⇒ Boolean
:nodoc: Testing method for presence of parse
109 110 111 |
# File 'lib/serialbar/listener.rb', line 109 def parse_missing? #:nodoc: Testing method for presence of parse parse("test string") end |
#poll(send, lines = 1) ⇒ Object
Poll device by sending string
Attributes
-
send- string to send to the serial port -
lines- number of lines to read back
Examples
data = listener.poll(“#001\n”)
read lines not implemented yet
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/serialbar/listener.rb', line 49 def poll(send,lines=1) if port_initialized? begin @sp.write(send) data = @sp.readline rescue Interrupt puts "Exiting" end parse(data) end end |
#poll_every_n_minutes(send, lines = 1, n = 1) ⇒ Object
Poll device with a timer could use clever missing_method stuff here
data is passed as an argument to parse
Attributes
-
send- string to send to the serial port -
lines- number of lines to read back -
n- number of minutes between each poll of the device
89 90 91 92 93 |
# File 'lib/serialbar/listener.rb', line 89 def poll_every_n_minutes(send,lines=1,n=1) timer = Timers.new every_seconds = timer.every(60*n) { parse(poll(send,lines)) } loop { timers.wait } end |
#poll_every_n_seconds(send, lines = 1, n = 1) ⇒ Object
Poll device with a timer could use clever missing_method stuff here
data is passed as an argument to parse
Attributes
-
send- string to send to the serial port -
lines- number of lines to read back -
n- number of seconds between each poll of the device
72 73 74 75 76 |
# File 'lib/serialbar/listener.rb', line 72 def poll_every_n_seconds(send,lines=1,n=1) timer = Timers.new every_seconds = timer.every(n) { parse(poll(send,lines)) } loop { timers.wait } end |
#port_initialized? ⇒ Boolean
113 114 115 116 117 118 119 120 |
# File 'lib/serialbar/listener.rb', line 113 def port_initialized? if @sp.nil? raise ::Exceptions::PortNotInitialized, "Call setup on listener class to initialize serial port" else return true end end |
#run ⇒ Object
Trigger listening on setup serial port
simply reads each line from the port and passes it as string to implemented parse method
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/serialbar/listener.rb', line 22 def run #is the serial port setup? puts "Listening on serial port #{@portname}" if port_initialized? @sp.flush_input begin while data = @sp.readline parse(data) end rescue Interrupt puts "Exiting" end end end |
#serial_port ⇒ Object
Direct access to the serialport serial port object
15 16 17 |
# File 'lib/serialbar/listener.rb', line 15 def serial_port return @sp if port_initialized? end |
#setup(port, baud = 9600, data_bits = 8, stop_bits = 1, parity = 1) ⇒ Object
Setup the serial port
8 9 10 11 12 |
# File 'lib/serialbar/listener.rb', line 8 def setup(port, baud=9600, data_bits=8, stop_bits=1, parity=1) @portname = port @sp = SerialPort.new(@portname,baud,data_bits,stop_bits,parity) @setup = true end |