2

I am using C#'s SerialPort class to communicate over a USB COM port. The SerialPort opens fine and works fine until an exception occurs. After the exception when I try to use the port through my application I get another exception: "The port 'COM6' does not exist." I keep on getting the exception after closing the application, restarting Visual Studio, etc. The only way to get the port back is to restart the computer.

Has anyone seen this problem and know a way around it? I need this to be more durable.

dsolimano
  • 8,870
  • 3
  • 48
  • 63
zam664
  • 757
  • 1
  • 7
  • 18
  • A question: why do you get an exception? What's happening in your code? – Marco Mar 29 '13 at 01:00
  • Communication timeout. I am programming the serial device also and I am debugging it. The comm timeout is caused from a programming error in the serial device, but bad comms shouldn't crash the port. My screw up actually creates a good test case for comm hardening. – zam664 Mar 29 '13 at 01:52
  • I've definitely seen this issue when using USB as a COM device like that. At a low level there must still be a handle to the port open so when you have an unhandled exception the OS won't get back the device until you restart. Is there no way to catch the exception and close the port in a finally block? Also, it's possible that it reassigns the USB device to a new COM port so you might have to detect it on a higher one. – Jake Anderson Mar 29 '13 at 02:28
  • 1
    There are a lot of bad USB drivers out there. You are fingering the serial device but it is just as likely that it is the driver that causes the timeout. With continuing trouble after that. Throw it away and get another one from a different manufacturer. – Hans Passant Mar 29 '13 at 13:27
  • @JakeAnderson: Device manager still reports it as COM6. I tried a couple different COMs and it did not move. – zam664 Mar 29 '13 at 16:02

1 Answers1

0

Typically this kind of an issue occur due to your USB to serial driver is not properly written to handle exceptions.

Rather than rebooting the machine, try to unplug and plug it again ( after few seconds). By doing that previous instance may unload, and when plugging it, it will load the relevant driver again.

This will enumerate a com number ( in your case Com6) in the device manager.

There is a way that you can restart the USB connection using "devcon.exe" as given in following link.

http://digital.ni.com/public.nsf/allkb/1D120A90884C25AF862573A700602459

You can integrate it to console application and reconnect your USBTOSerial without restarting your machine