Sending a write command to the serial port simultaneously during a read operation.

1 vue (au cours des 30 derniers jours)
Hi,
I'm interfacing MAX10 fpga development kit with matlab. I want to send a command from the matlab to the board at the same time when read operation is taking place in the matlab from the buffer. I'm attaching the code:
if( strcmp(stopCondition , 'STOP') == 1 )
% fprintf(s, stopCondition);
disp('STOP requested');
fprintf(s, stopCondition);
end
When I'm using the above code the Matlab is not able to write the "STOP" command to the buffer until whole read operation is completed. But I'm acquiring data continuously so I need to send the "STOP" command in the middle of the read operation. Can anyone please kindly help me resolve this issue ?
  3 commentaires
Swarnava Pramanik
Swarnava Pramanik le 24 Juin 2016
Modifié(e) : Walter Roberson le 25 Juin 2016
Hi Walter,
Here is my code :
function data_from_serial_port = serial_port_read
serObJ = serial('COM4');
set(serObJ,'BaudRate',baud);
serObJ.BytesAvailableFcn = {@store_plot_data,size_data};
serObJ.BytesAvailableFcnCount = size_data;
serObJ.InputBufferSize = 30000;
serObJ.BytesAvailableFcnMode = 'byte';
fopen(serObJ);
fprintf(serObJ, samples_cat);
if( strcmp(samples , '-1') == 1)
disp(' To Stop Data Acquisition enter "STOP" ');
prompt_stop = ' Enter "STOP" to stop acquiring : ';
stopCondition = input(prompt_stop,'s');
if( strcmp(stopCondition , 'STOP') == 1 )
disp('STOP requested');
fprintf(serObJ, stopCondition);
end
end
function store_plot_data(serialObject,event,chunk)
strRecv = fscanf(serialObject, '%s', chunk);
if( strcmp(strRecv(end), ',') == 1)
data_draw = [ data_draw dataStringTail strRecv ];
dataStringTail = [];
elseif( ( isempty(dataStringTail) == 0 ) && ( strcmp(strRecv(end), ',') == 0))
lastCommaIndex = max(strfind(strRecv,','));
% dataStringTail = strRecv(lastCommaIndex : end);
data_draw = [data_draw dataStringTail strRecv];
dataStringTail = [];
else
lastCommaIndex = max(strfind(strRecv,','));
dataStringTail = strRecv(lastCommaIndex : end);
data_draw = [data_draw strRecv(1 : lastCommaIndex - 1)];
end
Please note that as you have suggested me in my previous post I'm using callbacks to read the data continuously. When I'm giving the "STOP" command Matlab is not sending the "STOP" command to the serial port which does make sense, that whenever I'm acquiring data continuously, it never gets out of the callback function "store_plot_data", hence no command is being send. Could you please kindly help me in this issue as you have help me previously ? I'm also wondering does matlab has any NON-BLOCKING fprintf so that if I want to get the data from the user inside the callback function it won't wait indefinitely when no input is given by the user.
Thanks,
Swarnava Pramanik
Swarnava Pramanik le 25 Juin 2016
Modifié(e) : Swarnava Pramanik le 25 Juin 2016
Hi Walter,
In the above function "function store_plot_data(serialObject,event,chunk)" , I need to call this function within itself or else I'm loosing some samples. I checked using "serialObject.BytesAvailable" that after data acquisition has stopped and the serial port object has been closed there are some bytes left(corresponding to the missing samples) which are not being read but if I call the function within itself as shown in the code below then I'm able to get all the samples. Could you please kindly let me know why is this happening ? I'm attaching the code below :
function store_plot_data(serialObject,event,chunk)
%...after the main if block of the function
if( serialObject.BytesAvailable ~= 0)
store_plot_data(serialObject,event,chunk,handles);
end
Thanks,

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by