linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Serial Reading
@ 2006-04-20 13:17 ronaldo.afonso
  2006-04-20 13:23 ` Mihai Dontu
  2006-04-20 13:39 ` Steve Graegert
  0 siblings, 2 replies; 3+ messages in thread
From: ronaldo.afonso @ 2006-04-20 13:17 UTC (permalink / raw)
  To: linux-c-programming





Hello all,

I'm trying to read data from a "tty" one character at a time. I'd like that
when a single byte was inputed on one side of my tty, it imediataly must be
read by my user space program on the otherside. It is not happening in my
environment and I belive the cause is a buffer in the tty driver. I'd like
to know if it could be happening, I mean, could a tty be buffering data
before send to user space, and if so, what can I do to inform the tty
driver not buffering data anymore?
Thanks.


====================================================
Ronaldo Z. Afonso
Software Engineering
Avocent Corporation, formerly Cyclades
ronaldo.afonso@avocent.com
Phone: 55 11 5033-3361
Fax: 55 11 5033-3388
www.avocent.com
====================================================


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

* Re: Serial Reading
  2006-04-20 13:17 Serial Reading ronaldo.afonso
@ 2006-04-20 13:23 ` Mihai Dontu
  2006-04-20 13:39 ` Steve Graegert
  1 sibling, 0 replies; 3+ messages in thread
From: Mihai Dontu @ 2006-04-20 13:23 UTC (permalink / raw)
  To: linux-c-programming

ronaldo.afonso@avocent.com wrote:
> 
> 
> 
> Hello all,
> 
> I'm trying to read data from a "tty" one character at a time. I'd like that
> when a single byte was inputed on one side of my tty, it imediataly must be
> read by my user space program on the otherside. It is not happening in my
> environment and I belive the cause is a buffer in the tty driver. I'd like
> to know if it could be happening, I mean, could a tty be buffering data
> before send to user space, and if so, what can I do to inform the tty
> driver not buffering data anymore?
> Thanks.
> 
> 
> ====================================================
> Ronaldo Z. Afonso
> Software Engineering
> Avocent Corporation, formerly Cyclades
> ronaldo.afonso@avocent.com
> Phone: 55 11 5033-3361
> Fax: 55 11 5033-3388
> www.avocent.com
> ====================================================
> 
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 
> 
Hi,

This is a small demo I made some time ago. Should help.

#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>
#include <termios.h>
#include <unistd.h>

int main( void )
{
     fd_set read_fd;
     struct termios t;
     struct timeval tv;

        /* Since '0' is open()-ed to STDIN... let's use this */
        /* "See" what the terminal knows */
     tcgetattr( 0, &t );

        /* "Tell" the terminal not to wait for EOL (Enter) */
     t.c_lflag &= ~( ICANON );

        /* This should be restored if not needed anymore */
        tcsetattr( 0, TCSANOW, &t );

     printf( "Waiting for ESC ...\n" );
     while ( 1 ) {
          memset( &tv, 0, sizeof( tv ) );
          tv.tv_sec = 1;

                /* These two below shold not be needed (should be outside the loop), but somehow the program won't work without
them */
          FD_ZERO( &read_fd );
          FD_SET( 0, &read_fd );

                /* Aaa... multiplexing for the penguins :-) */
          if ( select( 1, &read_fd, NULL, NULL, &tv ) > 0 && FD_ISSET( 0, &read_fd ) ) {
               unsigned v = 0;

                        /* Read what we've got so far */
               read( 0, &v, sizeof( v ) );

                        /* Mask out the extra data and leave the character code */
               v &= 0x7F;

                        /* This has been deduced experimentaly :-) */
                if ( v == 0x1b ) printf( "ESC!\n" );
          } else {
                       /* Do something */
                       printf( "Zum\n" );
                }
     }
     return 0;
}

Enjoy,
M.D.

-- 

|_|0|_|
|_|_|0|
|0|0|0|


-- 
This message was scanned for spam and viruses by BitDefender.
For more information please visit http://www.bitdefender.com/


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

* Re: Serial Reading
  2006-04-20 13:17 Serial Reading ronaldo.afonso
  2006-04-20 13:23 ` Mihai Dontu
@ 2006-04-20 13:39 ` Steve Graegert
  1 sibling, 0 replies; 3+ messages in thread
From: Steve Graegert @ 2006-04-20 13:39 UTC (permalink / raw)
  To: linux-c-programming

On 4/20/06, ronaldo.afonso@avocent.com <ronaldo.afonso@avocent.com> wrote:
>
> Hello all,
>
> I'm trying to read data from a "tty" one character at a time. I'd like that
> when a single byte was inputed on one side of my tty, it imediataly must be
> read by my user space program on the otherside. It is not happening in my
> environment and I belive the cause is a buffer in the tty driver. I'd like
> to know if it could be happening, I mean, could a tty be buffering data
> before send to user space, and if so, what can I do to inform the tty
> driver not buffering data anymore?
> Thanks.

By default, the terminal is configured to be in line-by-line mode to
allow pre-processing of characters and the like.  That mode provides
better usability and improves bandwidth utilization.

To read a character at a time you will have to put the device into raw
mode using the tcsetattr(3) function.  First of all configure the
termios structure accordingly, which means to turn off echoing and
canonical mode.

The following example is adopted (not sure, but I think so) from the
Stevens text and should illustrate the basic idea:

--- BEGIN ---

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>

struct termios tty, otty;

void raw(struct termios tty) {
	tty.c_lflag     &= ~(ECHO | ECHOK | ICANON);
	tty.c_cc[VTIME] = 1;
	tcsetattr(STDIN_FILENO, TCSANOW, &tty);
}


void cooked(struct termios tty) {
	tty.c_lflag     = otty.c_lflag;
	tty.c_cc[VTIME] = 0;
	tcsetattr(STDIN_FILENO, TCSANOW, &tty);
}


int main(int argc, char **argv) {
	int i, j;
	
	/* Get original tty settings and save them in otty */
	tcgetattr(STDIN_FILENO, &otty);
	tty = otty;
	
	/* Set desired mode */
	raw(tty); /* cooked() for canonical mode */
	
	for (i = 0; i < 10; i++) {
		fprintf(stdout, "Type a character: ");
		fflush(stdout);
		j = getchar();
		fprintf(stdout, "%c\n", j);
		fflush(stdout);
	}
	
	/* Reset to the original settings */
	tcsetattr(STDIN_FILENO, TCSANOW, &otty);
	return (0);
}

--- END ---

	\Steve

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

end of thread, other threads:[~2006-04-20 13:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-04-20 13:17 Serial Reading ronaldo.afonso
2006-04-20 13:23 ` Mihai Dontu
2006-04-20 13:39 ` Steve Graegert

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 NNTP newsgroup(s).