필터 지우기
필터 지우기

How to configure serialport callback in app designer?

조회 수: 84 (최근 30일)
Jingyi Zhou
Jingyi Zhou 2020년 6월 27일
댓글: Javier de Hoyos García 2022년 9월 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

댓글을 달려면 로그인하십시오.

답변 (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 CenterFile Exchange에서 Serial and USB Communication에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by