资源描述:
《matlab实时接收uart数据画图》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、comPortInstrcallback.m文件内容functioncomPortInstrcallback(obj,event)%数据包协议解析部分globalRecordBuffer,ReacordValid=0;DataByteLen=17000;%中断缓存字节长度ValidWaveLen=1000;%波形数据的长度RecordLen=17;%uartDataBuf=zeros(1,DataByteLen,'uint8');%x=zeros(2,3,'int8');uartCurDataBuf=fread(obj,DataByteLen,
2、'uint8');WaveDataBuf=zeros(1,ValidWaveLen,'uint32');%接收的数据不够一包数据的if(ReacordValid>0)tempBuf=zero(1,ReacordValid,'uint8');fori=1:ReacordValidtempBuf(i)=RecordBuffer(i);enduartDataBuf=[tempBuf;uartCurDataBuf];%组合数组elseuartDataBuf=[uartCurDataBuf];endj=1;k=0;fori=1:DataByteLenif
3、(uartDataBuf(i)==255&&uartDataBuf(i+1)==255)%查找包头if(DataByteLen-i>=(RecordLen-1))WaveDataBuf(j)=65536*uartDataBuf(i+2)+256*uartDataBuf(i+3)+uartDataBuf(i+4);j=j+1;elsefork=1:(DataByteLen-i)RecordBuffer(k)=uartDataBuf(i+k);endbreak;endendendif(k>0)ReacordValid=k;elseReacordVa
4、lid=0;endplot(WaveDataBuf,'r'),gridon;Instrcallback.m文件修改的内容instrcallback.m原路径在x:ProgramFilesMATLABR2008atoolboxmatlabiofun@instrument需要在原路径中更改文件,复制到程序目录是不能使用的functioninstrcallback(obj,event)comPortInstrcallback(obj,event);%添加的代码%INSTRCALLBACKDisplayeventinformationfo
5、rtheevent.%%INSTRCALLBACK(OBJ,EVENT)displaysamessagewhichcontainsthe%typeoftheevent,thetimeoftheeventandthenameofthe%objectwhichcausedtheeventtooccur.%%Ifanerroreventoccurs,theerrormessageisalsodisplayed.%Ifapineventoccurs,thepinthatchangedvalueandthepin%valueisalsodisplayed
6、.%%INSTRCALLBACKisanexamplecallbackfunction.Usethiscallback%functionasatemplateforwritingyourowncallbackfunction.%%Example:%s=serial('COM1');%set(s,'OutputEmptyFcn',{'instrcallback'});%fopen(s);%fprintf(s,'*IDN?','async');%idn=fscanf(s);%fclose(s);%delete(s);%%MP2-24-00%Copy
7、right1999-2004TheMathWorks,Inc.%$Revision:1.4.4.3$$Date:2004/01/1620:03:49$%Defineerrormessage.error1='Type''helpinstrumentinstrcallback''foranexampleusingINSTRCALLBACK.';error1Id='MATLAB:instrument:instrcallback:invalidSyntax';switchnargincase0error(error1Id,sprintf(['This
8、functionmaynotbecalledwith0inputs.',...'Type''helpinstrumentinstrcallbac