How to get Usb ComPorts in C#?

Janardhan Mukkamala 0 Reputation points
2023-12-14T09:35:37.8566667+00:00

Basically I'm working on a small project which involve serial message transfer. I tried GetPortNames() method in C#. It sends Standard Serial Over Bluetooth links instead of Com Ports or USB Ports what to do?

Developer technologies | Windows Forms
Windows for business | Windows Client for IT Pros | User experience | Other
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 91,506 Reputation points
    2023-12-14T10:49:26.42+00:00

    You can get anything with SetupDi APIs and use the right GUID to filter them

    For example with GUID_DEVINTERFACE_COMPORT :

                    //IntPtr hDeviceInfo = SetupDiGetClassDevs(ref GUID_DEVINTERFACE_USB_HUB, IntPtr.Zero, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
                    IntPtr hDeviceInfo = SetupDiGetClassDevs(ref GUID_DEVINTERFACE_COMPORT, IntPtr.Zero, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
                    if (hDeviceInfo != IntPtr.Zero)
                    {
                        var DeviceInfoData = new SP_DEVINFO_DATA() { cbSize = Marshal.SizeOf(typeof(SP_DEVINFO_DATA)) };
                        int nNumberOfDevicesPresent = 0;
                        while (SetupDiEnumDeviceInfo(hDeviceInfo, nNumberOfDevicesPresent, ref DeviceInfoData))
                        {
                            StringBuilder sbDeviceName = new StringBuilder(260); 
                            bool bRet = SetupDiGetDeviceRegistryProperty(hDeviceInfo, ref DeviceInfoData, SPDRP_FRIENDLYNAME, 0, sbDeviceName, (uint)sbDeviceName.Capacity, IntPtr.Zero);
                            if (bRet)
                            {
                                Console.WriteLine("Device Name : {0}", sbDeviceName.ToString());                           
                            }
                            else
                            {
                                bRet = SetupDiGetDeviceRegistryProperty(hDeviceInfo, ref DeviceInfoData, SPDRP_DEVICEDESC, 0, sbDeviceName, (uint)sbDeviceName.Capacity, IntPtr.Zero);
                                if (bRet)
                                {                                
                                    Console.WriteLine("Device Description : {0}", sbDeviceName.ToString());                               
                                }
                            }
                            nNumberOfDevicesPresent++;
                        }
                    }
    

    With :

          public static Guid GUID_DEVINTERFACE_COMPORT = new Guid("{86E0D1E0-8089-11D0-9CE4-08003E301F73}");
          public static Guid GUID_DEVINTERFACE_USB_HUB = new Guid("{f18a0e88-c30c-11d0-8815-00a0c906bed8}");
    
          [DllImport("Setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
          public static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, IntPtr Enumerator, IntPtr hWndParent, int Flags);
    
          public const int DIGCF_PRESENT = 0x00000002;
          public const int DIGCF_DEVICEINTERFACE = 0x00000010;
          public const int ERROR_INSUFFICIENT_BUFFER = 122;
          
        	#if WIN64
            public const int PACK_SIZE = 8;
    			#else
            public const int PACK_SIZE = 1;
    			#endif
          [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = PACK_SIZE)]
          public struct SP_DEVINFO_DATA
          {
              public int cbSize;
              public Guid ClassGuid;
              public int DevInst;
              public IntPtr Reserved;
          }
          
          [DllImport("Setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
          public static extern bool SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, int MemberIndex, ref SP_DEVINFO_DATA DeviceInfoData);
    
          [DllImport("Setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
          public static extern bool SetupDiGetDeviceRegistryProperty(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, uint Property,
              uint PropertyRegDataType, StringBuilder PropertyBuffer, uint PropertyBufferSize, IntPtr RequiredSize);
    
          public const int SPDRP_DEVICEDESC = (0x00000000);  // DeviceDesc (R/W)
          public const int SPDRP_FRIENDLYNAME = (0x0000000C); // FriendlyName (R/W)
          public const int SPDRP_LOCATION_INFORMATION = (0x0000000D);  // LocationInformation (R/W)
    
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.