[comp.unix.xenix] getc help needed

tmm33@leah.Albany.Edu (Terry McCoy) (09/27/89)

Does anyone known what would be needed to create a function similar to getc()
that would return control to the calling function if the buffer was empty.

My application running SCO XENIX 2.3 has opened a serial port and monitors it
for incoming data.  The data is coming as packets.  I have tried using the
system functions getc, fread and read but they all wait for data to arrive
before returning control back to the calling function.

I have looked at the getc function in the stdio.h header file and noticed that
it calls a function _filbuf.  I think that this function is located within the
kernal and controls the pointers, flags and the counter within the structure
FILE.  Any information on how this function works or how I could tell if data
has arrived at the serial port without waiting would be helpful.


Terry McCoy
SUNY Albany National Lightning Detection Network
Research Foundation  -  State University of New York at Albany
------------------------------------------------------------------------------
internet:    tmm33@leah.albany.edu
Phone:       (518) 442-4588
Snail Mail:  Terry McCoy ES-235
             SUNY Albany
             1400 Washington Ave.
             Albany, NY 12222

davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) (09/28/89)

The usual way to do this is to use the ioctl which sets the read
non-blocking. This will return a byte count of zero if there is no data.
This is portable and more likely to continue working than playing around
with things inside macros.

-- 
bill davidsen	(davidsen@crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
"The world is filled with fools. They blindly follow their so-called
'reason' in the face of the church and common sense. Any fool can see
that the world is flat!" - anon

cpcahil@virtech.UUCP (Conor P. Cahill) (09/28/89)

In article <2056@leah.Albany.Edu>, tmm33@leah.Albany.Edu (Terry McCoy) writes:
> I have looked at the getc function in the stdio.h header file and noticed that
> it calls a function _filbuf.  I think that this function is located within the
> kernal and controls the pointers, flags and the counter within the structure

Nope.  _filbuf is a stdio library function that eventually uses read(2) to 
fill the buffer.

> FILE.  Any information on how this function works or how I could tell if data
> has arrived at the serial port without waiting would be helpful.

Since you are reading data off of the serial port you should use the 
following type of code:

	#include <fcntl.h>

	fd = open(TTY_PORT,O_RDWR+O_NDELAY....)

	if( (cnt=read(fd,buffer,512)) > 0 )
		/* process the data */
	else if( cnt == -1 )
		/* handle read failure */
	/* else cnt = 0 (no data available) */

-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+

les@chinet.chi.il.us (Leslie Mikesell) (09/28/89)

In article <2056@leah.Albany.Edu> tmm33@leah.albany.edu.UUCP (Terry McCoy) writes:
>Does anyone known what would be needed to create a function similar to getc()
>that would return control to the calling function if the buffer was empty.

You can set up a signal handler for SIGALRM and use alarm() to interrupt
the read().
Or, use fcntl() to set O_NDELAY so read() will return if no characters
are available. (Don't forget to turn it off before any write()'s).
Or, use ioctl() and set the c_cc[VMIN] and c_cc[VTIME] fields in the
termio struct to zeros and turn off ICANON. (See termio(7)).

Depending on the application, it may also be possible to do the input
in a separate process which can then block without problems.

Les Mikesell

SATAM@ecs.umass.edu (10/01/89)

Read without wait.
 you can try read with NO_DELAY flag. the exact constant
is given in <fcntl.h>.
The read call will return -1 if there is no character in the
buffer. Also take care to open the port in raw mode. Otherwise,
the mysterious four character buffering will be a headache.

- K G Satam 
email "satam@ecs.umass.edu"