Create Callback for Comport in App designer?

5 次查看(过去 30 天)
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.

回答(1 个)

Abhijeet
Abhijeet 2024-1-9
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.

类别

Help CenterFile Exchange 中查找有关 App Building 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by