Code covered by the BSD License  

Highlights from
cport

  • cportclose; function stat=cportclose(H) closes an open serial or parallel port
  • cportconfig; function stat=cportconfig(H,Name,Value,Name,Value,Name,Value...)
  • cportgetchar; function Params[ch,err]=cportstate(H,nchar) retrieves characters from a
  • cportopen; function H=cportopen(Port) opens a serial or parallel communication port
  • cportpurge; function stat=cportpurge(H) discards all characters from the output or
  • cportreset; function [stat,ErrStr]=cportreset(H) resets a previously opened communications
  • cportstate; function Params=cportstate(H) retrieves the configuration and timeout
  • cportwrite; function nW=cportwrite(H,Data,EOL) sends data to a previously opened
  • s=cportgetline(H,nMax,EOL,Tim... function s=cportgetline(H,nMax,EOL,Timeout) reads in one line of character
  • Contents.m
  • View all files
from cport by Eyal Doron
The CPORT minitoolbox supplies basic functions to open, configure, read from,etc to ports.

s=cportgetline(H,nMax,EOL,Timeout);
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 rc==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


Contact us at files@mathworks.com