WaitCommEvent()????

This is a discussion on WaitCommEvent()???? within the Windows Programming forums, part of the Platform Specific Boards category; Hi: Here is to monitor the any character arrives in COM Port Code: while(TRUE) { if (!WaitCommEvent(COM1, &dwCommEvent, &osStatus)) { ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    173

    Unhappy WaitCommEvent()????

    Hi:
    Here is to monitor the any character arrives in COM Port
    Code:
    
    while(TRUE)
    {
        if (!WaitCommEvent(COM1, &dwCommEvent, &osStatus))
       {
            if (GetLastError() != ERROR_IO_PENDING)	  // Wait not delayed!!
           {
             							/*ERROR HANDLING HERE*/ 			
    
           }
           else
         //   while(TRUE)
            //{
                    deRes = WaitForSingleObject(osStatus.hEvent,3000);
                    switch(deRes)
                   {
                        case WAIT_OBJECT 0:
                        /*osStatus.hEvent gets signaled*/
                        break;
                         case WAIT_TIMEOUT:
                        MessageBox(0,"timeouts","ok",MB_OK);
                        break;
                        default:
                        ............
                        break;
                   }
           //    }//end while(TRUE)
           }
    }
    
    Here I hope those varaibles are cleared with the names.
    
    The problem is that the GetLastError() is not pending after the first round WM_TIMEOUT,I just can't say the reason, but is there any way to avoid the error if I want to restart from WaitCommEvent() after every WM_TIMEOUT??
    Thanx for the help!!!
    Last edited by SuperNewbie; 03-08-2004 at 12:14 AM.
    Don't laugh at me,I am just a SuperNewbie.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Not gone thru your code fully. Just skimmed it but found an error.
    Think you missed a brace { changing the meaning of the code.

    Try
    Code:
    //I like to have a loop variable
    int iLoop=TRUE;
    while(iLoop)
    {
        if (!WaitCommEvent(COM1, &dwCommEvent, &osStatus))
       {
            if (GetLastError() != ERROR_IO_PENDING)	  // Wait not delayed!!
           {
             	/*ERROR HANDLING HERE*/ 			
    
           }
           else
           {//this brace is missing 
                    deRes = WaitForSingleObject(osStatus.hEvent,3000);
                    switch(deRes)
                   {
                        case WAIT_OBJECT 0:
                        /*osStatus.hEvent gets signaled*/
                        break;
                         case WAIT_TIMEOUT:
                        MessageBox(0,"timeouts","ok",MB_OK);
                        break;
                        default:
                        ............
                        break;
                   }
           }
       }
       else //is this where we should exit?
    	iLoop=FALSE;
    }
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    173
    Thanks, but what I should do if I don't want to quit while() loop? some problem occurs in WaitCommEvent() after the first timeouts.

    Thanks alot
    Don't laugh at me,I am just a SuperNewbie.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    I'm not sure what you want to do so....

    Code:
    do//until we get the object signaled
    {
               deRes = WaitForSingleObject(osStatus.hEvent,3000);
               if(deRes ==WAIT_OBJECT )
               {                    
                        /*osStatus.hEvent gets signaled*/
                        //set main loop var here to exit?
               }
               else if(//error occurs)
               //ect
    }
    while (deRes==WAIT_TIMEOUT)//three seconds passed without change
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cancel WaitCommEvent on win2k?
    By yoxler in forum Windows Programming
    Replies: 0
    Last Post: 05-08-2003, 12:34 AM
  2. WaitCommEvent?
    By yoxler in forum Windows Programming
    Replies: 4
    Last Post: 04-10-2003, 06:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21