Solved

Is Serial Port in Use

Posted on 2002-05-27
1
270 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

706 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

17 Experts available now in Live!

Get 1:1 Help Now