Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Hardware & Devices
  4. list com ports

list com ports

Scheduled Pinned Locked Moved Hardware & Devices
com
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SL555
    wrote on last edited by
    #1

    Hello How I can receive the list of all com ports

    M 1 Reply Last reply
    0
    • S SL555

      Hello How I can receive the list of all com ports

      M Offline
      M Offline
      MatrixCoder
      wrote on last edited by
      #2

      Just go to Control Panel - System - Device Manager - COM Ports, unless you meant something else.


      Trinity: Neo... nobody has ever done this before. Neo: That's why it's going to work.

      S 1 Reply Last reply
      0
      • M MatrixCoder

        Just go to Control Panel - System - Device Manager - COM Ports, unless you meant something else.


        Trinity: Neo... nobody has ever done this before. Neo: That's why it's going to work.

        S Offline
        S Offline
        SL555
        wrote on last edited by
        #3

        That's something at least. But I want receive the list of all com ports from my program. To use only WinApi.

        E L 2 Replies Last reply
        0
        • S SL555

          That's something at least. But I want receive the list of all com ports from my program. To use only WinApi.

          E Offline
          E Offline
          Ed Poore
          wrote on last edited by
          #4

          Enumerate the HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM registry key.

          1 Reply Last reply
          0
          • S SL555

            That's something at least. But I want receive the list of all com ports from my program. To use only WinApi.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Hi, it is rather simple to try a series of port names, as in the following code:

            // get a list of existing serial ports
            public static object[] GetSerialPorts() {
            ArrayList ports=new ArrayList();
            byte[] bytes=new byte[1024];
            for (int port=1; port<30; port++) {
            string portName="COM"+port;
            int size=1024;
            // getting default settings succeeds if device exists,
            // independent of port wnership
            bool exists=GetDefaultCommConfig(portName, bytes, ref size);
            if (exists) ports.Add(portName);
            }
            return ports.ToArray();
            }

            // get Windows port parameters (fails for non-existing ports)
            [DllImport("kernel32.dll", CharSet=CharSet.Ansi,
            CallingConvention=CallingConvention.Cdecl)]
            public static extern bool GetDefaultCommConfig(string portName, byte[] buf,ref int bufSize);

            The above works on all .NET versions. Starting with .NET 2.0 you can use SerialPort.GetPortNames() to get all serial port names (also those that are not COM#). :)

            Luc Pattyn [My Articles]

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups