	SUBROUTINE DIAL_DF112
C
C	This routine dials the phone number for the DEC DF112.
C
	INCLUDE 'COM.INC/NOLIST'

	CHARACTER*(*) CONNECTION_NFG

	LOGICAL FSS, READ_DATA
	INTEGER*4 AUTO_RETRY, DIAL_TMO, I, SIZE, STATUS
	DATA DIAL_TMO /45/

	PARAMETER (CONNECTION_NFG = SS//
	1 '*** The connection was not established. ***'//BELL//SS)

	AUTO_RETRY = 0			! Initialize the retry count.
	CALL CLEAR_TYPEAHEAD()		! Clear the typeahead buffer.
C
C	We will use the DF112 burst mode when sending the phone number.
C	In burst mode, the phone number immediatly follows the CTRL/A
C	character.  The CTRL/A and the phone number and the initial
C	"READY" message is not displayed.  Unfortunatly, we don't know
C	if the dialer is actually ready or not.  If this is a problem,
C	the DF112 interactive mode can be used.
C
C	Prepare to send the phone number with following format:
C
C	Sequence 	Description
C	--------	-----------
C	   1		Start of header (SOH = CTRL/A).
C	   2		Telephone digits (0 - 9, P, T, or =).
C	   3		Start dialing character (#).
C
C	The user must specify 'T' preceeding the phone number to tone
C	dial.  The default is pulse (P) dialing.  Both pulse and tone
C	dialing may be specified in the same number string.
C
C	The equal sign (=) is used to wait for a second dial tone.
C
C	The maximum number string allowed by the DF112 is 26 characters.
C
	AUTO_RETRY = 0			! Reset the retry count.
	XBUFFER(1) = SOH		! Start of header (SOH = CTRL/A).
	SIZE = 2			! Starting position for number.
C
C	Loop through the number discarding invalid characters.  This
C	allows us to use additional characters to format the phone
C	number (i.e. 9 - (area_code) - 1 - phone_number).
C
	DO 300 I = 1,PHONE_SIZE
	    XBUFFER(SIZE) = ICHAR(PHONE_NUMBER(I:I))
	    IF ( ((XBUFFER(SIZE) .GE. '0') .AND.
	1		(XBUFFER(SIZE) .LE. '9')) .OR.
	1		(XBUFFER(SIZE) .EQ. 'P') .OR.
	1		(XBUFFER(SIZE) .EQ. 'T') .OR.
	1		(XBUFFER(SIZE) .EQ. '=') ) THEN
		SIZE = SIZE + 1		! Point to the next position.
	    ENDIF
300	CONTINUE
	XBUFFER(SIZE) = '#'		! Start dialing character (#).
C
C	Send the formatted phone number to the autodialer.
C
400	CALL WRITE_BYTE (XBUFFER, SIZE)	! Send it to the autodialer.
C
C	The response from the dialer will be one of the following:
C
C	[A]ttached
C		Indicates the remote end has answered and responded with
C		answerback tone.
C
C	[E]rror
C		Indicates that the modem is not ready (CTRL/A or CTRL/B
C		was not issued) or that an invalid entry was made in the
C		dialing string.  This response places the modem in the
C		idle state which requires a CTRL/A or CTRL/B to reactivate
C		the auto dialer.
C
C	[N]o Answer
C		Indicates the remote end has not answered the call within
C		the allotted time of 30 seconds.  This response places the
C		modem in an idle state which requires a CTRL/A or CTRL/B
C		to reactivate the auto dialer.
C
C	[N]o Dial Tone
C		Indicates a dial tone was not detected within the allotted
C		time of 15 seconds after initiating a call.  This response
C		places the modem in an idle state which requires a CTRL/A
C		or CTRL/B to reactivate the auto dialer.
C
C	[R]eady
C		Indicates that the auto dialer is ready to perform a
C		command.  This response is always used to indicate that
C		the dialer is ready with one exception:  when CTRL/A is
C		used, the inital READY response is omitted.
C
C	For long form response messages, switchpack S3, switch 3 must be
C	set to the OFF position.  For short form, set switch 3 ON.
C
C	This following should work with either long or short form messages.
C	All messages are preceeded by a carriage-return/line feed sequence.
C
	IF (READ_DATA (RBUFFER, .FALSE., RDESC, DIAL_TMO)) THEN
	    I = 1				! Character to check status.
	    IF (RBUFFER(1) .EQ. CR) THEN
		I = 3				! Presume CR/LF sequence.
	    ENDIF
	    IF (RBUFFER(I) .EQ. 'A') THEN	! [A]ttached = success.
		REMOTE = .TRUE.			! Show modem is connected.
		MODEM_ONLINE = .TRUE.		! Show the modem is online.
		RETURN				! We're all finished ...
	    ELSEIF (RBUFFER(I) .EQ. 'E') THEN	! [E]rror = failure.
		CALL WRITE_USER ('*** Not ready or invalid character')
	    ELSEIF (RBUFFER(I) .EQ. 'N') THEN	! [N]o xxx = failure.
		IF     (FSS ('ANSWER', RDESC)) THEN
			CALL WRITE_USER ('*** No Answer')
		ELSEIF (FSS ('DIAL TONE', RDESC)) THEN
			CALL WRITE_USER ('*** No Dial Tone')
		ELSE
			CALL WRITE_USER ('*** No Answer or Dial Tone')
		ENDIF
	    ELSEIF (RBUFFER(I) .EQ. 'R') THEN	! [R]eady = failure.
		CALL WRITE_USER ('*** Unexpected "READY" message')
	    ELSE
		CALL WRITE_USER ('*** Unexpected response "')
		CALL WRITE_USER (RDESC)		! Write the received text.
		CALL WRITE_USER ('"')
	    ENDIF
	ELSE
	    IF (CONTROLC_TYPED) THEN		! CTRL/C typed to abort.
		XBUFFER(1) = CAN		! Send any character to
		CALL WRITE_BYTE (XBUFFER, 1)	!   abort the auto dialer.
		CALL WRITE_USER (CONNECTION_NFG)! Tell user not connected.
		RETURN				! We're all done here.
	    ELSE
		CALL WRITE_USER ('*** No response')
	    ENDIF
	ENDIF
	CALL WRITE_USER (' received, retrying ...  ***'//SS)
C
C	Connection not established, retry if limit has not been exceeded.
C
	AUTO_RETRY = AUTO_RETRY + 1		! Bump the retry count.
	IF (AUTO_RETRY .EQ. AUTODIAL_LIMIT) THEN
	    CALL WRITE_USER (CONNECTION_NFG)	! Tell user not connected.
	ELSE
	    GO TO 400				! Send the number again ...
	ENDIF
	RETURN
	END
