Hi I wanted to know how I can pass data to a COM port from different callbacks without constantly having to open a COM port in the different callbacks?
so here is the code
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);endfunction 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;
as you can see I have to open the Com port for each function. Ideally I would like it to remain open and be accessible
Best Answer