0

I have a program giving me output like below.

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)

I would like to get the COM# value out and assign it to a variable using vbscript like so

P1=COM3
P2=COM4
P3=COM6

The persona this link http://vbcity.com/forums/t/154919.aspx is getting the output I want using the line.

System.IO.Ports.SerialPort.GetPortNames 

but I am not sure how to implement this in my script.

Thanks

2 Answers 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
Sign up to request clarification or add additional context in comments.

4 Comments

This appears to work, I'll have to wait till I get home to see if it will work with multiple options. Just out of curiosity, what is up with using Dim to note a variable in VBscript. I've read that you're suppose to do it, but never noticed a difference if I don't. Seems like it just adds clutter to the script.
DIM means dimension and, historically, was used to declare the size of arrays in BASIC. However, it's been re-purposed to declare any variable. It's just a matter of good practice, but, VBScript doesn't enforce this practice by default. Generally, when, writing scripts, one should use OPTION EXPLICIT to enforce this behavior. This will trap typos. For example, in the script I wrote, if I mispelt any of my variables, the script would run incorrectly instead of raising an error.
So this works mostly, the problem is that I need to generate a variable to feed into the rest of the script, so instead of having a message box with P1=COM1, P2=COM2, I need it to write the variables P1=COM1. I see that I can't put P(num)=PORT then extract this, after the loop.
If you come up with something that would be nice, in the mean time I just added an "if then" and put in a variable, since the user of the program is likely never going to have more than 3 COMS, this isn't a problem, but would be nice to know how to do this with a loop.
0

I saw your other question on here. You should be able to get this from WMI from the Win32_SerialPort class. Does this help?

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

1 Comment

This script comes up with 0 instances. I think the problem may be that the serial port is actually a USB>serial adapter, so it ma y not be listed under the WIN32_serialport.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.