Serial Data Logging From Arduino Using App Builder
조회 수: 10 (최근 30일)
이전 댓글 표시
Hello,
I am trying to read a simple string "Hello World" from Arduino in MATLAB via serial port in application programmed using App Builder tool. For Arduino, I am using following piece of code:
%%%%%%%%%%%%%%%%%%%%%%%% ARDUINO CODE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop()
{
Serial.println("Hello World");
delay(1000); // delay in between reads for stability
}
My Application is somewhat like the one in the link app_gui.png. The MATLAB Code View for this application is as under:
classdef My_matlab_app < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
ConnectButton matlab.ui.control.Button
DisconnectButton matlab.ui.control.Button
DataRXTextAreaLabel matlab.ui.control.Label
DataRXTextArea matlab.ui.control.TextArea
ArduinoSerialDataReadLabel matlab.ui.control.Label
Lamp matlab.ui.control.Lamp
COMPortsDropDownLabel matlab.ui.control.Label
COMPortsDropDown matlab.ui.control.DropDown
FixandRefreshButton matlab.ui.control.Button
end
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.COMPortsDropDown.Items=seriallist;
%comPort = seriallist;
% formatSpec = "Port Available: %s";
% str = sprintf(formatSpec,comPort);
% app.DataRXTextArea.Value = str;
app.Lamp.Color = 'r';
end
% Button pushed function: ConnectButton
function ConnectButtonPushed(app, event)
display('Connected');
%msgbox('Arduino Connected!');
% uf = uitextarea;
% tarea = uitextarea(uf);
% tarea.Value = 'Connected!';
% answer = 'Arduino is Connected';
% app.DataRXTextArea.Value = answer;
delete(instrfindall);
comPort = seriallist;
%a = arduino(comPort,'Uno');
s = serial(comPort);
formatSpec = "Port Available: %s";
str = sprintf(formatSpec,comPort);
app.DataRXTextArea.Value = str;
app.DataRXTextArea.Value = newline;
app.Lamp.Color = 'g';
fopen(s);
serial_str = fscanf(s,'%s',500);
app.DataRXTextArea.Value = serial_str;
end
% Button pushed function: DisconnectButton
function DisconnectButtonPushed(app, event)
display('Disconnected');
%msgbox('Arduino Connected!');
app.DataRXTextArea.Value = 'Disconnected!';
app.Lamp.Color = 'r';
fclose(serial(seriallist));
clear serial(seriallist);
end
% Value changed function: DataRXTextArea
function DataRXTextAreaValueChanged(app, event)
changingvalue = event.Value;
app.ArduinoSerialDataReadLabel.Text = char(string(changingvalue)+"%");
end
% Button pushed function: FixandRefreshButton
function FixandRefreshButtonPushed(app, event)
app.DataRXTextArea.Value = 'Fixed and Refreshed!';
delete(instrfindall);
end
% Button down function: UIFigure
function UIFigureButtonDown(app, event)
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 558 347];
app.UIFigure.Name = 'UI Figure';
app.UIFigure.ButtonDownFcn = createCallbackFcn(app, @UIFigureButtonDown, true);
% Create ConnectButton
app.ConnectButton = uibutton(app.UIFigure, 'push');
app.ConnectButton.ButtonPushedFcn = createCallbackFcn(app, @ConnectButtonPushed, true);
app.ConnectButton.Position = [27 233 100 23];
app.ConnectButton.Text = 'Connect';
% Create DisconnectButton
app.DisconnectButton = uibutton(app.UIFigure, 'push');
app.DisconnectButton.ButtonPushedFcn = createCallbackFcn(app, @DisconnectButtonPushed, true);
app.DisconnectButton.Position = [147 232 100 23];
app.DisconnectButton.Text = 'Disconnect';
% Create DataRXTextAreaLabel
app.DataRXTextAreaLabel = uilabel(app.UIFigure);
app.DataRXTextAreaLabel.HorizontalAlignment = 'right';
app.DataRXTextAreaLabel.Position = [14 167 59 22];
app.DataRXTextAreaLabel.Text = 'Data (RX)';
% Create DataRXTextArea
app.DataRXTextArea = uitextarea(app.UIFigure);
app.DataRXTextArea.ValueChangedFcn = createCallbackFcn(app, @DataRXTextAreaValueChanged, true);
app.DataRXTextArea.Position = [88 13 458 178];
% Create ArduinoSerialDataReadLabel
app.ArduinoSerialDataReadLabel = uilabel(app.UIFigure);
app.ArduinoSerialDataReadLabel.FontSize = 18;
app.ArduinoSerialDataReadLabel.FontWeight = 'bold';
app.ArduinoSerialDataReadLabel.Position = [168 284 224 23];
app.ArduinoSerialDataReadLabel.Text = 'Arduino Serial Data Read';
% Create Lamp
app.Lamp = uilamp(app.UIFigure);
app.Lamp.Position = [307 242 20 20];
% Create COMPortsDropDownLabel
app.COMPortsDropDownLabel = uilabel(app.UIFigure);
app.COMPortsDropDownLabel.HorizontalAlignment = 'right';
app.COMPortsDropDownLabel.Position = [344 241 65 22];
app.COMPortsDropDownLabel.Text = 'COM Ports';
% Create COMPortsDropDown
app.COMPortsDropDown = uidropdown(app.UIFigure);
app.COMPortsDropDown.Items = {};
app.COMPortsDropDown.Position = [424 241 100 22];
app.COMPortsDropDown.Value = {};
% Create FixandRefreshButton
app.FixandRefreshButton = uibutton(app.UIFigure, 'push');
app.FixandRefreshButton.ButtonPushedFcn = createCallbackFcn(app, @FixandRefreshButtonPushed, true);
app.FixandRefreshButton.Position = [88 200 100 23];
app.FixandRefreshButton.Text = 'Fix and Refresh';
end
end
methods (Access = public)
% Construct app
function app = My_matlab_app
% 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
I wanna read 500 values outta serial port in the text field with "Hello World" appended to next line for 500 values (or any number I want). That is, upon pressing "Connect Button", there should be displayed "Port Available: COM <any port>" and then from new line onwards in text area, "Hello World" should be printed at each new line in text area. Therefore, I need a simple hyperterminal type application for serial read from Arduino. I am using Arduino Uno in my case. Any help would be appricated, please.
댓글 수: 0
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Arduino Hardware에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!