Class: Vmopt::SerialPortOperation

Inherits:
Object
  • Object
show all
Defined in:
lib/vmopt/serialport_operation.rb

Instance Method Summary collapse

Instance Method Details

#get_serial_portObject

参数:无功能:查询串口的基本信息返回值:默认



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/vmopt/serialport_operation.rb', line 13

def get_serial_port 
    data_value={} 
  colItems = WMI.execquery ("select * from Win32_SerialPort")
  for objItem in colItems do
    name = (objItem.Name)[-5,4]
    str ={"串口名称" => name,
          "状态" => objItem.Status
         }      
    data_value["#{name}"] = str
  end
  return data_value
end

#read(strcom) ⇒ Object

参数:串口号功能:从串口读入字符串返回值:读到的com口字符串



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/vmopt/serialport_operation.rb', line 46

def read(strcom)
  begin
  stroutput=""
    sp = SerialPort.new "#{strcom}", 9600
      sp.read_timeout=4000 #定时4秒

      stroutput = sp.read(50)
      if stroutput.empty?
       return false
      end
  rescue Exception
    return false
  end
  return stroutput
end

#write(strcom, strinput) ⇒ Object

参数:串口号,写入的字符串功能:将串口写入字符串返回值:默认



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vmopt/serialport_operation.rb', line 30

def write(strcom,strinput)
  strinput
  begin
  File.open(strcom, 'w+') do |file|
    file.write(strinput)
    end
  rescue Exception
    return false
  end
  return true
end