Using the code below I can create a box with a combo box that shows the current com ports What I need to do is show what is attached to the com Port, for example I want it to list COM PORT1 FTDI USB serial adapter, the reason is to let you the user know which port to enter in a batch file that runs when another button is clicked ( i have removed that part of the code as its not important) I have done some google work and found this link http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/331a26c1-0f42-4cf1-8adb-32fb09a18953/ But that just errors out
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Public Class Form1
'------------------------------------------------
Dim myPort As Array
Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading
errors during receiveing of data
'------------------------------------------------
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
myPort = IO.Ports.SerialPort.GetPortNames()
ComboBox1.Items.AddRange(myPort)
End Sub
End Class
The following shows how to get a list of COM devices in VB.NET for both .NET 6 and .NET Framework 4.8 in VS 2022. If a USB COM (serial port) device is added/removed, the ComboBox will be updated.
Option 1 - Windows Forms App: (.NET 6)
Create a new project:
Windows Forms App
(name: SerialPortGetComDevices)Download/install the following NuGet packages:
System.IO.Ports
System.Management
Option 2 - Windows Forms App (.NET Framework) - v4.8:
Create a new project:
Windows Forms App (.NET Framework)
(name: SerialPortGetComDevices)Add Reference:
The instructions below are the same for both
Windows Forms App
andWindows Forms App (.NET Framework)
(ie: the instructions below are the same regardless of which option you've chosen above).Create a class (name: ComPortInfo.vb)
ComPortInfo.vb:
Open Solution Explorer:
Open Properties Window:
Add
Load
Event HandlerForm1.vb
and selectView Designer
.Add
FormClosing
Event HandlerForm1.vb
and selectView Designer
.Add a ComboBox to the Form (name: ComboBoxComPorts)
(Name)
toComboBoxComPorts
DropDownStyle
toDropDownList
Select one of the options below. The 1st option uses
ManagementEventWatcher
to detect USB device insertion and removal. The 2nd option overridesWndProc
.Note: The
WndProc
version (Option 2) seems to have slightly better performance.Option 1 (ManagementEventWatcher)
Note: The code for detecting the insertion and removal of a USB device, is adapted from here.
Add the code below in your Form (ex: Form1.vb)
Form1.vb
Option 2 (override WndProc)
Note: The code for detecting the insertion and removal of a USB device, is adapted from here.
Add a Module (name: UsbDeviceNotification.vb)
UsbDeviceNotification.vb
Add the code below in your Form (ex: Form1.vb)
Form1.vb
The following PowerShell commands may also provide useful information.
PowerShell:
Get-CimInstance -Namespace Root\Cimv2 -Query "Select * From Win32_SerialPort Where Name like '%COM%'"
Get-CimInstance -Namespace Root\Cimv2 -Query "Select * From Win32_SerialPortConfiguration"
Get-CimInstance -Namespace Root\Cimv2 -Query "Select * From Win32_PnPEntity where PnPClass = 'Ports' and Name like '%COM%'"
mode
Resources: