代码之家  ›  专栏  ›  技术社区  ›  user6248190

如何从不同的函数将数据发送到同一个comport,而不必经常打开comport?

  •  0
  • user6248190  · 技术社区  · 8 年前

    嗨,我想知道如何从不同的回调向comport传递数据,而不必在不同的回调中不断打开和关闭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 回复  |  直到 8 年前
        1
  •  1
  •   Juha Lipponen    8 年前

    有点像这样 (以下是来自的示例形式: 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