Thar ABPR-20 RS232 communication via handheld emulation

Controls and reads from a Thar ABPR-20, by emulating the handheld controller.

tharbprobjdisplaymenu(serialObject)

function response = tharbprobjdisplaymenu(serialObject)
% BPROBJREADDISPLAYMENU navigates to the first display menu of Thar BPRs
% response = tharbprobjdisplaymenu(serialObject) navigates to the display
% menu of Thar BPRs. serialObject is the BPR serial object (generated by
% tharbproj). Response is the displayed menu itself, so that its contents
% can be used immediately if necessary.
% checks the number of arguments
error(nargchk(1, 1, nargin))
% fetches contents of screen
try
% fetches the most recent screen contents - it will error if
% there is no data but will not be a valid "menu" if its not fully
% collected
response = tharbprobjcomm('read', serialObject);
% tests it
if ~isMenu(response)
% otherwise, as it didn't error before, we can press F3 again to
% get into the first half of the display menu, regardless of our
% location in the menu structure
tharbprobjcomm('write', serialObject, 19)
% fetches screen contents - it SHOULD be the display menu, but if
% it errors it is not a problem anyway
response = tharbprobjcomm('read', serialObject);
end
catch
% if there is no data from the first read, we know we are probably in a
% sub-menu
% if the sub-menu is the system configuration menu, then almost any
% keystroke will take you back to the more options menu, where <ESC>
% will not break anything
% press A (ascii 97) - this is to choose a profile if we are in the
% annoying profile menu which does not allow escaping, then shift + z
% to get out (<ESC>)
tharbprobjcomm('write', serialObject, [97, 27])
% try reading the display again
response = tharbprobjcomm('read', serialObject);
% if we are in the second part of the display menu...
if strcmp(response{2}(1:14), 'External Heat:')
% press F3 again
tharbprobjcomm('write', serialObject, 19)
% try reading the display again
response = tharbprobjcomm('read', serialObject);
end
end
% if the profile isn't there, display the response and error
if ~isMenu(response)
% displays a warning
warning('tharBPRObjDisplayMenu:CouldNotReachMenu', 'Could not access the main display menu.')
% displays the response
response
% errors
error('Could not get to the main display menu as stuck - unit possibly not responding to keypresses.')
end
% decides if its in the right screen or not
function value = isMenu(response)
% it must have...
secondLineA = numel(response{2}) >= 8 && strcmp(response{2}(1:8), 'Profile:');
secondLineB = numel(response{2}) >= 16 && strcmp(response{2}(12:16), 'CTRL:');
thirdLineA = numel(response{3}) >= 4 && strcmp(response{3}(1:4), 'POS:');
thirdLineB = numel(response{3}) >= 16 && strcmp(response{3}(12:16), 'VTMP:');
fourthLineA = numel(response{4}) >= 4 && strcmp(response{4}(1:4), 'SET:');
fourthLineB = numel(response{4}) >= 15 && strcmp(response{4}(11:16), 'PRESS:');
% returns a value
value = secondLineA && secondLineB && thirdLineA && thirdLineB && fourthLineA && fourthLineB;