APP Designer 中设计串口通信,接收​数据时回调函数未执行​,如何解决?

33 views (last 30 days)
Bei
Bei on 26 Mar 2020
Answered: changshu zhan on 19 Feb 2022
最近在使用R2019b中的APP Designer功能设计串行通信软件,目前发送数据是正常的,但是接收数据时提示回调函数存在问题。
串口配置代码如下:
function OpenSerialButtonPushed(app, event)
app.COM = get(app.DropDown,'value') ;
if strcmp(get(app.OpenSerialButton,'Text'),'打开串口')
try
app.s = serialport(app.COM,9600) ;
app.s.Parity = "none" ;
app.s.DataBits = 8 ;
app.s.StopBits = 1 ;
app.s.FlowControl = "none" ;
app.s.ByteOrder = "little-endian" ;
app.s.Timeout = 10 ;
configureCallback(app.s,"byte",1,@ReadSerialData) ;
app.OpenSerialButton.Text = '关闭串口' ;
app.Lamp.Color = [0 1 0] ;
catch err
msgbox("串口打开失败") ;
end
else
try
app.OpenSerialButton.Text = "打开串口" ;
app.Lamp.Color = [0.15 0.15 0.15] ;
catch err
msgbox("串口关闭失败") ;
end
delete(app.s) ; %关闭串口
end
end
上面的代码中关于接收数据的回调函数代码为:
configureCallback(app.s,"byte",1,@ReadSerialData) ;
回调函数ReadSerialData的定义如下:
methods (Access = private)
function ReadSerialData(app,event)
msgbox("回调成功") ;
end
end
执行后,当接收到数据时,Matalab窗口就会提示错误,错误内容如下:
警告: 执行为类 asyncio.InputStream 定义的事件 DataWritten 的侦听程序回调时出现错误:
未定义与 'internal.Serialport' 类型的输入参数相对应的函数 'ReadSerialData'
出错 internal.Serialport/callbackFunction (line 1448)
obj.BytesAvailableFcn(obj, dataAvailableInfo);
出错 internal.Serialport>@(varargin)obj.callbackFunction(varargin{:}) (line 953)
obj.Transport.BytesAvailableFcn = @obj.callbackFunction;
出错 matlabshared.seriallib.internal.Serial/onDataReceived
出错 matlabshared.seriallib.internal.Serial>@(varargin)obj.onDataReceived(varargin{:})
出错 asyncio.Channel/onDataReceived (line 487)
notify(obj.InputStream, 'DataWritten', ...
出错 asyncio.Channel>@(source,data)obj.onDataReceived() (line 425)
@(source, data) obj.onDataReceived());
> In asyncio/Channel/onDataReceived (line 487)
In asyncio.Channel>@(source,data)obj.onDataReceived() (line 425)
这个错误是什么意思呢,应该如何修改代码才是正确的?

Answers (2)

li xu
li xu on 14 Oct 2021
你试试这样:
configureCallback(app.s,"byte",1,@app.ReadSerialData) ;
function ReadSerialData(app,src,event)
msgbox("回调成功") ;
end

changshu zhan
changshu zhan on 19 Feb 2022
同样的串口采样,为什么不调用读串口回调函数?

Community Treasure Hunt

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

Start Hunting!