Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Win API / June 2007



Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
kudruu@gmail.com - 21 Jun 2007 22:10 GMT
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!
Bob Butler - 21 Jun 2007 22:18 GMT
> Hi,
> I tried posting this in comp.lang.C but need more specific help using
> winAPI.

The problem with that is that this newsgroup is for using the windows API
from VB, not C
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.