Why does my serial port code stop prematurely when I deploy it with the MATLAB Compiler?

3 Ansichten (letzte 30 Tage)
I have some serial port code which uses the callback functions from the serial port object to read data. It works fine in MATLAB, but if I deploy it as a standalone application it stops almost immediately.

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 22 Jan. 2010
This is due to how the deployed application determines when to stop executing. Since the callbacks run seperately from the rest of the MATLAB code, the function you wrote exits normally. The application then terminates the MCR and exits because the function stopped.
You can force the application to keep running by forcing the function to not return immediately. The following code demonstrates how this can be done using a TIMER object and the WAITFOR function:
% Create the timer
tmr = timer;
% Set the timer so that it repeately runs.
tmr.ExecutionMode = 'fixedSpacing';
% You need to set some function as a callback.
% This simply prints an empty string.
tmr.TimerFcn = @(varargin)(fprintf(''));
% You can't wait for a timer with infinite TasksToExecute, so set it really
% large instead.
tmr.TasksToExecute = 1e12;
% Add normal code here
% Wait ~forever (1e12 seconds)
% Stop the timer (must be used if you CTRL-C this script)
% These commands can be used to stop and clear up any stray timers

Weitere Antworten (0)


Mehr zu C Shared Library Integration finden Sie in Help Center und File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by