Solved

Is Serial Port in Use

Posted on 2002-05-27
1
288 Views
Last Modified: 2010-04-02
Hello,
I have an app that accesses the serial port. If another program is using the port my app crashes. Is there a way i could check if the port is in use (so i could alert the user and tell them to change the config settings in my app)
Thank you
0
Comment
Question by:axia
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 7037607
Just call 'CreateFile()' on the port - if it succeds, the port is available, if not, it isn't - that's what I used to create a list of available COM ports:

    //  create list of available comm ports

    char    acPort  [   5]  =   "COM1";

    for (   UCHAR   uc  =   0;  uc  <   8;  uc++)
        {
            HANDLE  hPort   =   INVALID_HANDLE_VALUE;

            acPort  [   3]  +=  uc;

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

            if  (   INVALID_HANDLE_VALUE    !=  hPort)
                {
                   // port COMx is available
                    CloseHandle (   hPort);
                }
             else
                {
                   // port COMx is unavailable
                }

        }
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

773 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