嗨,我想知道如何从不同的回调向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端口,然后将其关闭。理想情况下,我希望它保持打开状态,并且可以从任何回调访问。