Bug in Win32file WaitCommEvent ???

Grant Edwards grante at visi.com
Tue Nov 19 20:05:24 EST 2002


In article <slrnatll92.qu5.grante at localhost.localdomain>, Grant Edwards wrote:

>> I don't think this last step is correct.
> 
> Perhaps.  If it isn't correct, where does the event mask get returned?

Here's some example code I found at
http://www.codeproject.com/system/serial_com.asp (among other sites). 

It uses GetCommMask() to read the mask value after the WaitCommEvent event
has triggered. However, the MSDN page for GetCommMask says that it returns
"a mask of events that are currently enabled".  To me that means that
GetCommMask() just reads back the value set by SetCommMask().  

But, the bit definitions say that bits mean things like "A character was
received and placed in the input buffer" not that this particular event is
unmasked.

while ( abContinue )
    {
        BOOL abRet = ::WaitCommEvent(apThis->m_hCommPort,&dwEventMask, &ov);
        if ( !abRet )
            ASSERT( GetLastError () == ERROR_IO_PENDING);
        arHandles[1] = ov.hEvent ;
        dwWait = WaitForMultipleObjects (2,arHandles,FALSE,INFINITE);
        switch ( dwWait )
        {
        case WAIT_OBJECT_0:
	    _endthreadex(1);
            break;
        case WAIT_OBJECT_0 + 1:
                DWORD dwMask;
                if (GetCommMask(apThis->m_hCommPort,&dwMask) )
                {
                   if ( dwMask & EV_TXEMPTY )
		     TRACE("Data sent");
                   ResetEvent ( ov.hEvent );
                   continue;
                }
                else 
                {
                   //read data here and reset ov.hEvent
                }
        }
    }

And then other web pages claim that "An event mask, which controls which
events set the state of the event object [...], can be set using the
SetCommMask function. The current event mask can be retrieved by calling the
GetCommMask function."

Is it just me, or is a "mask" and the set of pending events two different
things? Cripes, are all MS documents this bad?  If this were Linux, we'd
have just had a peek at the kernel code and settled this in 30 seconds. :)

Here's another example I found at
http://groups.google.com/groups?selm=%23Cq5e4pxAHA.2280%40tkmsftngp02

It apparently expects it to work the way I originally described it:

if (WaitCommEvent(hCom, &dwEvtMask, &o))
  dRes = dwEvtMask;
else
{
   fSuccess = GetLastError();
   if(fSuccess == ERROR_IO_PENDING)
     {
	DWORD res;
	MSG msg;
	do
	  {
	     res = WaitForSingleObject(o.hEvent,nTimeOut);
	     if(PeekMessage(&msg,NULL,WM_USER, FM_MSG_LAST,PM_REMOVE))
	       ProcessPostMsg(msg);
	  }while(res == WAIT_TIMEOUT && !nTimeOut);
	switch(res)
	  {
	   case WAIT_OBJECT_0:
	     dRes = dwEvtMask;
	   case WAIT_TIMEOUT:
	     break;
	   default:
	     break;
	  }
     }
}
if(dRes == EV_RXCHAR)
{
   // Lots of stuff
}
PurgeComm(hCom,PURGE_RXCLEAR);
dwEvtMask = 0;
dRes = 0x8000;


How do you guys in Win32land answer questions like this?  MSDN is useless,
MS support is even worse according to the Win32 guys I've talkd to, and
there doesn't seem to be any consensus on how this works among people
actually using it!

-- 
Grant Edwards                   grante             Yow!  I feel like I am
                                  at               sharing a "CORN-DOG" with
                               visi.com            NIKITA KHRUSCHEV...



More information about the Python-list mailing list