Timeout on serial port read

Bruno Pape root at sgzh.uucp
Sat May 5 02:13:43 AEST 1990


In article <9005030929.aa08086 at VMB.BRL.MIL> moss at BRL.MIL ("Gary S. Moss", VLD/VMB) writes:
>< I am trying to set the ioctl call so that a raw read from a
>< serial port will time out after a few seconds. No matter what I
>< set the TIME value for in the control structure, the machine
>< still hangs waiting for characters. What am I doing wrong, if anything.
><
><     new_settings.c_iflag = IGNBRK | IGNPAR;
><     new_settings.c_oflag = NULL;
><     new_settings.c_cflag = B9600 | CS8 | CREAD | CLOCAL;
><     new_settings.c_lflag = ICANON;
><     new_settings.c_line  = NULL;
><     new_settings.c_cc[0] = NULL;
><     new_settings.c_cc[1] = NULL;
><     new_settings.c_cc[2] = NULL;
><     new_settings.c_cc[3] = NULL;
><     new_settings.c_cc[4] = NULL;
><     new_settings.c_cc[5] = TIMEOUT;
>
>
>For one thing, if you set ICANON, you will not get RAW input; do the
>following instead.  Also, if you want to get 1 character at a time
>without blocking, set the VMIN (element 4) of c_cc to 1, and set
>TIMEOUT to 0.
>
>new_settings.c_lflag &= ~ICANON;	/* Canonical input OFF. */
>new_settings.c_cc[VMIN] = 1;
>new_settings.c_cc[VTIME] = 0;
>
>Other indices for c_cc should be written using their mnemonics as defined
>in /usr/include/sys/termio.h for portability if nothing else.
>
>CAVIAT: the VMIN/VTIME mechanism is intended for reading DMA bursts, and
>does not really work for general timeouts.  What you probably need to
>use poll(2) or select(2) rather than ioctl(2) (if I understand your
>question).

While the above is along the right path it too will hang forever waiting
for one character.  If he wants it to time out at some point I believe
the following would work better.  Replace delay with the number of seconds
you want it to wait before timing out.

		tty.c_iflag	= 0;
		tty.c_oflag	= 0;
		tty.c_cflag	= B9600 | CS8 | CREAD;
		tty.c_lflag	= 0;
		tty.c_cc[VTIME]	= delay;
		tty.c_cc[VMIN]	= 0;
		if ( ioctl( descriptor, TCSETA, &tty ) < 0 ) perror("ioctl");

Bruno

With 999999 possiblities between "cooked" and "raw" you know you're not going
to have any fun.  Boo hoo.



More information about the Comp.sys.sgi mailing list