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

采纳的回答

Geoff Hayes
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 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by