【问题标题】:Matlab real time ploting multiple data from serial portMatlab实时绘制来自串口的多个数据
【发布时间】:2014-07-23 12:36:07
【问题描述】:

我的应用程序通过 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 个数据(data、data2、data3)。请帮帮我。

【问题讨论】:

标签: matlab plot serial-port arduino


【解决方案1】:

我实际上在我的一个项目中使用了与您相同的来源的代码。如果您仍然需要帮助,这是我用来绘制加速度计的三个数据线的代码的修改版本。它的情节很漂亮。但是,由于执行 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;

【讨论】:

  • 你的代码中输入的格式是什么?加速度计的三轴值。
【解决方案2】:

使用line 函数在同一个图上再创建一条线。
从帮助->MatLab->图形->处理图形对象->高级与低级

如果您第二次调用 line 函数,MATLAB 会在当前坐标区中绘制第二条线,而不删除第一条线。

【讨论】:

    【解决方案3】:

    只需使用以下内容:

    plotGraph = plot(time(time > time(count) - scrollWidth), data(time > time(count) - scrollWidth),...
    time(time > time(count) - scrollWidth), data2(time > time(count) - scrollWidth),...
    time(time > time(count) - scrollWidth),data3(time > time(count) - scrollWidth));
    grid on;    
    axis([time(count) - scrollWidth time(count) min max]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-31
      • 2019-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-28
      • 1970-01-01
      相关资源
      最近更新 更多