function stopasync(obj)
%STOPASYNC Stop asynchronous read and write.
%
%    STOPASYNC(OBJ) stops the asynchronous read and write operation that
%    is in progress with the device connected to serial port object, OBJ.
%    OBJ can be an array of serial port objects.  
%
%    Data can be written asynchronously with the FPRINTF or FWRITE functions.
%    Data can be read asynchronously with the READASYNC function and by 
%    configuring the ReadAsyncMode property to continuous. In-progress
%    asynchronous operations are indicated by the TransferStatus property.
%
%    After the in-progress asynchronous operations are stopped, the 
%    TransferStatus property is configured to idle, the output buffer
%    is flushed and the ReadAsyncMode property is configured to manual.
%
%    Data in the input buffer is not flushed. This data can be returned to
%    the MATLAB workspace using any of the synchronous read functions, for
%    example, FREAD or FSCANF. 
%
%    If OBJ is an array of serial port objects and one of the objects cannot
%    be stopped, the remaining objects in the array will be stopped and a 
%    warning will be displayed.
%
%    See also SERIAL/READASYNC, SERIAL/FREAD, SERIAL/FSCANF, SERIAL/FGETL,
%    SERIAL/FGETS.
%

%    MP 7-13-99
%    Copyright 1999-2000 The MathWorks, Inc. 
%    $Revision: 1.4 $  $Date: 2000/08/29 23:48:46 $

% Initialize variables.
errorOccurred = 0;
jobject = igetfield(obj, 'jobject');

% Call stopasync on each java object.  Keep looping even 
% if one of the objects could not be stopped.  
for i=1:length(jobject)
   try
      stopasync(jobject(i));
   catch
   	  errorOccurred = 1;	    
   end   
end   

% Report error if one occurred.
if errorOccurred
    if length(jobject) == 1
   		error(lasterr);
    else
        warnState = warning;
        if ~strcmp(warnState, 'off')
            warning('on');
        end
        warning('An object in OBJ could not be stopped.');
        warning(warnState);
    end
end
