MATLAB从串口滞后实时绘图

时间:2016-03-26 18:12:47

标签: matlab plot mbed

我使用下面的代码实时绘制Cortex M3微控制器的五个模拟输入(实时)。 我注意到经过一段时间(几秒钟)之后,情节正在响应。滞后大约10秒,显然它不断增加。 当我在下面的代码中评论星号之间的线条时,我注意到滞后停止了。由于微控制器模块上的LED以恒定速率闪烁,因此注意到了这一点。为了确认它不是来自微控制器方面,我还使用了没有注意到问题的Putty。所以我假设信号的绘图是造成问题的原因。有谁知道为什么会这样?我能做些什么来解决这个问题?

以下是使用的代码:

% Reference: https://developer.mbed.org/cookbook/Interfacing-with-Matlab (SEE 'Serial Communication' SECTION)

delete(instrfindall);             % if any port is already opened by MATLAB its gonna find and close it

TIMEOUT = 5;    %time to wait for data before aborting
XPOINTS = 1000;   %number of points along x axis

try   % this is the try/catch to Handle Errors

    %create serial object to represent connection to mbed
    mbed = serial('COM5', ...
                  'BaudRate', 9600, ...
                  'Parity', 'none', ...
                  'DataBits', 8, ...
                  'StopBits', 1);       %change depending on mbed configuration

    set(mbed,'Timeout',TIMEOUT);        %adjust timeout to ensure fast response when mbed disconnected

    fopen(mbed);        %open serial connection

    position = 1;       %initialise graph variables
    time = 1;
    x = [(1:XPOINTS)' (1:XPOINTS)' (1:XPOINTS)' (1:XPOINTS)' (1:XPOINTS)'];
    xlabels = (1:XPOINTS);
    y = zeros(XPOINTS,5);

    while(1)

        values = fscanf(mbed, '%d,%d,%d,%d,%d');    %get values from serial into vector 

        y(position,:) = values';    %put into y to be displayed

        %update position on x-axis and x-axis labels
        xlabels(position) = time;
        time = time + 1;
        if (position < XPOINTS)
            position = position + 1;
        else
            position = 1;
        end

        %*******************************************************************************************
        %display
        plot(x,y);
        set(gca, 'XTick', 1:XPOINTS);
        set(gca, 'XTickLabel', xlabels);

        drawnow;    %this is required to force the display to update before the function terminates
        %*******************************************************************************************

    end

    fclose(mbed);   %close connection
    delete (mbed)   % deleting serial port object
    clear mbed                               
    clear all

catch
    %in case of error or mbed being disconnected
    disp('Failed!');
    fclose(mbed);   %close connection to prevent COM port being lokced open
    delete (mbed)                             % deleting serial port object
    clear mbed                               
    clear all
end 

0 个答案:

没有答案