0

我有一个程序给我如下输出。

Listing 3 device(s) for setup class "Ports" (Ports (COM & LPT)).
USB\VID_067B&PID_2303\5&164808BA&0&3                        : Prolific USB-to-Serial Comm Port (COM3)
USB\VID_068B&PID_2304\5&164808BA&0&3                        : Prolific USB-to-Serial Comm Port (COM4)
USB\VID_069B&PID_2305\5&164808BA&0&3                        : Prolific USB-to-Serial Comm Port (COM6)

我想取出 COM# 值并使用 vbscript 将其分配给一个变量,就像这样

P1=COM3
P2=COM4
P3=COM6

此链接http://vbcity.com/forums/t/154919.aspx的角色正在使用该行获得我想要的输出。

System.IO.Ports.SerialPort.GetPortNames 

但我不确定如何在我的脚本中实现这一点。

谢谢

4

2 回答 2

1

I took your output and saved it in a text file called OUTPUT.TXT, so the command I'm using to run it is:

TYPE OUTPUT.TXT

However, feel free to replace TYPE OUTPUT.TXT with your actual program. The Following VBScript shows how to run this command and capture and parse the output in VBScript. I use INSTR and MID to locate and extract the substrings I need:

Dim objShell
Set objShell = CreateObject("WScript.Shell")
Dim objExec
Set objExec = objShell.Exec("CMD /C TYPE OUTPUT.TXT")
Dim lines
lines = objExec.StdOut.ReadAll
Dim line
Dim P(10)
Dim num
num = 0
For Each line in split(lines, vbCrLf)
  Dim idx
  idx = InStr(line, "Prolific USB-to-Serial Comm Port (")
  If idx > 0 Then
    idx = idx + 34
    Dim idx2
    idx2 = InStr(idx, line, ")")
    num = num + 1
    P(num) = Mid(line, idx, idx2 - idx)
    WScript.Echo "P" & num & "=" & P(num)
  End If
Next
于 2013-10-29T22:05:00.887 回答
0

我在这里看到了你的另一个问题。您应该能够从 Win32_SerialPort 类的 WMI 中获取此信息。这有帮助吗?

On Error Resume Next

strComputer = "."

Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/CIMV2" )
Set colInstances = objWMIService.ExecQuery( "SELECT * FROM Win32_SerialPort" )

If colInstances.Count = 1 Then
    WScript.Echo "1 instance:" & vbCrLf
Else
    WScript.Echo colInstances.Count & " instances:" & vbCrLf
End If

For Each objInstance In colInstances
    WScript.Echo "Availability                : " & objInstance.Availability
    WScript.Echo "Binary                      : " & objInstance.Binary
    WScript.Echo "Capabilities                : " & Join( objInstance.Capabilities, "," )
    WScript.Echo "CapabilityDescriptions      : " & Join( objInstance.CapabilityDescriptions, "," )
    WScript.Echo "Caption                     : " & objInstance.Caption
    WScript.Echo "Description                 : " & objInstance.Description
    WScript.Echo "DeviceID                    : " & objInstance.DeviceID
    WScript.Echo "Name                        : " & objInstance.Name
    WScript.Echo "PNPDeviceID                 : " & objInstance.PNPDeviceID
    WScript.Echo "ProviderType                : " & objInstance.ProviderType
    WScript.Echo
Next
于 2013-10-29T21:04:12.753 回答