module Serialbar::Listener
Public Instance Methods
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
# 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 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
# File lib/serialbar/listener.rb, line 89 def poll_every_n_minutes(send,lines=1,n=1) timer = Timers::Group.new every_seconds = timer.every(60*n) { parse(poll(send,lines)) } loop { timers.wait } end
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
# File lib/serialbar/listener.rb, line 72 def poll_every_n_seconds(send,lines=1,n=1) timer = Timers::Group.new every_seconds = timer.every(n) { parse(poll(send,lines)) } loop { timers.wait } end
# File lib/serialbar/listener.rb, line 113 def port_initialized? if @sp.nil? raise Serialbar::Exceptions::PortNotInitialized, "Call setup on listener class to initialize serial port" else return true end end
Trigger listening on setup serial port
simply reads each line from the port and passes it as string to implemented parse method
# 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
Direct access to the serialport serial port object
# File lib/serialbar/listener.rb, line 15 def serial_port return @sp if port_initialized? end
Setup the serial port
# 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