SerialPort.GetPortNames() behavior

后端 未结 4 548
南方客
南方客 2020-12-12 02:05

In my C# 2008 application, I use the SerialPort.GetPortNames() function to retrieve the list of currently available ports. What I have noticed is, when ever I plug in a USB

4条回答
  •  庸人自扰
    2020-12-12 02:41

    It is correct that GetPortNames() reads the ports from the Registry key

    HKLM\Hardware\DeviceMap\SerialComm
    

    This is automagically updated by Windows every time a port is opened or closed.

    But nevertheless it has happened to me that there is a non-existent Port listed in the Registry, and also returned from GetPortNames(). When I try to open this port I get "The Port XYZ does not exist".

    What is that ???

    I now found out the reason: This happens always after using PortMon from www.sysinternals.com. This tool is buggy and lets the dead port hanging around in the Registry if the port is closed while it is monitored.

    In this case the only remedy is to reboot the computer.

提交回复
热议问题