class NXT::Interface::SerialPort

Implements serial port connectivity to the NXT 2.0 module.

Constants

BAUD_RATE
DATA_BITS
PARITY
READ_TIMEOUT
STOP_BITS

Attributes

dev[R]

Public Class Methods

new(dev) click to toggle source
Calls superclass method
# File lib/nxt/interface/serial_port.rb, line 19
def initialize(dev)
  super()
  self.dev = dev
end

Public Instance Methods

connect() click to toggle source
# File lib/nxt/interface/serial_port.rb, line 30
def connect
  @connection = ::SerialPort.new(@dev, BAUD_RATE, DATA_BITS, STOP_BITS, PARITY)

  raise SerialPortConnectionError, "Could not establish a SerialPort connection to #{dev}" if @connection.nil?

  @connection.flow_control = ::SerialPort::HARD
  @connection.read_timeout = READ_TIMEOUT

  @connection
rescue ArgumentError
  raise SerialPortConnectionError, "The #{dev} device is not a valid SerialPort"
end
connected?() click to toggle source
# File lib/nxt/interface/serial_port.rb, line 47
def connected?
  @connection && !@connection.closed?
end
dev=(dev) click to toggle source
# File lib/nxt/interface/serial_port.rb, line 24
def dev=(dev)
  raise InvalidDeviceError unless File.exist?(dev)

  @dev = dev
end
disconnect() click to toggle source
# File lib/nxt/interface/serial_port.rb, line 43
def disconnect
  @connection.close if connected?
end
receive() click to toggle source
# File lib/nxt/interface/serial_port.rb, line 69
def receive
  # This gets the length of the received data from the header that was sent
  # to us. We unpack it, as it's stored as a 16-bit Little Endian number.
  #
  # Reference: Appendix 1, Page 22
  length = @connection.sysread(2)
  @connection.sysread(length.unpack1('v')).from_hex_str
end
send(msg) click to toggle source
# File lib/nxt/interface/serial_port.rb, line 51
def send(msg)
  # The expected data package structure for NXT Bluetooth communication is:
  #
  #     [Length Byte 1, Length Byte 2, Command Type, Command, ...]
  #
  # So here we calculate the two leading length bytes, and rely on the
  # passed in argument to give us the rest of the message to send.
  #
  # Note that the length is stored in Little Endian ie. LSB -> MSB
  #
  # Reference: Appendix 1, Page 22
  msg = [(msg.length & 255), (msg.length >> 8)] + msg

  msg.each do |b|
    @connection.putc(b)
  end
end