Info

This question is closed. Reopen it to edit or answer.

How can I access serial object without reopening serial in GUI functions

1 view (last 30 days)
I am writing a program that controls a 3 Axis drilling machine.
The GUI interfaces with an Arduino board.
I want the serial port connection to remain open throughout entire use of the GUI. I do not want to reopen the connection at the start of each different function. Is there a way to make the object a global or persistent variable? Would I need to define the object in the OpeningFcn?
My code below displays the Callback for pressing the Machine Run pushbutton. I only included up to the else because the code is irrelevant beyond that.
function runMachine_Callback(hObject, eventdata, handles)
% hObject handle to runMachine (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
status = {'Initializing MakerBot'}
obj1 = instrfind('Type', 'serial', 'Port', 'COM3', 'Tag', '');
% Create the serial port object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
obj1 = serial('COM3');
else
fclose(obj1);
obj1 = obj1(1)
end
fopen(obj1)
set(handles.text5, 'String', status)
status = 'Connected to MakerBot';
sendXYZCoord = get(handles.checkbox2,'Value');
if sendXYZCoord == 1
mbStatus = 'Sending Coords';
set(handles.text5, 'String', mbStatus);
xCoord = get(handles.xCoord_text, 'String');
yCoord = get(handles.yCoord_text, 'String');
zCoord = get(handles.zCoord_text, 'String');
xCoord_val = str2num(xCoord);
yCoord_val = str2num(yCoord);
zCoord_val = str2num(zCoord);
fprintf(obj1, sprintf('[%s %s %s]',xCoord,yCoord,zCoord));
else

Answers (0)

Community Treasure Hunt

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

Start Hunting!