how serial comunication port list in arduino
4 views (last 30 days)
Show older comments
Imam Pratama Setiady
on 19 Jul 2021
Commented: Imam Pratama Setiady
on 19 Jul 2021
i have a question. i would make a serial comunication with arduino device, for the comunication i would make a port list in the comunication. if i write "app.Property = serialport("COM3",9600,"Timeout",5)" its works but "app.Property = serialportlist("available")" but doesnt works. why did happen ? thanks before
this my full code
classdef arduino1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
UIAxes matlab.ui.control.UIAxes
start matlab.ui.control.Button
stop matlab.ui.control.Button
export matlab.ui.control.Button
UITable matlab.ui.control.Table
HardwarePanel matlab.ui.container.Panel
GridLayout11 matlab.ui.container.GridLayout
Refresh matlab.ui.control.Button
PortLabel matlab.ui.control.Label
PortList matlab.ui.control.DropDown
ConnLamp matlab.ui.control.Lamp
ConnectionLabel matlab.ui.control.Label
end
properties (Access = private)
%% define semua kebutuhan
% Description
Property
% colors
GREEN_COLOR = [0 1 0]
RED_COLOR = [1 0 0]
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
% app.Property = serialportlist("available") %%error
app.Property = serialport("COM3",9600,"Timeout",5)
end
% Button pushed function: start
function startButtonPushed(app, event)
app.ConnLamp.Color = app.GREEN_COLOR;
global val_stop val_data %%val_voltage val_const
val_stop = 0;
val_sample = 300;
val_voltage = 0;
val_count = 1;
while val_count <= val_sample
if val_stop == 1
break;
else
ADC_value = str2double(readline(app.Property));
val_const(val_count) = 5;
val_voltage(val_count) = ADC_value(1)*5/308;
plot(app.UIAxes,val_voltage);
app.UIAxes.XLim = [0 val_count+50];
app.UIAxes.YLim = [0 5.1];
val_time(val_count) = val_count;
val_data = [val_voltage; val_time];
app.UITable.Data =val_data;
val_count = val_count + 1;
end
end
end
% Button pushed function: stop
function stopButtonPushed(app, event)
app.ConnLamp.Color = app.RED_COLOR;
global val_stop
val_stop = 1;
end
% Button pushed function: export
function exportButtonPushed(app, event)
global val_data
writematrix(val_data','data.xls');
end
% Button pushed function: Refresh
function RefreshButtonPushed(app, event)
if license('test', 'instr_control_toolbox') %% not function
% delete all serial port objets from MATLAB memory
% (Instrument Control Toolbox required)
delete(instrfind);
% looking for available serial ports
% (Instrument Control Toolbox required)
p = instrhwinfo('serial');
app.PortList.Items = p.AvailableSerialPorts;
end
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 640 480];
app.UIFigure.Name = 'UI Figure';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Title')
xlabel(app.UIAxes, 'X')
ylabel(app.UIAxes, 'Y')
app.UIAxes.Position = [24 32 410 279];
% Create start
app.start = uibutton(app.UIFigure, 'push');
app.start.ButtonPushedFcn = createCallbackFcn(app, @startButtonPushed, true);
app.start.Position = [25 328 118 26];
app.start.Text = 'start';
% Create stop
app.stop = uibutton(app.UIFigure, 'push');
app.stop.ButtonPushedFcn = createCallbackFcn(app, @stopButtonPushed, true);
app.stop.Position = [152 328 118 26];
app.stop.Text = 'stop';
% Create export
app.export = uibutton(app.UIFigure, 'push');
app.export.ButtonPushedFcn = createCallbackFcn(app, @exportButtonPushed, true);
app.export.Position = [463 32 145 32];
app.export.Text = 'export to .xls';
% Create UITable
app.UITable = uitable(app.UIFigure);
app.UITable.ColumnName = {'Column 1'; 'Column 2'};
app.UITable.RowName = {};
app.UITable.Position = [455 100 161 242];
% Create HardwarePanel
app.HardwarePanel = uipanel(app.UIFigure);
app.HardwarePanel.Title = 'Hardware';
app.HardwarePanel.Position = [24 370 246 92];
% Create GridLayout11
app.GridLayout11 = uigridlayout(app.HardwarePanel);
app.GridLayout11.ColumnWidth = {'1x', '3x'};
app.GridLayout11.Padding = [5 5 5 5];
% Create Refresh
app.Refresh = uibutton(app.GridLayout11, 'push');
app.Refresh.ButtonPushedFcn = createCallbackFcn(app, @RefreshButtonPushed, true);
app.Refresh.Layout.Row = 2;
app.Refresh.Layout.Column = 2;
app.Refresh.Text = 'Refresh';
% Create PortLabel
app.PortLabel = uilabel(app.GridLayout11);
app.PortLabel.HorizontalAlignment = 'right';
app.PortLabel.Layout.Row = 1;
app.PortLabel.Layout.Column = 1;
app.PortLabel.Text = 'Port';
% Create PortList
app.PortList = uidropdown(app.GridLayout11);
app.PortList.Items = {};
app.PortList.Editable = 'on';
app.PortList.BackgroundColor = [1 1 1];
app.PortList.Layout.Row = 1;
app.PortList.Layout.Column = 2;
app.PortList.Value = {};
% Create ConnLamp
app.ConnLamp = uilamp(app.UIFigure);
app.ConnLamp.Position = [289 429 20.6 20.6];
app.ConnLamp.Color = [1 0 0];
% Create ConnectionLabel
app.ConnectionLabel = uilabel(app.UIFigure);
app.ConnectionLabel.Position = [320 427 123.6 24.3636363636364];
app.ConnectionLabel.Text = 'Connection';
% 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 = arduino1
% Create UIFigure and 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
0 Comments
Accepted Answer
Walter Roberson
on 19 Jul 2021
app.Property = serialport("COM3",9600,"Timeout",5)
would set Property to a serialport object
app.Property = serialportlist("available")
would try to set Property to a string array. The string array is not certain to be scalar -- might be empty, might list multiple ports. And it is not a serialport.
Alternate code:
spl = serialportlist("available");
if isempty(spl)
error('no serial ports available')
end
app.Property = serialport(spl(1),9600,"Timeout",5)
4 Comments
Walter Roberson
on 19 Jul 2021
With what you posted it is not completely clear whether you call serialport() again after the user chooses a different entry?
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!