none
Getting the list of available serial ports in C++ RRS feed

回答

すべての返信

  • Use QueryDosDevice. Serial port names begins with "COM"
    MSMVP VC++
    2009年3月9日 21:48
  • How do I use QueryDosDevice? an example please?

    Thanks.

    Brandon C.

    Got RAM?
    2009年3月9日 22:17
    • 回答の候補に設定 Yangghi Min 2009年3月9日 23:56
    • 回答としてマーク Nancy Shao 2009年3月16日 11:14
    2009年3月9日 23:08
  • I don't think QueryDosDevice is what I'm looking for. All I want to do is find how many COM ports there are, and display them in a ComboBox.
    Got RAM?
    2009年3月9日 23:28
  • Enumerate registry key HKLM\Hardware\DeviceMap\SerialComm.  It is solid, that's what .NET's System.IO.Ports.SerialPort.GetPortNames() uses.
    Hans Passant.
    • 回答の候補に設定 Yangghi Min 2009年3月10日 2:16
    • 回答としてマーク Nancy Shao 2009年3月16日 11:15
    2009年3月10日 0:10
    モデレータ
  • I really don't understand. I just started C++ and I don't know what a Register key is. 
    Got RAM?
    2009年3月10日 1:35
  • Hi, Brandon110

    I was interested in the title of your post 'cause I've been there a long time ago.
    IMO, the QueryDosDevice() example suggested by Sheng Jiang seems to be one of possible solutions.
    (But don't use the sample code as it is 'cause it has a minor bug in it - it assumes that any device name starts with 'com' should be the name of a COM port, which is not always true.)

    It's a bit complicated to get the list of physical COM ports and the most popular way of getting it is to rely on the registry information like Hans suggested above.

    If you're not familiar with retrieving the registry information, the following straightforward but a bit tedious example can be one of your choices;

        CUIntArray arrComPortNo;  
        TCHAR szComPort[8];  
        HANDLE hCom = NULL;  
     
        for (int i = 1; i <= _max_COMports_to_iterate; ++i)  
        {  
            if (i < 10)
              wsprintf(szComPort, _T("COM%d"), i);
            else
              wsprintf(szComPort, _T("\\\\.\\COM%d"), i); 
     
            hCom = CreateFile(szComPort,  
                GENERIC_READ|GENERIC_WRITE, // desired access should be read&write  
                0,                          // COM port must be opened in non-sharing mode  
                NULL,                       // don't care about the security  
                OPEN_EXISTING,              // IMPORTANT: must use OPEN_EXISTING for a COM port  
                0,                          // usually overlapped but non-overlapped for existance test  
                NULL);                      // always NULL for a general purpose COM port  
     
            if (INVALID_HANDLE_VALUE == hCom)  
            {  
                if (ERROR_ACCESS_DENIED == hCom)  
                {   // then it exists and currently opened  
                    arrComPortNo.Add(i);  
                }  
            }  
            else  
            {   // COM port exist  
                arrComPortNo.Add(i);  
                CloseHandle(hCom);  
            }  
        }  
     
        int nComportsAvailable = arrComPortNo.GetSize();  
     

    Regards,

    • 編集済み Yangghi Min 2009年3月10日 3:41
    • 回答の候補に設定 ryanpavlik 2015年1月29日 16:21
    2009年3月10日 2:13
  • Thanks, i appreciate the help. But when I compile it I get 34 errors. Am I missing something?

    Error    1    error C2065: 'CUIntArray' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    867
    Error    2    error C2146: syntax error : missing ';' before identifier 'arrComPortNo'    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    867
    Error    3    error C2065: 'arrComPortNo' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    867
    Error    4    error C2065: 'TCHAR' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    868
    Error    5    error C2146: syntax error : missing ';' before identifier 'szComPort'    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    868
    Error    6    error C2065: 'szComPort' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    868
    Error    7    error C2065: 'HANDLE' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    869
    Error    8    error C2146: syntax error : missing ';' before identifier 'hCom'    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    869
    Error    9    error C2065: 'hCom' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    869
    Error    10    error C2065: 'NULL' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    869
    Error    11    error C2065: '_max_COMports_to_iterate' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    871
    Error    12    error C2065: 'szComPort' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    873
    Error    13    error C3861: 'wsprintf': identifier not found    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    873
    Error    14    error C3861: '_T': identifier not found    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    873
    Error    15    error C2065: 'hCom' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    875
    Error    16    error C2065: 'szComPort' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    875
    Error    17    error C2065: 'GENERIC_READ' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    876
    Error    18    error C2065: 'GENERIC_WRITE' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    876
    Error    19    error C2065: 'NULL' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    878
    Error    20    error C2065: 'OPEN_EXISTING' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    879
    Error    21    error C2065: 'NULL' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    881
    Error    22    error C3861: 'CreateFile': identifier not found    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    875
    Error    23    error C2065: 'INVALID_HANDLE_VALUE' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    883
    Error    24    error C2065: 'hCom' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    883
    Error    25    error C2065: 'ERROR_ACCESS_DENIED' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    885
    Error    26    error C2065: 'hCom' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    885
    Error    27    error C2065: 'arrComPortNo' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    887
    Error    28    error C2228: left of '.Add' must have class/struct/union    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    887
    Error    29    error C2065: 'arrComPortNo' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    892
    Error    30    error C2228: left of '.Add' must have class/struct/union    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    892
    Error    31    error C2065: 'hCom' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    893
    Error    32    error C3861: 'CloseHandle': identifier not found    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    893
    Error    33    error C2065: 'arrComPortNo' : undeclared identifier    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    897
    Error    34    error C2228: left of '.GetSize' must have class/struct/union    c:\users\brandon\documents\visual studio 2008\projects\mini-sequencer\mini-sequencer\Form1.h    897


    Got RAM?
    2009年3月10日 2:21
  •  -_-;

    Could you tell me what type of project you're working on now?
    And the reason that you put the code snippet above in your header file, Form1.h, instead of a cpp file?

    You need some headers and libraries for windows programming...not only for MFC classes but also for any windows programming. I think you need to check it out how to build an application program using C/C++ before figuring out how to enumerate the COM ports available.

    Good luck, ;)
    2009年3月10日 2:33
  • Duh! :P

    I'm actually working on a project for a robot. It creates sequences for the servos and stores them for playback.

    I have about 3 years experience with programming, but I just started C++. I'm still getting used to it.

    Thanks!

    Edit: I put it in a .cpp file and it has 1 error:
    Error    1    fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?    c:\Users\Brandon\Documents\Visual Studio 2008\Projects\Mini-Sequencer\Mini-Sequencer\Comports.cpp    37

    And when I included the stdafx.h file it gives me like 30 errors.

    Got RAM?
    2009年3月10日 2:43
  • You can get information and source code for exemple programming in serial port.
    http://thaiio.com/prog-cgi/programing.cgi?0023  Part I 
    http://thaiio.com/prog-cgi/programing.cgi?0024 Part II
    2009年3月10日 3:55
  • void SelectComPort() //added fucntion to find the present serial ports of the system;
    {

        TCHAR lpTargetPath[5000]; // buffer to store the path of the COMPORTS
        DWORD test;
        bool gotPort=0; // in case the port is not found
        
        for(int i=0; i<255; i++) // checking ports from COM0 to COM255
        {
            CString str;
            str.Format(_T("%d"),i);
            CString ComName=CString("COM") + CString(str); // converting to COM0, COM1, COM2
            
            test = QueryDosDevice(ComName, (LPSTR)lpTargetPath, 5000);

                // Test the return value and error if any
            if(test!=0) //QueryDosDevice returns zero if it didn't find an object
            {
                m_MyPort.AddString((CString)ComName); // add to the ComboBox
                gotPort=1; // found port
            }

            if(::GetLastError()==ERROR_INSUFFICIENT_BUFFER) //in case buffer got filled
            {
                lpTargetPath[10000]; // in case the buffer got filled, increase size of the buffer.
                continue;
            }

        }

        if(!gotPort) // if not port
        m_MyPort.AddString((CString)"No Active Ports Found"); // to display error message incase no ports found

    }
    2013年9月9日 5:18
  • This is beautiful! It took so much searching to find this.
    2019年8月28日 4:59