Krishgy
Krishgy

Reputation:

How to detect whether serial port is in use or not in .net

We have industrial automation application developed in .NET 3.5 framework.

I have to list out the available serial ports in the computer for our user. As part this, if any of the serial port is already opened, I have to show "In Use" status. I tried google but no luck.

Can someone help me please?

Regards,

Krishgy

Upvotes: 1

Views: 2353

Answers (2)

Paul Nathan
Paul Nathan

Reputation: 40299

Try opening the com port with an exclusive lock, if that fails, flag it as open.

Upvotes: 2

Windows programmer
Windows programmer

Reputation: 8065

There are undocumented APIs to find all the handles that are open in the system, and then you could check which ones are open to serial ports. That's if the user will always be an administrator and you don't care about staying with .Net and documented APIs.

There are documented APIs to find all the COM ports that exist, and then you could try opening all of them. This sounds pretty crude and slow, but it's less evil than the alternative.

Upvotes: 0

Related Questions