Connecting to a serial port in App Designer
Hello,
I want to connect to a serial port using App Designer, I know how to just use it over the command line but in App Designer I am not sure how to do two things, first how do I connect to the device at once rather than connecting to the device everytime I press a button, second I want to define a variable "rel" through which I can define how much the piezo stage can move. But for the error that I am getting is
Reference to non-existent field ‘rel’.
Error in app1/upButtonPushed (line 29)
temp=app.rel.Value
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 335)
Error while evaluating Button PrivateButtonPushedFcn.
The code fo the APP designer is:
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
up matlab.ui.control.Button
left matlab.ui.control.Button
right matlab.ui.control.Button
down matlab.ui.control.Button
relEditFieldLabel matlab.ui.control.Label
rel matlab.ui.control.NumericEditField
end
properties (Access = public)
s % Description
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: up
function upButtonPushed(app, event)
clear all
app.s = serialport("COM5",38400);
configureTerminator(app.s,"CR")
temp=app.rel.Value
writeline(app.s, sprintf(‘2MVR%d’,temp));
%writeline(app.s, ‘2MVR-2’);
%data=readline(s)
end
% Button pushed function: left
function leftButtonPushed(app, event)
clear all
app.s = serialport("COM5",38400);
configureTerminator(app.s,"CR")
writeline(app.s, ‘1MVR2’);
end
% Button pushed function: right
function rightButtonPushed(app, event)
clear all
app.s = serialport("COM5",38400);
configureTerminator(app.s,"CR")
writeline(app.s, ‘1MVR-2’);
end
% Button pushed function: down
function downButtonPushed(app, event)
clear all
app.s = serialport("COM5",38400);
configureTerminator(app.s,"CR")
writeline(app.s, ‘2MVR2’);
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure(‘Visible’, ‘off’);
app.UIFigure.Position = [100 100 319 165];
app.UIFigure.Name = ‘UI Figure’;
% Create up
app.up = uibutton(app.UIFigure, ‘push’);
app.up.ButtonPushedFcn = createCallbackFcn(app, @upButtonPushed, true);
app.up.Icon = ‘icons8-collapse-arrow-48.png’;
app.up.Position = [73 115 39 22];
app.up.Text = ”;
% Create left
app.left = uibutton(app.UIFigure, ‘push’);
app.left.ButtonPushedFcn = createCallbackFcn(app, @leftButtonPushed, true);
app.left.Icon = ‘icons8-back-48.png’;
app.left.Position = [18 71 39 22];
app.left.Text = ”;
% Create right
app.right = uibutton(app.UIFigure, ‘push’);
app.right.ButtonPushedFcn = createCallbackFcn(app, @rightButtonPushed, true);
app.right.Icon = ‘icons8-forward-48.png’;
app.right.Position = [128 71 39 22];
app.right.Text = ”;
% Create down
app.down = uibutton(app.UIFigure, ‘push’);
app.down.ButtonPushedFcn = createCallbackFcn(app, @downButtonPushed, true);
app.down.Icon = ‘icons8-expand-arrow-48.png’;
app.down.Position = [73 31 39 22];
app.down.Text = ”;
% Create relEditFieldLabel
app.relEditFieldLabel = uilabel(app.UIFigure);
app.relEditFieldLabel.HorizontalAlignment = ‘right’;
app.relEditFieldLabel.Position = [158 125 25 22];
app.relEditFieldLabel.Text = ‘rel’;
% Create rel
app.rel = uieditfield(app.UIFigure, ‘numeric’);
app.rel.Position = [198 125 100 22];
app.rel.Value = -2;
% Show the figure after all components are created
app.UIFigure.Visible = ‘on’;
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = app1
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
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
endHello,
I want to connect to a serial port using App Designer, I know how to just use it over the command line but in App Designer I am not sure how to do two things, first how do I connect to the device at once rather than connecting to the device everytime I press a button, second I want to define a variable "rel" through which I can define how much the piezo stage can move. But for the error that I am getting is
Reference to non-existent field ‘rel’.
Error in app1/upButtonPushed (line 29)
temp=app.rel.Value
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 335)
Error while evaluating Button PrivateButtonPushedFcn.
The code fo the APP designer is:
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
up matlab.ui.control.Button
left matlab.ui.control.Button
right matlab.ui.control.Button
down matlab.ui.control.Button
relEditFieldLabel matlab.ui.control.Label
rel matlab.ui.control.NumericEditField
end
properties (Access = public)
s % Description
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: up
function upButtonPushed(app, event)
clear all
app.s = serialport("COM5",38400);
configureTerminator(app.s,"CR")
temp=app.rel.Value
writeline(app.s, sprintf(‘2MVR%d’,temp));
%writeline(app.s, ‘2MVR-2’);
%data=readline(s)
end
% Button pushed function: left
function leftButtonPushed(app, event)
clear all
app.s = serialport("COM5",38400);
configureTerminator(app.s,"CR")
writeline(app.s, ‘1MVR2’);
end
% Button pushed function: right
function rightButtonPushed(app, event)
clear all
app.s = serialport("COM5",38400);
configureTerminator(app.s,"CR")
writeline(app.s, ‘1MVR-2’);
end
% Button pushed function: down
function downButtonPushed(app, event)
clear all
app.s = serialport("COM5",38400);
configureTerminator(app.s,"CR")
writeline(app.s, ‘2MVR2’);
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure(‘Visible’, ‘off’);
app.UIFigure.Position = [100 100 319 165];
app.UIFigure.Name = ‘UI Figure’;
% Create up
app.up = uibutton(app.UIFigure, ‘push’);
app.up.ButtonPushedFcn = createCallbackFcn(app, @upButtonPushed, true);
app.up.Icon = ‘icons8-collapse-arrow-48.png’;
app.up.Position = [73 115 39 22];
app.up.Text = ”;
% Create left
app.left = uibutton(app.UIFigure, ‘push’);
app.left.ButtonPushedFcn = createCallbackFcn(app, @leftButtonPushed, true);
app.left.Icon = ‘icons8-back-48.png’;
app.left.Position = [18 71 39 22];
app.left.Text = ”;
% Create right
app.right = uibutton(app.UIFigure, ‘push’);
app.right.ButtonPushedFcn = createCallbackFcn(app, @rightButtonPushed, true);
app.right.Icon = ‘icons8-forward-48.png’;
app.right.Position = [128 71 39 22];
app.right.Text = ”;
% Create down
app.down = uibutton(app.UIFigure, ‘push’);
app.down.ButtonPushedFcn = createCallbackFcn(app, @downButtonPushed, true);
app.down.Icon = ‘icons8-expand-arrow-48.png’;
app.down.Position = [73 31 39 22];
app.down.Text = ”;
% Create relEditFieldLabel
app.relEditFieldLabel = uilabel(app.UIFigure);
app.relEditFieldLabel.HorizontalAlignment = ‘right’;
app.relEditFieldLabel.Position = [158 125 25 22];
app.relEditFieldLabel.Text = ‘rel’;
% Create rel
app.rel = uieditfield(app.UIFigure, ‘numeric’);
app.rel.Position = [198 125 100 22];
app.rel.Value = -2;
% Show the figure after all components are created
app.UIFigure.Visible = ‘on’;
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = app1
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
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 Hello,
I want to connect to a serial port using App Designer, I know how to just use it over the command line but in App Designer I am not sure how to do two things, first how do I connect to the device at once rather than connecting to the device everytime I press a button, second I want to define a variable "rel" through which I can define how much the piezo stage can move. But for the error that I am getting is
Reference to non-existent field ‘rel’.
Error in app1/upButtonPushed (line 29)
temp=app.rel.Value
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 335)
Error while evaluating Button PrivateButtonPushedFcn.
The code fo the APP designer is:
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
up matlab.ui.control.Button
left matlab.ui.control.Button
right matlab.ui.control.Button
down matlab.ui.control.Button
relEditFieldLabel matlab.ui.control.Label
rel matlab.ui.control.NumericEditField
end
properties (Access = public)
s % Description
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: up
function upButtonPushed(app, event)
clear all
app.s = serialport("COM5",38400);
configureTerminator(app.s,"CR")
temp=app.rel.Value
writeline(app.s, sprintf(‘2MVR%d’,temp));
%writeline(app.s, ‘2MVR-2’);
%data=readline(s)
end
% Button pushed function: left
function leftButtonPushed(app, event)
clear all
app.s = serialport("COM5",38400);
configureTerminator(app.s,"CR")
writeline(app.s, ‘1MVR2’);
end
% Button pushed function: right
function rightButtonPushed(app, event)
clear all
app.s = serialport("COM5",38400);
configureTerminator(app.s,"CR")
writeline(app.s, ‘1MVR-2’);
end
% Button pushed function: down
function downButtonPushed(app, event)
clear all
app.s = serialport("COM5",38400);
configureTerminator(app.s,"CR")
writeline(app.s, ‘2MVR2’);
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure(‘Visible’, ‘off’);
app.UIFigure.Position = [100 100 319 165];
app.UIFigure.Name = ‘UI Figure’;
% Create up
app.up = uibutton(app.UIFigure, ‘push’);
app.up.ButtonPushedFcn = createCallbackFcn(app, @upButtonPushed, true);
app.up.Icon = ‘icons8-collapse-arrow-48.png’;
app.up.Position = [73 115 39 22];
app.up.Text = ”;
% Create left
app.left = uibutton(app.UIFigure, ‘push’);
app.left.ButtonPushedFcn = createCallbackFcn(app, @leftButtonPushed, true);
app.left.Icon = ‘icons8-back-48.png’;
app.left.Position = [18 71 39 22];
app.left.Text = ”;
% Create right
app.right = uibutton(app.UIFigure, ‘push’);
app.right.ButtonPushedFcn = createCallbackFcn(app, @rightButtonPushed, true);
app.right.Icon = ‘icons8-forward-48.png’;
app.right.Position = [128 71 39 22];
app.right.Text = ”;
% Create down
app.down = uibutton(app.UIFigure, ‘push’);
app.down.ButtonPushedFcn = createCallbackFcn(app, @downButtonPushed, true);
app.down.Icon = ‘icons8-expand-arrow-48.png’;
app.down.Position = [73 31 39 22];
app.down.Text = ”;
% Create relEditFieldLabel
app.relEditFieldLabel = uilabel(app.UIFigure);
app.relEditFieldLabel.HorizontalAlignment = ‘right’;
app.relEditFieldLabel.Position = [158 125 25 22];
app.relEditFieldLabel.Text = ‘rel’;
% Create rel
app.rel = uieditfield(app.UIFigure, ‘numeric’);
app.rel.Position = [198 125 100 22];
app.rel.Value = -2;
% Show the figure after all components are created
app.UIFigure.Visible = ‘on’;
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = app1
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
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 app designer MATLAB Answers — New Questions