Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Can someone help me with this com port enumarator

Can someone help me with this com port enumarator

Scheduled Pinned Locked Moved C / C++ / MFC
c++comhelptutorialquestion
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    ScarberryProd
    wrote on last edited by
    #1

    ok, im using this com port enumarator: http://www.codeproject.com/system/listports.asp and im haveing trouble using it to list the ports in a list box using MFC. Can someone send me a simple example doing that with this, using VC++ 6 preferably? thank you, This stupid thing is frustrating me beyond belief lol --------------------- And Like The Wind Our Hero Vanishes Off Into The Distance...

    B 1 Reply Last reply
    0
    • S ScarberryProd

      ok, im using this com port enumarator: http://www.codeproject.com/system/listports.asp and im haveing trouble using it to list the ports in a list box using MFC. Can someone send me a simple example doing that with this, using VC++ 6 preferably? thank you, This stupid thing is frustrating me beyond belief lol --------------------- And Like The Wind Our Hero Vanishes Off Into The Distance...

      B Offline
      B Offline
      Branislav
      wrote on last edited by
      #2

      In stdafx.h put: #include #include In EnumerSer.h put: #ifndef __AFXTEMPL_H__ #pragma message("EnumerateSerialPorts function requires afxtempl.h in your PCH") #endif void EnumerateSerialPorts(CUIntArray& ports); ... Demo.cpp: #include "stdafx.h" #include #include "EnumerSer.h" void main() { CUIntArray ports; EnumerateSerialPorts(ports); _tprintf(_T("The following serial ports are installed on this machine\n")); for (int i=0; i

      B S 2 Replies Last reply
      0
      • B Branislav

        In stdafx.h put: #include #include In EnumerSer.h put: #ifndef __AFXTEMPL_H__ #pragma message("EnumerateSerialPorts function requires afxtempl.h in your PCH") #endif void EnumerateSerialPorts(CUIntArray& ports); ... Demo.cpp: #include "stdafx.h" #include #include "EnumerSer.h" void main() { CUIntArray ports; EnumerateSerialPorts(ports); _tprintf(_T("The following serial ports are installed on this machine\n")); for (int i=0; i

        B Offline
        B Offline
        Branislav
        wrote on last edited by
        #3

        Sorry I forget: ///////////////////////////// Implementation ////////////////////////////////// void EnumerateSerialPorts(CUIntArray& ports) { //Make sure we clear out any elements which may already be in the array ports.RemoveAll(); //Determine what OS we are running on OSVERSIONINFO osvi; osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); BOOL bGetVer = GetVersionEx(&osvi); //On NT use the QueryDosDevice API if (bGetVer && (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)) { //Use QueryDosDevice to look for all devices of the form COMx. This is a better //solution as it means that no ports have to be opened at all. TCHAR szDevices[65535]; DWORD dwChars = QueryDosDevice(NULL, szDevices, 65535); if (dwChars) { int i=0; for (;;) { //Get the current device name TCHAR* pszCurrentDevice = &szDevices[i]; //If it looks like "COMX" then //add it to the array which will be returned int nLen = _tcslen(pszCurrentDevice); if (nLen > 3 && _tcsnicmp(pszCurrentDevice, _T("COM"), 3) == 0) { //Work out the port number int nPort = _ttoi(&pszCurrentDevice[3]); ports.Add(nPort); } // Go to next NULL character while(szDevices[i] != _T('\0')) i++; // Bump pointer to the next string i++; // The list is double-NULL terminated, so if the character is // now NULL, we're at the end if (szDevices[i] == _T('\0')) break; } } else TRACE(_T("Failed in call to QueryDosDevice, GetLastError:%d\n"), GetLastError()); } else { //On 95/98 open up each port to determine their existence //Up to 255 COM ports are supported so we iterate through all of them seeing //if we can open them or if we fail to open them, get an access denied or general error error. //Both of these cases indicate that there is a COM port at that number. for (UINT i=1; i<256; i++) { //Form the Raw device name CString sPort; sPort.Format(_T("\\\\.\\COM%d"), i); //Try to open the port BOOL bSuccess = FALSE; HANDLE hPort = ::CreateFile(sPort, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); if (hPort == INVALID_HANDLE_VALUE) { DWORD dwError = GetLastError(); //Check to see if the error was because some other app had the port open or a general failure

        1 Reply Last reply
        0
        • B Branislav

          In stdafx.h put: #include #include In EnumerSer.h put: #ifndef __AFXTEMPL_H__ #pragma message("EnumerateSerialPorts function requires afxtempl.h in your PCH") #endif void EnumerateSerialPorts(CUIntArray& ports); ... Demo.cpp: #include "stdafx.h" #include #include "EnumerSer.h" void main() { CUIntArray ports; EnumerateSerialPorts(ports); _tprintf(_T("The following serial ports are installed on this machine\n")); for (int i=0; i

          S Offline
          S Offline
          ScarberryProd
          wrote on last edited by
          #4

          umm u have a few includes there with nothing after them, what do i include? ty --------------------- And Like The Wind Our Hero Vanishes Off Into The Distance...

          B 1 Reply Last reply
          0
          • S ScarberryProd

            umm u have a few includes there with nothing after them, what do i include? ty --------------------- And Like The Wind Our Hero Vanishes Off Into The Distance...

            B Offline
            B Offline
            Branislav
            wrote on last edited by
            #5

            Sorry I did not check "Do not treat <'s as HTML tags." In Stdafx.h should be: #include #include In Demo.cpp missed #include

            S 1 Reply Last reply
            0
            • B Branislav

              Sorry I did not check "Do not treat <'s as HTML tags." In Stdafx.h should be: #include #include In Demo.cpp missed #include

              S Offline
              S Offline
              ScarberryProd
              wrote on last edited by
              #6

              im about to shove a stick of dynomite into my comps serial port, lol i get this error: cannot convert parameter 1 from 'unsigned int' to 'const char *' for this line of code: m_port.AddString(ports.ElementAt(i)); how do i fix that? thank you

              B 1 Reply Last reply
              0
              • S ScarberryProd

                im about to shove a stick of dynomite into my comps serial port, lol i get this error: cannot convert parameter 1 from 'unsigned int' to 'const char *' for this line of code: m_port.AddString(ports.ElementAt(i)); how do i fix that? thank you

                B Offline
                B Offline
                Branislav
                wrote on last edited by
                #7

                You have to convert this integer into string such as: char str[7]; sprintf(str, "COM%d", ports.ElementAt(i)); m_port.AddString(str);

                S 1 Reply Last reply
                0
                • B Branislav

                  You have to convert this integer into string such as: char str[7]; sprintf(str, "COM%d", ports.ElementAt(i)); m_port.AddString(str);

                  S Offline
                  S Offline
                  ScarberryProd
                  wrote on last edited by
                  #8

                  o ok thank you

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups