public inbox for ecos-discuss@sourceware.org
 help / color / mirror / Atom feed
* [ECOS] Select() issues
@ 2007-01-09 20:21 Andre-John Mas
  2007-01-10 15:32 ` Andrew Lunn
  2007-01-11  1:20 ` Andre-John Mas
  0 siblings, 2 replies; 4+ messages in thread
From: Andre-John Mas @ 2007-01-09 20:21 UTC (permalink / raw)
  To: ecos-discuss

Hi,

We have a solution working on the AdderII platform, using eCos.
One component of the application is a serial terminal, with
termios support. While putting the UI through its paces we
came across a small issue:

If we hit enter a number of times prior to the first select()
call, select() does not return when a key is pressed. I
thought that it could be to a full UART buffer, but trying

cin.ignore()

or

tcflush(fd,TCIOFLUSH)

prior to the select does not fix the issue.

My code looks something as follows:

   struct termios ts, ots; /* termios setting, old termios setting */
   int            fd = STDIN_FILENO;
   struct timeval tv;
   struct timeval *tvPtr = NULL;

   tcgetattr( fd, &ts );
   ots = ts;
   ts.c_flag &= ~(ICANON);
   tcsetattr(fd, TCSAFLUSH, &ts);

   if ( this->timeout > -1 )
   {
       tv.tv_sec = this->timeout;
       tv.tv_usec = 0;
       tvPtr = &tv;
   }

   FD_ZERO( &fds );
   FD_SET( STDIN_FILENO, &fds );

   int result = select(fd + 1, &fds, NULL, NULL, tvPtr);

Am I missing something? Any help would be appreciated.

Andre



-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 4+ messages in thread
* Re: Re: [ECOS] Select() issues
@ 2007-01-10 16:28 Andre-John Mas
  2007-01-11 18:20 ` Andrew Lunn
  0 siblings, 1 reply; 4+ messages in thread
From: Andre-John Mas @ 2007-01-10 16:28 UTC (permalink / raw)
  To: Andrew Lunn; +Cc: ecos-discuss

Andrew Lunn wrote:
> 
> >    struct termios ts, ots; /* termios setting, old termios setting */
> >    int            fd = STDIN_FILENO;
> 
> Im not an eCos termios expert, but shouldn't this be a file handle
> returned by opening /dev/termios0 and not standard in?
> 
>          Andrew
> 


On 10-Jan-07, at 10:32 , Andrew Lunn wrote:

   struct termios ts, ots; /* termios setting, old termios setting */
   int            fd = STDIN_FILENO;

Im not an eCos termios expert, but shouldn't this be a file handle
returned by opening /dev/termios0 and not standard in?

         Andrew

Up until this particular issue I have had no problems using "standard in".
I will look to see what effect opening /dev/termios0, but most examples
I have seen use STDIN_FILENO directly.

Changing my code slightly, I now find that I now block at cin.get().
I wondering whether I am doing something wrong when changing to canocial
mode?

char InputHandler::DoPressAnyKey()
{
    struct termios  ts, ots;
    int             c  = 0;
    int             fd = STDIN_FILENO;  
                  
    cout << "--0\n";
          
    tcflush(fd,TCIOFLUSH);    
       
    cout << "--1\n";  
          
    WaitForData();
                        
    tcgetattr(fd, &ts);     /* save tty state */
    ots = ts;               /* structure copy */
    ts.c_lflag &= ~( ICANON );
    ts.c_cc[VMIN] = 1;
    ts.c_cc[VTIME] = 0;
    tcsetattr(fd, TCSAFLUSH, &ts);
         
    cout << "--2\n";
        
    // INFO: make sure to reset terminal attributes before
    //       leaving method 
        
    if ( !IsTimedOut() ) 
    {
       cout << "--2.1\n";
       c = cin.get();
       cout << "--2.2\n";
    }
    
    cout << "--3\n";
        
    tcsetattr(fd, TCSAFLUSH, &ots); /* restore TTY state */
    
    cout << "--4\n";
        
    if ( IsTimedOut() ) 
    {
        c = 0;
    }
    
    return c;
     
}

void InputHandler::WaitForData()
{
    int             fd = STDIN_FILENO;    
    fd_set          fds;
    struct timeval  tv;
    struct timeval  *tvPtr = NULL; 
    
    cout.flush();
           
    do
    {   
        // INFO: if the time out is not zero then define the timeval and have tvPtr
        //       refer to it. if it is zero then tvPtr uses a NULL value. I things this
        //       way to avoid having to duplicate the select call and also not need to
        //       worry about memory allocation/deallocation
        
        cout << "--a\n";
        
        if ( this->timeout != 0 )
        {
            tv.tv_sec = this->timeout; 
            tv.tv_usec = 0;
            tvPtr = &tv;
        }
        else
        {
            tvPtr = NULL;
        }
        FD_ZERO( &fds );
        FD_SET( STDIN_FILENO, &fds );

        cout << "--b\n";
        
        int result = select(fd + 1, &fds, NULL, NULL, tvPtr);
        
        cout << "--c\n";
        
        if ( result == 0 )
        {
            this->timedout = true;
            break;           
        }
        else if ( result > 0 && FD_ISSET(fd,&fds) )
        {      
            break;
        }
        
        cout << "--d\n";
    }
    while ( true );
  
    cout << "--e\n";
}



-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-01-12  3:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-01-09 20:21 [ECOS] Select() issues Andre-John Mas
2007-01-10 15:32 ` Andrew Lunn
2007-01-11  1:20 ` Andre-John Mas
2007-01-10 16:28 Andre-John Mas
2007-01-11 18:20 ` Andrew Lunn
2007-01-12  3:45   ` Andre-John Mas

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).