;control_lakeshore
;
;PURPOSE:
;	Open a serial port and control the temperature of the Lakeshore heaters.
;	
;	Note the settings for the LakeShore are the following: 
;	(Press the Interface Button(6))
;
;	SERIAL
;		TERMINATOR:	 	LF
;		BPS:			9600
;		PARITY: 		7Odd1
;
;	If the terminator setting on the lake shore is adjusted to CR or CR LF
;	or LF CR, the corresponding ascii characters appended to each command
;	in this script must be adjusted as well.
;
;CALLING SEQUENCE
;   control_lakeshore
;
;KEYWORD PARAMATERS:
;
;MODIFICIATION HISTORY:
;	Written by:
;		Lance Simms, January 29, 2006

pro control_lakeshore

If (not keyword_set(outdir)) then outdir='./'

TempFile='C:\RSI\IDL63\bin\idl_scripts\Temperature.txt'

;Special ASCII characters
cr = string(13b)
esc = string(27b)
lf=string(10b)

;Close serial port if already open
resclose=comm_close(/all)

;Open the port with settings defined by keywords
h=comm_open('COM1',baud=9600,data=7,parity='O',mode=3,stop=1)
get_lun,Templun
openw,Templun,TempFile

Time=1
TimeFinal=10
while Time lt TimeFinal do begin
  ;Get Date/Time
  reswrite=comm_write(h,'DATETIME?'+lf)
  wait,.5
  resread=comm_read(h)
  if resread(0) eq -1 then break
  Datereadstring=string(resread)
  print,'Date and Time'+Datereadstring

  reswrite=comm_write(h,'KRDG?0'+lf)
  wait,.5
  resread=comm_read(h)
  if resread(0) eq -1 then break
  TempAreadstring=string(resread)
  print,'Temperature A='+TempAreadstring

  reswrite=comm_write(h,'KRDG?1'+lf)
  wait,.5
  resread=comm_read(h)
  if resread(0) eq -1 then break
  TempBReadString=string(resread)
  print,'Temperature B='+TempBreadstring

  printf,Templun,DateReadString,TempAReadString,TempBReadString,$
	   FORMAT='(%"%s\t%s\t%s")'

  Time=Time+1

endwhile

close,Templun
free_lun,Templun
stop

resclose=comm_close(/all)


end
