function callback(s,BytesAvailable,p) global i; global t; global x; global m;% p = plot(t,m,… % 'EraseMode','background','MarkerSize',5); out = fscanf(s) %read data from s
data = str2num(out); if data>-2 & data < 2 %detect if data is legal % disp('*******data comes! \n'); t=[t 0.01*2*pi*i]; %0 0.1 0.2 0.3.. %Matrix 1*(i+1), column is increasing m=[m data]; %Matrix 2*(i+1), 2row, i+1 column. %http://blog.csdn.net/sonictl %set(p(1),'XData',t,'YData',m(1,:)) %set(p(2),'XData',t,'YData',m(2,:)) set(p,'XData',t,'YData',m(1,:)) %http://blog.csdn.net/sonictl drawnow x = x + 3.6/360*2*pi; % x = x+0.1 axis([x x+2*pi -1.5 1.5]); pause(0.1); i = i+1; end %http://blog.csdn.net/sonictl end</pre><br>
MATLAB动画显示串口数据【实例】
经过几天的学习,现在能实现串口数据的实时plot并记录在MATLAB变量中。
After a few days' learning, we can implement thatreal-time plot the data from serial port.
先看一下演示的视频吧:(see demo video)http://v.youku.com/v_show/id_XNDk2OTA1OTQw.html
升级版(双数据接收,绘图):http://v.youku.com/v_show/id_XNDk3NzM0NzE2.html
The outline of the video:
演示视频大纲:
1.介绍系统构成
2.介绍板载C语言程序
3.用Linux界面演示由MATLAB发送给目标板的命令,及理论返回值。
4.介绍MATLAB本实例的m文件的主要功能,发送的命令,判断,画图
5.演示整个MATLAB程序的实现效果
完成这个实例需要学习“MATLAB串口操作官方教程.pdf” 和 "Matlab中使用Plot函数动态画图方法总结", 请点击。
To finish this subject, we need firstly learn"Official tutorial for MATLAB serial port operation "
板卡产生数据源程序(C语言): The source code for generating original data on the TLL main board.
--------------------#include"stdio.h" #include"math.h" #include"string.h"#define pi 3.1415926
void delay(int a){ int i,j,delay; for(i = 0 ; i < 1000a; i++){ delay = 0; for(j = 0; j < 1000a; j++){ delay = delay+1; } } }
int main(){ float angle, x, y,rad; int i,N,dltime; angle = 0; //getchar(); printf("input the num of circles, delay time: "); scanf("%d,%d", &N, &dltime); for (i = 0; i < 100N ; i++){ angle = angle + 3.6; rad = angle/180pi; x = cos(rad); y = sin(rad); printf("%f \n", x); delay(dltime); } return 0;
}
--------------------------------------------
MATLAB 接收数据主程序serial-test.m:
The main program for receiving data in MATLAB:
clc;%% % Use EraseMode,draw dynamically. Change axis dynamically. % Multi-Line drawing line global i; global t; global x; global m; t = [0]; % initialize m = [0]; % initialize i = 0; x = -pi ;
p = plot(t,m,… 'EraseMode','background','MarkerSize',5); axis([x x+2*pi -1.5 1.5]); % x = [-1.5 pi, 0.5 pi] grid on;
%%
s = serial('COM3'); % creat serial port obj set(s,'BaudRate',115200,'DataBits',8,'StopBits',1,… 'Parity','none','FlowControl','none'); % set properties for serial
s.BytesAvailableFcnMode = 'terminator'; % byte number or terminator s.BytesAvailableFcn = {@callback,p}; % {@mycallback,time}
fopen(s);
fprintf(s,'cd /home'); pause(1); % pause function, 1s fprintf(s,'./matlab-serial'); pause; % pause function fprintf(s,'1,1');
pause; %right here is receiving the data and plot fclose(s); delete(s) clear s close all; clear all;
MATLAB 接收数据,判断,动态绘图,回调函数 callback.m:
The Callback function for receiving, judge, plotting in MATLAB.
Explore more: Real-time plotting in MATLAB for the Three axis accelerometer data
三轴力传感器数据在MATLAB中实时绘图显示:
http://blog.csdn.net/sonictl/article/details/8495973
--------------------------------------------
MATLAB动画显示串口数据【实例】(修正链接)
comments powered by Disqus