	SUBROUTINE NEXSAM( IPOS, LROT)
C  Jim Walker, Aug 1987 for Diffrac-500 with a Radix Databox.
C  Advance the automatic sample changer.
C
	INCLUDE '[102,2]INP.COM/NOLIST'	
	INCLUDE '[102,2]ERRBLK.COM/NOLIST'	
	INCLUDE '[102,2]RAWDES.COM/NOLIST'	
	INCLUDE 'INTERFACE.COM'
	INTEGER*4 ISTAT, ITIME, I, NCHAR, IRETRY
	INTEGER*2 IPOS
	LOGICAL LROT
	PARAMETER CR=CHAR(13)
	CHARACTER BUFFER*255
C
	IERR(5) = 0
	ISTAT = 0
C
C  If sample is supposed to be in first position, don't bother
C  to advance.
	IF(IPOS.EQ.1) GOTO 9999
C
C  Move the goniometer so the sample is level.
	CALL GOSTAR( 20.0 )
	IF(IERR(5).NE.0) GOTO 9000
C
C  Send command to interface to get into Option mode and Advance.
	CALL NULL('O'//CR//'A', 3, ISTAT)
	IF(ISTAT.NE.1) GOTO 9000
C
C  Send the carriage return to start it off.
	CALL ECHO(CR, 1, ISTAT)
	IF(ISTAT.NE.1) GOTO 9000
C
C  Wait for completion
	ITIME = 60
	IRETRY = 0
10	   IRETRY = IRETRY + 1
	   IF(IRETRY.GT.5) GOTO 9000
	   CALL READ(BUFFER, NCHAR, ISTAT, ITIME)
	   IF(ISTAT.EQ.-2) GOTO 10
	IF(ISTAT.NE.1) GOTO 9000
C
C  Get back to monitor mode
	CALL NULL('E'//CHAR(13), 2, ISTAT)
	IF(ISTAT.NE.1) GOTO 9000
	GOTO 9999
C
C  Error return
9000	IERR(5)=138	! interface error
	IERR(4)=ISTAT
C
9999	RETURN
	END
