function [err,errmsg] = OsiDisconnect(ConNo)
%
% OsiDisconnect.m			Disconnect MATLAB session from OLGA server.    
%
% The calling syntax is:
%
%   [err, errmsg] = OsiDisconnect(ConNo);
% Input:
%   ConNo: Connnection number (default: ConNo=1)
% 
% Copyright (c) 1998 by ABB Corporate Research.
% All Rights Reserved.
%
% $Revision: 1.0, 1/10-1998 by Kjetil Havre, ABB-NOCRC$
% $Revision: 1.1, 20/7-2005 by Espen Storkaas, ABB-EOP, included user-defined connection number$ 


  global OsiIBODYsetInputVar OsiIBODYsetProfileVar OsiIBODYsetTrendVar
  
  if nargin==0
      ConNo=1;
  end
  if nargin >1 
     errmsg = 'OsiDisconnect usage: [err, errmsg] = OsiDisconnect(ConNo)';
     disp(errmsg);
     err = -1;
     return
  end
  
  [errping] = OsiPing(ConNo);
  if errping == 0
     [errstop, errmsgstop] = OsiStopServer(ConNo);
  end
  
  [err, msg, ibody, rbody, errmsg] = osmi(ConNo, 'OLGA_DISCONNECT', [], [], []);
  
  if err == 0
    clear OsiIBODYsetInputVar OsiIBODYsetProfileVar OsiIBODYsetTrendVar INITIALIZED;
  end 