How to find available COM ports?

How to find available COM ports on my PC? I am using framework v1.1. Can I find all COM ports? If possible, help me solve the problem.

+12
c # serial-port
Jul 04 '09 at 9:25
source share
8 answers

Like others, you can use WMI. You can find a sample in CodeProject

try { ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSSerial_PortName"); foreach (ManagementObject queryObj in searcher.Get()) { Console.WriteLine("-----------------------------------"); Console.WriteLine("MSSerial_PortName instance"); Console.WriteLine("-----------------------------------"); Console.WriteLine("InstanceName: {0}", queryObj["InstanceName"]); Console.WriteLine("-----------------------------------"); Console.WriteLine("MSSerial_PortName instance"); Console.WriteLine("-----------------------------------"); Console.WriteLine("PortName: {0}", queryObj["PortName"]); //If the serial port instance name contains USB //it must be a USB to serial device if (queryObj["InstanceName"].ToString().Contains("USB")) { Console.WriteLine(queryObj["PortName"] + " is a USB to SERIAL adapter/converter"); } } } catch (ManagementException e) { Console.WriteLine("An error occurred while querying for WMI data: " + e.Message); } 
+10
Jul 04 '09 at 12:02
source share

Framework v1.1 AFAIK does not allow you to do this.

There is a static function in 2.0

 SerialPort.GetPortNames() 

http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.getportnames.aspx

+30
Jul 04 '09 at 9:51
source share

Available serial ports can also be found in the values ​​in HKEY_LOCAL_MACHINE\hardware\devicemap\serialcomm in the registry.

+8
Jul 04 '09 at 11:16
source share

How about asking a direct question from the operating system:

 using System; using System.Collections.Generic; using Microsoft.Win32; using System.Runtime.InteropServices; using Microsoft.Win32.SafeHandles; public class MyClass { private const uint GENERIC_ALL = 0x10000000; private const uint GENERIC_READ = 0x80000000; private const uint GENERIC_WRITE = 0x40000000; private const uint GENERIC_EXECUTE = 0x20000000; private const int OPEN_EXISTING = 3; public const int INVALID_HANDLE_VALUE = -1; public static void Main() { for (int i = 1; i <= 32; i++) Console.WriteLine ("Port {0}: {1}", i, PortExists (i)); } private static bool PortExists (int number) { SafeFileHandle h = CreateFile (@"\\.\COM" + number.ToString (), GENERIC_READ + GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); bool portExists = !h.IsInvalid; if (portExists) h.Close (); return portExists; } [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] private static extern SafeFileHandle CreateFile (string lpFileName, System.UInt32 dwDesiredAccess, System.UInt32 dwShareMode, IntPtr pSecurityAttributes, System.UInt32 dwCreationDisposition, System.UInt32 dwFlagsAndAttributes, IntPtr hTemplateFile); } 
+2
Jul 04 '09 at 11:34
source share

WMI contains a lot of hardware information. Request for Win32_SerialPort instances.

(OTOH I can't remember how much support for WMI requests was in .NET 1.1.)

+1
Jul 04 '09 at 10:05
source share

SerialPort support is missing in .net v1.1. The most common solution for this was to use the active X-control MSCOMMCTL from the VB6.0 installation (import into your .net project as a COM component from the add link dialog box).

In later versions, Serial Port support is available through the System.IO.Ports namespace. Also note that there is no API that will provide you with a list of free ports.

You can get a list of all the port names, and then try to open the connection. An exception occurs if the port is already in use.

+1
Jul 04 '09 at 10:15
source share

Since you are using .net 1.1, one of them is to use the AxMSCommLib control.

Here is the web page that helped me start using the AxMSCommLib control. There is even a FindDevicePort () method that can be easily changed.

Since then, I switched to System.IO.Ports, which looks much more reliable.

http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=320

thank

Joe

+1
Aug 21 '09 at 16:25
source share

Use the QueryDosDevice API QueryDosDevice . This is a fragment of VB6:

  ReDim vRet(0 To 255) sBuffer = String(100000, 1) Call QueryDosDevice(0, sBuffer, Len(sBuffer)) sBuffer = Chr$(0) & sBuffer For lIdx = 1 To 255 If InStr(1, sBuffer, Chr$(0) & "COM" & lIdx & Chr$(0), vbTextCompare) > 0 Then vRet(lCount) = "COM" & lIdx lCount = lCount + 1 End If Next 
0
Jul 04 '09 at 11:01
source share



All Articles