Create Callback for Comport in App designer?

3 views (last 30 days)
I am trying to create a callback everytime the comport writes "\n", but it does not work (the function is never called). This is the way I do it
function ConnectButtonPushed(app, event)
app.s=serialport(app.COMportDropDown.Value, 115200);
configureTerminator(app.s,"CR/LF");
flush(app.s);
configureCallback(app.s,"terminator",@readCOM);
.
.
.
And this is the function that i try to call:
methods (Access = private)
function readCOM(~,src,~)
data = readline(src);
raw = strsplit(data,";");
if(raw{0} == '#')
rssi = str2int(raw{1});
ID = str2int(raw{2});
set(app.IDEditField, "Value", ID);
signID = str2int(raw{3});
set(app.SignEditField ,"Value", signID);
RoadID = str2int(raw{4});
set(app.RoadEditField ,"Value", RoadID);
siteID = str2int(raw{5});
set(app.SiteEditField ,"Value", siteID);
orient = str2int(raw{6});
set(app.OrientationGauge ,"Value", orient);
end
end
end
The function is never called, and I have tried every single step and it still does not work. Can somebody help me? Thank You in advance.

Answers (1)

Abhijeet
Abhijeet on 9 Jan 2024
Hi César,
I understand that you have created a callback function which is not getting triggered.
You mentioned using "CR/LF" which corresponds to Carriage return and Line Feed. To make sure callback is gettting triggered verify that the device you are communicating with is actually sending these characters as terminators. Otherwise, the callback will not be triggered.
In the code you have provided , i suggest you to make the following changes in callback function :-
  • In MATLAB, array indexing starts at 1. So, 'raw{1}' is invalid and should be 'raw{1}' if you want to check the first element of the split data.
  • The function 'str2int' function does not exist in MATLAB. You should use 'str2double' or 'str2num' to convert string to number.
I hope this resolves the issue you were facing.

Categories

Find more on App Building in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!