How can i plot graph of the data coming from serial comm

3 views (last 30 days)
i have try a lot to get plotted the multiple data on graph but was not successfull if any one have a sample program to guide me..
  3 Comments
Chris Barnhart
Chris Barnhart on 24 Jan 2015
Not sure what the question is. Are you able to receive data from the serial port (and turn it into numbers)? Or is the problem related to plotting?

Sign in to comment.

Answers (1)

Gaurav Khadasane
Gaurav Khadasane on 27 Jan 2015
Edited: Gaurav Khadasane on 27 Jan 2015
if true
s = serial('com4');
%set(s, ‘ Terminator’, ‘LF’); % Default terminator is \n
set(s,'BaudRate', 9600);
set(s,'DataBits', 8);
set(s,'StopBits', 1);
fopen(s);
s.ReadAsyncMode = 'continuous';
% Various variables numberOfDatas = 20;
i = 1; d = 0;
% Main graph figure figure(1); hold on;
% Start asynchronous reading
readasync(s);
while(i<=numberOfDatas)
if((fscanf(s,'%c',1)) == '#')
d = [fscanf(s,'%c',3)];
end
% Plot the data
figure(1);
subplot(2,2,1);
plot(i,d,'--rs','LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',10)
axis([0 20 0 1000])
if(i>20)
xlim([i-10 i]);
set(gca,'xtick',[i-10 i-9 i-8 i-7 i-6 i-5 i-4 i-3 i-2 i-1 i])
end
subplot(2,2,2);
plot(i, data(i), '*');
axis([0 20 0 1000])
hold on;
if(i>20)
xlim([i-10 i]);
set(gca,'xtick',[i-10 i-9 i-8 i-7 i-6 i-5 i-4 i-3 i-2 i-1 i])
end
subplot(2,2,3);
plot(i, data(i), '*');
axis([0 20 0 1000])
% Ensure there are always 10 tick marks on the graph
if(i>20)
xlim([i-10 i]);
set(gca,'xtick',[i-10 i-9 i-8 i-7 i-6 i-5 i-4 i-3 i-2 i-1 i])
end
subplot(2,2,4);
plot(i, data(i), '*');
axis([0 20 0 1000])
% Ensure there are always 10 tick marks on the graph
if(i>20)
xlim([i-10 i]);
set(gca,'xtick',[i-10 i-9 i-8 i-7 i-6 i-5 i-4 i-3 i-2 i-1 i])
end
% Draw and flush
drawnow;
%Increment the counter
i=i+1;
end % Give the external device some time… pause(3);
% Some of these crash the program – it depends. The serial port is left % open, which is not good. %stopasync(s); fclose(s); % bad %delete(s); %clear s; end
in this code i try to plot the 4 subplot but i m not able to get the logic how to distinguish the 4 different data coming from the serial comm and plot it.

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!