Effacer les filtres
Effacer les filtres

MATLAB app designer, how to update value of a property in declared callback function

23 vues (au cours des 30 derniers jours)
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?
Yuandi Wu
properties (Access = public)
methods (Access = private)
function readSerialData(app,src)
app.motorPos = readline(src);
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.arduino = serialport ("COM3", 9600, "Timeout", 0.1);
app.arduino.DataBits = 8;
%initially, upon startup, display these values
app.motorPos = '0';
  2 commentaires
Yuandi Wu
Yuandi Wu le 19 Jan 2022
I have attempted to implement the code and it seems to be working!
Thank you so much for your response, I really appreciate it! Definitely saved me for this assignment!

Connectez-vous pour commenter.

Réponse acceptée

Michael Van de Graaff
Michael Van de Graaff le 22 Jan 2022
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
Maybe this would work?
configureCallback(app.arduino,"terminator",@(src) readSerialData(app,src))

Plus de réponses (1)

Mohammad Sami
Mohammad Sami le 18 Jan 2022
You can try the following.
  1 commentaire
Yuandi Wu
Yuandi Wu le 19 Jan 2022
Thank you so much for taking the time to reply, I have tried this solution, and unfortunately, was met with this error. This error was displayed on the command window, whereas before it would have displayed the location.
Unfortunately, it also seems that it did not save the value as a property, as when used in later call back functions, there were no response. In the previous code, it would treat the motorPos as 0, and move it as such, however now it does not move at all. I think that it is overwriting the motorPos and updating it, but maybe just not with the desired value due to the error?
What would be your take on this?
Thanks again,
Yuandi Wu
Warning: Error occurred while executing the listener callback for event DataWritten defined for class
Error using TestApp2/readSerialData
Too many input arguments.
Error in TestApp2>@(varargin)app.readSerialData(varargin{:}) (line 53)
Error in internal.Serialport/callbackFunction (line 1406)
obj.BytesAvailableFcn(obj, dataAvailableInfo);
Error in internal.Serialport>@(varargin)obj.callbackFunction(varargin{:}) (line 957)
obj.StringClient.StringReadFcn = @obj.callbackFunction;
Error in matlabshared.transportclients.internal.StringClient.StringClient/DataAvailableCallback
Error in
Error in matlabshared.seriallib.internal.Serial/onDataReceived
Error in matlabshared.seriallib.internal.Serial>@(varargin)obj.onDataReceived(varargin{:})
Error in asyncio.Channel/onDataReceived (line 487)
notify(obj.InputStream, 'DataWritten', ...
Error in asyncio.Channel>@(source,data)obj.onDataReceived() (line 425)
@(source, data) obj.onDataReceived());
> In asyncio/Channel/onDataReceived (line 487)
In asyncio.Channel>@(source,data)obj.onDataReceived() (line 425)

Connectez-vous pour commenter.


En savoir plus sur Downloads dans Help Center et File Exchange




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by