Matlab实时绘制来自串行端口的多个数据 [英] Matlab real time ploting multiple data from serial port
问题描述
我的应用程序通过ARDUINO UNO平台从传感器读取数据,然后通过串行端口读取数据,我设法在MATLAB中读取了我需要的所有数据.所以现在我有3个要实时绘制的数据(data,data2,data3)在同一图形上.
My application reads data from sensors trough an ARDUINO UNO platform and then trough serial port I managed to read all the data that I need in MATLAB. so now I have 3 datas that I want to plot (data, data2, data3) in real time ON THE SAME GRAPHIC.
我还设法一次用我在mathworks上找到的代码绘制了一个数据,并对其进行了一些修改,这不适合我的项目.
I also managed to plot one data at a time with some code I found on mathworks and modified it a bit, which does not suit my project.
这是我用来绘制数据之一的matlab代码:
Here is the matlab code that I'm using to plot one of the data:
clear
clc
%User Defined Properties
serialPort = 'COM7'; % define COM port #
baudeRate = 115200;
plotTitle = 'Serial Data Log'; % plot title
xLabel = 'Elapsed Time (s)'; % x-axis label
yLabel = 'Data'; % y-axis label
plotGrid = 'on'; % 'off' to turn off grid
min = -200; % set y-min
max = 200; % set y-max
scrollWidth = 10; % display period in plot, plot entire data log if <= 0
delay = .01; % make sure sample faster than resolution
%Define Function Variables
time = 0;
data = 0;
data2 = 0;
data3 = 0;
count = 0;
%Set up Plot
plotGraph = plot(time,data,time,data2,time,data3);
title(plotTitle,'FontSize',25);
xlabel(xLabel,'FontSize',15);
ylabel(yLabel,'FontSize',15);
axis([0 10 min max]);
grid(plotGrid);
%Open Serial COM Port
s = serial(serialPort, 'BaudRate',baudeRate)
disp('Close Plot to End Session');
fopen(s);
tic
while ishandle(plotGraph) %Loop when Plot is Active
dat = fscanf(s,'%f'); %Read Data from Serial as Float
if(~isempty(dat) && isfloat(dat)) %Make sure Data Type is Correct
count = count + 1;
time(count) = toc; %Extract Elapsed Time
data(count) = dat(1); %Extract 1st Data Element
data2(count) = dat(2);
data3(count) = dat(3);
data(count);
data2(count);
data3(count);
%Set Axis according to Scroll Width
if(scrollWidth > 0)
set(plotGraph,'XData',time(time > time(count)-scrollWidth),'YData',data3(time > time(count)-scrollWidth));
%plot(time(time > time(count)-scrollWidth),data3(time > time(count)-scrollWidth));
axis([time(count)-scrollWidth time(count) min max]);
%set(plotGraph,'XData',time(time > time(count)-scrollWidth),'YData',data3(time > time(count)-scrollWidth));
%axis([time(count)-scrollWidth time(count) min max]);
else
set(plotGraph,'XData',time,'YData',data);
axis([0 time(count) min max]);
end
%Allow MATLAB to Update Plot
pause(delay);
end
end
%Close Serial COM Port and Delete useless Variables
fclose(s);
clear count dat delay max min baudRate plotGraph plotGrid plotTitle s ...
scrollWidth serialPort xLabel yLabel;
disp('Session Terminated...');
我需要在图形上用不同的颜色绘制所有3个数据(data,data2,data3).请在这里帮助我.
I need to plot all the 3 datas (data, data2, data3) on the graphic with different colors. Please help me out here.
推荐答案
我实际上使用了与我的项目之一相同的源代码.如果您仍然需要帮助,这是我用来从加速度计绘制三条数据线的代码的修改后的版本.它绘制精美.但是,由于执行fscanf()需要花费时间,因此如果没有混叠,就无法实时获得和绘制信号,其速度要比每50ms左右的时间更快.
I actually used the code from the same source you are for one of my projects. If you still need assistance, here is my modified version of the code I used to plot three data lines from an accelerometer. It plots beautifully. However, because of the time it takes to do fscanf(), you can't obtain and plot a signal in real time faster than every 50ms or so without aliasing.
clear
clc
%User Defined Properties
serialPort = 'COM7'; % define COM port #
plotTitle = 'Serial Data Log'; % plot title
xLabel = 'Elapsed Time (s)'; % x-axis label
yLabel = 'Acceleration'; % y-axis label
plotGrid = 'on'; % 'off' to turn off grid
min = -1.5; % set y-min
max = 2.5; % set y-max
scrollWidth = 10; % display period in plot, plot entire data log if <= 0
delay = .0000001; % make sure sample faster than resolution
%Define Function Variables
time = 0;
data = zeros(3,1);
count = 0;
%Set up Plot
plotGraph = plot(time,data(1,:),'-r',...
'LineWidth',2,...
'MarkerFaceColor','w',...
'MarkerSize',2);
hold on
plotGraph1 = plot(time,data(2,:),'-m',...
'LineWidth',1,...
'MarkerFaceColor','w',...
'MarkerSize',2);
hold on
plotGraph2 = plot(time,data(3,:),'-b',...
'LineWidth',1,...
'MarkerFaceColor','w',...
'MarkerSize',2);
title(plotTitle,'FontSize',25);
xlabel(xLabel,'FontSize',15);
ylabel(yLabel,'FontSize',15);
axis([0 10 min max]);
grid(plotGrid);
%Open Serial COM Port
s = serial(serialPort, 'BaudRate', 115200)
disp('Close Plot to End Session');
fopen(s);
tic
while ishandle(plotGraph) && ishandle(plotGraph2) && ishandle(plotGraph1) %Loop when Plot is Active
dat = fscanf(s,'%f'); %Read Data from Serial as Float
if(~isempty(dat) && isfloat(dat)) %Make sure Data Type is Correct
count = count + 1;
time(count) = toc; %Extract Elapsed Time in seconds
data(:,count) = dat(:,1); %Extract 1st Data Element
%Set Axis according to Scroll Width
if(scrollWidth > 0)
set(plotGraph,'XData',time(time > time(count)-scrollWidth),...
'YData', data(3,time > time(count)-scrollWidth));
set(plotGraph1,'XData',time(time > time(count)-scrollWidth),...
'YData', data(2,time > time(count)-scrollWidth));
set(plotGraph2,'XData',time(time > time(count)-scrollWidth),...
'YData', data(1,time > time(count)-scrollWidth));
axis([time(count)-scrollWidth time(count) min max]);
else
set(plotGraph,'XData',time,'YData',data(3,:));
set(plotGraph1,'XData',time,'YData',data(2,:));
set(plotGraph2,'XData',time,'YData',data(1,:));
axis([0 time(count) min max]);
end
%Allow MATLAB to Update Plot
pause(delay);
end
end
%Close Serial COM Port and Delete useless Variables
fclose(s);
clear count dat delay max min plotGraph plotGraph1 plotGraph2 plotGrid...
plotTitle s scrollWidth serialPort xLabel yLabel;
disp('Session Terminated');
prompt = 'Export Data? [Y/N]: ';
str = input(prompt,'s');
if str == 'Y' || strcmp(str, ' Y') || str == 'y' || strcmp(str, ' y')
%export data
csvwrite('accelData.txt',data);
type accelData.txt;
else
end
clear str prompt;
这篇关于Matlab实时绘制来自串行端口的多个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!