Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
20 years ago

break loop with 'q'

Hello,

this may be a very beginner question, but I cannot get ahead.

I have a loop wich should be performed until the user presses 'q'. I tried with getchar() but it seems that getchar() waits for a return and my loop is not performed anymore. So I'm looking for a command which is not waiting for a return, or maybe a command who tells me if the uart buffer is empty or not.

Thank you for each suggestion, Ralf

do
{
  printf( "do something" );
} while( getchar() != 'q' );

3 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    This happens because the default STDIN is blocking; it waits for an input before returning a value. To make the system do what you want you must make it a non-blocking call. If you are using the 5.0 release of Nios II you can use the fcntl call and change STDIN to non blocking (O_NONBLOCK flag). If you are using an earlier release, you can change the flags directly or create a new file handle for the serial line and create to be non-blocking.

    Hope this helps.

    Michael
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thank you very much, works fine now.

    My code:

    int32 KeyPressed( void )
    {
      int32 key = -1;
      int32 flags;
      
      /* save flags */
      flags = fcntl( fileno(stdin), F_GETFL );
      
      /* set non blocking */
      if( ! fcntl( fileno(stdin), F_SETFL, flags | O_NONBLOCK ) );
      {
        key = getchar();
      }
      
      /* restore flags */
      fcntl( fileno(stdin), F_SETFL, flags );
      
      return( key );
    }
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi,

    This code works with Quartus II 9.0 using the JTAG uart but failed when I use an UART core.

    Have you an idea to allow non-blocking with the UART corre from ALTERA ??