How to configure serialport callback in app designer?
77 views (last 30 days)
Show older comments
I want to receive streaming data using serial port. The data stream is dense and I don't want to miss any part of it. So I want to configure the BytesAvailableFcn property of serialport by the command 'configureCallback'. I want to read a line of data every time encountering a ternimator. The same function has been realised by .m file, as follows:
arduinoObj = serialport("COM6",9600)
configureTerminator(arduinoObj,"CR/LF");
flush(arduinoObj);
arduinoObj.UserData = struct("Data","","Count",1)
configureCallback(arduinoObj,"terminator",@ UpdateDisplayText)
function UpdateDisplayText(src,~)
data = readline(src);
src.UserData.Data(src.UserData.Count) = string(data);
src.UserData.Count = src.UserData.Count + 1;
if src.UserData.Count > 10
configureCallback(src, "off");
end
end
However, when I want to do the same thing in app designer. It doesn't work. I want to click a button to open and configure this serialport object. And the callback function is called "UpdateText".
function Button_OpenCloseSerialValueChanged(app, event)
value = app.Button_OpenCloseSerial.Value;
if(value==true)
try
app.Button_OpenCloseSerial.Text = "Close";
app.Serial_Port = serialport(app.DropDown_SerialPort.Value,str2double(app.DropDown_Baudrate.Value));
configureTerminator(app.Serial_Port,"CR/LF");
flush(app.Serial_Port);
app.Serial_Port.UserData = struct("Data","","Count",1);
configureCallback(app.Serial_Port,"terminator",@UpdateText);
catch ex
msgbox(ex.message);
end
end
if(value==false)
try
app.Button_OpenCloseSerial.Text="Open";
app.Serial_Port = [];
catch ex
msgbox(ex.message);
end
end
end
Then I add a private method as callback function.
methods (Access = private)
function UpdateText(app,~)
app.TextArea_Display.Value ="Successful";
end
end
When receiving a teiminator, there are always errors. and the callback function is never executed.
Could anyone help me to explain what went wrong?
Any help would be great appreciated~
3 Comments
Answers (3)
chen junjin
on 22 Dec 2020
1 Comment
Jesse Lackey
on 18 Jan 2021
Shame on you Mathworks for not having anything about how to implement this important capability documented. RIDICULOUS.
Chirag
on 15 Dec 2021
Edited: Chirag
on 15 Dec 2021
methods (Access = public)
function readSerialData(app,src,~) % Make Sure to add 'app' as first arguement and 'src' as second arguement
raw = readline(src);
data = str2num(raw{1});
addpoints(app.h,data(1),data(2));
end
end
function StartButtonPushed(app, event)
app.arduinoObj = serialport(app.comPort,app.baudRate);
configureTerminator(app.arduinoObj,"CR/LF","CR/LF");
% Please configure the Callback as below
configureCallback(app.arduinoObj,"terminator",@(src, event) readSerialData(app,src,event));
end
Sebastian Neumann
on 12 Oct 2021
I also struggeling to get the callback function running within app designer in conjunction with the "configureCallback" function.
I have set up an arduino which deivers some serial data within a 5s period.
Can anyone help me with this issues?
It seams to me too that the callback function is never executet, as you stated at the beginning of your question (it works perfect if I run it from a script)
I have searched the entire day for some documentation but I can't find anything ... Apprechiate your help!!
properties (Access = private)
arduinoObj % Serial Arduino Object
end
methods (Access = private)
function Init(app)
app.arduinoObj = serialport("COM4",9600);
configureCallback(app.arduinoObj,"terminator",@app.readSerialData);
disp("Init ok")
end
function readSerialData(app,~,~)
%data = readline(src);
%disp(data)
%Status = data(25:27)
app.SerialInterfaceTextArea.Value = "I'm in the loop";
disp("I'm in the loop")
end
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
Init(app);
end
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!