Matlab Uart receive data and print in the frame of axes

  • Button callback funciton

global L1;
global times;
if (get(hObject,'Value'))
axes(handles.axes);
cla;
times = 0;
delete(instrfindall('Type','serial')); 
scom = serial('COM1');
set(scom,'BaudRate', 115200,'DataBits',8,'StopBits',1,'Parity','none','FlowControl','none');    
scom.BytesAvailableFcnMode = 'terminator';
scom.BytesAvailableFcn = {@my_callback, handles};

try
fopen(scom);
catch
msgbox('串口不可获得!');
return;
end

%set(handles.axes,'XLim',[0 500],'YLim',[0 500])
set(handles.axes,'YLim',[0 500]);
L1 = animatedline('color','r');

set(hObject,'String','restart');

else

scoms = instrfind;
fclose(scoms);
delete(scoms);
set(hObject,'String','start');

end

  • my_callback.m

  function my_callback(obj,~,handles)
    global L1;
    global times;
    
    outdata = fscanf(obj);
    x = times +1;
    times = x;
    %fprintf(obj,'%d\r\n', outdata,'async' );
    
    Ldata = str2double(outdata);

    set(handles.Xdata, 'String', x);
    set(handles.Ydata, 'String', Ldata);

    addpoints(L1,x,Ldata); 
    drawnow  update %limitrate

  end

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页