function [h] = LabInst_GetHandle(inst,labbench)
% function [h] = LabInst_GetHandle(inst,labbench)
%  establishes connection to instrument with
%  inst={"mdo"|"mfg"|"gdm"|"gpp"}
% for respectively oscilloscope, function generator, multimeter, power
% supply.
% labbench=[1-6]
% h is the handle you will need to interact with the instrument afterwards
% h=[] for failure
% Author: Philipp Häfliger, 29-Jan-2025
stderror_fid=2;
h=[];
switch inst
    case "mdo" %oscilloscope
        socket=3000;
    case "mfg" %function generator
        socket=1026;
    case "gdm" %multi meter
        socket=3001;
    case "gpp" %power supply
        socket=1026;
    otherwise
        fprintf(stderror_fid,"Error: instrument identifier not recognized\n");
        return;
end; %switch
if ~ismember(labbench,1:6)
    fprintf(stderror_fid,"Error: lab bench number needs to be in [1-6]\n");
    return;
end; %if
%h = visadev("TCPIP::nano-slab-"+string(labbench)+"-"+string(inst)+".uio.no::"+string(socket)+"::SOCKET");
h=tcpclient("nano-slab-"+string(labbench)+"-"+string(inst)+".uio.no",socket)
if ~isempty(h)
    h.configureTerminator("LF");
    h.writeline("*IDN?") %+char(10)
    h.readline
else
    fprintf(stderror_fid,"Error: cannot reach instrument");
    return;
end; %if
fopen(h);
end

