function [sys,x0,str,ts] = openOPC2(t,x,u,flag,server,host,rate,tm) %OPENOPC2 S-function for opening an OPC server %INPUTS: %1.SERVER: name of OPC Server %2.HOST: name of host: localhost or remotehost %3.RATE: rate in ms for notifications %4.TM: time for a loop (use for Sleep) %CLOSEOPC: s-FUNCTION NEEDED TO CLOSE THE LOOP %IN: -OPEN SERVER %OUT: -START SLEEP switch flag, case 0 [sys,x0,str,ts]=mdlInitializeSizes(server,host,rate,tm); case { 1, 2,4,9} sys=[]; case 3 sys=mdlOutputs(t,x,u,tm); otherwise error(['Unhandled flag = ',num2str(flag)]); break; end function [sys,x0,str,ts]=mdlInitializeSizes(server,host,rate,tm) sizes = simsizes; sizes.NumContStates = 0; sizes.NumDiscStates = 0; sizes.NumOutputs = 0; sizes.NumInputs = 0; sizes.DirFeedthrough = 0; sizes.NumSampleTimes = 1; sys = simsizes(sizes); x0 = []; ts = [-1 0]; str=[]; s=server; h=host; r=rate; mxOPC('Verbose',4); mxOPC('ReadMode','cache'); hr=mxOPC('open',s,h,r); if (hr~=0) hr=mxOPC('open',s,h,r); if (hr~=0) disp('Error connecting to OPC server'); break; end end function sys=mdlOutputs(t,x,u,tm) %tic; mxOPC('sleep',tm); sys = [];