Solved

Waitforsingleobject always results in a timeout !!!

Posted on 2004-08-03
1
2,041 Views
Last Modified: 2013-12-03
My waitforsingleobject always results in a timeout and i am unable to figure out why.. Please look at my code below..
i have a read file call .that gets called as soon as i receive a character in the input buffer. Hence i am waiting on waitcommevent and when a character appears, i issue a readfile to read all the characters that have come through my serial port.. However, waitcommevent is a blocking call. I had to make the waitcommevent timeout.. Hence, i moved to overlapped IO. But now..my waitcommevent always timeouts even though i know that there are characters to read. Please let me know what is wrong with my code..
/*********************************************************************************************/
int ReadProc1(char *str)
{
     
     /*
     Using the EV_RXCHAR flag will notify the thread that a byte arrived at the port. This event,
     used in combination with the ReadFile function, enables a program to read data only after
     it is in the receive buffer, as opposed to issuing a read that waits for the data to arrive*/


     DWORD dwCommEvent; //a variable that receives a mask indicating the type of event that occurred
     char chRead;          // Character read every time a read takes place.
     DWORD dwRead =0,Err,Bytes;          //Pointer to the variable that receives the number of bytes read
     char * str =calloc(100,sizeof(char));
     unsigned int i=0,j=2;
     BOOL fWaitingOnRead = FALSE,Ok= FALSE;

     OVERLAPPED osReader= {0};

     

     FillMemory(str, strlen(str), 0);
     //if((strcmp(p->PType,"13") == 0) && (strcmp(p->Data,"0") != 0)) stop = 0x06;
     if (stop == 0x06) terminate=1;
     if (stop == 0x03) terminate=0;
     
     
     memset(&osReader,0,sizeof(OVERLAPPED));
     osReader.hEvent = CreateEvent(
    NULL,   // no security attributes
    TRUE,  // auto reset event
    FALSE,  // not signaled
    NULL    // no name
    );
     if (osReader.hEvent == NULL)
     {
          LogEvent(hComm,"Error creating overlapped event");
     }

     for ( ; ; ) {
          if (!fWaitingOnRead)
          {

               if (!(WaitCommEvent(hComm, &dwCommEvent,&osReader)))
               {

                    if (GetLastError() != ERROR_IO_PENDING)
                    {
                         LogEvent (ghwnd," Error in Wait Comm Event");
                         return FALSE;
                    }
                    else
                    {
                         fWaitingOnRead = TRUE;
                         Err = WaitForSingleObject(hComm, READ_TIMEOUT);
                         
                         switch(Err)
                         {
                         case WAIT_TIMEOUT:
                              LogEvent(ghwnd,"Timed out waiting for response from device.. check connections/power");
                              return FALSE;
                              break;


                         case WAIT_OBJECT_0:
                          // Event triggered. Check the status.
                             
                              Ok = GetOverlappedResult(hComm, &osReader, &Bytes, TRUE);
                              if (Ok)
                              {
                       
                                   do
                                   {
                                                           
                                        if (ReadFile(hComm, &chRead, 1, &dwRead, NULL))
                                        {
                                   
                                             // A byte has been read; process it.
                                             str[i++]=chRead;
                                                                                     }
                                        else
                                        {
                                        // An error occurred in the ReadFile call.
                                             LogEvent(ghwnd," Read Failed With Error Code ");
                                             break;
                                        }
                         
                                   } while (dwRead );
                              }
                              else
                              {
                                   LogEvent(ghwnd,"Get Overlapped Result Failed ");
                                   return FALSE;
                              }
                              break;
                         default:

                              LogEvent (ghwnd," Error in wait for single object");
                              return FALSE;
                              break;
                         }//End of Switch
                    }//End of GetLastError
               }
               else
               {
                    LogEvent(ghwnd,"Read Completed Immedialtely");
                       /* So Proceed to readfile
                                 }//End of Wait Comm Event
          }// End of Waiting on Read
         
     
     }

         
     }
     
 
/*********************************************************************************************
0
Comment
Question by:shubha_mohan
1 Comment
 
LVL 48

Accepted Solution

by:
AlexFM earned 250 total points
ID: 11704971
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now