
function [sys,x0,str2,ts] = writeOPCs(t,x,u,flag,nit,item2)
% WRITEOPCs S-function for WRITING SIMPLE to an OPC Server 
% through item2= array that contains names of items 
% and created previously in Matlab
% It writes only each time that inputs change
% It is made by functions mxopc by IPCOS client
%  (Server defined by OPENOPC block) 

%Used with OPENOPC block

%Inputs= Number of writing ITEMS and name of storage 
%IN-
	%Establish CACHE
%OUT-%Wrote to CACHE

global str2
switch flag;

case 0
   [sys,x0,str2,ts]=mdlInitializeSizes(nit,item2);
case { 1,4,9}
    sys=[];
case 3
   sys=mdlOutputs(t,x,u,str2,nit);   
case 2
    sys=mdlUpdate(t,x,u);
otherwise
    error(['Unhandled flag = ',num2str(flag)]);
end

function [sys,x0,str2,ts]=mdlInitializeSizes(nit,item2)
sizes = simsizes;
sizes.NumContStates  = 0;
sizes.NumDiscStates  = nit;
sizes.NumOutputs     = 0;
sizes.NumInputs      = -1;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;   
sys = simsizes(sizes);
x0  = zeros(nit,1);
ts  = [-1 0];
str2=item2;
%for i=1:1:nit
%   ite2=char(item2(i));
%   hr=mxOPC('setdoublecache',ite2,0,1); 
%   if hr==0
%   	sprintf('Cache OK para item %s',ite2)
%	else
%        hr=mxOPC('setdoublecache',ite2,0,1);
%		if hr==0
%   				sprintf('Cache OK para item %s',ite2)
%		else
%       	sprintf('ERROR ESTABLISHING CACHE FOR item %s',ite2)
%            mxopc('close');
%        disp('FAILED COMUNNICATION WITH OPC SERVER');
%        break
%		end
%    end
%end

function sys=mdlUpdate(t,x,u)
sys =u;

function sys=mdlOutputs(t,x,u,str2,nit)
iteme2=str2;
f=0;
if t==0
   for j=1:1:nit
      	ite2=char(iteme2(j));
  			h4(j)=mxOPC('writedouble',ite2,u(j));
   		if h4(j)==0
   			%sprintf('ESCRITURA OK para item %s',ite2)
		else
   			h4(j)=mxOPC('writedouble',ite2,u(j));
			if h4(j)==0
  				%sprintf('ESCRITURA OK para item %s',ite2)
           	else
                %sprintf('ERROR WRITING item %s',ite2)
            	mxopc('close');
                disp('FAILED COMMUNICATION WITH OPC SERVER');
                break
			end
   		end
   end
 %  h44=mxOPC('writecache');
 %  if h44==0
 %  	  disp('ESCRITURA COMPLETA EN CACHE OK')
 %  else
%      h44=mxopc('writecache');
 %     if h44==0
 %  		 disp('ESCRITURA COMPLETA EN CACHE OK')
 %     else
%         disp('ERROR WRITING CACHE')
 %        mxopc('close');
 %        disp('FAILED COMMUNICATION WITH OPC SERVER');
%		 break
%      end
%	end
end
for j=1:1:nit
      if x(j)~=u(j)
         f=1;
      end
end
if f==1
   for j=1:1:nit
      	ite2=char(iteme2(j));
  			h4(j)=mxOPC('writedouble',ite2,u(j));
   		if h4(j)==0
   			%sprintf('ESCRITURA OK para item %s',ite2)
			else
        			h4(j)=mxOPC('writedouble',ite2,u(j));
					if h4(j)==0
   					%sprintf('ESCRITURA OK para item %s',ite2)
            	else
               %sprintf('ERROR WRITING item %s',ite2)
            	mxopc('close');
               disp('FAILED COMMUNICATION WITH OPC SERVER');
               break
					end
    		end
 	end
 %	h44=mxOPC('writecache');
%	if h44==0
%   	disp('ESCRITURA COMPLETA EN CACHE OK')
%   else
%      h44=mxopc('writecache');
%      if h44==0
%   			disp('ESCRITURA COMPLETA EN CACHE OK')
%      else
%        	disp('ERROR WRITING CACHE')
%         mxopc('close');
%         disp('FAILED COMMUNICATION WITH OPC SERVER');
%			break
%     	end
%	end
end
sys = [];
