MATLAB app designer, how to update value of a property in declared callback function
24 ビュー (過去 30 日間)
古いコメントを表示
Yuandi Wu
2022 年 1 月 18 日
コメント済み: Alessandro Livi
2024 年 7 月 21 日
Hello all,
I am rather new to MATLAB, and therefore a little unfamiliar with the app designer, apologies in advance.
I am trying to write a code that tracks the position of a stepper motor over serial communication. Currently, I have the setup such that it will callback the readSerialData Function everytime the terminator "CR" is reached.
Ideally I would like to access the value read. I wanted to save this value to the motorPos property of the app for use in separate functions later down the line. However, from tests, it seems that the value of app.motorPos does not update and remains the initial value of 0, as defined in the startup function. While it does display the correct value in the command window (from the disp(app.arduino) function), the value of the property app.arduino seem to remain 0, when called upon in later functions.
How should I go about updating the value of property motorPos?
Thanks,
Yuandi Wu
properties (Access = public)
arduino
motorPos
end
methods (Access = private)
function readSerialData(app,src)
app.motorPos = readline(src);
disp(app.motorPos);
end
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
delete(instrfindall);
app.arduino = serialport ("COM3", 9600, "Timeout", 0.1);
app.arduino.DataBits = 8;
configureTerminator(app.arduino,"CR")
configureCallback(app.arduino,"terminator",@readSerialData)
%initially, upon startup, display these values
app.motorPos = '0';
disp(app.motorPos)
end
2 件のコメント
Michael Van de Graaff
2022 年 1 月 18 日
does this help? https://www.mathworks.com/matlabcentral/answers/555832-how-to-configure-serialport-callback-in-app-designer?s_tid=answers_rc1-2_p2_MLT
Maybe this would work?
configureCallback(app.arduino,"terminator",@(src) readSerialData(app,src))
採用された回答
Michael Van de Graaff
2022 年 1 月 22 日
I put the following in a comment, and OP says it worked, so i'm copying this into an answer in the hopes that it can be accepted officially
------------------------------------------
does this help? https://www.mathworks.com/matlabcentral/answers/555832-how-to-configure-serialport-callback-in-app-designer?s_tid=answers_rc1-2_p2_MLT
Maybe this would work?
configureCallback(app.arduino,"terminator",@(src) readSerialData(app,src))
0 件のコメント
その他の回答 (1 件)
Mohammad Sami
2022 年 1 月 18 日
You can try the following.
configureCallback(app.arduino,"terminator",@app.readSerialData)
2 件のコメント
Alessandro Livi
2024 年 7 月 21 日
2 year later and still no definitive answer that works
Pick one or offer yet another that works:
function SerInput(app,src)
function SerInput(~,src)
Pick one or offer yet another that works: (MyCom is the serial port handle
app.MyCom.configureCallback("terminator",@SerInput);
configureCallback(app.MyCom,"terminator",@SerInput);
configureCallback(app.MyCom,"terminator",@app.MyInput); % answer 555832
% As recommended by staff in answer 555832 below above
configureCallback(app.MyCom,"terminator",@(src) MyInput(app,src));
Tried last option: App Designer is now stopping debug (returning to >Run) leaving my app running and NO error message there or in Command Window when I get a char from the MyCom
参考
カテゴリ
Help Center および File Exchange で Interactive Control and Callbacks についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!