串口保存数据为txt格式:
clc ; %死机的时候,在命令窗口输入return
%getcom=instrhwinfo ('serial') %寻找串口
delete(instrfindall) %这句话必须有
global xx;
global nn;
global data1;
global s;
xx=0;
nn=500;
data1(1:nn)=0;
s=serial('COM8');
set(s,'BaudRate',115200,'DataBits',8,'StopBits',1,'Parity','none','TimeOut',1,'FlowControl','none');
s.BytesAvailableFcnMode='terminator';
s.BytesAvailableFcn='callback';
fopen(s) ;
fwrite(s,8) ; % fwrite发送二进制数 8
% out=fscanf(s); % fscanf接收字符串,以\r\n结束
% disp(out);
pause; %不然就跳出去了
dlmwrite('result.txt',data1,'delimiter','\t')
fclose(s) ;
delete(s) ;
clear s ;
回调函数:
function callback
global s;
global nn;
global data1;
out=fscanf(s);
% disp(out);
% yy = regexp(out, '\s+', 'split');%按照空格分隔字符串,成为单个cell
% ww=str2double(yy(2));
% data1(1:nn)=[data1(2:nn), ww];
data1(1:nn)=[data1(2:nn), str2double(out)];
plot(data1);
axis([0,nn+1,500,3500]);
pause(0.001);
fwrite(s,8) ; % fwrite发送二进制数 8
end