我正在使用下面的代码实时(实时)绘制来自 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