Enumerating serial ports

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

User avatar
mol
Posts: 3774
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Enumerating serial ports

Post 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
User avatar
danielmaximiliano
Posts: 2625
Joined: Fri Apr 09, 2010 4:53 pm
Location: Argentina
Contact:

Re: Enumerating serial ports

Post 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
*´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´. (¸.·` *
.·`. Harbour/HMG : It's magic !
(¸.·``··*

Saludos / Regards
DaNiElMaXiMiLiAnO

Whatsapp. := +54901169026142
Telegram Name := DaNiElMaXiMiLiAnO
User avatar
gfilatov
Posts: 1090
Joined: Fri Aug 01, 2008 5:42 am
Location: Ukraine
Contact:

Re: Enumerating serial ports

Post 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-)
Attachments
EnumKeys.zip
Enumerate serial port sample
(2.99 KiB) Downloaded 433 times
Kind Regards,
Grigory Filatov

"Everything should be made as simple as possible, but no simpler." Albert Einstein
User avatar
mol
Posts: 3774
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Enumerating serial ports

Post by mol »

many Thanks, Grigori!
You are great!

Marek
User avatar
salamandra
Posts: 311
Joined: Thu Jul 31, 2008 8:33 pm
DBs Used: DBF, MySQL, SQL
Location: Brazil

Re: Enumerating serial ports

Post by salamandra »

Hi Grigori :) ,

A very nice job !!!


[]´s Salamandra
There is one time in which is crucial awakening. That time is now. ( Buddha )
User avatar
mol
Posts: 3774
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Enumerating serial ports

Post by mol »

How to get an array containing info, eg { { "\Device\Serial0" , "COM1"} , { "\Device\porte40", "COM40" } }
Image
User avatar
salamandra
Posts: 311
Joined: Thu Jul 31, 2008 8:33 pm
DBs Used: DBF, MySQL, SQL
Location: Brazil

Re: Enumerating serial ports

Post 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 6948 times
[]´s Salamandra
There is one time in which is crucial awakening. That time is now. ( Buddha )
pjquiroz
Posts: 4
Joined: Mon Oct 17, 2011 3:44 pm
DBs Used: DBF, MYSQL
Location: Chile

Re: Enumerating serial ports

Post 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
Attachments
EnumKeys.zip
Enumerate serial port sample
w7 tested
(803.24 KiB) Downloaded 360 times
User avatar
Rathinagiri
Posts: 5480
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: Enumerating serial ports

Post by Rathinagiri »

Thanks a lot and welcome to the forum Pquiroz
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
mol
Posts: 3774
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Enumerating serial ports

Post by mol »

Many thanks!
I've just to seek solution of this problem :lol:
You're great!
Post Reply