From: Mihai Dontu <mdontu@bitdefender.com>
To: linux-c-programming@vger.kernel.org
Subject: Re: Serial Reading
Date: Thu, 20 Apr 2006 16:23:20 +0300 [thread overview]
Message-ID: <44478B48.8050001@bitdefender.com> (raw)
In-Reply-To: <OF6B1B71E6.FB0713A3-ON83257156.004830D6-83257156.0048FB31@Cyclades.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.
>
>
> ====================================================
> 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/
next prev parent reply other threads:[~2006-04-20 13:23 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-04-20 13:17 Serial Reading ronaldo.afonso
2006-04-20 13:23 ` Mihai Dontu [this message]
2006-04-20 13:39 ` Steve Graegert
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=44478B48.8050001@bitdefender.com \
--to=mdontu@bitdefender.com \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).