From: "Steve Graegert" <graegerts@gmail.com>
To: linux-c-programming@vger.kernel.org
Subject: Re: Serial Reading
Date: Thu, 20 Apr 2006 15:39:01 +0200 [thread overview]
Message-ID: <6a00c8d50604200639s256665efja4bb31353862f6ae@mail.gmail.com> (raw)
In-Reply-To: <OF6B1B71E6.FB0713A3-ON83257156.004830D6-83257156.0048FB31@Cyclades.com>
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
prev parent reply other threads:[~2006-04-20 13:39 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
2006-04-20 13:39 ` Steve Graegert [this message]
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=6a00c8d50604200639s256665efja4bb31353862f6ae@mail.gmail.com \
--to=graegerts@gmail.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).