How to know on which serial port external device is connected and read data from that device?
<p>I have attached 1 device which generated some random number and i want to read that number through my <strong>Windows application</strong>.</p>

<p>I am using this code:</p>

<pre><code>public static void Main()
{
    SerialPort mySerialPort = new SerialPort("COM19");

    mySerialPort.BaudRate = 115200;
    mySerialPort.Parity = Parity.None;
    mySerialPort.StopBits = StopBits.One;
    mySerialPort.DataBits = 8;
    mySerialPort.Handshake = Handshake.None;

    mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

    mySerialPort.Open();

    Console.WriteLine("Press any key to continue...");
    Console.WriteLine();
    Console.ReadKey();
    mySerialPort.Close();
}

private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
    SerialPort sp = (SerialPort)sender;
    string indata = sp.ReadExisting();
    Debug.Print("Data Received:");
    Debug.Print(indata);
}
</code></pre>

<p>With this code I am successfully getting data.</p>

<p>But problem here is I have hardcoded my <code>com port(Com19)</code> that is from device manager I have checked that to which port my device is connected so I have hardcoded that but I don't want to do this.</p>

<p>Instead I will give user <code>1 dropdown</code> in which user will see only that port to which user device is connected. So when fetching data from this connected device I will use that user selected dropdown port.</p>

<p>I am very much new to windows application and never done anything related to serial port.</p>