|
改一下串口号即可
clc;
clear all;
close all;
delete(instrfindall);
global h;
h = animatedline('MaximumNumPoints',1000); %1000这个值是显示的个数,可以改
h.Color = 'r';
h.LineStyle = '-.';
h.LineWidth = 1.0;
try
s = serial('com6'); %改串口号
catch
error('cant serial');
end
set(s,'BaudRate',115200,'DataBits',8,'StopBits',1,'Parity','none','FlowControl','none');
s.BytesAvailableFcnCount = 50;
s.BytesAvailableFcnMode = 'byte';
s.BytesAvailableFcn = {@callback};
fopen(s);
pause; % 暂停函数
fclose(s);
delete(s);
%fclose(fid);
clear s;
clear all;
function callback(s,BytesAvailable)
global h;
out = fscanf(s);
fprintf('%s',out(1:end-1));
outdata= str2num(fscanf(s));
if outdata(1) == 99 && outdata(2) == 11 && outdata(11) == 22 && outdata(12) == 88 %判断帧头与帧尾
%如串口数据
% 99 11 1 8 0 2 949.77 1880.63 949.65 1874.3 22 88
% 99 11 1 8 0 2 944.4 1888.62 942.35 1884.66 22 88
addpoints(h,outdata(7),outdata(8));%显示第七第八列
drawnow;
end
end
|
|