[En-Nut-Discussion] Sample code to perform non blocking check for incoming UART characters (Inkey$)

Brett Abbott Brett.Abbott at digital-telemetry.com
Thu May 8 22:43:07 CEST 2003


Hi

The following snippet of code can be used to check for the existance of 
incoming characters without having to actually receive them.  Useful for 
"Press the Return key or wait 10 seconds to reload last known 
configuration" type messages.  Improved versions of version portable 
code welcomed.

It uses the uart library.  Is there a more generic stream or file 
function call I could use?  I chose this approach rather than write a 
custom function to hopefully support future and past nutos releases.

Hopefully this will save someone some effort.

Cheers
Brett

// Globals
FILE *uart;
u_long UartStatus;
u_long *UartStatus_ptr;

// Sample Code to check for key press, Assumes uart file already open. 
Does not block if no characters were received.

   // Were any keys pressed?
   result=UartAvrIOCtl(((NUTFILE *) (_fileno(uart)))->nf_dev, 
UART_GETSTATUS, UartStatus_ptr);
   if((UartStatus & UART_RXBUFFEREMPTY)==0) // One or more keys were 
pressed
        if((rlen = _read(_fileno(uart), rxbuff_ptr, RXBUFF_LEN)) > 0) // 
Get the characters
           if(strchr(rxbuff_ptr, '\r') || strchr(rxbuff_ptr, '\n'))    
// Did the user hit <CR> or <LF> ?
               UserEnterNewConfig();  // Yes? go to Configure mode

-- 
-----------------------------------------------------------------
Brett Abbott, Managing Director, Digital Telemetry Limited
Email: Brett.Abbott at digital-telemetry.com
PO Box 24 036 Manners Street, Wellington, New Zealand
Phone +64 (4) 5666-860  Mobile +64 (21) 656-144
------------------- Commercial in confidence --------------------






More information about the En-Nut-Discussion mailing list