	SUBROUTINE READ( INBUF, NCHAR, ISTAT, TIMEOUT)
C  Programmer: Jim Walker, Aug 1987.
C  This is for a VAX listening to a RADIX DATABOX.
C  Reads characters the terminal on ICHAN into INBUF
C  and waits up to TIMEOUT seconds for a terminator.
C  NCHAR returns number of characters.
C  ISTAT returns QIO error code or -2 if line ended with return.
C  ISTAT = -1 for timeout error or -3 for data overrun
C  ISTAT = 1 if terminator was Null (interface ready for input).
C  Terminators for read will be Null and Return (^@ and ^M)
C
	IMPLICIT NONE
	CHARACTER*(*) INBUF
	INTEGER*4 SYS$QIOW, ISTAT, NCHAR, I, J, MASK(2), TIMEOUT
	DATA MASK/0,8193/	! Terminator mask.
	INCLUDE 'INTERFACE.COM'
	INCLUDE '($IODEF)'
	INCLUDE '($SSDEF)'
C
C  Read the data and wait for a Null or Return.
	ISTAT = SYS$QIOW( , %VAL(ICHAN),
	1	%VAL( IO$_READVBLK+IO$M_NOECHO+IO$M_TIMED ),
	2	IOSB, , ,
	3	%REF(INBUF), %VAL(LEN(INBUF)), %VAL(TIMEOUT), MASK, 
	4	,  )
C
C  If there was a "directive error" then signal it and bail out.
	IF(.NOT.ISTAT) THEN
		IF(DEBUG) CALL LIB$SIGNAL(%VAL(ISTAT))
		GOTO 99
	ENDIF
C
C  Now we only worry about I/O errors.
	IF(ISTAT) ISTAT=IOSB(1)
C
C  If there was an input error other than timeout then bail out.
	IF(IOSB(1).EQ.SS$_TIMEOUT) THEN
		ISTAT = -1
	ELSEIF(IOSB(1).EQ.SS$_DATAOVERUN) THEN
		ISTAT = -3
	ELSEIF(.NOT.IOSB(1)) THEN
		IF(DEBUG) CALL LIB$SIGNAL(%VAL(IOSB(1)))
		GOTO 99
C
C  If Return character was recieved than exit with ISTAT=-2.
	ELSEIF(IOSB(3).EQ.13) THEN
	   ISTAT = -2
	ENDIF
C
C  In debug mode print data recieved from interface.
	IF(DEBUG) THEN
	   IF(IOSB(1).EQ.SS$_TIMEOUT) TYPE*,' Timeout'
	   TYPE *,'"',(INBUF(J:J),J=1,IOSB(2)),'"'
14	   FORMAT(' ',72A1,'-')
	ENDIF
C
C All done here, go back to main program.
C
99    NCHAR=IOSB(2)
      IF(DEBUG) TYPE*,' READ ISTAT=',ISTAT,' NCHAR=',NCHAR
      RETURN
      END
