如何在Matlab gui中保存串口数据到数组



我想每0.1s读取一个串行端口,并将传入的数据附加到数组中,这次我可以显示数据,但数组似乎只存储最新的数据。有人能告诉我为什么吗?谢谢。下面是一些代码:

function wtsMat_OpeningFcn(hObject, eventdata, handles, varargin)
.....
%%tact is the array to store data
tact=ones(1,84);
handles.tact=tact;
% Update handles structure
guidata(hObject, handles);

scom=serial(com_cur,'BaudRate',baud_curNum,'Parity','none','DataBits',8,'StopBits',1,...
    'InputBufferSize',1000,...
    'TimeOut',1,...
    'TimerPeriod',0.1,...
    'timerfcn',{@mycallback,handles});
fopen(scom);
handles.scom=scom;
guidata(hObject,handles);

这里是mycallback函数

function mycallback(scom,BytsAvailable,handles)
%start single frame acquisition
showData=ones(84,1);
showWin=ones(14,6);
%%get previous data from handles
tact=handles.tact;
fwrite(scom,uint8(hex2dec(['AA';'AA';'AA';'20';'01';'00';'00';'8F';'83'])));
myData = fread(scom,183);%read raw data from sensor
for i=1:84
    showData(i,1)=myData(13+i*2)*16*16+myData(12+i*2);
end
%%append to tact array
tact=[tact;showData'];
handles.tact=tact;
....

关闭com时保存策略

function pb_close_Callback(hObject, eventdata, handles)
% hObject    handle to pb_close (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
scom=handles.scom;
%stop acquising
fwrite(scom,uint8(hex2dec(['AA';'AA';'AA';'22';'00';'00';'0E';'76'])));
fclose(scom);
tact=handles.tact;
save('tact.mat','tact');

尝试创建一个缓冲区,并在每次迭代时将新数据保存在最后一个位置

buf_len = 100;
index = 1:buf_len;
%Initialize array
arrayOfData = zeros(buf_len,1);
% get Data here. Let's say the new value is theta
arrayOfData = [ arrayOfData (2:end) ; theta ];
plot(index,Tdata,'r','LineWidth',2);

相关内容

  • 没有找到相关文章

最新更新