【问题标题】:How to save data from serial port to an array in Matlab gui?如何将数据从串口保存到 Matlab gui 中的数组?
【发布时间】:2014-04-27 06:45:06
【问题描述】:

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

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的设置

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);

这是我的回调函数

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;
....

关闭scom时保存技巧

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');

【问题讨论】:

    标签: arrays matlab user-interface serial-port


    【解决方案1】:

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

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-11
      • 1970-01-01
      • 2011-06-30
      • 2017-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多