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[i][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!
kelvin.koogan@googlemail.com - 22 Jun 2007 14:37 GMT
On 21 Jun, 22:44, kud...@gmail.com wrote:
> 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;
}
kudruu@gmail.com - 22 Jun 2007 15:45 GMT
On Jun 22, 7:37 am, kelvin.koo...@googlemail.com wrote:
> On 21 Jun, 22:44, kud...@gmail.com wrote:
>
[quoted text clipped - 18 lines]
> ptr = temp_ptr + 1;
> }
That is exactly what I was looking for! Thank you!
kudruu@gmail.com - 25 Jun 2007 22:29 GMT
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.