CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2012
    Posts
    181

    Question Why such code sometimes don't work

    Such code work
    Code:
      DWORD dwRead = 0;
      while (!dwRead)
      {
       ClearCommError(h, &Err, &ComStat);
       dwRead = ComStat.cbInQue;
       Sleep(10);
      }
      DWORD dwRead2=0;
      while (dwRead != dwRead2)
      {
       dwRead2 = dwRead;
       ClearCommError(h, &Err, &ComStat);
       dwRead = ComStat.cbInQue;
       Sleep(10);    
      }
      hb->b = true;
      return;
    Such code sometimes don't work:
    Code:
     WaitCommEvent(h, &Mask, ReadOL);
      DWORD Signaled = WaitForSingleObject(ReadOL->hEvent, INFINITE);
      if (Signaled == WAIT_OBJECT_0)
    	{
    	  DWORD BytesTrans;
    	  if (GetOverlappedResult(h, ReadOL, &BytesTrans, false))
    	  {
            if (Mask & EV_RXCHAR)
    		  { 
    		    hb->ReadOL = ReadOL;
    			hb->b = true;
                            ReadFile(hComPort, buff_read, min(dwRead, 256), &tmpBytesRead, ReadOL)// Or          //                                                                                                                      NULL also don't work
    //                                                                                                                         correctly...
    			CloseHandle(ReadOL->hEvent);
    		}
    	  }
        }
    And is it correctly to use first snippet?

  2. #2
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Why such code sometimes don't work

    The first snippet is polling, and polling is evil (since it sucks up 100% CPU doing essentially nothing) and should be avoided.

    As for the second snippet, well, what does "don't work" mean? You need to explain yourself better.

    In a quick look at the code, I think it is a mistake to call WaitForSingleObject unless, the call to WaitCommEvent returns a GetLastError() value of ERROR_IO_PENDING. You are calling WFSO every single time. See the final "else" branch of the sample code at http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

    You might also want to use a second event that your own application sets, so you can break out of the wait loop when you want to, using WaitForMultipleObjects(), as described in the "Event Driven Approach" section of the following article: "Serial Communication in Windows" by Ashish Dhar at http://www.codeproject.com/Articles/...ion-in-Windows

    Mike

  3. #3
    Join Date
    Jul 2005
    Posts
    19

    Re: Why such code sometimes don't work

    doesn't work means what?

    do you mean to say, sometimes it stuck in deadlock?


    regards,
    Vatsa
    www.objectiveprogramming.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured