	SUBROUTINE DEVATA
C  Open a channel and initialize the interface.
C  Jim Walker, Aug 1987 with Sept 1987 changes.
C  Look for SETUP to find the lines to change
C  to fix interface parameters.
C
	INCLUDE '[102,2]INP.COM/NOLIST'	
	INCLUDE '[102,2]ERRBLK.COM/NOLIST'	! IERR
	INCLUDE '[102,2]RAWDES.COM/NOLIST'	! SAMPLE ID
	INCLUDE '[102,2]XRYDES.COM/NOLIST'	! DIFTE1
	INCLUDE 'INTERFACE.COM'
	COMMON /RADIXC/ INBUF
	CHARACTER*255 INBUF
	INTEGER*4 SYS$ASSIGN, ISTAT, I, J, NCHAR, IRETRY, ITIME
	CHARACTER*255 BUFFER
	PARAMETER CR=CHAR(13)
C
C  Debug mode on or off.  Normally debug mode is off.  Turn it on
C  by saying RADIX$DEBUG:==YES on the $ prompt before getting into D500.
C  Turn it off again by saying RADIX$DEBUG:==NO
	DEBUG = .FALSE.
	CALL LIB$GET_SYMBOL('RADIX$DEBUG',BUFFER,,)
	IF(BUFFER(1:1).EQ.'Y') 	DEBUG=.TRUE.
C
C  Display first 10 points.
	IDSPLY = 10
C
	IERR(1) = 0
	IERR(2) = 0
	IERR(3) = 0
	IERR(4) = 0
	IERR(5) = 0
C
C  Assign a channel to the device.
	IF(ICHAN.EQ.0) THEN
		ISTAT=SYS$ASSIGN(DIFTE1,ICHAN,,)
		IF(.NOT.ISTAT) THEN
			IERR(5)=353
			IF(DEBUG) CALL LIB$SIGNAL(%VAL(ISTAT))
			GOTO 9001
		ENDIF
		IF(DEBUG) TYPE*,'ICHAN=', ICHAN
	ENDIF
C
C  FIND OUT WHAT STATE THE INTERFACE IS IN
C
	IRETRY=0
	GOTO 110
C
C  This bit tries again up to ten times.
100	IRETRY=IRETRY+1
	IF(IRETRY.GT.10) GOTO 9998
	IF(DEBUG) TYPE*,' TRYING TO GET INTERFACE TO SHUT UP ',IRETRY
C
C  First send an X-ON and carriage return to try and wake it up.
	CALL ECHO(CHAR(17), 1, ISTAT)
	CALL ECHO(CHAR(13), 1, ISTAT)
C
C  Read noise until it times out, or the interface settles down
C  to printing its counts per second.
105	CALL READ(BUFFER, NCHAR, ISTAT, 2)
C  Counts per second display has backspace characters in it.
	IF(INDEX(BUFFER(1:NCHAR),CHAR(8)).NE.0) THEN
		IF(DEBUG) TYPE*,' FOUND A BACKSPACE CHARACTER '
		GOTO 110
	ENDIF
C  If we ended with carriage return go get some more.
	IF(ISTAT.GT.1 .OR. ISTAT.EQ.-2 .OR. ISTAT.EQ.-3) GOTO 105
C
C  Ask for Help.
110	CALL NULL('?', 1, ISTAT)
C
C  Find the beginning of the text.
	DO 200, I=1,IOSB(2)
		IF(INBUF(I:I).GE.'A' .AND. INBUF(I:I).LE.'Z') GOTO 210
200	CONTINUE
C
C Nothing in INBUF, try again.
	IF(DEBUG) TYPE*, 'INBUF=[',INBUF(1:IOSB(2)),']'
	GOTO 100
C
210	CONTINUE
C
C  Based on where we are, get into calibrate mode.
	IF(INBUF(I:I+4).EQ.'CALIB') GOTO 250 ! already in calibrate mode
	IF(INBUF(I:I+6).EQ.'MONITOR') GOTO 220
C  Valid memory needs to be cleared then we are in calibrate mode.
	IF(INBUF(I:I).EQ.'V') THEN
		IF(DEBUG) TYPE*,' CLEARING MEMORY ON POWERUP'
		CALL ECHO('C',1,ISTAT)
		GOTO 100
C  Edit or Option mode.
	ELSEIF(INBUF(I:I+3).EQ.'EDIT' .OR. INBUF(I:I+5).EQ.'OPTION') THEN
		CALL NULL('E'//cr, 2, ISTAT)
	ELSEIF(INBUF(I:I+8).EQ.'EXECUTION') THEN
		CALL NULL('T'//cr, 2, ISTAT)
	ELSE
		IF(DEBUG) TYPE*,'UNRECOGNIZED RESPONSE ',INBUF(I:I+8)
		GOTO 100
	ENDIF
C  Get into calibrate mode.
220	CALL NULL('C'//cr, 2, ISTAT)
C
C  SET UP THE INTERFACE
C
C  Set up the standard things.  
C
250	WRITE(BUFFER,254) 13,13,13,13,13,13,13,9,13,13
C
C  CHANGE THESE LINES TO CHANGE THE SETUPS ON THE INTERFACE.
C
254	FORMAT('NE',a1,	! Enable null prompts to computer
	1 'C0',a1,	! First turn off continuous c.p.s. report
	6 'S200',a1,	! number of steps per degree.
	2 'B0.5',a1,	! Backlash angle (after steps / degree)
	3 'D3',a1,	! Display 3 decimal places
	4 'F960',a1,	! Factor time= 960 hz
	5 'LT',a1,	! Count for constant time
	6 a1,'300',a1	! integrate speed = 300 hz
	7 'M700',a1,'~')	! Motor frequency (Hz)
C
	I = INDEX(BUFFER,'~')-1
	CALL NULL(BUFFER, I, ISTAT)
	IF(ISTAT.GT.1) GOTO 9000
C
C  INITIALIZE THE CURRENT ANGLE.
C
C  Get the default angle.
	INBUF=' '
	CALL NULL('A'//CR, 2, ISTAT)
	IF(ISTAT.NE.1) GOTO 9000
C  Set up the question.
	J=INDEX(INBUF,')')
	ITIME = -1
	IF(J.EQ.0) THEN
		INBUF(8:)='  (respond within one minute)'
		J=INDEX(INBUF,')')
		ITIME = 60
	ENDIF
	DO 96,NCHAR=LEN(RAWFIL),1,-1
	  IF(RAWFIL(NCHAR:NCHAR).NE.' ') GOTO 98
96	CONTINUE
	NCHAR=1
98	WRITE(BUFFER, 97) 13,10,7,RAWFIL(1:NCHAR),DNUM,DVER,
	1	13,10, DIFDES, 13,10, INBUF(1:J)
97	FORMAT(3A1,'Starting run ',A,' on unit ',I2,3A1, A,2A1
	1 ' Enter current 2-theta [D: ',A,'] : ')
C  Ask for the new current angle
	CALL ASKTT( BUFFER(1:3+INDEX(BUFFER,']')), INBUF, NCHAR,
	1	ITIME, ISTAT, 'TT:')
C
C  Can't display counts if TT: doesn't exist.
C  Do it anyway; DPMSTA will open FOR$TYPE for us.
CC	IF(ISTAT.EQ.-4) IDSPLY = 0
C  Timeout or Nosuchdev is acceptable, don't set angle.
	IF(ISTAT.EQ.-1 .or. ISTAT.EQ.-4) GOTO 900
C  Any other error us inacceptable.
	IF(ISTAT.NE.1) GOTO 9998
C
C  Add the command to whats in the buffer.
	BUFFER(1:1)='A'
	BUFFER(2:NCHAR+1) = INBUF(1:NCHAR)
	BUFFER(NCHAR+2:NCHAR+2)=CR
C
C  Send the command
	CALL NULL(BUFFER, NCHAR+2, ISTAT)
	IF(ISTAT.NE.1) GOTO 9998
C
C  Leave interface in Monitor Mode
900	CALL NULL('E'//CR,2,ISTAT)
	IF(ISTAT.NE.1) GOTO 9000
	GOTO 9999
C
9998	IERR(5) = 118	! Error in angle syncronization.
	GOTO 9001
C
9000	IERR(5)=139	! error attaching daco
9001	IERR(4)=ISTAT
	IF(IERR(5).NE.0) CALL ERRMSG('DEVATA',RAWFIL,1,BUFFER)
C
9999	RETURN
	END
