System.IO.Ports.SerialPort.GetSerialPorts() throwing exception RRS feed

  • Question

  • Hi I am getting exception when I call GetSerialPorts() method.

    Source code
    string[] ports = SerialPort.GetPortNames();
                return false;

    Exception Messagge: No more data is available.

    Stack Trace:
    at Microsoft.Win32.RegistryKey.Win32Error(int32 errorCode, string str)
    at Microsoft.Win32.RegistryKey.GetValueNames()
    at System.IO.Ports.SerialPort.GetPortNames()

    What will be the reason for this. Please help me out.

    Friday, August 31, 2012 11:19 AM

All replies

  • Look at the devices GUI on your computer in the control Panel and make sure the device exists.

    Computer Manager - Device Manager - Ports (COM & LPT)


    Friday, August 31, 2012 11:37 AM
  • That should not happen. Something is wrong. (If there were no devices the GetPortNames call should return an empty array, if one an array of len 1, etc)

    Does it only happen on one machine?? Please try it on a different PC.

    GetPortNames reads the list of Serial device names from the Registry at "HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM"  There is either some corruption there, or you're being very unlucky as a device disappears *just* as you're in the middle of reading the names and so there are less to read that there were when it started reading...

    Try running the following command in PowerShell:

    C:\> Get-WmiObject -query "select DeviceID from Win32_SerialPort"

    Does that work?

    http://www.alanjmcf.me.uk/ Please follow-up in the newsgroup. If I help, please vote and/or mark the question answered. Available for contract programming.

    Saturday, September 1, 2012 8:10 PM