필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
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
Jan
Jan 2011년 2월 26일
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.

댓글을 달려면 로그인하십시오.

답변 (5개)

Jan
Jan 2011년 2월 26일
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
Jan
Jan 2011년 2월 27일
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 2011년 2월 27일
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..

댓글을 달려면 로그인하십시오.


Paulo Silva
Paulo Silva 2011년 2월 26일
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 2011년 2월 26일
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
Paulo Silva
Paulo Silva 2011년 2월 26일
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))

댓글을 달려면 로그인하십시오.


vishwash saxena
vishwash saxena 2011년 2월 27일
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 2011년 4월 15일
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

카테고리

Help CenterFile Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by