Simulink freezes after long simulation

6 views (last 30 days)
Tim  Van Winckel
Tim Van Winckel on 19 Sep 2016
Commented: NN on 20 Nov 2019
Hi all,
I am building a model in which I used level 2 matlab S function to create my own TCP/IP block. The model suppose to taking input value through TCP/IP protocol then feed them into a PID block to control to a pump. I am able to run the whole model, however, after 1 - 10 hours of simulation Simulink freezes so I had to use Ctrl + C to terminate the simulation and I got this error "Error evaluating registered method 'Outputs' of MATLAB S-Function 'Read_Analog_IO_1' in 'AVN_COD_control/Analog_IO_1/Level-2 MATLAB S-Function'. Program interruption (Ctrl-C) has been detected." And the time of freezing varies for each simulation.
Any suggestion to solve this problem will be highly appreciated.
Below is how my level 2 matlab S-function code look like:
function Read_Analog_IO_1(block)
setup(block);
function setup(block)
% Register number of ports
block.NumInputPorts = 1;
block.NumOutputPorts = 4;
% Setup port properties to be inherited or dynamic
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
block.RegBlockMethod('SetInputPortSamplingMode',@SetInputPortSamplingMode);
block.RegBlockMethod('SetInputPortDimensions', @SetInpPortDims);
block.RegBlockMethod('Outputs', @Output);
% Override input port properties
block.InputPort(1).Dimensions = 1;
block.InputPort(1).DatatypeID = 0; % double
block.InputPort(1).Complexity = 'Real';
block.InputPort(1).DirectFeedthrough = true;
% Override output port properties
block.OutputPort(1).Dimensions = 1;
block.OutputPort(1).DatatypeID = 0; % double
block.OutputPort(1).Complexity = 'Real';
block.OutputPort(1).SamplingMode = 'Sample';
block.OutputPort(2).Dimensions = 1;
block.OutputPort(2).DatatypeID = 0; % double
block.OutputPort(2).Complexity = 'Real';
block.OutputPort(2).SamplingMode = 'Sample';
block.OutputPort(3).Dimensions = 1;
block.OutputPort(3).DatatypeID = 0; % double
block.OutputPort(3).Complexity = 'Real';
block.OutputPort(3).SamplingMode = 'Sample';
block.OutputPort(4).Dimensions = 1;
block.OutputPort(4).DatatypeID = 0; % double
block.OutputPort(4).Complexity = 'Real';
block.OutputPort(4).SamplingMode = 'Sample';
% Register parameters
block.NumDialogPrms = 0;
% Register sample times
% [0 offset] : Continuous sample time
% [positive_num offset] : Discrete sample time
%
% [-1, 0] : Inherited sample time
% [-2, 0] : Variable sample time
block.SampleTimes = [1 0];
block.SimStateCompliance = 'DefaultSimState';
block.RegBlockMethod('PostPropagationSetup', @DoPostPropSetup);
block.RegBlockMethod('InitializeConditions', @InitializeConditions);
block.RegBlockMethod('Start', @Start);
block.RegBlockMethod('Outputs', @Outputs); % Required
block.RegBlockMethod('Update', @Update);
block.RegBlockMethod('Derivatives', @Derivatives);
block.RegBlockMethod('Terminate', @Terminate); % Required
function SetInputPortSamplingMode(block, idx, fd)
block.InputPort(idx).SamplingMode = fd;
block.OutputPort(1).SamplingMode = fd;
block.OutputPort(2).SamplingMode = fd;
block.OutputPort(3).SamplingMode = fd;
block.OutputPort(4).SamplingMode = fd;
function SetInpPortDims(block, idx, di)
block.InputPort(idx).Dimensions = di;
block.OutputPort(1).Dimensions = di;
block.OutputPort(2).Dimensions = di;
block.OutputPort(3).Dimensions = di;
block.OutputPort(4).Dimensions = di;
function DoPostPropSetup(block)
block.NumDworks = 1;
block.Dwork(1).Name = 'x1';
block.Dwork(1).Dimensions = 1;
block.Dwork(1).DatatypeID = 0; % double
block.Dwork(1).Complexity = 'Real'; % real
block.Dwork(1).UsedAsDiscState = true;
function InitializeConditions(block)
function Start(block)
block.Dwork(1).Data = 0;
function Outputs(block)
IPADDR = '128.1.1.101';
tcpip_pipe1=tcpip(IPADDR, block.InputPort(1).Data);
set(tcpip_pipe1, 'InputBufferSize', 4000);
tcpip_pipe1.ByteOrder='bigEndian';
fopen(tcpip_pipe1);
while ~strcmp(tcpip_pipe1.Status,'open'),end
message = [...
%*** TRANSACTION ID ***%
uint8(0); uint8(3); ... % Two byte transaction ID
%*** PROTOCOL ***%
uint8(0); uint8(0); ... % Two byte protocol ID - all zeros means Modbus TCP
%*** BYTES REMAINING ***%
uint8(0); uint8(6); ... % Two byte number of bytes for everything after this
%*** SLAVE ID ***%
uint8(255); ... % Slave ID - use if end device is after a modbus tcp/rtu router, otherwise use 255
%*** FUNCTION ID ***%
uint8(4); ... % 4 - read input registers
%*** DATA ***%
%***** Starting Register *****%
uint8(0); uint8(6); ... % Two byte number that gives the starting register to read
%***** Number of Registers to Read *****%
uint8(0); uint8(12)]; % Two byte number that gives how many registers to read
fwrite(tcpip_pipe1, message,'int8');
while ~tcpip_pipe1.BytesAvailable,end
response = fread(tcpip_pipe1,tcpip_pipe1.BytesAvailable);
block.OutputPort(1).Data = (response(10)*256+response(11))/1000;
block.OutputPort(2).Data = (response(12)*256+response(13))/1000;
block.OutputPort(3).Data = (response(14)*256+response(15))/1000;
block.OutputPort(4).Data = (response(16)*256+response(17))/1000;
fclose(tcpip_pipe1);
delete(tcpip_pipe1);
clear;
%end Outputs
function Update(block)
block.Dwork(1).Data = block.OutputPort(1).Data;
function Derivatives(block)
%end Derivatives
function Terminate(block)
%end Terminate
  2 Comments
Jordan Ross
Jordan Ross on 22 Sep 2016
Hi Tim,
When you do terminate the simulation does it always get the following error:
"Error evaluating registered method 'Outputs' of MATLAB S-Function 'Read_Analog_IO_1' in 'AVN_COD_control/Analog_IO_1/Level-2 MATLAB S-Function'"
If so, you might have a corner case in which your while loops are becoming infinite loops.
NN
NN on 20 Nov 2019
i am also facing similar issue while running simulink file of power system.How can i know which loop is creating issue and how can i correct such an infinite loop if there is any ?please advice.

Sign in to comment.

Answers (0)

Categories

Find more on Simulink Functions in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!