How do I utilize a timer in the App Designer to capture serial data?

6 visualizzazioni (ultimi 30 giorni)
Hey all,
I'm trying to capture serial data from an app programmed in the app designer but am having trouble with the timer object. I have a snippet of code below as a simple example that has an text box for displaying an increment value and the timer status.
I have a callback function which executes to increment the value of the text box each 0.1 s. But when I run the program, or click the button to start the timer, the timer object does not start. Can anyone help me out?
classdef App4 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
EditFieldLabel matlab.ui.control.Label
EditField matlab.ui.control.NumericEditField
EditField2Label matlab.ui.control.Label
EditField2 matlab.ui.control.EditField
Button matlab.ui.control.Button
end
properties (Access = private)
tickr = 69; % Description
end
properties (Access = public)
timr;
end
methods (Access = private)
end
methods (Access = public)
function incrimentTick(app)
app.EditField.Value = app.tickr;
end
function incrimentTick2(app)
app.tickr = app.tickr + 1;
app.EditField.Value = app.tickr;
end
end
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.timr = timer('StartDelay', 0, 'Period', 0.5,'ExecutionMode', 'fixedRate');
app.timr.TimerFcn = @(obj,event)incrementTick(app);
start(app.timr);
start(app.timr);
if (strcmp(app.timr.Running,'on') == 1)
app.EditField2.Value = 'on';
end
if (strcmp(app.timr.Running,'off') == 1)
app.EditField2.Value = 'off';
end
end
% Button pushed function: Button
function ButtonPushed(app, event)
start(app.timr);
if (strcmp(app.timr.Running,'on') == 1)
app.EditField2.Value = 'on';
end
if (strcmp(app.timr.Running,'off') == 1)
app.EditField2.Value = 'retry';
end
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'UI Figure';
setAutoResize(app, app.UIFigure, true)
% Create EditFieldLabel
app.EditFieldLabel = uilabel(app.UIFigure);
app.EditFieldLabel.HorizontalAlignment = 'right';
app.EditFieldLabel.Position = [231.03125 233 56 15];
app.EditFieldLabel.Text = 'Edit Field';
% Create EditField
app.EditField = uieditfield(app.UIFigure, 'numeric');
app.EditField.Position = [302.03125 229 100 22];
% Create EditField2Label
app.EditField2Label = uilabel(app.UIFigure);
app.EditField2Label.HorizontalAlignment = 'right';
app.EditField2Label.Position = [228.703125 159 62 15];
app.EditField2Label.Text = 'Edit Field2';
% Create EditField2
app.EditField2 = uieditfield(app.UIFigure, 'text');
app.EditField2.Position = [305.703125 155 100 22];
% Create Button
app.Button = uibutton(app.UIFigure, 'push');
app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
app.Button.Position = [443 155 100 22];
end
end
methods (Access = public)
% Construct app
function app = App4()
% Create and configure components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end

Risposte (1)

prabhat kumar sharma
prabhat kumar sharma il 3 Apr 2024
Hi Max,
I understand you are facing issues with your app in visualizing the counter with the timer.
Here are the following suggestions I found in your code snippet.
  1. Make sure that the EditField is a Edit Field(Numeric) Component, while EditField2 is a Edit Field (text) Component.
  2. Typo in Timer Function Call: You have app.timr.TimerFcn = @(obj,event)incrementTick(app); which should be app.timr.TimerFcn = @(obj,event)incrimentTick2(app); based on your method definitions.
  3. Timer Period Mismatch: You mentioned wanting to increment every 0.1 seconds, but your timer is set to a period of 0.5 seconds. You should adjust the 'Period' property of the timer to 0.1.
  4. Redundant Timer Start Calls: In your startupFcn, you're calling start(app.timr); twice consecutively, which is unnecessary. A single call is sufficient to start the timer.
Here I am attaching the code snippet
classdef App4 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
EditFieldLabel matlab.ui.control.Label
EditField matlab.ui.control.NumericEditField
EditField2Label matlab.ui.control.Label
EditField2 matlab.ui.control.EditField
Button matlab.ui.control.Button
end
properties (Access = private)
tickr = 69; % Initial value for incrementing
end
properties (Access = public)
timr; % Timer object
end
methods (Access = public)
function incrimentTick2(app)
app.tickr = app.tickr + 1;
app.EditField.Value = app.tickr;
end
end
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.timr = timer('StartDelay', 0, 'Period', 0.1, 'ExecutionMode', 'fixedRate');
app.timr.TimerFcn = @(obj, event)incrimentTick2(app);
start(app.timr);
end
% Button pushed function: Button
function ButtonPushed(app, event)
if strcmp(app.timr.Running, 'off')
start(app.timr);
end
app.EditField2.Value = app.timr.Running;
end
end
% App initialization, component creation, and other methods...
% Remain unchanged from your original code
end
I hope it helps!

Categorie

Scopri di più su Develop Apps Using App Designer in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by