資源描述:
《matlab實時接收uart數(shù)據(jù)畫圖》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、comPortInstrcallback.m文件內(nèi)容functioncomPortInstrcallback(obj,event)%數(shù)據(jù)包協(xié)議解析部分globalRecordBuffer,ReacordValid=0;DataByteLen=17000;%中斷緩存字節(jié)長度ValidWaveLen=1000;%波形數(shù)據(jù)的長度RecordLen=17;%uartDataBuf=zeros(1,DataByteLen,'uint8');%x=zeros(2,3,'int8');uartCurDataBuf=fread(obj,DataByteLen,
2、'uint8');WaveDataBuf=zeros(1,ValidWaveLen,'uint32');%接收的數(shù)據(jù)不夠一包數(shù)據(jù)的if(ReacordValid>0)tempBuf=zero(1,ReacordValid,'uint8');fori=1:ReacordValidtempBuf(i)=RecordBuffer(i);enduartDataBuf=[tempBuf;uartCurDataBuf];%組合數(shù)組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文件修改的內(nèi)容instrcallback.m原路徑在x:ProgramFilesMATLABR2008atoolboxmatlabiofun@instrument需要在原路徑中更改文件,復(fù)制到程序目錄是不能使用的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