0

I'm using this(link) library to detect attach and detach events of USB devices. After detecting the right device attached(identified with specific VID/PID combination) I open a serial port connection and similarly when the device is detached I close the serial port connection.

My attach event works great and at the end I get a serial port that I can use but I'm having problems inside the detach event. The detach event fires correctly but when code execution reaches closeCOM(serialPort1) point it jumps to wndProc override loop and prevents execution of closeCOM() and whatever follows.

The library needs WndProc messages to detect the events and my code is implemented like this:

//Catch Windows messages and pass them to the USBClass class
protected override void WndProc(ref Message m)
{
    USBPort.ProcessWindowsMessage(ref m);

    base.WndProc(ref m);
}

Then I have a detach event which executes the following code:

 if (!USBClass.GetUSBDevice(VID, PID, ref USBDeviceProperties, false) && USBDeviceConnected)
            {
                //Device is removed
                USBDeviceConnected = false;

                //Close COM port just in case we forgot to disconnect
                closeCOM(serialPort1);

                //update status strip
                strip_device_status.Text = "Disconnected";
            }  

and here is the closeCOM function:

    //This function handles COM port closing.
private void closeCOM(SerialPort port)
{
    // If a serial port is open, disconnect it 
    if (port.IsOpen)
    {
        // close the serial port 
        port.Close();
    }
        //update com port status flag
        COMConnected = false;

        //update status strip
        strip_COM_status.Text = "Disconnected";
 }

It is closeCOM() in particular that's causing this "conflict" because if I comment out that line everything works, except I'm left with an open serial port possibly causing errors later on. What am I doing wrong that's causing the code to jump to WndProc override instead of normal execution? I would like to be able to do the port closing in the detach event.

pb2q
  • 54,061
  • 17
  • 135
  • 139
Florin C.
  • 256
  • 5
  • 15
  • 2
    Running this on a 64-bit operating system? Debug + Exceptions, tick the Thrown checkbox for CLR exceptions. Or change the platform target to AnyCPU. The approach is pretty hopeless, you need a time machine that closes the serial port *before* the USB device is detached. – Hans Passant Oct 10 '12 at 12:58
  • Yes I'm running on win7 64bit. I did what you suggested and I get a `IOException : The specified port does not exist` for port.Close(). How is that possible since I first check if port.IsOpen ? Regarding your second suggestion, this event will trigger when the external device resets or the connection is lost for whatever reason so I cannot know in advance when it is going to happen. – Florin C. Oct 10 '12 at 16:06
  • possible duplicate of [Releasing a unplugged virtual Serial Port](http://stackoverflow.com/questions/9835881/releasing-a-unplugged-virtual-serial-port) – Hans Passant Oct 10 '12 at 16:48
  • I think I need to open a new question because it seems the problem is `serialport` related. I check to see if the serialport is not null and it still throws `The specified port does not exist`, this doesn't make sense to me. – Florin C. Oct 10 '12 at 17:09

0 Answers0