如何在不必经常打开comport的情况下将数据从不同的函数发送到同一个comport?

时间:2016-06-27 16:39:16

标签: matlab callback serial-port matlab-guide

您好我想知道如何在不同的回调中不断打开和关闭comport的情况下,如何将数据传递给不同的回调?

所以这是代码

function submitButton_Callback(hObject, eventdata, handles)
% hObject    handle to submitButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if(get(handles.HitButton,'Value') == 1)
    filename = 'testing.xls';
    writevar({'Hit'},1,filename);
    set(handles.text2,'visible','off');
    set(handles.words,'visible','off');
    set(handles.rating,'visible','on');
    set(handles.readyToGo,'visible','off');
    set(handles.readyButton,'visible','off');
    set(handles.submitButton,'visible','off');
    set(handles.rateButton, 'visible','on');
    set(handles.HowSure,'visible', 'on');
    drawnow;
    sPort=serial('COM4');
    fopen(sPort);
    fprintf(sPort, '%d', 3);
    fclose(sPort);
    delete(sPort);
else
    filename = 'testing.xls';
    writevar({'Bit'},1,filename);
    set(handles.text2,'visible','off');
    set(handles.words,'visible','off');
    set(handles.rating,'visible','on');
    set(handles.readyToGo,'visible','off');
    set(handles.readyButton,'visible','off');
    set(handles.submitButton,'visible','off');
    set(handles.rateButton, 'visible','on');
    set(handles.HowSure,'visible', 'on');
    drawnow;
    sPort=serial('COM4');
    fopen(sPort);
    fprintf(sPort, '%d', 3);
    fclose(sPort);
    delete(sPort);
end
function readyButton_Callback(hObject, eventdata, handles)
% hObject    handle to readyButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
s = load('train.mat');
sPort=serial('COM4');
fopen(sPort);
fprintf(sPort, '%d', 1);
player = audioplayer(s.y,8192);
playblocking(player);
fprintf(sPort, '%d', 2);
fclose(sPort);
delete(sPort);
%pause(4);
set(handles.text2,'visible','on');
set(handles.words,'visible','on');
set(handles.HowSure,'visible', 'off');
set(handles.rating,'visible','off');
set(handles.readyToGo,'visible','off');
set(handles.readyButton,'visible','off');
set(handles.submitButton,'visible','on');
drawnow;

你可以看到我必须打开每个功能的Com端口然后关闭它。理想情况下,我希望它保持开放状态,并且可以从任何回调中访问。

1 个答案:

答案 0 :(得分:1)

像这样的东西 (以下示例形式来自: http://se.mathworks.com/help/matlab/creating_guis/share-data-among-callbacks.html?requestedDomain=www.mathworks.com 部分: 将数据存储为应用程序数据)

function my_slider()
%generate figure and specify a delete callback function to close 
%the com port when figure is closed 
hfig = figure('DeleteFcn',@closeCom); 

sPort=serial('COM4');
fopen(sPort);
setappdata(hfig,'comPort',sPort);

slider = uicontrol('Parent', hfig,'Style','slider',...
         'Units','normalized',...
         'Position',[0.3 0.5 0.4 0.1],...
         'Tag','slider1',...
         'Callback',@slider_callback);

button = uicontrol('Parent', hfig,'Style','pushbutton',...
         'Units','normalized',...
         'Position',[0.4 0.3 0.2 0.1],...
         'String','Display Values',...
         'Callback',@button_callback);
end

function slider_callback(hObject,eventdata)
  hComPort=getappdata(hObject.Parent,'comPort');
  fprintf(hComPort, '%d', 3);
end

function button_callback(hObject,eventdata)
  hComPort=getappdata(hObject.Parent,'comPort');
  fprintf(hComPort, '%d', 1);
  fprintf(hComPort, '%d', 2);

end

function closeCom
  figHandle=gcbo %get handle for the calling figure
  hComPort=getappdata(figHandle,'comPort');
  fclose(hComPort);
  delete(hComPort);
end