1

I have a .NET4 C# application with some GUI. It works with serial network devices via USB-UART converter FT2232. The converter implemets a hub with several USB, which are shown as a number of COMxx in the system.

I supported a dynamic port add/remove operation (over WMI) so as ports would refresh in UI automatically when plugged/unplugged.

Plug-in operation works fine, i.e. ports become visible in a second or two after insertion. But unpluging is the issue: when unplugged not all hub`s ports disappear, but only one or two out of four (by this time all ports are not in use, closed). The issue is valid for WinXP x86 and Win7 x64.

Some investigation showed that it does not help to refresh port list calling SerialPort.GetPortNames() after awhile or by timer. The list stays stale. BUT: if I close the window with ports (it is not the main window) and reopen it again, one phantom port disappears, then again and all phantom ports are gone!

Has anybody run into similar situations or have some ideas?

May it be somehow connected with that I call GetPortNames from UI context?

Or this is rather HW driver issue?

oxod
  • 166
  • 1
  • 7
  • I had a look at the reference source for SerialPort. There doesn't appear to be any sort of caching going on, it just reads the port names from `HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM`. So I would expect if you monitor that registry key you'd see similar results there. Do the ports stay in the device manager as well? – Chris Feb 02 '15 at 19:41
  • This is a generic problem with USB devices that emulate a serial port. They date from the stone age, no support for Plug & Play. So no decent way to find out that the port isn't there anymore. So no decent way for that screen to update. It is always crucial to use the "Safely remove hardware" tray icon before you unplug them. – Hans Passant Feb 02 '15 at 19:46
  • Yes, GetPortNames takes information from registry and it is the same, i.e. phantom ports stay there. But on the other hand they disappear from the device manager right away after removal. So it is obvious that information in the device manager and registry is either taken from different sources of not synchronized. Than another question is how device manager get notified about it!? – oxod Feb 03 '15 at 06:23
  • Another interesting note is that if I don`t run my app at all, but only plug in and unplug the USB hub ports get shown and disappear from registry correctly without any delay. Thus, somehow the issue has something to do with application resources, but I dunno... – oxod Feb 03 '15 at 06:28

1 Answers1

2

I think right now I solved the problem:

I found that in the event handler of WMI device added/removed ports information does not refresh and strangely there are a lot of add/remove event fires on plug/unplug action.

So if I do not check for new ports added/removed right away but let it pass higher to the level of my view model, then ports information updates (between of event fires) and I have a valid list of connected USB-UART devices.

Anyhow, generally the issue still remains not clear in terms of what exactly causes ports to get updated in registry in some situations while in others it is not.

oxod
  • 166
  • 1
  • 7