Hi,
I am designing an interface in the App Designer. I have an Arduino board connected to some sensors. I would like to be able to run the interface multiple times, but right now I run it once perfectly and when I click run again it tells me I have an existing connection at my COM port and I need to clear it. I am unsure how to do this as I am very new to app designer! Any help would be greatly appreciated!
Thanks!

4 Kommentare

Asad Mirza
Asad Mirza am 20 Feb. 2019
Mind posting some of app designer code for us?
I suspect what is happening is you're trying to connect to the arduino each time you say click a button to send a signal or something. What you should do instead is connect to the arduino once and then when you click the button to do something have the button code only send a signal rather than try to connect to the arduino.
I'd like to help more but I would need to see the code as it is first.
classdef app2 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
RUNButton matlab.ui.control.Button
STOPButton matlab.ui.control.Button
UIAxes matlab.ui.control.UIAxes
COMPortEditFieldLabel matlab.ui.control.Label
COMPortEditField matlab.ui.control.EditField
SaveAsEditFieldLabel matlab.ui.control.Label
SaveAsEditField matlab.ui.control.EditField
end
properties (Access = private)
stop;
port;
done=0;
timeLogs;
forceLogs;
timeSecs;
filename;
T;
h;
a;
end
methods (Access = private)
% Code that executes after component creation
function startupFcn(app, a)
app.a=app.a;
end
% Value changed function: COMPortEditField
function COMPortEditFieldValueChanged(app, event)
value = app.COMPortEditField.Value;
app.port=value;
end
% Button pushed function: RUNButton
function RUNButtonPushed(app, event)
app.a=arduino(app.port,'uno');
figure
app.h=animatedline;
ax=gca;
ax.YGrid='on';
ax.YLim=[0 100];
app.stop=false;
starttime=datetime('now');
while ~app.stop
% Read current voltage value
voltage1 = readVoltage(app.a,'A0');
voltage2 = readVoltage(app.a,'A1');
% Calculate force from voltage (based on data sheet)
% Flipped for ease of use
force1=-(13.499*voltage1)+68.534; %from calibration with standard weights
force2=-(9.9586*voltage2)+49.882; %from calibration with standard weights
force=force1+force2;
% Get current time
t = datetime('now') - starttime;
% Add points to animation
addpoints(app.h,datenum(t),force)
% Update axes
ax.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
drawnow
% Check stop condition
app.stop = app.done;
end
end
% Button pushed function: STOPButton
function STOPButtonPushed(app, event)
app.done=1;
[app.timeLogs,app.forceLogs] = getpoints(app.h);
app.timeSecs = (app.timeLogs-app.timeLogs(1))*24*3600;
app.T = table(app.timeSecs',app.forceLogs','VariableNames',{'Time_sec','Force_N'});
% Write table to file
writetable(app.T,app.filename)
[app.timeLogs,app.forceLogs] = getpoints(app.h);
app.timeSecs = (app.timeLogs-app.timeLogs(1))*24*3600;
plot(app.UIAxes,app.timeSecs,app.forceLogs)
end
% Value changed function: SaveAsEditField
function SaveAsEditFieldValueChanged(app, event)
value = app.SaveAsEditField.Value;
app.filename=value;
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';
% Create RUNButton
app.RUNButton = uibutton(app.UIFigure, 'push');
app.RUNButton.ButtonPushedFcn = createCallbackFcn(app, @RUNButtonPushed, true);
app.RUNButton.BackgroundColor = [0 1 0];
app.RUNButton.Position = [388 22 100 31];
app.RUNButton.Text = 'RUN';
% Create STOPButton
app.STOPButton = uibutton(app.UIFigure, 'push');
app.STOPButton.ButtonPushedFcn = createCallbackFcn(app, @STOPButtonPushed, true);
app.STOPButton.BackgroundColor = [1 0 0];
app.STOPButton.Position = [509 22 100 31];
app.STOPButton.Text = 'STOP';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Force vs. Time')
xlabel(app.UIAxes, 'Time')
ylabel(app.UIAxes, 'Force')
app.UIAxes.YGrid = 'on';
app.UIAxes.Position = [11 72 619 398];
% Create COMPortEditFieldLabel
app.COMPortEditFieldLabel = uilabel(app.UIFigure);
app.COMPortEditFieldLabel.HorizontalAlignment = 'right';
app.COMPortEditFieldLabel.Position = [44 52 59 22];
app.COMPortEditFieldLabel.Text = 'COM Port';
% Create COMPortEditField
app.COMPortEditField = uieditfield(app.UIFigure, 'text');
app.COMPortEditField.ValueChangedFcn = createCallbackFcn(app, @COMPortEditFieldValueChanged, true);
app.COMPortEditField.Position = [118 52 100 22];
app.COMPortEditField.Value = 'Com';
% Create SaveAsEditFieldLabel
app.SaveAsEditFieldLabel = uilabel(app.UIFigure);
app.SaveAsEditFieldLabel.HorizontalAlignment = 'right';
app.SaveAsEditFieldLabel.Position = [45 12 54 22];
app.SaveAsEditFieldLabel.Text = 'Save As:';
% Create SaveAsEditField
app.SaveAsEditField = uieditfield(app.UIFigure, 'text');
app.SaveAsEditField.ValueChangedFcn = createCallbackFcn(app, @SaveAsEditFieldValueChanged, true);
app.SaveAsEditField.Position = [114 12 214 22];
end
end
methods (Access = public)
% Construct app
function app = app2(varargin)
% Create and configure components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @(app)startupFcn(app, varargin{:}))
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
This is the entire code for the app designer. I think the arduino connection is in the Run button function. I attached a photo of the interface as well for reference! Thank you!
Mohammed Yakoob
Mohammed Yakoob am 2 Sep. 2024
Hello dear You can use a= arduino(com, Uno); For example to connect and in the another callback a=[]; to clear the port for new connection.
Mohammed Yakoob
Mohammed Yakoob am 19 Jan. 2025
Verschoben: Walter Roberson am 19 Jan. 2025

