Page 1 of 3

Enumerating serial ports

Posted: Tue Mar 27, 2012 6:05 am
by mol
Hi!
I need to enumeratre serial port. I've found some CPP code, but, I'm not familiar with C and can't translate it to use with harbour.
I have whole archive file enumser.zip created by:
PJ Naughter
Email: pjna@naughter.com
Web: http://www.naughter.com
whih contains all definitions and working .exe file.
I've cutted some code to present the problem:

Code: Select all


#ifndef NO_ENUMSERIAL_USING_REGISTRY
#if defined CENUMERATESERIAL_USE_STL
#if defined _UNICODE
BOOL CEnumerateSerial::UsingRegistry(std::vector<std::wstring>& ports)
#else
BOOL CEnumerateSerial::UsingRegistry(std::vector<std::string>& ports)
#endif
#elif defined _AFX
BOOL CEnumerateSerial::UsingRegistry(CStringArray& ports)
#else
BOOL CEnumerateSerial::UsingRegistry(CSimpleArray<CString>& ports)
#endif
{
  //Make sure we clear out any elements which may already be in the array(s)
#if defined CENUMERATESERIAL_USE_STL
  ports.clear();
#else
  ports.RemoveAll();
#endif  

  //What will be the return value from this function (assume the worst)
  BOOL bSuccess = FALSE;

  HKEY hSERIALCOMM;
  if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"), 0, KEY_QUERY_VALUE, &hSERIALCOMM) == ERROR_SUCCESS)
  {
		//Get the max value name and max value lengths
		DWORD dwMaxValueNameLen;
		DWORD dwMaxValueLen;
		DWORD dwQueryInfo = RegQueryInfoKey(hSERIALCOMM, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &dwMaxValueNameLen, &dwMaxValueLen, NULL, NULL);
		if (dwQueryInfo == ERROR_SUCCESS)
		{
			DWORD dwMaxValueNameSizeInChars = dwMaxValueNameLen + 1; //Include space for the NULL terminator
			DWORD dwMaxValueNameSizeInBytes = dwMaxValueNameSizeInChars * sizeof(TCHAR);
			DWORD dwMaxValueDataSizeInChars = dwMaxValueLen/sizeof(TCHAR) + 1; //Include space for the NULL terminator
			DWORD dwMaxValueDataSizeInBytes = dwMaxValueDataSizeInChars * sizeof(TCHAR);
		
			//Allocate some space for the value name and value data			
      ATL::CHeapPtr<TCHAR> szValueName;
      ATL::CHeapPtr<BYTE> byValue;
      if (szValueName.Allocate(dwMaxValueNameSizeInChars) && byValue.Allocate(dwMaxValueDataSizeInBytes))
      {
				bSuccess = TRUE;

				//Enumerate all the values underneath HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM
				DWORD dwIndex = 0;
				DWORD dwType;
				DWORD dwValueNameSize = dwMaxValueNameSizeInChars;
				DWORD dwDataSize = dwMaxValueDataSizeInBytes;
				memset(szValueName.m_pData, 0, dwMaxValueNameSizeInBytes);
				memset(byValue.m_pData, 0, dwMaxValueDataSizeInBytes);
				LONG nEnum = RegEnumValue(hSERIALCOMM, dwIndex, szValueName, &dwValueNameSize, NULL, &dwType, byValue, &dwDataSize);
				while (nEnum == ERROR_SUCCESS)
				{
					//If the value is of the correct type, then add it to the array
					if (dwType == REG_SZ)
					{
						TCHAR* szPort = reinterpret_cast<TCHAR*>(byValue.m_pData);
          #if defined CENUMERATESERIAL_USE_STL
            ports.push_back(szPort);
          #else
            ports.Add(szPort);
          #endif  						
					}

					//Prepare for the next time around
					dwValueNameSize = dwMaxValueNameSizeInChars;
					dwDataSize = dwMaxValueDataSizeInBytes;
					memset(szValueName.m_pData, 0, dwMaxValueNameSizeInBytes);
					memset(byValue.m_pData, 0, dwMaxValueDataSizeInBytes);
					++dwIndex;
					nEnum = RegEnumValue(hSERIALCOMM, dwIndex, szValueName, &dwValueNameSize, NULL, &dwType, byValue, &dwDataSize);
				}
      }
      else
		    SetLastError(ERROR_OUTOFMEMORY);
		}
		
		//Close the registry key now that we are finished with it    
    RegCloseKey(hSERIALCOMM);
    
    if (dwQueryInfo != ERROR_SUCCESS)
			SetLastError(dwQueryInfo);
  }
	
	return bSuccess;
}
#endif
Is somebody able to help me?
Regards, Marek

Re: Enumerating serial ports

Posted: Tue Mar 27, 2012 1:16 pm
by danielmaximiliano
Dear Mol:
look in :http://www.itk.ru/clip-doc.en/categserialio.html
I hope it help
Function COM_NUM()

COM_NUM() --> nMaxCom

This function returns the number of the serial interface port available. This is not to say that a return value of 3 necessarily means that ports 1 through 3 are available; for example, COM2 could be missing. Whether or not a serial port can actually be used can be tested using the COM_OPEN() or COM_INIT() functions.

Returns :

Return the number of the highest available serial interface port
See also : COM_DTR() COM_RTS() COM_SOFT() COM_TIMEOUT() COM_HARD() COM_COUNT()

Example :

? COM_NUM() // e.g. 4

Re: Enumerating serial ports

Posted: Tue Mar 27, 2012 1:24 pm
by gfilatov
mol wrote:Hi!
I need to enumeratre serial port.
...
Is somebody able to help me?
Hi Marek,

Yes, of course :!:

Please try an attached working sample (source only) 8-)

Re: Enumerating serial ports

Posted: Tue Mar 27, 2012 2:58 pm
by mol
many Thanks, Grigori!
You are great!

Marek

Re: Enumerating serial ports

Posted: Tue Mar 27, 2012 7:18 pm
by salamandra
Hi Grigori :) ,

A very nice job !!!


[]´s Salamandra

Re: Enumerating serial ports

Posted: Wed Mar 28, 2012 12:40 pm
by mol
How to get an array containing info, eg { { "\Device\Serial0" , "COM1"} , { "\Device\porte40", "COM40" } }
Image

Re: Enumerating serial ports

Posted: Sun Apr 01, 2012 11:43 pm
by salamandra
Hi Mol :) ,
I do not know if you can get an array with this data :cry:
Did you post a Windows XP screenshot ?

Take a look a Windows 7 registry screenshot... it has a different layout...
To get this code working properly on Windows 7 you need change
cReg value to "SOFTWARE\MICROSOFT\WINDOWS NT\CURRENTVERSION\PORTS"



ScreenCapture.JPG
ScreenCapture.JPG (91.07 KiB) Viewed 6945 times
[]´s Salamandra

Re: Enumerating serial ports

Posted: Tue Jul 17, 2012 12:26 am
by pjquiroz
hi all
i'm new in the forum,
i´m from chile my name is Pablo.

In the previus Post, mol say
"How to get an array containing info, eg { { "\Device\Serial0" , "COM1"} , { "\Device\porte40", "COM40" } }"
well i needed the same

I made a slight modification to (emunkeys) code, in order to work in Windows 7


Regards / Saludos
Pquiroz

Re: Enumerating serial ports

Posted: Tue Jul 17, 2012 2:18 am
by Rathinagiri
Thanks a lot and welcome to the forum Pquiroz

Re: Enumerating serial ports

Posted: Tue Jul 17, 2012 6:02 am
by mol
Many thanks!
I've just to seek solution of this problem :lol:
You're great!