Can't get variable out of Callback function. Already defined as global.

11 vues (au cours des 30 derniers jours)
Kaan Inal
Kaan Inal le 29 Mai 2022
Commenté : Kaan Inal le 31 Mai 2022
I have a serialport bytesavailable callback function.
function readFrame(src,~)
global data
global payloadBytes
message = read(src,payloadBytes,"uint8");
src.UserData = message;
data = message;
return
I read the buffer into the variable message and then save it as global variable data.
i get the variable in my workspace. Then something happens that I can't explain. I try to svae this global variable in a local variable in my Mainscript.
processedData = data;
Problem is processedData is always empty. So when i want to make a 2D Matrice to save different data liek this;
processedData(frameCount,:) = data;
I get an exception: indices doesn't match. Thats no wonder.
Can somebody tell me what I am doing wrong.
Thank you.
  10 commentaires
Kaan Inal
Kaan Inal le 29 Mai 2022
Okay i know have coded something i think it could work.
Maybe you can review it to make sure the concept ist right.
I made a circular buffer of size 16 with each cell a fixed size of 1xpayloadBytes.
Im not sure with the use of the counter as global variable.
function readFrame(src,~)
global payloadBytes
global cbuffw
global cbuff
message = read(src,payloadBytes,"uint8");
if cbuffw < 17
cbuffw=cbuffw+1;
elseif cbuffw == 17
cbuffw = 1;
end
cbuff(cbuffw,:) = message;
return
Function for Buffer
function [cbuff,cbuffr,cbuffw] = circularBuffer(size,payload)
%create a 1xpayload cell (preallocation).
cbuffSize = zeros(1,payload);
%create a ringbuffer with size x cbuffsize
cbuff(size,:) = cbuffSize;
%set writepointer at start
cbuffw = 1;
%set readpointer at start
cbuffr = 1;
end
Main script:
global cbuff
global cbuffw
[cbuff,cbuffr,cbuffw] = circularBuffer(16,payloadBytes);
Then in the main i would check if cbuffw matches cbuffw like you said.
Something like this:
if cbuffr == cbuffw
%no new data
elseif cbuffr < cbuffw
%read data out of cbuff
cbuffr=cbuffr+1
elseif cbuffr > cbuffw
%bufferoverflow throw error. Should not happen.
end
I think the last part is not correct.
Thank you very much.
Kaan Inal
Kaan Inal le 29 Mai 2022
Ok sorry maybe im too dumb to get it but it doesn't work. The callback works i get alle the data i need inside cbuff. But i can't check cbuffw inside my main skript and so cbuff doesn't contain the data. I feel really dumb.
In the main script.
cbuffw always 1.
cbuff always 1xpayloadBytes filled with zeros.
In the workspace
cbuff stores all values read.
cbuffw increases.
After stopping the mainscript i can see all the serialdata in cbuff.
Mainscript:
if cbuffr == cbuffw
disp("no new data");
elseif cbuffw > cbuffr
disp("w>r");
cbuffr = cbuffr+1;
data(frameCount,:) = cbuff(cbuffr,:) ;
frameCount= frameCount +1
if cbuffr == 16
cbuffr = 1;
end
end

Connectez-vous pour commenter.

Réponses (1)

Image Analyst
Image Analyst le 29 Mai 2022
Try assigning it like this
if isempty(data)
fprintf('Skipping frameCount = %d because data vector is empty!\n', frameCount);
elseif processedData <= 1 || isempty(processedData)
% The first time through just assign it so that processedData has the same number of columns as data
processedData = reshape(data, 1, []); % Make sure it's a row vector.
else
% Append on data into a new row for the second and subsequent times through.
processedData(frameCount,:) = data;
end
  9 commentaires
Image Analyst
Image Analyst le 30 Mai 2022
You need to get it visible in the same scope. For example attach it to the app structure, like
app.data = read(src,payloadBytes,"uint8");
Then app.data should be available everywhere because app is available everywhere. It's like a global variable.
Kaan Inal
Kaan Inal le 31 Mai 2022
I got the solution. I had to use drawnow(). With using drawnow() variable and its value was useable in the mainscript.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by