Hello, I'm facing in problem that the internal buffers is fully and need to clear it, so how I can do this step when I am using arduino () class?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Asad Mirza
Asad Mirza am 22 Feb. 2019

0 Stimmen

You could try changing your startup to just this instead.
function startupFcn(app, a)
app.a=arduino(app.port,'uno');
end
Now remove the arduino(app.port,'uno'); line from your RUNButtonPushed(app, event) function.
This way you only create the arduino object once when you startup the app and then after that all you do is send signals with the button. I do not have an arduino nearby to test this on unfortunately.

3 Kommentare

Allison McCrady
Allison McCrady am 25 Feb. 2019
See the problem with that is that they need to identify the com port and having it in the startup function doesnt allow that. Do you know if I can write the app to search for the com port? THat would be best. Thanks!
Asad Mirza
Asad Mirza am 26 Feb. 2019
Bearbeitet: Asad Mirza am 26 Feb. 2019
Attached you'll find a quick app I wrote that first finds all available COM ports. You then select the port your arduino is connected to from the list and then press connect to link it to the arduino. You can edit this app such that now that you've connected to the Arduino separatly you can do all the plotting afterwards.
I suggest you make like a button on the app that now plots data from any of the input arduino pins but only after you've connected. You can use the uibuton enable property to make plotting impossible unless you've already connected to an arduino.
Arduino_Ex.JPG
Santiago Restrepo Estrada
Santiago Restrepo Estrada am 28 Jun. 2019
Hello,
I was wondering how could I integrate this into an existing app.
thank you

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Kihak Lee
Kihak Lee am 28 Dez. 2019

0 Stimmen

Dear Asad Mirza
I tested your attached files(Arduino_Ex.mlapp) several times. There is a problem in disconnect button.
When push disconnect button, error is happend. I can not fix it.
Plese check your code!
thanks
Mohammed Yakoob
Mohammed Yakoob am 19 Jan. 2025

0 Stimmen

Hello, I'm facing in problem that the internal buffers is fully and need to clear it, so how I can do this step when I am using arduino () class?

Kategorien

Mehr zu MATLAB Support Package for Arduino Hardware finden Sie in Hilfe-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