Why does my serial port code stop prematurely when I deploy it with the MATLAB Compiler?
1 view (last 30 days)
Show older comments
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.
Accepted Answer
MathWorks Support Team
on 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;
start(tmr)
% Add normal code here
% Wait ~forever (1e12 seconds)
waitfor(tmr);
% Stop the timer (must be used if you CTRL-C this script)
stop(tmr)
% These commands can be used to stop and clear up any stray timers
stop(timerfindall)
delete(timerfindall)
0 Comments
More Answers (0)
See Also
Categories
Find more on Startup and Shutdown 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!