Class: SerialPortProfile

Inherits:
Object
  • Object
show all
Defined in:
lib/communication/serial_port_profile.rb

Instance Method Summary collapse

Constructor Details

#initialize(device, serial_port_connection_class = SerialPort) ⇒ SerialPortProfile

Returns a new instance of SerialPortProfile.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
# File 'lib/communication/serial_port_profile.rb', line 7

def initialize(device, serial_port_connection_class=SerialPort)
  raise ArgumentError.new('device must be non-nil') if device.nil?

  @device = device
  @connection_class = serial_port_connection_class
  @connection = nil
end

Instance Method Details

#connectObject



15
16
17
# File 'lib/communication/serial_port_profile.rb', line 15

def connect
  establish_connection if @connection.nil?
end

#disconnectObject



19
20
21
# File 'lib/communication/serial_port_profile.rb', line 19

def disconnect
  @connection.close unless @connection.nil? || @connection.closed?
end

#receive_data_package(max_length = (64+2)) ⇒ Object



30
31
32
33
34
35
# File 'lib/communication/serial_port_profile.rb', line 30

def receive_data_package(max_length=(64+2))
  # No intelligence here, just read the max length
  # (default is 64 bytes plus 2 length bytes for bluetooth, but that is leaking abstractions)
  # As long as there's SOMETHING on the wire, there will be no EOFError
  @connection.sysread(max_length)
end

#send_data_package(package) ⇒ Object



23
24
25
26
27
28
# File 'lib/communication/serial_port_profile.rb', line 23

def send_data_package(package)
  # No intelligence here, just send the package
  package.each do |byte|
    @connection.putc byte
  end
end

#settingsObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/communication/serial_port_profile.rb', line 37

def settings
  {
    'baud' => 57600,
    'data_bits' => 8,
    'stop_bits' => 1,
    'parity' => ::SerialPort::NONE,
    'flow_control' => ::SerialPort::HARD,
    'read_timeout' => 5000
  }

end