class XBee::XBee

Either specify the port and serial parameters

xbee = XBee::Xbee.new device_path: '/dev/ttyUSB0', rate: 9600

or pass in a SerialPort like object

xbee = XBee::XBee.new io: some_serial_mockup_for_testing

Public Class Methods

new(device_path: '/dev/ttyUSB0', rate: 115200, io: nil) click to toggle source
# File lib/xbee/xbee.rb, line 37
def initialize(device_path: '/dev/ttyUSB0', rate: 115200, io: nil)
        @device_path = device_path
        @rate = rate
        @io = io
        @connected = false
        @logger = nil
end

Public Instance Methods

close() click to toggle source
# File lib/xbee/xbee.rb, line 57
def close
        @io.close if @io
        @connected = false
end
connected?() click to toggle source
# File lib/xbee/xbee.rb, line 63
def connected?
        @connected
end
Also aliased as: open?
io=(io) click to toggle source
# File lib/xbee/xbee.rb, line 105
def io=(io)
        @io = io
end
open() click to toggle source
# File lib/xbee/xbee.rb, line 46
def open
        @io ||= SerialPort.new @device_path, @rate
        @io_input = Enumerator.new do |y|
                loop do
                        y.yield @io.readbyte
                end
        end
        @connected = true
end
open?()
Alias for: connected?
read_frame() click to toggle source
# File lib/xbee/xbee.rb, line 100
def read_frame
        Frames::Frame.from_packet read_packet
end
read_packet() click to toggle source
# File lib/xbee/xbee.rb, line 93
def read_packet
        Packet.from_byte_enum(@io_input).tap do |packet|
                logger.trace 'Packet received.', bytes: packet.bytes
        end
end
write_frame(frame) click to toggle source
# File lib/xbee/xbee.rb, line 75
def write_frame(frame)
        if frame.packet
                # TODO: Is it right to assume the packet is in sync with the frame?
                write_packet frame.packet
        else
                packet = frame.to_packet
                write_packet packet
        end
end
write_packet(packet) click to toggle source
# File lib/xbee/xbee.rb, line 69
def write_packet(packet)
        @io.write packet.bytes_escaped.pack('C*').force_encoding('ascii')
        @io.flush
end
write_request(request) click to toggle source
# File lib/xbee/xbee.rb, line 86
def write_request(request)
        logger.measure_trace('Packet sent.', payload: { bytes: request.packet.bytes }) do
                write_packet request.packet
        end
end