from Serial control magnet power supply IPS-120 by Laurens Willems van Beveren
This code controls a superconducting magnet.

setMagnetserial(value, checktime)
function setMagnetserial(value, checktime)
%value in [mT] INTEGER VALUES only !!!!!!

% instrfind
% clear all; % this clears the workspace
% delete(instrfind)
global instr; % do we really need this variable here?

s = serial('COM1');
s.Timeout = 2; % [s]
s.Terminator = 'CR'; % sets CR
sPause = 0.5;

fopen(s)

fprintf(s, ['$C3']);  % remote mode and unlocked
pause(sPause);
fprintf(s, ['$Q0']);  % Q0: normal resolution, default value
%fprintf(s, ['$Q4']);  
pause(sPause);


%value = 100
num = value; % '100 mT'
nulstr='0000';
numstr=num2str(num);
wstr=[nulstr(1:4-length(numstr)) numstr];
%wstr

%value = 500; % [mT] 
cmd=['$J+' wstr];  %set setpoint in units of T to 1G
fprintf(s, cmd);
pause(sPause);

% works!!!
%fprintf(s, '$J+1000');  % [mT]  Q0 sets the setpoint value to magnet
%pause(sPause);

clc;

%%%%%%%% read set point
%fopen(s)
%fprintf(s, 'R8' ) % this reads the setpoint field
%fprintf(s, ['R8']) % this reads the setpoint field in units of mT
%pause(sPause);
%setpoint = fscanf(s);
%pause(sPause);
%setpoint % display the result back from the magnet " Q R+00040" is returned


%%%%%%%% read persistent field
%fprintf(s, ['R18']) % this reads the persistent field
%pause(sPause);
%fprintf(s, ['V' 13]) % this sends a carriage return 
%persistentfield = fscanf(s);
%pause(sPause);
%persistentfield 

%fclose(s);
%delete(s); % to delete the object
%clear all; % to clear from workspace
%%%%%%%%%%

fprintf(s, '$A1');  % goto setpoint
pause(sPause);

%fprintf(s, 'R7'); % reads the demand field
%pause(sPause);

%cval=fscanf(s);
%pause(sPause);
%cval

%b=abs(str2num(cval(2:end)) - value);
%b

%fclose(s);

finished=0;
while ~finished % while not equal to zero
    %fopen(s);
    clc;
    fprintf(s, 'R7'); % reads the demand field
    pause(sPause);
    
    cval=fscanf(s); % puts demand field in cval
    pause(sPause);
    % cval % for display ramp only
   
    
    %fclose(s);

    if  abs(str2num(cval(2:end)) - value) <= 1 % accuracy 1 mT
        finished=1;
        'Demand field reached ;-)' 
    else
        pause(checktime);
        %finished=1;
        
    end
end

fclose(s);


Contact us