Trying to Retrieve a List of Active Serial/Com Ports in C

K

kudruu

Hi,
I tried posting this in comp.lang.C but need more specific help using
winAPI. 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:


int fd[2] = {-1,-1};
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[0], GENERIC_READ,
0,
0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
if( (HANDLE)fd[0] == INVALID_HANDLE_VALUE ) {
printf("CreateFile error");
exit(EXIT_FAILURE);
}
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!
 
K

kelvin.koogan

Is there a function in C (maybe using winAPI) that will return the
list of com ports?
Try QueryDosDevice, e.g.

TCHAR szDevices[65535];
unsigned long dwChars = QueryDosDevice(NULL, szDevices, 65535);
TCHAR *ptr = szDevices;

while (dwChars)
{
int port;
if (sscanf(ptr, "COM%d", &port) == 1)
{
// Add to list of com ports
}
TCHAR *temp_ptr = strchr(ptr, 0);
dwChars -= (DWORD)((temp_ptr - ptr) / sizeof(TCHAR) + 1);
ptr = temp_ptr + 1;
}
 
K

kudruu

Is there a function in C (maybe using winAPI) that will return the
list of com ports?

Try QueryDosDevice, e.g.

TCHAR szDevices[65535];
unsigned long dwChars = QueryDosDevice(NULL, szDevices, 65535);
TCHAR *ptr = szDevices;

while (dwChars)
{
int port;
if (sscanf(ptr, "COM%d", &port) == 1)
{
// Add to list of com ports
}
TCHAR *temp_ptr = strchr(ptr, 0);
dwChars -= (DWORD)((temp_ptr - ptr) / sizeof(TCHAR) + 1);
ptr = temp_ptr + 1;
}

That is exactly what I was looking for! Thank you!
 
K

kudruu

Here's another question: how does QueryDosDevice() work? The function
you helped me with works just fine but I don't exactly know the
hardware performances that take place. Is QueryDosDevice opening all
of my ports and not closing them because I've been getting invalid fd
handle values after I've called QueryDosDevice and ReadFile hangs up
rather than either working or returning an error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top