locked
c# serial port enumeration RRS feed

  • Question

  • I need to enumerate the available serial ports. I'm using c#. Anyone know how?

    cheers,

    gareth

    Thursday, August 24, 2006 6:19 PM

Answers

  • try this:

     

    string[] theSerialPortNames = System.IO.Ports.SerialPort.GetPortNames();

     

    that should return you a list of valid serial ports

    Thursday, August 24, 2006 6:29 PM
  • You can, just do what GetPortNames() does: enumerate the values in HKLM\Hardware\DeviceMap\SerialComm registry key.
    Wednesday, September 20, 2006 6:40 PM

All replies

  • try this:

     

    string[] theSerialPortNames = System.IO.Ports.SerialPort.GetPortNames();

     

    that should return you a list of valid serial ports

    Thursday, August 24, 2006 6:29 PM
  • Spot on. Thanks, you're a star.
    Thursday, August 24, 2006 7:28 PM
  • no worries, glad I could help :-)
    Thursday, August 24, 2006 7:42 PM
  •  ahmedilyas wrote:

    try this:

    string[] theSerialPortNames = System.IO.Ports.SerialPort.GetPortNames();

    that should return you a list of valid serial ports



    this works with fw 2.0....how can I do the same thing with 1.1?

    Thanks
    Wednesday, September 20, 2006 10:01 AM
  •  ahmedilyas wrote:

    try this:

    string[] theSerialPortNames = System.IO.Ports.SerialPort.GetPortNames();

    that should return you a list of valid serial ports



    this works with .NET 2.0....how can I do the same thing with 1.1?

    Thanks

    Wednesday, September 20, 2006 10:03 AM
  •  ahmedilyas wrote:

    try this:

    string[] theSerialPortNames = System.IO.Ports.SerialPort.GetPortNames();

    that should return you a list of valid serial ports



    this works with .NET 2.0....how can I do the same thing with 1.1?

    Thanks
    Wednesday, September 20, 2006 10:04 AM
  • you can't - SerialPorts class was made for .NET 2.0/included in .NET 2.0. I guess you would have to use some Win32 API calls to get the serial ports in .NET 1.1
    Wednesday, September 20, 2006 2:12 PM
  • You can, just do what GetPortNames() does: enumerate the values in HKLM\Hardware\DeviceMap\SerialComm registry key.
    Wednesday, September 20, 2006 6:40 PM
  • System.IO.Ports is a namespace/class that was added in .NET 2.0 and not available in .NET 1.0/1.1. But as suggested you can go through the reg keys as NoBugz has shown - did not mean that no its not possible end of - :-)
    Wednesday, September 20, 2006 7:00 PM
  • How about setting the Port.For example a printer to a COM1 or LPT1 or COM2 ...etc. vise versa.

    Can C# be able to assign Port programmatically?

     

    An example is on your WinXP when you :

    1. click Start  and click Printer and faxes.

    2. Right click any of the printer installed in your local machine and click Properties.

    3. It will show "<Your Local Machine Printer name> Properties."

    4. Click on the Ports tab and there it will show the following port(s) or the enumerated ports.

    5. If I check or choose any of the Port e.g. COM1, COM2 ..etc ... LPT1, LPT2.. etc.. .. FILE:, USB ..etc .etc

    QUESTION: Can C# do this programmatically? Is there any windows API to programmatically select or put a check mark on the Printer Port selected.  Of course alot of the forums as showing the EnumPorts

    [DllImport("winspool.drv", EntryPoint = "EnumPorts")]

    public static extern int EnumPortsA(string pName, int Level, int lpbPorts, int cbBuf, ref int pcbNeeded, ref int pcReturned);

    But this does not help me solve my issue.

     

    Thank you for hearing me out. Hope anyone can share their coding experience regarding this issue.

     

    you may email or send me a reply : janverge@gmail.com

     

     

     

     

     

    Wednesday, January 24, 2007 3:39 AM
  • You'd better start a new thread with this question.  It is completely unrelated to this thread.
    Wednesday, January 24, 2007 6:39 AM
  • And just for anyone that is needing it, for enumerating these into a combo Box, you can use this:

                string[] Ports = System.IO.Ports.SerialPort.GetPortNames();
                foreach (string port in Ports)
                {
                    cmb_serialPorts.Items.Add(port);
                }
    you can use this else where, just substitute the for each statement with something else.

    Monday, April 2, 2012 6:18 PM
  • And just for anyone that is needing it, for enumerating these into a combo Box, you can use this:

                string[] Ports = System.IO.Ports.SerialPort.GetPortNames();
                foreach (string port in Ports)
                {
                    cmb_serialPorts.Items.Add(port);
                }

    you don't need the foreach; assuming combo is a WPF ComboBox, (and using System.IO.Ports)

    combo.ItemsSource = SerialPort.GetPortNames();

    Friday, May 4, 2012 7:52 AM