I have VB6 code which I inherited and modified to run an instrument and calculate information from the collected data. That code is distributed to many locations throughout my company, but the personnel at those locations are not skilled in adjusting settings. On older PCs, the instruments were connected to 9-pin serial ports. The newer PCs which my company is purchasing replace the 9-pin serial with USB ports. I use a USB-to-serial converter to connect the instrument. Can anyone provide VB6 code that will allow me to identify the comm port number of each USB-to-serial converter and find what instrument is attached to it?

I know the instruments and procedures well, but I am not skilled with minipulating VB6. I would very much appreciate some help.

Dani AI

Generated

Two practical, reliable options when adding support for USB-to-serial adapters without changing corporate hardware policies (as suggested as a hardware alternative): 1) enumerate the virtual COM ports and their device IDs, and 2) probe each COM port with the instrument's identification command. The device-side identity comes from the adapter's PnPDeviceID (VID/PID and, when present, the adapter serial) or from the instrument's protocol response.

Use WMI in VB6 to list COM names plus the PnPDeviceID (this works for most USB-serial drivers). The Win32SerialPort class returns DeviceID (like COM3) and PNPDeviceID you can parse for VID and PID_. Example VB6 snippet:

Dim objWMI As Object
Dim colPorts As Object
Dim objPort As Object

Set objWMI = GetObject("winmgmts:\\.\root\CIMV2")
Set colPorts = objWMI.ExecQuery("SELECT DeviceID, PNPDeviceID, Description FROM Win32_SerialPort")
For Each objPort In colPorts
    Debug.Print objPort.DeviceID & " | " & objPort.PNPDeviceID & " | " & objPort.Description
Next

To identify the instrument, open each reported COM port and send the instrument's ID/query string (SCPI instruments respond to *IDN? for example). A simple MSComm-based probe (open port, write ID command, wait a short timeout, read response) will confirm which instrument is attached:

' MSComm must be on form as MSComm1
With MSComm1
  .CommPort = 3
  .Settings = "9600,N,8,1"
  .InputLen = 0
  .RThreshold = 1
  .PortOpen = True
End With

MSComm1.Output = "*IDN?" & vbCrLf
' wait/read logic here...
MSComm1.PortOpen = False

Notes and cautions: some adapters do not expose a unique serial in PNPDeviceID, so identical adapters may be indistinguishable unless you query the instrument. Windows can reassign COM numbers on different machines; store a mapping of PNPDeviceID -> logical instrument name after detection for reproducibility. If Win32_SerialPort returns nothing on some OS/driver combos, query Win32_PnPEntity for captions containing “(COM” or use the SetupAPI device interface GUID for ports. See Win32_SerialPort and Win32_PnPEntity for details: Win32_SerialPort and Win32_PnPEntity.

Recommended Answers

All 2 Replies

Hi,

I have tried to use USB to Serial converters withpout coding them and find them unreliable.

I therefore suggest you get your company to put serial cards in the new PCs. This means you have same code running on all machines, same cables, etc.

Best of luck

Denis

Thanks, but that may not be a possibility with current corporate IT directives.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.