function [sys,x0,str1,ts] = readOPC(t,x,u,flag,nitems,item) % READOPC S-function for READING from an OPC Server % through item= array that contains names of items % and created previously in Matlab % It is made by functions mxopc by IPCOS client % (Server defined by OPENOPC block) %Used with OPENOPC block %Inputs= Number of reading ITEMS and name of storage %IN- %Establish CACHE %OUT-%Read from CACHE global str1 switch flag, case 0 [sys,x0,str1,ts]=mdlInitializeSizes(nitems,item); case { 1, 2,4,9} sys=[]; case 3 sys=mdlOutputs(t,x,u,str1,nitems); otherwise error(['Unhandled flag = ',num2str(flag)]); break end function [sys,x0,str1,ts]=mdlInitializeSizes(nitems,item) sizes = simsizes; sizes.NumContStates = 0; sizes.NumDiscStates = 0; sizes.NumOutputs = nitems; sizes.NumInputs = 0; sizes.DirFeedthrough = 0; sizes.NumSampleTimes = 1; sys = simsizes(sizes); x0 = []; ts = [-1 0]; str1=item; for i=1:1:nitems ite=char(item(i)); hr=mxOPC('setdoublecache',ite,1,0); if hr==0 %sprintf('Cache OK para item %s',ite2) else hr=mxOPC('setdoublecache',ite,1,0); if hr==0 %sprintf('Cache OK para item %s',ite2) else sprintf('ERROR STABLISHING CACHE FOR item %s',ite) mxopc('close'); disp('FAILED COMUNNICATION WITH OPC SERVER'); break end end end function sys=mdlOutputs(t,x,u,str1,nitems) iteme=str1; h22=mxopc('readcache'); if h22==0 %disp('LECTURA COMPLETA DE CACHE OK') else h22=mxopc('readcache'); if h22==0 %disp('LECTURA COMPLETA DE CACHE OK') else disp('ERROR READING CACHE') mxopc('close'); disp('FAILED COMMUNICATION WITH OPC SERVER'); break end end for i=1:1:nitems ite=char(iteme(i)); [PV hr]=mxopc('readdouble',ite); if hr==0 %sprintf('LECTURA OK para item %s',ite2) else [PV,hr]=mxOPC('readdouble',ite); if hr==0 %sprintf('LECTURA OK para item %s',ite2) else sprintf('ERROR READING item %s',ite) mxopc('close'); disp('FAILED COMMUNICATION WITH OPC SERVER'); break end end P(i)=PV; end sys = P;