How can I send data to the same COM port from different functions without having to constantly open the comport?
조회 수: 1 (최근 30일)
이전 댓글 표시
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);
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;
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
댓글 수: 0
채택된 답변
Geoff Hayes
2016년 6월 27일
Yasir - try saving your serial port object to the handles structure so that the other callback will be able to access it. For example, in either of the callbacks do
if ~isfield(handles,'serialObj')
handles.serialObj = serial('COM4');
fopen(handles.serialObj);
guidata(hObject,handles);
end
fprintf(handles.serialObj,...);
In the above, we check to see if the serialObj is a field of the handles structure. If it isn't, then we create and open it. We use guidata to save the updated handles structure so that the other callbacks (when fired) receive this updated structure with the open serial object.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!