Thread: Available COM ports scan

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    7

    Available COM ports scan

    Hi, I am fairly new in C/C++ programming.
    I want to scan available com ports 1-99.

    This ugly method works, anyone having a
    better propose?

    CreateFile(pcCommPort, etc...
    does not accept that pcCommPort is a string.

    I am using dev-cpp

    Code:
    char* pcCommPort;
    char portnbr;
    
    for (portnbr = 1; portnbr <=99; portnbr++) {
      // 1..7
       if (portnbr == 8) { pcCommPort = "\\\\.\\COM8"; }
       if (portnbr == 9) { pcCommPort = "\\\\.\\COM9"; }
       if (portnbr == 10) { pcCommPort = "\\\\.\\COM10"; }
       if (portnbr == 11) { pcCommPort = "\\\\.\\COM11"; }
      // 12..99
    
    hComm = CreateFile(pcCommPort,
                          GENERIC_READ | GENERIC_WRITE,
                          0,    // comm devices must be opened w/exclusive-access
                          NULL, // no security attributes
                          OPEN_EXISTING, // comm devices must use OPEN_EXISTING
                          0,    // non overlapped I/O
                          NULL  // hTemplate must be NULL for comm devices
                          );

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    if you need to pass a C string to a function, then the C++ string has the c_str() function:
    Code:
    #include <string>
    #include <iostream>
    // ...
    std::string s("hello, world");
    std::cout << s.c_str();
    Then you can use the C++ way to concatenate strings with other types..

    Code:
    #include <sstream>
    #include <string>
    #include <iostream>
    
    int main()
    {
        int num(10);
        std::stringstream ss;
        ss << "Hello, World  " << num;
        std::string foo;
        std::getline(ss, foo);
        std::cout << foo.c_str();
    }
    Last edited by Bench82; 05-11-2006 at 07:44 AM.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    look into strings and stringstreams to see how you can append the value in portnbr to the pcCommPort. Bascly what you want is to have pcCommPort contain "\\\\.\\COM" at first and then you just append the value in portnbr to that, and thats your final string
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    7

    :(

    Hmm I try but still have the same problem.
    I get compiler error:

    cannot convert `std::string' to `const CHAR*' for argument `1' to `void* CreateFileA(const CHAR*, DWORD, DWORD, _SECURITY_ATTRIBUTES*, DWORD, DWORD, void*)'

    Asking humble for help

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    need to use c_str() like this:
    std::string yourString;
    SomeFuncThatRequresCharPointer(yourString.c_str()) ;
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    7

    I solved it!

    Ok TY ALL!

    Finally with some help I solved it
    here is the solution

    Code:
    for (int i=1; i<99; i++) {
        stringstream ss;
        ss << "\\\\.\\COM" << i;
        
    
       hComm = CreateFile(ss.str().c_str(), .........etc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. virtual ports
    By royuco77 in forum Networking/Device Communication
    Replies: 5
    Last Post: 07-02-2005, 10:33 AM
  2. Control of ports
    By Blip in forum Networking/Device Communication
    Replies: 1
    Last Post: 05-02-2005, 09:04 AM
  3. while (scan != 'y' or 'n) or if(scan != 'y' or 'n)
    By Blizzarddog in forum C++ Programming
    Replies: 6
    Last Post: 10-23-2002, 01:16 PM
  4. Controlling ports
    By lockpatrick in forum Linux Programming
    Replies: 2
    Last Post: 06-09-2002, 11:58 AM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM