Solved

Check is com port is open

Posted on 2004-09-15
5
343 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12064517
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
ID: 12065003
Do you have an example of how to do this?
0
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 12065094
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
ID: 12068427
jkr,

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

Expert Comment

by:jkr
ID: 12068733
Ooops, sorry, should have mentioned that...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

All of the resources available today make learning a new digital media easier than ever-- if you know where to begin. This is a clear, simple guide to a few of the basic digital art mediums and how to begin learning them on your own.
Invest in your employees with these five simple steps to improve employee engagement and retention.
This video shows how use content aware, what it’s used for, and when to use it over other tools.
This video demonstrates how to use each tool, their shortcuts, where and when to use them, and how to use the keyboard to improve workflow.

729 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