none
Serial port reconnecting RRS feed

  • Question

  • Hi,

    My application need to communicate a periferial using RS-232. My computer is connected to the periferial by RS232-to-USB of Prolific.

    I’m trying to reconnect after the following scenario:

    I take the RS232 cable out of the computer.

    I run my application and try to connect to the port.

    Of course, the connection fails.

    I connect the RS232 cable to the computer.

    I try to connect the port again.

    And again, I fail.

    The only way I can connect now is by closing the application and running it again.

    It looks like I fail to clean something, but I don't know what...

    My close function looks like this:

    public

    override void Close()

    {

    Log4NetWrapper.Instance.LogDebug("Rs232Communicator", "Closing", "");

    try

    {

    if (m_Port != null && m_Port.IsOpen == true)

    {

    m_Port.Close();

    }

    m_Port.Dispose();

    m_Port =

    null;

    }

    catch (Exception ex)

    {

    System.Windows.Forms.

    MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);

    }

    }

    Sunday, June 9, 2013 8:18 AM

All replies

  • Don't check if it's Open.  Close it and ignore the exception.  That is "I take the RS232 cable out of the computer.  I run my application and try to connect to the port.  Of course, the connection fails.  I connect the RS232 cable to the computer.  I try to connect the port again.  And again, I fail."

    Close the port and ignore the exception and you should be able to Open the port.

    Sunday, June 9, 2013 8:42 AM
  • I'm afraid it did not help. Thank you anyway.
    Monday, June 10, 2013 12:13 PM
  • Then your code is in error.  For a more complete solution with automatic closure and opening of the port, search my threads for "SearialPort Plug Unplug" or similar.

    Monday, June 10, 2013 1:10 PM