function [] = RmonitoringReflow_v20170802()
%Resistance monitoring of solder reflow process
% Monitor electrical resistance with a Keithley 2700 multimeter integrated 
% with a 7700 multiplexer. The example code below is for monitoring 8 
% samples. The temperature is monitored with Pt100 connected to a Keithley 
% 2700 multimeter. The resistance is visualized in real time on PC screen. 
% This code is developed to monitor solder reflow process, and can be 
% used for similar purposes with necessary modifications.
% By Di Erick Xu, University of Waterloo, 2017.Aug.02

% Hardware: GPIB-USB cable, two Keithley 2700, and a Keithley 7700.

% Tags: GPIB, Digital multimeter, Multiplexer, Resistance monitoring,
% Solder reflow, Temperature monitoring,

%% initialzation
close all;
clear;
clc;
instrreset;

%% variable definations
totalMeasSec = 4*60*60; % seconds

sampleName = 'Demo4MathworksCentral';
targetTemp = '50C20min_185C10min'; % The target temperature for the reflow process

% Samples 1-8 are connected to Channels 1-8 of multiplexer 
% in this example, the second slot of 2700 is used for the multiplexer
sampleCHs = 201:208; 

resistance = nan(1, length(sampleCHs)); % create an initial array of NaNs for resistance
time = nan(1, length(sampleCHs)); % create an initial array of NaNs for the time for resistance measurements
temp = nan; % create an initial NaN for temperature
tempTime = nan; % create an initial NaN for the time for temperature measurements
fname=[datestr(now, 30) '_' sampleName '_' num2str(targetTemp)]; 

%% gpib connections
% A national instrument ('ni') gpib-usb adapter is used to connect PC with
% the instruments. The board number of the adapter is 0. The addresses of
% the Keithley 2700 multimeter with a 7700 multiplexer, and the other 
% Keithley 2700 multimeter are 24, and 22, respectively, in this example.
multiplexer = gpib('ni', 0, 24);
tempMeter = gpib('ni', 0, 22);

% Open communication
fopen(multiplexer);
fopen(tempMeter);

%% reset and configure both multimeters
% Multiplexer configuration for 2w resistance measurement
fprintf(multiplexer, '*rst');
fprintf(multiplexer, '*opc?');
fscanf(multiplexer);
fprintf(multiplexer, '*cls');
fprintf(multiplexer, 'conf:res');

% Temperature measurement configuration for keithley 2700 with Pt100 
fprintf(tempMeter, '*rst');
fprintf(tempMeter, '*opc?');
fscanf(tempMeter);
fprintf(tempMeter, '*cls');
fprintf(tempMeter, 'CONFigure:TEMPerature FRTD');
fprintf(tempMeter, ['SENSe:TEMPerature:NPLC ' num2str(tempNPLC)]);
fprintf(tempMeter, 'SENSe:TEMPerature:TRANsducer:FRTD:OCOMpensated ON');

%% monitoring starts
tic; % start timer

j = 0; % use j as a counter for the number of resistance measurement for each sample

while toc < totalMeasSec
    j = j+1; % the current loop number
    
    %% Measurements
    % The 8 samples in a for loop
    for i = 1:length(sampleCHs) 
        % Open all channels
        fprintf(multiplexer, 'rout:open:all');
        fprintf(multiplexer, '*opc?');
        fscanf(multiplexer);

        % Close the channel for the ith sample.
        fprintf(multiplexer, ['rout:clos (@', num2str(sampleCHs(i)), ')']);
        fprintf(multiplexer, '*opc?');
        fscanf(multiplexer);

        % make a measurement, and save the time and resistance values
        fprintf(multiplexer, 'read?');
        rawStr = fscanf(multiplexer);
        time(j, i) = toc;
        resStr = rawStr(1:strfind(rawStr, 'OHM')-1);
        resistance(j, i) = str2double(resStr);
    end
    % The temperature measurement
    fprintf(tempMeter, 'read?');
    temp(j) = str2double(fscanf(tempMeter));
    tempTime(j) = toc;
    
    %% Visualization
    tt=time(max([1 j-200]):j, :);
    rr=resistance(max([1 j-200]):j, :);
    semilogy(tt, rr); 
    title(['j: ' num2str(j) ', R1: ' num2str(resistance(j, 1)) ' Ohm, ' ...
        num2str(length(tt(:, 1))/range(tt(:, 1)),5) ' pts/s']);
    xlabel('Time [s]'); ylabel('R [Ohm]');
    if j>10;
        a=axis;axis([min(tt(:, 1)) max(tt(:, end)) a(3:4)]);
    end
    drawnow;

    %% Data save
    save([fname '.mat']);
end

fclose(multiplexer);
fclose(tempMeter);

%% Plot results
close all
figure
subplot(2,1,1)
colors = {[0.25 0.25 0],'m','c','r','g','b','k',[0.5 0.5 0],[0.5 0 0.5],...
    [0 0.5 0.5],[0.5 0 0],[0 0.5 0],[0 0 0.5],[0.5 0.5 0.5]};
for i = 1:8
    h = loglog(time(:, i)/60, resistance(:, i), '-',time(:, i)/60, resistance(:, i), '.');
    hold on;
    set(h, 'Color', colors{i});
end

grid on;
xlabel('Time [min]');
ylabel('Resistance [Ohm]');
title(fname);

subplot(2,1,2)
semilogx(tempTime/60, temp);
xlabel('Time [min]');
ylabel('Temperature [°C]');

figwind=[30          64        1493         729];
set(gcf,'Position',figwind);
saveas(gcf,[fname '_fig'],'fig');

end
