1
  1. SerialPort.GetPortNames() is wrong
  2. SerialPort.GetPortNames() behavior

I have the same situation as described in the question from the link 2 above, where if I disconnect a device while it is still in use (port open) it will still be present in the array of SerialPort.GetPortNames until I close the application from which the port has been opened.

How do I know this?
I have been watching the changes in Registry Editor (HKLM\Hardware\DeviceMap\SerialComm) while disconnecting the device while it's in use and I have noticed that when I close/terminate/kill the application the erroneous port will be removed immediately after that. This leads me to the conclusion that the problem is not in the operating system but in my application.

What did I do to solve the issue?
I have tried to close the port, to dispose the port, to set the variable of the port to null and then call the garbage collector. None of these seemed to be working.

If my assumption that the inconsistency is caused by my application is correct, then I would like to know whether it is possible to (forcibly) reset the inner state of the application without closing it.

PS: I have been playing with connecting/disconnecting and what I have noticed is that I can make it to show one single port (e.g. COM5) multiple times (as multiple rows) in the Registry Editor. When I terminate the application all of them will disappear.

Bashyar
  • 11
  • 1
  • Is the device a USB device? If so, see https://stackoverflow.com/questions/16245706/check-for-device-change-add-remove-events and https://stackoverflow.com/questions/38754609/managementobjectsearcher-does-not-work-within-global-hook. You may also want to look at using WMI class WIN32_PnpEntity: https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-pnpentity – Tu deschizi eu inchid Dec 26 '20 at 04:58
  • I have exactly the same problem. Did you find a solution? – Dominic Jonas Jul 20 '22 at 08:58
  • @DominicJonas: Please open your own question. The following may be helpful: https://stackoverflow.com/a/65971845/10024425 . – Tu deschizi eu inchid Jul 20 '22 at 18:01

1 Answers1

0

Please share relevant code to what exactly you are calling/doing, because I'm not getting the same behavior. What you describe is not how it works, which leads me to believe you are doing something wrong with your code. Also, why are you disconnecting devices without properly closing the connection first (that's really bad for your PC).

I made a new form with a button and pressing the button calls GetPortNames and writes each one to the console. You can see from my results that I have a COM36. If I remove that device from the PC (unplug it via USB) and press the button again, COM36 is no longer showing up. Note that I had a hyperterminal connection open on COM36.

private void button1_Click(object sender, EventArgs e)
{
    string[] theSerialPortNames = System.IO.Ports.SerialPort.GetPortNames();

    foreach (string s in theSerialPortNames)
    {
        Console.WriteLine(s);
    }
}

First Button Press:

COM3 
COM1 
COM5 
COM33 
COM36

Second Button Press (physically unplugged a serial device):

COM3
COM1
COM5
COM33

Also see https://stackoverflow.com/a/2755035/2009197 he answers the question well.

Baddack
  • 1,947
  • 1
  • 24
  • 33
  • There's no relevant code for this, only the `System.IO.Ports.SerialPort.GetPortNames()` method. Or maybe you think the code for opening the port: `_serialPort = new SerialPort();` `_serialPort.Open();` – Bashyar Jan 01 '21 at 19:04