Winapi Serial Port List

Winapi Serial Port List

P: n/a
Hi,
I am trying to find a way to populate a list of active Com ports on a
computer. There may be around 30 on one computer and all connected to
different Buses but I am looking for one in particular that is
emitting packets that I need to monitor.
Is there a function in C (maybe using winAPI) that will return the
list of com ports?
Another question I have is whether I am using CreateFile correctly.
For debugging purposes you can see I have declared 'char
activecomports' to contain three com ports that I have on my current
computer, although there is nothing connected to them, I expect the
program to cycle through them anyway.
The program runs until the first action after the do{} loop (nBuf =
read1...) where it halts and does not continue, am I passing in my com
ports incorrectly or does my 'fd' value get messed up somewhere? Here
is the relevant code I am using:
char comPort[20];
UCHAR *adrs; //This is an unsigned char
time_t TimeSec = 0;
int i,j,udi,k, nBuf, readAgain=0;
int Msg_ID, length, elmThree;
const char *activecomports[3] = {'COM1', 'COM3', 'COM4'};
for (i=0; i<4;i++)
{
(HANDLE)fd[0] = CreateFile(&activecomports[i][0], GENERIC_READ, 0,
0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
if( (HANDLE)fd[0] INVALID_HANDLE_VALUE ) {
printf('CreateFile error');
exit(0);
}
do {
nBuf = read1( fd[0], (buf[0]+nData[0]), (RS232BUFSIZ-nData[0]) );
if( nBuf 0 ) {
nData[0] += nBuf;
readAgain = ( nData[0] RS232BUFSIZ );
while( (i = findNextPacket( TimeSec, buf[0], &strt[0],
&nData[0] )) != -1 ) {
Msg_ID = *(buf[0]+i+1);
length = *(buf[0]+i+2);
elmThree = *(buf[0]+i+3);
adrs = buf[0]+i+4;
switch( Msg_ID ) {
default:
continue;
case EVENT_MSG_TYPE:
break;
} } }
} while( readAgain );
}
updateConnection( (HANDLE)fd[0] );
int read1( int fd, UCHAR *buf, int bufSiz ) {
int nBytesRead;
ReadFile( (HANDLE)fd, (LPVOID)buf, (DWORD)bufSiz,
(LPDWORD)&nBytesRead, NULL );
return nBytesRead;
}
Thank you so much in advance for any help you can provide!

Linux Serial Port List

Reading and writing serial ports and USB VCP devices on VB6/VBA with WinAPI calls. Win32::SerialPort - User interface to. To allow higher-level use of common API call sequences for dealing with serial ports. Uses features of the Win32 API to.