class Geppeto::Connection::Serial
Attributes
logger[RW]
Public Class Methods
new(port = false, baud = 115200, logger = nil)
click to toggle source
# File lib/connections/serial.rb, line 8 def initialize(port = false, baud = 115200, logger = nil) @logger = logger.nil? ? Logger.new(STDOUT) : logger port ||= `ls /dev`.split("\n").grep(/tty.usb/i).map{|d| "/dev/#{d}"}.first #TODO: Check that this works on RPi/Begalbone OSs @serial = SerialPort.new(port, baud) @serial.read_timeout = 100 sleep 1 begin @logger.debug(@serial.read_nonblock(4096)) rescue EOFError # no data end @logger.info("Pinoccio Serial Connection started: #{port}") end
Public Instance Methods
read(command = nil)
click to toggle source
# File lib/connections/serial.rb, line 35 def read(command = nil) begin raw_result = @serial.read_nonblock(4096) if command.nil? raw_result else cmd = Regexp.escape(command) matches = /#{cmd}\r\n(?<response>.*)\r\n>\s.*/m.match(raw_result) if matches[:response] begin JSON.parse(matches[:response]) rescue matches[:response] end else raw_result end end rescue nil end end
shutdown()
click to toggle source
# File lib/connections/serial.rb, line 22 def shutdown @serial.close if @serial rescue IOError @logger.warn("Pinoccio Serial Connection closed.") end
write(command, ms = nil)
click to toggle source
# File lib/connections/serial.rb, line 28 def write(command, ms = nil) @serial.write command+"\n" ms ||= 0.1 sleep ms read command end