;+
; NAME:
;     fst _dsread
; PURPOSE:
;     To read data from a LabVIEW data socket
; CATEGORY:
;     OVRO FST DATA ANALYSIS
; CALLING SEQUENCE:
;     data=fst_dsread(source,timeout=timeout,error=error,path =path,noerase=noerase)
; INPUTS:
;
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
;     source- datasocket URL
;             if not provided it is set to the default 'dstp://localhost/lv2idl'
;     timeout- ms to wait for the operation to be completed (default 1000ms)
;     path- the complete path of the DS.DLL external library, if not local
;     error- set this keyword to retrive an error code:
;                0 -  no error,
;                56-  The network operation exceeded
;                     the user-specified or system time limit.
;                63-  The network connection was refused by the server.
;
; ROUTINES CALLED:
;     fst_call_dsread
; OUTPUTS:
;  return value: existent data or a null pointer if none.
; COMMENTS:
;
; SIDE EFFECTS:
;     By default, the data read is deleted from the data socket. Use /noerase to avoid this.
; RESTRICTIONS:
;     some LabVIEW data types might not be yet supported
; MODIFICATION HISTORY:
;     Written March 31-2005 by Gelu Nita (gnita@njit.edu)

function fst_dsread,source,timeout=timeout,error=error,path =path,noerase=noerase
 error=fst_call_dsread(source=source,bytestr=b,timeout=timeout,path=path)
 if error ne 0 then return,ptr_new()
 if total(b) ne 0 then begin
	openw,lun,'temp', /GET_LUN
	writeu,lun,b
	free_lun,lun
	length=0l
	openr,lun,'temp', /GET_LUN,/swap_if_little
	readu,lun,length
	free_lun,lun
	b=bytarr(length)
	error=fst_call_dsread(source=source,bytestr=b,timeout=timeout,path=path)
	if error ne 0 then return,ptr_new()
	openw,lun,'temp', /GET_LUN
	writeu,lun,b
	free_lun,lun
	openr,lun,'temp', /GET_LUN,/swap_if_little
	data=fst_variant(lun,/free)
 endif else return,ptr_new()
 if not keyword_set(noerase) then begin
	  error=fst_call_dswrite(target=source,timeout=timeout,path=path)
 end
 return,data
end