Plot real time graph with data coming from serial port.
Mostrar comentarios más antiguos
Hi there,
I'm trying to plot data coming from the serial port in real time. In detail I receive temperature, humidity and current consumption from serial port. I want to plot the values into a unique figure using subplot. My main problem is the x values. I want to plot the y date referred to the time elapsed from the script start.
With my script I obtain wrong values plotted on x axis.
This is my script:
MATLAB code
clear all
close all
delete(instrfindall);
FSM_WAIT_START_S = 0;
FSM_READ_PAYLOAD = 1;
FSM_UPDATE_GRAPH = 3;
% Serial Port Settings
serialPort = serial( 'COM21' );
set( serialPort,'BaudRate', 9600 );
fopen( serialPort );
% Graph Settings
figure('units','normalized','outerposition',[0 0 1 1])
f1 = subplot( 3, 1, 1 );
title( 'Current consumption' );
ylabel( 'Current [A]' );
f1.XGrid = 'on';
f1.YGrid = 'on';
f1.YLim = [-5 5];
ani1 = animatedline('Color','r', 'LineWidth', 2 );
f2 = subplot( 3, 1, 2 );
title( 'Temperature' );
ylabel( 'T [°c]' );
f2.XGrid = 'on';
f2.YGrid = 'on';
f2.YLim = [0 50];
ani2 = animatedline('Color','b', 'LineWidth', 2);
f3 = subplot( 3, 1, 3 );
title( 'Humidity' );
ylabel( 'rH [%]' );
f3.XGrid = 'on';
f3.YGrid = 'on';
f3.YLim = [0 100];
ani3 = animatedline('Color','g', 'LineWidth', 2);
fsmState = FSM_WAIT_START_S;
charCnt = 0;
charBuffer = blanks(64);
secondsStartTime = second(datetime( 'now' ));
while(1)
% Read Characters from serial port
c = fscanf( serialPort, '%s', 1 );
switch( fsmState )
case FSM_WAIT_START_S
if( strcmp(c, 'S') == 1 )
cnt = 0;
fsmState = FSM_READ_PAYLOAD;
end
case FSM_READ_PAYLOAD
if( strcmp(c, 'E') == 1 )
fprintf( 'Data buffer: %s\n', charBuffer );
fsmState = FSM_UPDATE_GRAPH;
else
charBuffer( cnt + 1) = c;
cnt = cnt + 1;
end
case FSM_UPDATE_GRAPH
% Extract data
measures = strsplit( charBuffer, ';' );
current = str2double(measures(1, 3) );
temperature = str2double( measures(1, 4) );
humidity = str2double( measures(1, 5) );
if( temperature ~= 0 && humidity ~= 0 )
% Get current time
t = ceil((second(datetime( 'now' )) - secondsStartTime));
% Update axes
addpoints( ani1, datenum(t), current );
f1.XLim = datenum([t - seconds(30) t]);
datetick('x','keeplimits');
addpoints( ani2, datenum(t), temperature );
f2.XLim = datenum([t - seconds(30) t]);
datetick('x','keeplimits');
addpoints( ani3, datenum(t), humidity );
f3.XLim = datenum([t - seconds(30) t]);
datetick('x','keeplimits');
drawnow;
end
fsmState = FSM_WAIT_START_S;
end
end
Thanks in advance!
Respuestas (1)
Alberto Gontijo
el 5 de En. de 2021
0 votos
%MATLAB CODE TO PLOT VALUES FROM PORT IN REAL TIME
clc
clear all
close all
s1 = serial('COM26', 'BaudRate', 57600);
set(s1,'Terminator',35);
fopen(s1);
V=[];
val='';
time=200;
count=0;
f=0;
% **********************************************************************
% Read Serial values
tic
tt=toc;
while(tt<time)
val=fscanf(s1);
tt=toc;
%########################################################################
A=[];
B=[];
C=[];
b=[];
x=[];
j=1;
if(j<numel(val))
while(val(j)~='$')
a=val(j);
b=[b,a];
j=j+1;
end
A=[A,str2num(b)];
j=j+1;
b=[];
while(val(j)~='*')
a=val(j);
b=[b,a];
j=j+1;
end
B=[B,str2num(b)];
j=j+1;
b=[];
while(val(j)~='#')
a=val(j);
b=[b,a];
j=j+1;
end
C=[C,str2num(b)];
i=j;
b=[];
f=f+i;
end
x=[x,round(toc)];
if(~(isempty(A)&&isempty(B)&&isempty(C)))
subplot(1,2,1)
plot(x,A,'--rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','r','MarkerSize',5);
hold on % if u want this in different graph remove hold on and add "figure" command before each graph;
plot(x,B,'--rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',5);
grid on;
subplot(1,2,2)
plot(x,(B/(A+B))*100,'--rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','m','MarkerSize',5);
hold on
plot(x,C,'--rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','b','MarkerSize',5);
grid on;
pause(.01);
end
end
hold off
fclose(s1);
Categorías
Más información sobre Discrete Data Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!