Thread: Checking which Com Ports are free

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    71

    Checking which Com Ports are free

    Is the only way to see which Com Ports are open and available for use by polling each one?

    I am quite new to programming related to Com Ports and the original way I was going to tackle it was by just trying each Com Port like this on each Com Port until 10.
    Code:
    hPort = CreateFile(L"COM9", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
    Any suggestions or advice? Would greatly appreciate it.

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    71
    Here is my current code but trying all the COM Ports from 1-9 seems to only return an error and I'm not sure what's going on.

    Code:
    #include <string>#include <Windows.h>
    #include <conio.h>
    #include <iostream>
    #include <sstream>
    
    
    int main(){
        
        //std::string com = "\\\\.\\COM";
        std::string com = "COM";
        std::string portname;
        HANDLE hCom;
    
    
        for( int i = 1; i<10; i++)
        {
            std::stringstream sstm;
            std::cout << "Trying "<< i<<"\n";
            sstm << com << i;
            portname = sstm.str();
            TCHAR *szPort = (TCHAR*)(portname.c_str());
            //TCHAR *szPort = TEXT(port);
            //wsprintf( szPort, "COM%d", nPort );
    
    
            hCom = CreateFile(szPort,
                                GENERIC_READ | GENERIC_WRITE, 
                                0, 
                                NULL, 
                                OPEN_EXISTING, 
                                FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, 
                                NULL);
            if (hCom == INVALID_HANDLE_VALUE)
            {
                DWORD err=GetLastError();
                std::cout << "Failed\n";
            }
            else
            {
                std::cout << i << " Didn't Fail o.o";
                CloseHandle(hCom);
            }
        }
        return 0;
    }
    Last edited by workisnotfun; 02-20-2014 at 07:20 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > TCHAR *szPort = (TCHAR*)(portname.c_str());
    If TCHAR really is a wide character, then you need to do more than simply casting a pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Have a look here. Scroll down to the section on accessing ports under windows. Keep going down until you find some code by someone called Peter Burke (which works under both win95 and NT families).

    That should get you started.
    Last edited by grumpy; 02-24-2014 at 05:34 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 12-28-2012, 04:07 PM
  2. Checking to see if all memory is free
    By madmax2006 in forum C Programming
    Replies: 2
    Last Post: 03-18-2010, 12:17 AM
  3. new license-free lock-free data structure library published
    By Toby Douglass in forum Projects and Job Recruitment
    Replies: 19
    Last Post: 12-22-2009, 02:33 AM
  4. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM
  5. What's cooler than free boobs? 5 free assembly books from AMD.
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 02-13-2003, 08:22 PM