SerialPort.Open() takes very long time to execute

Posted by narancha on Stack Overflow See other posts from Stack Overflow or by narancha
Published on 2014-08-18T12:15:26Z Indexed on 2014/08/18 16:22 UTC
Read the original article Hit count: 73

Filed under:

I'm having trouble when I'm trying to use the SerialPort.Open() function. Sometimes it opens in 5 seconds and sometimes it takes several minutes.

This is my code:

public void InvokeSerialPortdetectedEvent(string s)
{
    SerialPortDetectEvent.Invoke(this, s); // the invoked funktion is called PortHandeler_SerialPortDetectEvent()
}


void PortHandeler_SerialPortDetectEvent(object sender, string name)
{
    OpenSerialPort(name);
    AddDongleToDeviceList();

}


private void OpenSerialPort(string Name)
{
    if (serialPort1.IsOpen)
    {
        return;
    }

    serialPort1.PortName = Name;
    try
    {
        serialPort1.Open();
        if (serialPort1.IsOpen)
        {
            Console.Write("Open Serialport: " + Name);
        }
    }
    catch (Exception e)
    {
        Console.Write(e.Message);
        Console.Write(e.StackTrace);
    }
}

© Stack Overflow or respective owner