termios & binary data
Eric Norum
eric at skatter.USask.Ca
Thu May 7 15:05:45 UTC 1998
Geoffroy Montel <g_montel at yahoo.com> wrote:
> Hello,
>
> Termios is cool for console purpose, it works well (thanks Eric).
> But now I'd like to add quick binary I/O in my bsp. First I thought
> about
> doing another serial driver, but Joel adviced me to ask about
> termios & binary data. Besides it would be better having a single
> driver!
>
> So, as, I quote termio man page, "Normally, terminal input is
> processed in
> units of lines", how could we do binary I/O with termios? There's a
> non-canonical mode input processing, but is it implemented?
>
> I also would need these things:
> 1. read a given number of bytes from an uart
> 2. blocking/non-blocking read
For raw output, just turn off OPOST. For raw input turn off
echoing, ISTRIP and ICANON. For blocking/non-blocking/timed input,
set c_cc[VMIN] and c_cc[VCTIME] appropriately. There is some
overhead involved in going through the termios driver for raw I/O,
but I'm willing to take the performance hit for the flexibility of
being able to use familiar POSIX terminal handling code in my
application.
Here's some of the code I used to test the termios driver:
/*
* Test raw (ICANON=0) input
*/
static void
testRawInput (int vmin, int vtime)
{
int i;
struct termios old, new;
rtems_interval ticksPerSecond, then, now;
unsigned int msec;
unsigned long count;
int nread;
unsigned char cbuf[100];
printf ("*** Raw input VMIN=%d VTIME=%d ***\n", vmin, vtime);
rtems_clock_get (RTEMS_CLOCK_GET_TICKS_PER_SECOND,
&ticksPerSecond);
i = tcgetattr (fileno (stdin), &old);
if (i < 0) {
printf ("tcgetattr failed: %s\n", strerror (errno));
return;
}
new = old;
new.c_lflag &= ~(ICANON | ECHO | ECHONL | ECHOK | ECHOE |
ECHOPRT | ECHOCTL);
new.c_cc[VMIN] = vmin;
new.c_cc[VTIME] = vtime;
i = tcsetattr (fileno (stdin), TCSANOW, &new);
if (i < 0) {
printf ("tcsetattr failed: %s\n", strerror (errno));
return;
}
do {
rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &then);
count = 0;
for (;;) {
nread = read (fileno (stdin), cbuf, sizeof cbuf);
if (nread < 0) {
printf ("Read error: %s\n", strerror
(errno));
goto out;
}
count++;
if (nread != 0)
break;
}
rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now);
msec = (now - then) * 1000 / ticksPerSecond;
printf ("Count:%-10lu Interval:%3u.%3.3d Char:",
count, msec / 1000, msec % 1000);
for (i = 0 ; i < nread ; i++)
printf (" %2.2x", cbuf[i]);
printf ("\n");
} while (cbuf[0] != 'q');
out:
i = tcsetattr (fileno (stdin), TCSANOW, &old);
if (i < 0)
printf ("tcsetattr failed: %s\n", strerror (errno));
}
.
.
.
.
testRawInput (0, 0);
testRawInput (0, 20);
testRawInput (5, 0);
testRawInput (5, 20);
---
Eric Norum eric at skatter.usask.ca
Saskatchewan Accelerator Laboratory Phone: (306) 966-6308
University of Saskatchewan FAX: (306) 966-6058
Saskatoon, Canada.
More information about the users
mailing list