Matlab实时绘制来自串口的多个数据

标签 matlab plot serial-port arduino

我的应用程序通过 ARDUINO UNO 平台从传感器读取数据,然后通过串行端口我设法在 MATLAB 中读取我需要的所有数据。所以现在我有 3 个数据要实时绘制(数据、数据 2、数据 3)在同一图形上

我还设法用我在 mathworks 上找到的一些代码一次绘制一个数据并对其进行了一些修改,这不适合我的项目。

这是我用来绘制其中一个数据的 matlab 代码:

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 个数据(数据、数据 2、数据 3)。请帮帮我。

最佳答案

我实际上在我的一个项目中使用了与您相同来源的代码。如果您仍然需要帮助,这里是我用来绘制来自加速度计的三条数据线的代码的修改版本。它的情节很漂亮。但是,由于执行 fscanf() 所花费的时间,您无法在没有混叠的情况下以快于每 50 毫秒左右的速度实时获取和绘制信号。

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实时绘制来自串口的多个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24910647/

相关文章:

performance - 如何加速matlab中的双循环

matlab - MATLAB 是否优化 diag(A*B)?

r - R 中的堆积条形图

c - 使用gpsd/libgps C获得gps时间

c - 使用 NETMF 的 SPI 主机到 PIC18F4550 从机同步 (C18)

java - 在组合框中创建选项作为可用串行端口

java - 如何应用补丁

database - MATLAB : why am I not getting the SQL result back in the cursor object?

r - 如何绘制按因子分组的数据,但不是作为箱线图

matlab - MATLAB 中的 xkcd 样式图