How to configure serialport callback in app designer?

53 ビュー (過去 30 日間)
Jingyi Zhou
Jingyi Zhou 2020 年 6 月 27 日
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 件のコメント
wu xianhua
wu xianhua 2020 年 11 月 16 日
i have the same problem too, have any solutions yet ?? THS
chen junjin
chen junjin 2020 年 12 月 22 日

サインインしてコメントする。

回答 (3 件)

chen junjin
chen junjin 2020 年 12 月 22 日
  1 件のコメント
Jesse Lackey
Jesse Lackey 2021 年 1 月 18 日
Shame on you Mathworks for not having anything about how to implement this important capability documented. RIDICULOUS.

サインインしてコメントする。


Chirag
Chirag 2021 年 12 月 15 日
編集済み: Chirag 2021 年 12 月 15 日
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
Sebastian Neumann 2021 年 10 月 12 日
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

カテゴリ

Help Center および File ExchangeSerial and USB Communication についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by