Search code examples
c#exceptionserial-portuart

"The port COMx does not exist" - Exception thrown on Open() of SerialPort


My title describes my problem... Can't do anything to solve it.

It is a serial COM device connected via USB cable (appropriate driver is installed). Also I can see that COM port is detected by the system and exists in the following registry key: HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM

Please advise.

UPDATE: Sorry for being not so explanative as it has to be. Probably I figured why it happens: my app subscribed on event of values added/removed from HKLM\HARDWARE\DEVICEMAP\SERIALCOMM. And app tried to open the port immediately right after it was noticed about it. But port may be not ready at that time and so that is why I got "The port doesn't exist". Simply Thread.Sleep(2000) solved my problem.

But this Sleep solution doesn't seem as good solution. Maybe I should check somewhere else for device connected other that in registry? So I will connect to it when it really exist and ready to connect.

Thank you for your responses! Love stackoverflow!


Solution

    1. Please provide a code snippet for your "open".

    2. Please specify the exact error text and message (for example, errno 57, errno 55, etc).

    3. Please specify your C#/VS and OS versions

    4. Q: what does System.IO.Ports.SerialPort.GetPortNames say?

    5. Q: are you absolutely, positively sure the port isn't already opened and in-use (for example, by another application)?

    6. Q: What happens if you reboot, and try re-running your program immediately afterwards?