我是靠谱客的博主 迷你芹菜,最近开发中收集的这篇文章主要介绍MATLAB串口采集数据(进阶)+实时显示数据曲线+以当前计算机时间为名字保存数据为txt,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
注意:接收的16进制要符合一定格式,才能读取显示,具体见程序
分了五个.m文件(一个主函数,四个子函数):
main:
%%
clc;
clear;
global t1;
global t2;
global t3;
global t4;
global m1;
global m2;
global m31;
global m32;
global m41;
global m42;
global ii1;
global ii2;
global ii3;
global ii4;
t1=[0],t2=[0],t3=[0],t4=[0];
m1=[0],m2=[0],m31=[0],m32=[0],m41=[0],m42=[0];
ii1=[0],ii2=[0],ii3=[0],ii4=[0];
subplot(2,2,1);
p1 = plot(t1,m1);
ylabel('ref1');
grid on;
subplot(2,2,2);
p2=plot(t2,m2);
ylabel('ref2')
grid on;
subplot(2,2,3);
p31=plot(t3,m31);
hold on
p32=plot(t3,m32);
ylabel('test1');
grid on;
subplot(2,2,4);
p41=plot(t4,m41);
hold on;
p42=plot(t4,m42);
ylabel('test2');
%axis([x-200 x+200 0 200]);
grid on;
%%
%try
% s7=serial('com7');
%catch
% error('cant serial');
%end
s1=serial('com16');
s2=serial('com17');
s3=serial('com10');
s4=serial('com14');
set(s1,'BaudRate', 9600,'DataBits',8,'StopBits',1,'Parity','none','FlowControl','none','TimeOut',1,'BytesAvailableFcn',{@callback1,p1},'BytesAvailableFcnMode', 'byte','BytesAvailableFcnCount',10);
set(s2,'BaudRate', 9600,'DataBits',8,'StopBits',1,'Parity','none','FlowControl','none','TimeOut',1,'BytesAvailableFcn',{@callback2,p2},'BytesAvailableFcnMode', 'byte','BytesAvailableFcnCount',10);
set(s3,'BaudRate', 9600,'DataBits',8,'StopBits',1,'Parity','none','FlowControl','none','TimeOut',1,'BytesAvailableFcn',{@callback3,p31,p32},'BytesAvailableFcnMode', 'byte','BytesAvailableFcnCount',10);
set(s4,'BaudRate', 9600,'DataBits',8,'StopBits',1,'Parity','none','FlowControl','none','TimeOut',1,'BytesAvailableFcn',{@callback4,p41,p42},'BytesAvailableFcnMode', 'byte','BytesAvailableFcnCount',10);
%set(s, 'BytesAvailableFcn', {'instrcallback'});
%s.BytesAvailableFcnMode = 'terminator';
%set(s, 'TimeOut', 1);
%set(s, 'BytesAvailableFcnCount', 32);
%s7.BytesAvailableFcn = {@callback7,p7};
%set(s, 'BytesAvailableFcnMode', 'byte');
% fopen(s1);
% fopen(s2);
fopen(s3);
fopen(s4);
pause;
%test file name defination
name_ref1=['ref1',datestr(now,30),'.txt'];
name_ref2=['ref2',datestr(now,30),'.txt'];
name_test11=['pm11',datestr(now,30),'.txt'];
name_test12=['pm12',datestr(now,30),'.txt'];
name_test21=['pm21',datestr(now,30),'.txt'];
name_test22=['pm22',datestr(now,30),'.txt'];
%end test file name definataion
% open file
fid_ref1=fopen(name_ref1,'w');
fid_ref2=fopen(name_ref2,'w');
fid_test11=fopen(name_test11,'w');
fid_test12=fopen(name_test12,'w');
fid_test21=fopen(name_test21,'w');
fid_test22=fopen(name_test22,'w');
% end open file
%wtire file
fprintf(fid_ref1,'%drn',m1);
fprintf(fid_ref2,'%drn',m2);
fprintf(fid_test11,'%drn',m31);
fprintf(fid_test12,'%drn',m32);
fprintf(fid_test21,'%drn',m41);
fprintf(fid_test22,'%drn',m42);
%end write file
%close file
fclose(fid_ref1);
fclose(fid_ref2);
fclose(fid_test11);
fclose(fid_test12);
fclose(fid_test21);
fclose(fid_test22);
%end close file
fclose(s1);
fclose(s2);
fclose(s3);
fclose(s4);
close all;
sub1:
%%
function callback1(s1,BytesAvailable,p1)
global t1;
% global x;
global m1;
global ii1;
% out = fscanf(s);
% data = str2num(out)
out=fread(s1,10);
data1=out(6)*2^8+out(5);
t1 = [t1 ii1];
m1 = [m1 data1];
% set(p, 'XData',t,'YData',m(1,:));
set(p1, 'XData',t1,'YData',m1);
drawnow
% x = x + 1;
% axis([x-200 x+200 0 200]);
ii1=ii1+1;
end
sub2:
%%
function callback2(s2,BytesAvailable,p2)
global t2;
% global x;
global m2;
global ii2;
% out = fscanf(s);
% data = str2num(out)
out=fread(s2,10);
data2=out(6)*2^8+out(5);
t2 = [t2 ii2];
m2 = [m2 data2];
% set(p, 'XData',t,'YData',m(1,:));
set(p2, 'XData',t2,'YData',m2);
drawnow
% x = x + 1;
% axis([x-200 x+200 0 200]);
ii2=ii2+1;
end
sub3:
%%
function callback3(s3,BytesAvailable,p31,p32)
global t3;
% global x;
global m31;
global m32;
global ii3;
% out = fscanf(s);
% data = str2num(out)
out=fread(s3,10);
data31=out(3)*2^24+out(4)*2^16+out(5)*2^8+out(6);
data32=out(7)*2^24+out(8)*2^16+out(9)*2^8+out(10);
t3 = [t3 ii3];
m31 = [m31 data31];
m32 = [m32 data32];
% set(p, 'XData',t,'YData',m(1,:));
set(p31,'XData',t3,'YData',m31);
set(p32,'XData',t3,'YData',m32);
drawnow
% x = x + 1;
% axis([x-200 x+200 0 200]);
ii3=ii3+1;
end
sub4:
%%
function callback4(s4,BytesAvailable,p41,p42)
global t4;
% global x;
global m41;
global m42;
global ii4;
% out = fscanf(s);
% data = str2num(out)
out=fread(s4,10);
data41=out(3)*2^24+out(4)*2^16+out(5)*2^8+out(6);
data42=out(7)*2^24+out(8)*2^16+out(9)*2^8+out(10);
t4 = [t4 ii4];
m41 = [m41 data41];
m42 = [m42 data42];
% set(p, 'XData',t,'YData',m(1,:));
set(p41, 'XData',t4,'YData',m41);
set(p42,'XData',t4,'YData',m42);
drawnow
% x = x + 1;
% axis([x-200 x+200 0 200]);
ii4=ii4+1;
end
源文件见https://download.csdn.net/download/fei_yang_yf/10849236
最后
以上就是迷你芹菜为你收集整理的MATLAB串口采集数据(进阶)+实时显示数据曲线+以当前计算机时间为名字保存数据为txt的全部内容,希望文章能够帮你解决MATLAB串口采集数据(进阶)+实时显示数据曲线+以当前计算机时间为名字保存数据为txt所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复