列出可用的串行端口

时间:2015-12-18 02:21:22

标签: ruby serial-port detection

我正在寻找列出可用串口的方法(无论ruby串口gem如何)。现在,我尝试了下面的红宝石:

我知道如何在Python中使用.NET / mono C#,Java,但是我无法在ruby中获取这些信息,任何想法?

1 个答案:

答案 0 :(得分:1)

仅适用于Windows

require 'rubyserial'

def seach_ports
  ports = []
  1.upto 64 do |index|
    begin
      serial = Serial.new  portname = 'COM' + index.to_s
      ports << portname if serial
      serial.close
    rescue  Exception => e
      ports << portname if e.to_s.include? "ACCESS_DENIED"
    end
  end
  return ports 
end

p ports = seach_ports

或端口状态

require 'rubyserial'

def seach_ports
  ports = {}
  1.upto 64 do |index|
    begin
      serial = Serial.new  portname = 'COM' + index.to_s
      ports[portname] = 'is available' if serial
      serial.close
    rescue  Exception => e
      ports[portname] = 'access denied' if e.to_s.include? "ACCESS_DENIED"
    end
  end
  return ports 
end

p seach_ports
相关问题