%Set up serial connection
%mcu = serial('COM9','BaudRate',9600,'DataBits',8,'StopBits',1);
%set up tcpip connection
%input_port = 8080;
%output_port = 8080;

%===========================================================
%Written by Bruce Land, BRL4@cornell.edu
%  12 May 2003
%Written for Joe Sisneros

%=====clean up any leftover serial connections==============
clear all
try
    fclose(instrfind) %close any bogus serial connections
end
%=====Data output file name=================================
[filename,pathname] = uiputfile( 'c:\my documents\*.xls','Calibrate SpreadSheet Name');
if isnumeric(filename) %check to see if Cancel buttion was hit
    return             %if so, stop program
end

%=====open a serial connection===============================
%set its rate to 9600 baud
%SR830 terminator character is  (ACSII 13)
%use fprintf(s,['m' mode]) to write
%and fscanf(s) to read the SR830
%s = serial('COM7',...
%    'baudrate',9600,...
%    'terminator',122); 
s=serial('COM5','BaudRate',9600,'DataBits',8,'StopBits',1);
s.terminator = 13;
s.timeout = 20;
fopen(s);
figure(1);
hold on
i =1;
cnt1=1;
cnt2=1;
cnt3=1;
cnt4=1;
cnt5=1;
cnt6=1;
e1=[];
e2=[];
e3=[];
e4=[];
e5=[];
e6=[];
while(i<21)
    %temp = fscanf(s, '%s');
    %if (findstr(temp, char([9])))
    %    break;
    %end
    Electrode(i) = str2num(fscanf(s,'%s')) ;
    Pressure(i) = str2num(fscanf(s,'%s')) ;
    V100(i) = str2num(fscanf(s, '%s')) ;
    Ph100(i) = str2num(fscanf(s, '%s')) ;
    V500(i)  = str2num(fscanf(s, '%s')) ;
    Ph500(i) = str2num(fscanf(s, '%s')) ;
    V1k(i)  = str2num(fscanf(s, '%s')) ;
    Ph1k(i) = str2num(fscanf(s, '%s')) ;
    
     
    Volt100(i) = V100(i) * 5/256;
    Volt500(i) = V500(i) * 5/256;
    Volt1k(i) = V1k(i) * 5/256;

    Phase100(i) = (Ph100(i)-5)/117 * pi/2; %~7 is in phase, ~145 is 90 degress out
    Phase500(i) = (Ph500(i)-5)/117 * pi/2;
    Phase1k(i) = (Ph1k(i)-5)/117 * pi/2;
    
    Imp100(i) = Volt100(i)/(1.25E-6)/50*.93;
    Imp500(i) = Volt500(i)/(1.25E-6)/50*.93;
    Imp1k(i) = Volt1k(i)/(1.25E-6)/50*.93;
    
    fprintf(1, '%d: %d: %.0f: %.3f: %.0f: %.3f: %.0f: %.3f\n', Electrode(i), Pressure(i), Imp100(i), Phase100(i), Imp500(i), Phase500(i), Imp1k(i), Phase1k(i));
   
    
    if (Electrode(i)==1)
       e1(cnt1,:) = [Pressure(i) Volt100(i) Phase100(i) Imp100(i) Volt500(i) Phase500(i) Imp500(i) Volt1k(i) Phase1k(i) Imp1k(i)];
       cnt1=cnt1+1;
    end
    if (Electrode(i)==2)
       e2(cnt2,:) = [Pressure(i) Volt100(i) Phase100(i) Imp100(i) Volt500(i) Phase500(i) Imp500(i) Volt1k(i) Phase1k(i) Imp1k(i)];
       cnt2=cnt2+1;
    end
    if (Electrode(i)==3)
       e3(cnt3,:) = [Pressure(i) Volt100(i) Phase100(i) Imp100(i) Volt500(i) Phase500(i) Imp500(i) Volt1k(i) Phase1k(i) Imp1k(i)];
       cnt3=cnt3+1;
    end
    if (Electrode(i)==4)
       e4(cnt4,:) = [Pressure(i) Volt100(i) Phase100(i) Imp100(i) Volt500(i) Phase500(i) Imp500(i) Volt1k(i) Phase1k(i) Imp1k(i)];
       cnt4=cnt4+1;
    end
    if (Electrode(i)==5)
       e5(cnt5,:) = [Pressure(i) Volt100(i) Phase100(i) Imp100(i) Volt500(i) Phase500(i) Imp500(i) Volt1k(i) Phase1k(i) Imp1k(i)];
       cnt5=cnt5+1;
    end
    if (Electrode(i)==6)
       e6(cnt6,:) = [Pressure(i) Volt100(i) Phase100(i) Imp100(i) Volt500(i) Phase500(i) Imp500(i) Volt1k(i) Phase1k(i) Imp1k(i)];
       cnt6=cnt6+1;
    end
    
    %plot(Pressure(i), V100(i), 'o');
    %plot(Pressure(i), V500(i), 'o');
    %plot(Pressure(i), V1k(i), 'o');
    
    i=i+1;
    %if (i == 6)
    %    break;
    %end     
end


%Res100 = 
fclose(s);

array = [Electrode', Pressure', Volt100', Phase100', Volt500', Phase500', Volt1k', Phase1k'];
sort = sortrows(array);
xlswrite([pathname,filename], sort);

Esort = sort(:,1);
Psort = sort(:,2);
Volt100s= sort(:,3);
Pha100s= sort(:,4);
Volt500s= sort(:,5);
Pha500s= sort(:,6);
Volt1ks= sort(:,7);
Pha1ks= sort(:,8);

figure(1)
hold on
plot(e1(:,1), e1(:,4), 'go');
plot(e1(:,1), e1(:,7), 'ro');
plot(e1(:,1), e1(:,10), 'bo');


if (~isempty(e2))
    figure(2)
    hold on
    plot(e2(:,1), e2(:,4), 'go');
    plot(e2(:,1), e2(:,7), 'ro');
    plot(e2(:,1), e2(:,10), 'bo');
end

if (~isempty(e3))
    figure(3)
    hold on
    plot(e3(:,1), e3(:,4), 'go');
    plot(e3(:,1), e3(:,7), 'ro');
    plot(e3(:,1), e3(:,10), 'bo');
end

if (~isempty(e4))
    figure(4)
    hold on
    plot(e4(:,1), e4(:,4), 'go');
    plot(e4(:,1), e4(:,7), 'ro');
    plot(e4(:,1), e4(:,10), 'bo');
end

if (~isempty(e5))
    figure(5)
    hold on
    plot(e5(:,1), e5(:,4), 'go');
    plot(e5(:,1), e5(:,7), 'ro');
    plot(e5(:,1), e5(:,10), 'bo');
end

if (~isempty(e6))
    figure(6)
    hold on
    plot(e6(:,1), e6(:,4), 'go');
    plot(e6(:,1), e6(:,7), 'ro');
    plot(e6(:,1), e6(:,10), 'bo');
end

