Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Is Serial Port in Use

Posted on 2002-05-27
1
Medium Priority
?
321 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 200 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

972 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