i want to read the serial data using for loop... and want to get a real time data plot

4 vues (au cours des 30 derniers jours)
vishwash saxena
vishwash saxena le 25 Fév 2011
Commenté : RITESH KUMAR le 12 Sep 2019
function plot_Callback(hObject, eventdata, handles)
a1=0;
for i=1:1000
obj = serial('COM1');
set(obj,'BaudRate',9600)
fopen(obj); %open port
a = fscanf(obj); % read data into variable a
plot(a);
drawnow;
pause(0.5)
end
title('Axes 1');
xlabel('X data');
ylabel('Y data');
fclose(obj)
delete(obj);
clear obj;
will it work????... and do i need to create the object and set it for each run of for loop... and can u pls tell me what is the difference between com1 and com2?
  1 commentaire
Jan
Jan le 26 Fév 2011
Please cleanup your code. "a1" is not used. Omit everything, which does not cause troubles, e.g. title and labels. The readers will spend more time im helping you, if the loose less time in interpreting your code. Therefore a standard code formatting is recommended also: Start with two spaces, leave an empty line before the code.

Connectez-vous pour commenter.

Réponses (5)

Jan
Jan le 26 Fév 2011
COM1 is the first serial interface of your computer, COM2 is the second. Guess what COM3 is.
It is not clear , what you want to achieve. With some wild guessing I assume it will not work.
  • Open the serial object once only before the loop.
  • Does FSCANF work with serial objects in the shown way?
  • Perhaps "plot(a)" would work, but it is not clear, if it does, what you want. Please describe any details.
obj = serial('COM1');
set(obj,'BaudRate',9600)
fopen(obj); %open port
for i=1:1000
a = fscanf(obj); % ??? Does this work?
plot(a); % ??? Display the value of a at first
pause(0.5); % Calls DRAWNOW implicitely
end
  3 commentaires
Jan
Jan le 27 Fév 2011
I asked you to cleanup the posted code as comment to your question. I asked three questions here in this answer. I cannot see a reaction from your side. Therefore I assume the problem is not urgent. Good luck.
vishwash saxena
vishwash saxena le 27 Fév 2011
sorry jan ... i m not much familiar with matlab so was trying to figure it out...
here is the code ...
obj = serial('COM1') %creating the object
set(obj,'BaudRate',9600) %setting baudrate
fopen(obj); %open port
set(obj,'terminator','cr') %providing the terminator
a1=0; % a variable for y axis
time1=now; % time is in x axis
while 2>1 % infinite loop
time2=now;
x=[time1 time2];
ip_data = fscanf(obj) ;
a2= str2num(ip_data) ;
a=[a1 a2];
line(x,a);
datetick('x','HH:MM') %change the axis
pause(0.5);
a1=a2;
time1=time2;
end
now .. is it correct???
and can u pls tell me how fscanf was/is used in wrong way..

Connectez-vous pour commenter.


Paulo Silva
Paulo Silva le 26 Fév 2011
Here's a code that I made that plots the memory used by matlab in real time, I used some tips from the experts ;) adapt it to your needs.
function test
fig=figure;[user,sys] = memory;a=0;
set(fig,'CloseRequestFcn','delete(gcf);')
ax=axes;set(ax,'Ylimmode','manual');
h = plot(ax,NaN, NaN,'LineWidth',5);
set(ax,'Ylimmode','manual');
set(ax,'YLim',[0 sys.PhysicalMemory.Total/10^6])
ylabel('Memory used by matlab in Megabytes');
t = timer('TimerFcn',@readdata,...
'Period', 0.5,'executionmode','fixedSpacing');start(t)
function readdata(g1,gg1)
if (~ishandle(fig)),stop(t),return,end
m=memory;mem=m.MemUsedMATLAB;
set(h,'XData', [get(h, 'XData'), a],...
'YData', [get(h, 'YData'), mem/10^6]);
xd=get(h,'XData');yd=get(h,'YData');a=a+1;
if numel(xd)>=100
set(h,'XData',[xd(80:100)]);set(h,'YData',[yd(80:100)]);
end
end
end

vishwash saxena
vishwash saxena le 26 Fév 2011
i want to get the current signal as output from a sensor via serial communication and want to plot the value of current vs time.
that means like current= 4ma at time 3.45pm, current= 6.5 ma at time 3.55pm Comment on this Answer
how can i achieve it?????
  1 commentaire
Paulo Silva
Paulo Silva le 26 Fév 2011
Here's one example that you can use and adapt, it reads the value of the memory used by matlab each second for 100 seconds and plots it, instead of your time values you can use just a number composed of hour minute second for each reading.
CData=[];
for a=1:100
c = clock; %[year month day hour minute seconds]
m=memory;
CData=[CData;m.MemUsedMATLAB/10^6 str2num(sprintf('%d%d%d',c(4),c(5),c(6)))]
pause(1)
end
%In the end you can plot the data
plot(CData(:,2),CData(:,1))

Connectez-vous pour commenter.


vishwash saxena
vishwash saxena le 27 Fév 2011
sorry jan ... i m not much familiar with matlab so was trying to figure it out...
here is the code ...
obj = serial('COM1') %creating the object
set(obj,'BaudRate',9600) %setting baudrate
fopen(obj); %open port
set(obj,'terminator','cr') %providing the terminator
a1=0; % a variable for y axis
time1=now; % time is in x axis
while 2>1 % infinite loop
time2=now;
x=[time1 time2];
ip_data = fscanf(obj) ;
a2= str2num(ip_data) ;
a=[a1 a2];
line(x,a);
datetick('x','HH:MM') %change the axis
pause(0.5);
a1=a2;
time1=time2;
end
now .. is it correct???
and can u pls tell me how fscanf was/is used in wrong way..

Ankit Desai
Ankit Desai le 15 Avr 2011
You might also want to consider using an event based read from serial port rather than a while or for loop.
If you are getting a datastream on the serial port you might want to take a look at the example I put on file exchange that shows event based read using a TCPIP object and plotting the read data. You can change the code to use a serial object.
If you are getting data after a query you might want to take a look at this example on file exchange that shows how to get data & plot using query based interactions.
Hope this helps,
-Ankit

Catégories

En savoir plus sur Data Import and Analysis 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