classdef DC_supply % Simple control of DC supply via fixed GBIB address. % Agilent E3646A properties(Access=public) active voltage end methods (Access=public) function obj = DC_supply(options) arguments options.active bool = true options.voltage double = [0,0]; end % fn = fieldnames(options); for n = 1:numel(fn) try obj.(fn{n}) = options.(fn{n}); end end assert(size(obj.voltage,1)==1,'Set Voltage for CH1 and CH2 must be given as vector [V1, V2]'); assert(size(obj.voltage,2)==2,'Set Voltage for CH1 and CH2 must be given as vector [V1, V2]'); end function success = set(obj) success = false; try %connect to device v = visadev("GPIB0::19::INSTR"); % fopen(v); writeline(v, '*IDN?;'); disp(['DC Source: ' readline(v)]); cmd = 'INST:SEL?'; fprintf(v, cmd); selected_channel = fscanf(v); for ch = 1:2 % choose channel cmd = ['INST:SEL OUT',num2str(ch)]; fprintf(v, cmd); % get current voltage level cmd = ['VOLT?']; fprintf(v, cmd); act_volt = str2num(fscanf(v)); % desired voltage (round to two digits after comma) des_volt = round(obj.voltage(ch),2); cnt = 0; while abs(act_volt-des_volt) > 0 selected_channel = ['OUT',num2str(ch)]; % get current voltage level cmd = ['VOLT?']; fprintf(v, cmd); act_volt = str2num(fscanf(v)); % difference diff_volt = act_volt-des_volt; % set new voltage increment_voltage = -0.01* sign(diff_volt) ; cmd = ['VOLT ',num2str(act_volt+increment_voltage)]; fprintf(v, cmd); cnt = cnt+1; if mod(cnt,100) == 0 wait(1); end % get current voltage level cmd = ['VOLT?']; fprintf(v, cmd); act_volt = str2num(fscanf(v)); end % check if voltage is set if act_volt ~= des_volt hMsgBox = msgbox('An error occurred in dc supply module. Check if voltage is set correctly.'); uiwait(hMsgBox); end end % choose channel cmd = ['INST:SEL ',selected_channel]; fprintf(v, cmd); %disconnect fclose(v); delete(v); catch end end end methods (Access=private) end end