就是接收到串口数据后解析,用图形显示出来波形,窗口一直在移动,只显示最近的几个数据,原来的显示是所有数据都显示在一个窗口上,数据越多,波形越不清晰,现在是只显示最新的几个数据
%%
clc;
clear;
global t1;
global t2;
global m1;
global m2;
global ii1;
global ii2;
t1=[0],t2=[0];
m1=[0],m2=[0];
ii1=[0],ii2=[0];
subplot(2,1,1);
p11 = plot(t1,m1);
xlabel('mpu6050-data1');
grid on;
subplot(2,1,2);
p12=plot(t2,m2);
xlabel('mpu6050-data2')
grid on;
s1=serial('com17');
set(s1,'BaudRate', 115200,'DataBits',8,'StopBits',1,'Parity','none','FlowControl','none','TimeOut',1,'BytesAvailableFcn',{@callback1,p11, p12},'BytesAvailableFcnMode', 'byte','BytesAvailableFcnCount',10);
fopen(s1);
pause;
name_ref1=['mpu6050_data1',datestr(now,30),'.txt'];
name_ref2=['mpu6050_data2',datestr(now,30),'.txt'];
%end test file name definataion
% open file
fid_ref1=fopen(name_ref1,'w');
fid_ref2=fopen(name_ref2,'w');
% end open file
%wtire file
fprintf(fid_ref1,'%d\r\n',m1);
fprintf(fid_ref2,'%d\r\n',m2);
%end write file
%close file
fclose(fid_ref1);
fclose(fid_ref2);
%end close file
fclose(s1);
close all;
%%
function callback1(s1,BytesAvailable,p11, p12)
global t1;
global m1;
global ii1;
global t2;
global m2;
global ii2;
% out=fread(s1,10);
% data1=(out(6)*2^8+out(5))/ 16384 * 10;
% data2=(out(8)*2^8+out(7))/ 16384 * 10;
out=fread(s1,10);
data1=(out(6)*2^8+out(5))/ 16384 * 10;
data2=(out(8)*2^8+out(7))/ 16384 * 10;
ii1=ii1+1;
t1 = [t1 ii1];
m1 = [m1 data1];
if (length(t1)) > 500
t1 = t1(:,2:end);%数组长度一旦超过50就丢弃第1列的值
end
if (length(m1)) > 500
m1 = m1(:,2:end);
end
set(p11, 'XData',t1,'YData',m1);
%%
ii2=ii2+1;
t2 = [t2 ii2];
m2 = [m2 data2];
if (length(t2)) > 500
t2 = t2(:,2:end);%数组长度一旦超过50就丢弃第1列的值
end
if (length(m2)) > 500
m2 = m2(:,2:end);
end
set(p12, 'XData',t2,'YData',m2);
drawnow
end