none
SetCommMask and WaitCommEvent is not working for TX output buffer?? RRS feed

  • Question

  • Dear Developers,

    Greetings!!

    As, I want to know the status of Tx output buffer that either it is empty or not and for that I have set the mask as

    SetCommMask(lhPort, EV_TXEMPTY)

    and create the event to wait for the above to be empty as 

    WaitCommEvent(lhPort, &dwWaitEvent,NULL)

    Actually , at the very first step of setting the communication mask, I am suing write operation and at the end of wait communication event I am using a printf statement and I have observed that the printf statement is not printing. The waitcommevent is waitng for the Tx output buffer to be empty. I

    I have connected, Tx of the board to the Rx of the Host device and on the console I can see full string value. 

    It means the value is moving out of the Tx output buffer but the waiting event is still waiting for the Tx output buffer to be empty.. Any specific reason  that why its still waiting?????

    My main motto is to check that either Tx output buffer is empty or not??

    Tuesday, May 19, 2015 12:55 PM

All replies

  • Dear Developers.

    Greetings!!

    As, my logic is as -

    if(SetCommMask(lhPort, EV_TXEMPTY)){
    		WaitCommEvent(lhPort,&dwWaitEvent , 0);
    			retval = WriteFile(lhPort,
    				 lwBuff,
    				 dwSize,
    				 &dwBytesWritten,
    				 NULL);
    
    RETAILMSG(TRUE,(TEXT(" ------------After Write---------\r\n")));
       if(retval){
    	return TRUE;
    
    		}else{
    			RETAILMSG(TRUE,(TEXT("The Last error is %d \r\n"),GetLastError()));
    			return FALSE;
    		}
    }

    At initial stage, the EV_TXEMPTY should be empty and WaitCommEvent should not wait for the set event. Atleast first time, Write should happen. But, its not happening. When I say write is not happening, it means WaitCommEvent is still waiting for pending IO.


    Wednesday, May 20, 2015 5:03 AM
  • You say "At initial stage, the EV_TXEMPTY should be empty and WaitCommEvent should not wait for the set event."  Which is interesting for you to say because that isn't at all what the documentation says.  Where did you read that?

    Next:  You are waiting for:

          EV_TXEMPTY The last character in the output buffer was sent.

    Note, this doesn't say that it happens simply because the Tx FIFO is empty, but instead because the last character of the "buffer" is sent.  If you check the documentation for your UART you will see that most likely this occurs before the Tx FIFO is empty - so characters will still be outputting on the line.  NOTE: "buffer" is a software buffer, usually an array and Tx FIFO is hardware.  So if you don't write, then nothing is in the buffer and therefore the last byte can't be sent - and not the biggie, wait for it...., if the last byte isn't sent, then no event is set.

    Why do you think that SetCommMask() is not failing?

    Are you calling WaitCommEvent anywhere else?  If so, please read the documentation.


    Bruce Eitman (eMVP) Senior Engineer Bruce.Eitman AT Eurotech DOT com My BLOG http://geekswithblogs.net/bruceeitman Eurotech Inc. www.Eurotech.com

    Wednesday, May 20, 2015 1:06 PM
    Moderator
  • Dear Developers,

    Greetings!!

    Bruce, Thanks for reply!!!

    I got it what you wanted to explain. Actually, it was by mistake. It should wait for an event. 

    Now, What I am doing. 

    I am writing some thing in the buffer and before writing I have created a read thread. and after writing, I am setting an event that is to be monitored and waiting for the event to be ocured when the last character has to be sent and it should come out from the wait and thats not happening here in my case.

    The code flow is as - 

    hlReadThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)fxnReadThreadlhPort
    ,lhPort,0, NULL);
    
    
    retval = WriteFile(lhPort,
    				 lwBuff,
    				 dwSize,
    				 &dwBytesWritten,
    				 NULL);
    		
    	if(SetCommMask(lhPort, EV_TXEMPTY)){
    		RETAILMSG(1,(TEXT("-------------1-------------\r\n")));
    
    		retval = WaitCommEvent(lhPort, &dwWaitEvent , NULL);
    		if(retval == 0){
    
    			RETAILMSG(1,(TEXT("Error in WaitCommEvent \r\n")));
    			RETAILMSG(1,(TEXT("The lase error is %d \r\n"),GetLastError()));
    		}else{
    
    				RETAILMSG(1,(TEXT("Keep Waiting for TX_BUFF to be empty \r\n")));
    		}
    
    		

    Thursday, May 21, 2015 4:20 AM