getting usb com port

Posted by I__ on Stack Overflow See other posts from Stack Overflow or by I__
Published on 2010-06-16T20:49:36Z Indexed on 2010/06/16 20:52 UTC
Read the original article Hit count: 189

Filed under:

i have this function:

private string GetUSBComPort()
{
    string[] sPorts = SerialPort.GetPortNames();
    foreach (string port in sPorts)
    {
        if (port != "COM1"
            && port != "COM4")
        {
            return port; 
        }
    }

    return null; 
}

this function is being called from form load:

private void Form1_Load(object sender, EventArgs e)
{
    serialPort1 = new SerialPort(GetUSBComPort());

    if (serialPort1.IsOpen)
    {
        serialPort1.Close();
    }

    serialPort1.Open();

    //ThreadStart myThreadDelegate = new ThreadStart(ReceiveAndOutput);
    //Thread myThread = new Thread(myThreadDelegate);
    //myThread.Start();

    this.serialPort1.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
}

the function is unfortunately returning null

how do i get the com port to which my phone is attached to via USB?

© Stack Overflow or respective owner

Related posts about c#