Solved

Is Serial Port in Use

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

856 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