function s=cportgetline(H,nMax,EOL,Timeout);
%function s=cportgetline(H,nMax,EOL,Timeout) reads in one line of character 
%data from a previously opened serial or parallel port. The parameters are:
%
%H       - The port handle returned by cportopen.
%nMax    - The maximal line length, including EOL. Default is -1 = unlimited.
%EOL     - End-of-line string, in sprintf format. Default is '\r\n'.
%Timeout - Timeout for reading, in seconds. Default is -1 = unlimited.
%
%s       - The output character array.

% Written by Eyal Doron, July 1999

if nargin<4, Timeout=-1; end
if nargin<3, EOL=sprintf'\r\n'; end
if nargin<2, nMax=-1; end

EOL=sprintf(EOL);

s='';
cportconfig(H,'ReadIntervalTimeout',-1,'ReadTotalTimeoutMultiplier',0, ...
              'ReadTotalTimeoutConstant',0);
if H==0
   error('CPORTGETLINE invalid port handle');
end

t0=clock; IsEOL=0; stat=1;
while ((Timeout<0) | (etime(clock,t0)<Timeout)) & ...
      ((nMax<0) | (length(s)<nMax)) & ~IsEOL & stat
   [ch,stat]=cportgetchar(H,1);
   if (stat==1) & ~isempty(ch)
      s=[s,ch]; 
      if ch==EOL(length(EOL))   % check for EOL condition
         SEnd=s(length(s)-(length(EOL):-1:1)+1);
         if strcmp(SEnd,EOL)==1, IsEOL=1; end
      end
   end
end


