function [err,errmsg] = OsiConnect(resource,ConNo, ser_offs)
%
% OsiConnect.m				Connect MATLAB session to OLGA server.    
%
% The calling syntax is:
%
%   [err, errmsg] = OsiConnect(resource, ser_offs);
%
% Inputs:
%   RESOURCE: String identifying the computer where OLGA is running and the 
%             name of the service to be used for TCP/IP communication.
%   SER_OFFS: Service offset [optional input], an additional integer added to the 
%             sockett number, the default value is zero.  
%
% Example usage: 
%   [err, errmsg] = OsiConnect('NOCRC-WKJHA142 olga_1');
%   
%   NOCRC-WKJHA142 is the computer where the OLGA server runs and 
%   olga_1         is the name of the service used for communication.
%   The default value (0) on the service offset SER_OFFS is used in this example. 
% 
% 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 INITIALIZED
  if isempty(INITIALIZED)
      OsiIBODYsetInputVar = []; OsiIBODYsetProfileVar = []; OsiIBODYsetTrendVar = [];
      INITIALIZED=1;
  end


  if nargin < 1 
     errmsg = 'OsiConnect usage: [err, errmsg] = OsiConnect(resource,ConNo, ser_offs)';
     disp(errmsg);
     err = -1;
     return
  end

  if nargin == 1
     ser_offs = 0;
     ConNo=1;
  end
  if nargin==2
      ser_offs=0;
  end

  if nargin > 2
     [m,n] = size(ser_offs);
     if m*n ~= 1
        disp('OsiConnect: using ser_offs(1,1)');
     end
  end

  [err, msg, ibody, rbody, errmsg] = osmi(ConNo, 'OLGA_CONNECT', ser_offs(1,1), [], resource);

  