While loop stuck in callback function

I have written a callback function in Matlab. My laptop is communicating with another laptop that is sending it bytes every few seconds that are recorded in a text file. For e.g. the laptop sends "66" and my laptop writes to the file Event_Markers.txt "66" continuously until the other laptop sends something else. The code is below.
The problem that I am currently facing is that in my callback function (below) I use a while loop to continuously write the same "information" (e.g. "66") to the text file until the other laptop sends something else. But this while loop gets stuck. This part is of a larger script that is acquiring data from a spectrometer and adding it to my script and causes everything to become stuck and the rest of the script is not executed. I tried to use an if loop instead of while and it only writes "66" twice instead of writing it continuously. It is, however, writing to the text file as I want it to.
Does anybody know if I need to add some other line of code to stop it becoming stuck? I have tried to add in a break but that results in the same problem as when I have an if loop, it only records "66" twice instead of continuously.
Thanks!
appenderFile=fopen('Event_Markers.txt','a+t');
s=serial('COM3');
set(s,'BytesAvailable',{@myCallback,appenderFile});
set(s,'BytesAvailableFcnCount',1);
set(s,'BytesAvailableFcnMode','byte');
fopen(s);
function myCallback(s,~,appenderFile)
bytes=(s,'BytesAvailable')
if(bytes==1)
[data count msg] = fread(s,bytes);
end
fprintf(appenderFile,'%d \n',data);
bytes=(s,'BytesAvailable');
while bytes==0
fprintf(appenderFile,'%d \n',data);
bytes=get(s,'BytesAvailable');
%if bytes~=0
%break
%end
end
end

 Respuesta aceptada

Image Analyst
Image Analyst el 1 de Abr. de 2016
Never use a while loop without a failsafe or else you get the problem you did. So you can put a loop counter on there
loopCounter = 1;
maxNumLoops = 1000; % Whatever you'd expect the max to ever get to.
while condition && loopCounter < maxNumLoops
% code
% Increment loop counter
maxNumLoops = maxNumLoops + 1;
end
Or else use tic and toc to bail out after so many seconds
maxSecondsToWait = 5; % Whatever
startTime = tic;
elapsedSeconds = 0;
while condition && elaspedSeconds < maxSecondsToWait
% code
% Update elapsed time.
elapsedSeconds = toc(startTime);
end

6 comentarios

Maheen Siddiqui
Maheen Siddiqui el 1 de Abr. de 2016
Editada: Maheen Siddiqui el 1 de Abr. de 2016
Thanks for your reply. I have tried for it to bail out after 10 seconds, the problem is that before those 10 seconds, the rest of the script is essentially "paused" which means that my data acquisition is paused. Is there a way to have the while loop running in the background without it interfering with the rest of the script?
The acquisition time is 1s but at the end when I stop the script, having this while loop makes it much slower. So instead of 1s its 2s and also there is a 12s pause..
Image Analyst
Image Analyst el 1 de Abr. de 2016
I don't know how to have two processes running - maybe use the Parallel Computing Toolbox or something.
I don't see why having a check for elapsed time would add much time or why bailing out would add a pause of 12 seconds. You'll just have to debug it. Call tech support if you need to.
Maheen Siddiqui
Maheen Siddiqui el 3 de Abr. de 2016
I was wondering whether it is possibly not a good idea that I am doing the fprintf in the callback function? Should I be doing it in another script?
Image Analyst
Image Analyst el 3 de Abr. de 2016
No, that's not a problem. It is a problem though if you don't call fclose() and I don't see you calling that so make sure you call that when you're done interacting with the file.
Maheen Siddiqui
Maheen Siddiqui el 4 de Abr. de 2016
I call fclose at the end of the bigger script.
After debugging I found that it was the fread and fprintf which are very slow. But I'm unsure what I could do instead? Do you think I should save "data" in an array instead of writing to a file?
Image Analyst
Image Analyst el 5 de Abr. de 2016
You could use SSDs instead of HDs.
Or, if you have your data originate from the same session of the same program, then sure, keep it as an array in memory and do not write to a temporary file on the drive. It will be faster.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 1 de Abr. de 2016

Comentada:

el 5 de Abr. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by