Problems with function handle in Appdesigner

조회 수: 6 (최근 30일)
Markus Langer
Markus Langer 2020년 5월 13일
댓글: Suraj Parameswaran 2022년 4월 26일
I am currently trying to create my first GUI with Appdesigner. I have a Polar Chest Strap (BLE) that I am reading out. A working (plain)Matlab minimal example is below.
Unfortunately, trying to import the functionality into Appdesigner fails, maybe because I have not done anything with object-oriented programming so far. The strap is recognized after the scan and I can connect; if data is pending, the specified function handle would have to be called and the light would have to be set to green. But this does not happen. Maybe someone can help me.
Here again the functional minimal example in Matlab:
clear all
clc
%% Connect
h10 = ble("EE3CC81051AD"); % Connect to Polar H10
hr_char = characteristic(h10,"Heart Rate","Heart Rate Measurement");
hr_char.DataAvailableFcn = @displayCharacteristicData;
function displayCharacteristicData(src,~)
[data,timestamp] = read(src,'oldest');
disp(data);
disp(timestamp);
end

답변 (1개)

Markus Langer
Markus Langer 2020년 5월 13일
Here is the relevant part from the Appdesigner (file is attached) :
methods (Access = private)
function displayCharacteristicData(app)
app.Lamp.Color = 'green';
% Lamp not green
[data,timestamp] = read(src,'oldest');
end
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: ScanButton
function ScanButtonPushed(app, event)
devicelist = blelist("Timeout", 5);
i = 1;
while(i <= length(devicelist.Index))
app.HRSensorListDropDown.Items{i} = devicelist.Name{i};
i = i + 1;
end
end
% Button pushed function: ConnectButton
function ConnectButtonPushed(app, event)
model = app.HRSensorListDropDown.Value;
chestbelt = ble(model);
hr_char = characteristic(chestbelt,"Heart Rate","Heart Rate Measurement");
hr_char.DataAvailableFcn = @displayCharacteristicData;
% app.Lamp.Color = 'green';
end
  댓글 수: 3
Shad
Shad 2021년 6월 11일
Has anyone figured out how to get BLE callbacks to work in App Designer? I can receive the data if I set the callback function to one in an external m-file, but that has other issues.
Suraj Parameswaran
Suraj Parameswaran 2022년 4월 26일
I am facing the exact same problem. I have come to understand that we cannot pass the app variable in the callback function because the function expects only a BLE characteristic object and an event as an argument.
I have tried. @Joris Lambrecht 's method and he has explained it didn't work. However, when I am defining the function inside a command button function as a nested function like below, I am able to see the output in the command window:
function ConnectButtonPushed(app, event)
function recv_data(data_channel,evt)
received = 1;
data_packet = char(data_channel.read('oldest'));
disp(data_packet);
end
data_channel = app.data;
app.data.DataAvailableFcn = @recv_data;
subscribe(app.data);
end
I am thinking the only way to work around this problem would be to use global variables and then extract the variables in them as the dataavailablefcn does not allow the 'app' to be passed in as an argument. Let me know if anyone has a better idea. Thanks.

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

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by