Solved

Check is com port is open

Posted on 2004-09-15
5
301 Views
Last Modified: 2013-11-20
I'm writing a program that needs to check is the user has the com port open. Is there any way to check if it is open and not disrupt the com port if it is?
0
Comment
Question by:greg_100
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Just call 'CreateFile()' on the COM port in question - if it is in use, you'll get ERROR_ACCESS_DENIED
0
 

Author Comment

by:greg_100
Comment Utility
Do you have an example of how to do this?
0
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
Comment Utility
Well, you could just use

    HANDLE hPort;

    hPort   =   CreateFile  (   "COM1",
                                GENERIC_READ | GENERIC_WRITE,
                                0,
                                0,
                                OPEN_EXISTING,
                                0,
                                0
                            );

    if  (   INVALID_HANDLE_VALUE ==  hPort)
        {

            if ( ERROR_ACCESS_DENIED == GetLastError ())
            {
                // port in use
            }
        }

0
 

Author Comment

by:greg_100
Comment Utility
jkr,

That worked all I had to add was CloseHandle(hPort); to the end.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Ooops, sorry, should have mentioned that...
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Healthcare organizations in the United States must adhere to the guidance of both the HIPAA (Health Insurance Portability and Accountability Act) and HITECH (Health Information Technology for Economic and Clinical Health Act) for securing and protec…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now