Class: SocketSwitcher::Port

Inherits:
Object
  • Object
show all
Defined in:
lib/socket_switcher/port.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(specifier, debug: false, timeout: 2_000, attempts: 3) ⇒ Port

Returns a new instance of Port.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/socket_switcher/port.rb', line 5

def initialize(specifier, debug: false, timeout: 2_000, attempts: 3)
  @serial_port = SerialPort.new(
    specifier,
    "baud"      => 9600,
    "data_bits" => 8,
    "stop_bits" => 1,
    "parity"    => 0
  )
  @debug    = debug
  @timeout  = timeout
  @attempts = attempts
  @devices  = {}
  @mutex    = Mutex.new
rescue => e
  raise SocketSwitcher::CommunicationError.wrap(nil, e)
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



22
23
24
# File 'lib/socket_switcher/port.rb', line 22

def debug
  @debug
end

#timeoutObject

Returns the value of attribute timeout.



24
25
26
# File 'lib/socket_switcher/port.rb', line 24

def timeout
  @timeout
end

Instance Method Details

#device(number) ⇒ Object



47
48
49
# File 'lib/socket_switcher/port.rb', line 47

def device(number)
  @devices[number] ||= SocketSwitcher::Device.new(self, number)
end

#devices(range = nil) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/socket_switcher/port.rb', line 38

def devices(range = nil)
  if range
    devices.select { |d| range.member?(d.number) }
  else
    ready?
    @devices.values
  end
end

#ready?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
# File 'lib/socket_switcher/port.rb', line 26

def ready?
  request do |response|
    if response =~ /^RDY (\d+) (\d+)/
      # initalize all devices
      ($1.to_i..$2.to_i).each { |i| device(i) }
      return true
    else
      try_again or return false
    end
  end
end