加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

matlab读取串口数据并显示曲线[转载]

(2014-01-20 20:24:24)
分类: Math_and_Matlab
本文转载自:http://blog.csdn.net/qwertyuj/article/details/12108155

matlab程序读取单片机通过串口传来的IMU角度数据,并用曲线显示出来。

上传的值为float型,每个数据后有回车符。范围90~-90

 

主文件 serial_test2.m

[plain] view plaincopy
  1. %%  
  2. clc;  
  3.   
  4. global t;  
  5. global x;  
  6. global m;  
  7. global ii;  
  8.   
  9. [0];  
  10. [0];  
  11. ii 0;  
  12. -100;  
  13. plot(t,m,'EraseMode','background','MarkerSize',5);  
  14. axis([x-200 x+200 -90 90]);  
  15. grid on;  
  16.   
  17. %%  
  18.   
  19. try  
  20.     s=serial('com4');  
  21. catch  
  22.     error('cant serial');  
  23. end  
  24. set(s,'BaudRate', 115200,'DataBits',8,'StopBits',1,'Parity','none','FlowControl','none');  
  25. s.BytesAvailableFcnMode 'terminator';  
  26. s.BytesAvailableFcn {@callback,p};  
  27.   
  28. fopen(s);  
  29.   
  30. pause;  
  31. fclose(s);  
  32. delete(s);  
  33. clear  
  34. close all;  
  35. clear all;  

 

回调函数 callback.m

[plain] view plaincopy
  1. %%  
  2. function callback(s, BytesAvailable,p)  
  3.       
  4.     global t;  
  5.     global x;  
  6.     global m;  
  7.     global ii;  
  8.   
  9.     out fscanf(s);  
  10.     data str2num(out)  
  11.       
  12.     [t ii];  
  13.     [m data];  
  14.     set(p, 'XData',t,'YData',m(1,:));  
  15.       
  16.     drawnow  
  17.     1;  
  18.     axis([x-200 x+200 -90 90]);  
  19.     ii=ii+1;  
  20. end  

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有