How to pass additional variables with serial callback function (R2020b)

16 ビュー (過去 30 日間)
Moritz
Moritz 2021 年 1 月 26 日
コメント済み: Jan 2021 年 1 月 27 日
I am working on continuously displaying data on a gui that comes through a serial port. The data comes in form of one string with a "LF/CR" at the end. The obvious idea is to create a callback function that updates the text field of the gui each time that terminator is detected. The problem is that the callback function need the be in the form of
function mycallback(src, evt)
...
end
but also needs the handles of the gui.
With older Matlab Versions I would just use
configureTerminator(s,"CR/LF")
s.BytesAvailableFcnMode = 'terminator';
s.BytesAvailableFcn = {@mycallback,guiHandles};
function mycallback (src, evt, handles)
...
end
But with the newer versions (I am using 2020b) I am forced to use
configureTerminator(s,"CR/LF")
configureCallback(s,"terminator",@mycallback)
I hoped I could just use
configureCallback(s,"terminator",{@mycallback, guiHandles})
or something like that but that doesnt seem to work as configureCallback requires a @callbackfcn and does not accept arrays or cells. I also couldn't find anything on the internet for this matlab version aswell.
Any ideas on how to solve that problem?
Thanks in advance
Moritz

採用された回答

Jan
Jan 2021 年 1 月 26 日
編集済み: Jan 2021 年 1 月 27 日
What about an anonymous function_
% [EDITED] Typo fixed: () around parameters of anonymous function, not {}!
configureCallback(s, "terminator", @(src, evt) mycallback(src, evt, handles))
The ugly solution would be to store the handles persistently in the callback:
function mycallback(src, evt)
persistent handles
if nargin == 1
handles = src;
return;
end
...
end
Then call the callback after creating the handles:
mycallback(handles);
This is prone to bugs, because you might forget to update the persistent data. The anonymous function is a more direct approach.
  2 件のコメント
Moritz
Moritz 2021 年 1 月 27 日
編集済み: Moritz 2021 年 1 月 27 日
The anonymous function worked, except I had to change the {} to (), thanks!
Jan
Jan 2021 年 1 月 27 日
Thanks, Moritz, I've fixed this typo.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDesktop についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by