Hello how can i plot data real time in a gui?
The code below keeps blocking.I want when i press the close_port_btn the reading to stop. Since i have seen there is no function to get the serial status of the port i created that flag. This flag is intended to stop the reading. Somehow it seems the code won't exit that part of code. Do i need to do multithreading? And if so how can i achieve that?
function read_btn_Callback(hObject, eventdata,handles)
% hObject handle to read_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global port;
fopen(port);
handles.flag=1;
N=100;
y=zeros(N,1);
t=linspace(0,N,1);
while (handles.flag~=0)
y(1:end-1)=y(2:end);
x=fscanf(port,'%d')/100
plot(y,'LineWidth',2);
grid on;
axis([0 100 0 50])
end
guidata(hObject,handles);
function close_port_btn_Callback(hObject, eventdata, handles)
% hObject handle to close_port_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global port;
fclose(port);
handles.flag=0;
guidata(hObject,handles);

 Respuesta aceptada

Walter Roberson
Walter Roberson el 11 de Abr. de 2016
function read_btn_Callback(hObject, eventdata,handles)
% hObject handle to read_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global port;
fopen(port);
handles.flag=1;
guidata(hObject, handles);
N=100;
y=zeros(N,1);
t=linspace(0,N,1);
while true
drawnow();
handles = guidata(hObject);
if handles.flag == 0
break;
end
y(1:end-1)=y(2:end);
x=fscanf(port,'%d')/100
plot(y,'LineWidth',2);
grid on;
axis([0 100 0 50])
end
fclose(port);
function close_port_btn_Callback(hObject, eventdata, handles)
% hObject handle to close_port_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.flag=0;
guidata(hObject,handles);

3 comentarios

Adrian Bercovici
Adrian Bercovici el 11 de Abr. de 2016
Thank you for your answer.Indeed it stops blocking if you press close port but it won't plot.
Walter Roberson
Walter Roberson el 11 de Abr. de 2016
You initialize your y to N zeros's and in your loop you shrink y by one element each pass through the loop. An all-zero line could easily be mistaken as not being there.
You read data into x in your loop but you do not store or plot that x.
I added a pause and it seems it did the trick however i get this error after closing the port.
I want the reading to be dependent on the status of the port ,handles.port_stt.
*
Error using serial/fscanf (line 154)
Unsuccessful read: OBJ must be connected to the hardware with FOPEN.*
function open_port_btn_Callback(hObject, eventdata, handles)
global myport;
%----------------------Port opening-----------------------%
%
handles=guidata(hObject);
if(~strcmp(handles.port_stt,'opened'))
fopen(myport);
handles.port_stt='opened';
guidata(hObject,handles);
end
% --- Executes on button press in close_port_btn.
function close_port_btn_Callback(hObject, eventdata, handles)
global myport;
%----------------------Port closing-----------------------%
handles=guidata(hObject);
if(~strcmp(handles.port_stt,'closed'))
fclose(myport);
handles.port_stt='closed';
guidata(hObject,handles);
end
% --- Executes on button press in read_btn.
function read_btn_Callback(hObject, eventdata, handles)
global myport;
handles=guidata(hObject);
while(strcmp(handles.port_stt,'opened'))
handles=guidata(hObject);
x=fscanf(myport,'%f')/100
pause(0.0001);
end

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by