function currentLocation = mitutoyoIndicators(varargin)
% a simple serial port interface to Gageway drivers for mitutoyo indicators
% currentLocation = mitutoyoIndicators; opens the serial ports or asks what
% serial ports have indicators and then returns the indicator value
% mitutoyoIndicators(1); closes the serial ports
% try setting up the communications ports
if ~ispref('mitutoyo', 'xComm')
setupMitutoyo;
end
if ~isappdata(0, 'mitutoyoX')
try
xComm = serial(['COM' sprintf('%0.0f', getpref('mitutoyo', 'xComm'))]);
set(xComm,...
'baudrate', 9600,...
'parity', 'none',...
'databits', 8,...
'stopbits', 1,...
'requesttosend', 'on',...
'dataterminalready', 'on',...
'timeout', .2,...
'outputbuffersize', 1024,...
'inputbuffersize', 512,...
'terminator', 'CR');
fopen(xComm)
catch
error(['Error with serial port COM' sprintf('%0.0f', getpref('mitutoyo', 'xComm')) ' in setting up mitutoyo X indicator']);
end
try
yComm = serial(['COM' sprintf('%0.0f', getpref('mitutoyo', 'yComm'))]);
set(yComm,...
'baudrate', 9600,...
'parity', 'none',...
'databits', 8,...
'stopbits', 1,...
'requesttosend', 'on',...
'dataterminalready', 'on',...
'timeout', .2,...
'outputbuffersize', 1024,...
'inputbuffersize', 512,...
'terminator', 'CR');
fopen(yComm)
catch
error(['Error with serial port COM' sprintf('%0.0f', getpref('mitutoyo', 'yComm')) ' in setting up mitutoyo Y indicator']);
end
% wait for the serial ports to open
while strcmp(get(yComm, 'status'), 'closed')
wait(0.1)
end
setappdata(0, 'mitutoyoX', xComm);
setappdata(0, 'mitutoyoY', yComm);
else
xComm = getappdata(0, 'mitutoyoX');
yComm = getappdata(0, 'mitutoyoY');
end
if nargin < 1
% ask for the current position
fprintf(xComm, 'R');
currentLocation = str2double(fgetl(xComm));
fprintf(yComm, 'R');
currentLocation(2) = str2double(fgetl(yComm));
else
% close the serial ports and clear out appdata
fclose(xComm);
fclose(yComm);
delete(xComm);
delete(yComm);
rmappdata(0, 'mitutoyoX');
rmappdata(0, 'mitutoyoY');
end
function setupMitutoyo
% determine what serial ports the indicators are on
xComm = inputdlg('Enter serial port to which mitutoyo X is connected', 'Comm ', 1, {'1'});
if isempty(xComm)
error('Must set a value for mitutoyo x indicator serial port')
end
if any(xComm{1} > 57 | xComm{1} < 48)
error('Value must be a number')
end
xResolution = questdlg('What is the gage''s resolution?', 'X Resolution', '1 micron', '10 microns', '10 microns');
if isempty(xResolution)
error('Must set X resolution')
end
yComm = inputdlg('Enter serial port to which mitutoyo Y is connected', 'Comm ', 1, {'2'});
if isempty(yComm)
error('Must set a value for mitutoyo y indicator serial port')
end
if any(yComm{1} > 57 | yComm{1} < 48)
error('Value must be a number')
end
yResolution = questdlg('What is the gage''s resolution?', 'Y Resolution', '1 micron', '10 microns', '10 microns');
if isempty(yResolution)
error('Must set Y resolution')
end
if strcmp(xResolution, '1 micron')
setpref('mitutoyo', 'xNumDigits', 6);
else
setpref('mitutoyo', 'xNumDigits', 5);
end
if strcmp(yResolution, '1 micron')
setpref('mitutoyo', 'yNumDigits', 6);
else
setpref('mitutoyo', 'yNumDigits', 5);
end
setpref('mitutoyo', 'xComm', str2double(xComm));
setpref('mitutoyo', 'yComm', str2double(yComm));