pro laser_write,buff,device,device=device1,resp=resp,rawresp=rawresp,nowait=nowait,noparse=noparse
;+
;  Name:
;    laser_write
;  Purpose:
;    send a command to the laser disk/player-- using C routine to get response.;
;  Calling Sequence:
;    laser_write, buff [,device=device, resp=resp]
;    laser_write, buff [,device, resp=resp]
;    laser_write, buff [,device]
;
;  Note:
;    the type of machine is determine by the response on the terminal
;    line.  the terminal line is expected to be 9600 baud for the PANASONIC,
;    and 1200 baud for the SONY; the SONY has problems losing characters
;    if they are sent too fast (might only be a problem with the
;    model 3000 series SONY).
;
;    this routine depends on the C routine 'call.c' written in 1/95
;    spawn with the /noshell option is used to run the C routine.
;    it keeps reading from the called C routine until a char=0 is
;    sent back.
;
;  Input:
;    buff   = String containing command, 'BP', 'EJ', string('60'xb)
;  Optional Input Keyword:
;    device = 0 or 1 for recorder or player (def=0)
;             (device can also just be a input parameter)
;    nowait = flag to tell C program NOT to wait for complete response from
;               the laser disk player.
;  Optional output keyword:
;    resp   = response from laser disk
;    rawresp = response from the laser disk (byte form) sometimes, ascii
;                               just won't do.
;    noparse = flag to make sure that we don't recurse forever in this
;		routine when we are parsing long commands.
;		the SONY has a tough time dealing with long commands.
;
;  Common Blocks:
;       LASER
;  Procedure:
;    sends the chars in 'buff' to the machine; also, if the machine is
;    the PANASONIC, it will attach the STX and ETX characters that are
;    required for the PANASONIC.
;
;  Modification History:
;       feb-1995 - written by L. Shing
;	oct-1995, LS , fixed a problem with /nowait
;-

COMMON LASER,device_str,device_type,call_path,SONY,PANASONIC,stx,etx

on_error,2			; Return to caller if there is a problem

r1=0
r2=0
if n_params() eq 0 then return

; initialize the common variables they aren't already defined
if (n_elements(device_type) eq 0) then laser_init

; Default to 0 (recorder) if undefined (parameter has precedence over keyword)
if (n_elements(device) eq 0) and (n_elements(device1) eq 0) then device = 0 $
else if (n_elements(device) eq 0) and (n_elements(device1) gt 0) then $
        device=device1


; recurse if buff is a vector of commands.
if (n_elements(buff) gt 1 ) then begin
	resp=''
	rawresp=bytarr(1)
        for indx=0,(n_elements(buff)-1) do begin
                        laser_write,buff(indx),device,resp=r1,rawresp=r2,nowait=nowait,noparse=noparse
			resp=[resp,r1]
			rawresp=[rawresp,r2]
			endfor
	resp=resp(1:*)
	rawresp=rawresp(1:*)
        return
endif


ENTER=string('40'xb)
cmd=''

; check to see if we can parse this command down for the SONY
; the ENTER char makes a good point to break things up.
if ((n_elements(noparse) ne 0) and (device_type(device) eq SONY) and (strpos(buff,ENTER) ne -1)) then if (noparse eq 2) then begin
	for a=0,strlen(buff)-1 do begin
	  p1=strpos(buff,ENTER,a)
	  if (p1 eq -1) then cmd=[cmd,strmid(buff,a,strlen(buff)-a)] $
		else begin
		     cmd=[cmd,strmid(buff,a,p1+1-a)]
		     a=p1
		     end
	endfor
	laser_write,cmd(1:*),device,resp=resp,rawresp=rawresp,/noparse,nowait=nowait
	return
	endif


  device_sel = device_str(device)

; construct the command array to spawn with /noshell
  cmd=call_path
  cmd=[cmd,device_sel]

; find out what machine (SONY or PANASONIC) we are talking to
  if (device_type(device) eq -1) then get_laser_type,device

; attach the STX and ETX chars if it's a PANASONIC
  if (device_type(device) eq PANASONIC) then begin
	buff = strupcase( buff )
	cmd = [cmd,stx+buff+etx]
      endif $
	else cmd = [cmd, buff]

chars=string(strlen(buff))
if (n_elements(nowait) ne 0) then cmd=[cmd,strtrim(chars,2)]

  spawn,cmd,/noshell,unit=unit


; keep reading from the spawned command until we get a char=0, then char=1
  r=bytarr(1)
  buf=r
  q=0
  repeat begin
 	readu,unit,r
	if (r(0) eq 0) then begin
		readu,unit,r
		if (r(0) eq 1) then q=1
		endif
 	if (q eq 0) then buf=[buf,r(0)]
    endrep until (q eq 1)

  free_lun,unit
  if (n_elements(nowait) ne 0) then return

  resp1=string(buf(1:*))


; get rid of white space if it's the PANASONIC
; don't do this for the SONY because every byte is important from the SONY
  if (device_type(device) eq PANASONIC) then begin
	resp = resp1(0)
  	for i=1,n_elements(resp1)-1 do resp = resp + resp1(i)
  	resp = strtrim(resp,2)
      endif $
	else resp=resp1

rawresp=buf(1:*)
return
end
