Class: Sim800c::PortScanner

Inherits:
Object
  • Object
show all
Defined in:
lib/sim800c/port_scanner.rb

Class Method Summary collapse

Class Method Details

.each_candidate(&block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sim800c/port_scanner.rb', line 26

def self.each_candidate(&block)
  ports =
    case RUBY_PLATFORM
    when /linux/
      Dir.glob('/dev/ttyUSB*') + Dir.glob('/dev/ttyACM*')
    when /darwin/
      Dir.glob('/dev/tty.usbmodem*') + Dir.glob('/dev/tty.usbserial*')
    when /mswin|mingw/
      (1..20).map { |i| "COM#{i}" }
    else
      []
    end

  ports.each(&block).first
end

.find_portObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sim800c/port_scanner.rb', line 5

def self.find_port
  each_candidate do |port|
    serial = Serial.new(port, 9_600)
    serial.write("AT\r")
    sleep 0.3
    response = serial.read(64).to_s
    next unless response.include?('OK')

    serial.write("ATI\r")
    sleep 0.3
    info = serial.read(128).to_s
    serial.close

    return port if info =~ /SIM[-_ ]?800C/i
  rescue StandardError
    false
  ensure
    serial&.close
  end
end