Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting a list of available serial ports (Visual C++)

10 views
Skip to first unread message

Davide Bruzzone

unread,
Mar 14, 2001, 4:48:40 PM3/14/01
to
Greetings all...

I'm writing an application that reads data from a Windows CE device's serial
port. So far so good (I'm using the API's standard functions to do this)...

What I can't figure out (and I've looked all over the place - MSDN, the rest
of the Internet, etc.) is how I can ask the device what serail ports are
available (so that, for example, I can display all the available ports in a
dropdown, and allow the user to select the serial port that they want to
use).

If anyone can point me in the direction of the information that I need to
look at, I'd be most appreciative.

Thanks in advance...

Dave Bruzzone


Paul G. Tobey

unread,
Mar 14, 2001, 5:24:33 PM3/14/01
to
Dave,

This isn't some kind of blessed code, but it works on our device:

Paul T.

int CountSerialPorts()
{
HKEY basekey;
int lastfound = 0;

// Open and enumerate the key [HKEY_LOCAL_MACHINE\Drivers\Active]
if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, _T( "\\Drivers\\Active" ),
0, 0, &basekey ) == ERROR_SUCCESS )
{
HKEY key;
DWORD index = 0;
TCHAR keyname[ 256 ];

// Read HKEY_LOCAL_MACHINE\Drivers\Active, looking for COMx: ports.
// We could also just look through the BuiltIn section, but this should,
// in theory at least, allow PC Card-based serial ports to be detected.
while ( 1 )
{
// This *has* to get set each time, since it is also returned
// when RegEnumKeyEx() is called.
DWORD keynamesize = sizeof( keyname ) / sizeof( keyname[ 0 ] );

if ( RegEnumKeyEx( basekey, index, keyname, &keynamesize, NULL,
NULL, NULL, NULL ) != ERROR_SUCCESS )
{
break;
}

// We have the name of the key. Open it and see if it is a serial
// port.
if ( RegOpenKeyEx( basekey, keyname, 0, 0, &key ) == ERROR_SUCCESS )
{
DWORD nametype;
TCHAR name[ 256 ];
DWORD namesize = sizeof( name ) / sizeof( name[ 0 ] );
// Check for serial port.

// There should be a Name subkey whose value will be COMx for
// a serial port.
if ( RegQueryValueEx( key, _T( "Name" ), NULL, &nametype,
(unsigned char *)&name[ 0 ], &namesize ) == ERROR_SUCCESS )
{
// Check the name for COM.
int val;
if ( _stscanf( name, _T( "COM%d:" ), &val ) == 1 )
{
// If the value is bigger than the current value, update
// it.
if ( val > lastfound )
lastfound = val;
}
}

// Close key.
RegCloseKey( key );

}

// Go to next installed driver.
index++;
}

// Close base key.
RegCloseKey( basekey );
}

return lastfound;
}


Steve Maillet

unread,
Mar 14, 2001, 5:39:26 PM3/14/01
to
Try EnumDevices() it provides a list of all device names. or RasEnumDevices
to get RAS devices and find those with a device Type = 0. Remember that
serial ports can have a prefix other then "COM" I've worked on systems where
there were ports named "AUX"

--
Steve Maillet
Entelechy Software Consulting
smaillet 'AT' EntelechyConsulting 'DOT' com
http://www.EntelechyConsulting.com
"Davide Bruzzone" <dbru...@home.com> wrote in message
news:#V8WbFNrAHA.1840@tkmsftngp03...

Davide Bruzzone

unread,
Mar 14, 2001, 5:48:53 PM3/14/01
to
Paul,

Thanks very much... It may not be blessed, but if it works...

Cheers...

Dave

"Paul G. Tobey" <nos...@nospam.com> wrote in message
news:#jteBWNrAHA.1408@tkmsftngp05...


> Dave,
>
> This isn't some kind of blessed code, but it works on our device:
>
> Paul T.
>
> int CountSerialPorts()
> {

<Snip>


Davide Bruzzone

unread,
Mar 16, 2001, 3:07:23 PM3/16/01
to
OK, I've determined that the EnumDevices function is part of the Windows CE
DDK. Do I therefore need to have something special installed in order to be
able to use this function in my application? If not, where is the function
declared? The compiler is complaining that it's an undeclared identifier.

This seems like the easiest way to do what I need to do, but if I can't
easily use the function in a Windows CE application, I'll have to do what I
need to do via the registry.

Cheers...

Dave

"Steve Maillet" <nos...@nospam.com> wrote in message
news:uGR6veNrAHA.2112@tkmsftngp05...

0 new messages