#!/usr/bin/perl

use lib 'd:/Programs/Perl/lib';
use lib '.';

use strict;

use MyApplication;
use Utils;
use GPIB;
use MyGPIB::Keithley6517A;

#データファイルの名前
my $DataFileName = "Test2.csv";

# 測定前のWait
my $WaitBefore = 1.0;
#my $WaitForMeasurement = 0.1;

# 測定電圧範囲
my $VStart = -3.0;
my $VEnd   =  3.0;
my $VStep  =  0.01;

#電圧源リミッターの設定
my $VSourceLimit = 3.0;

#電流測定リミッターの設定
#my $ISourceLimit = 21e-3;
#測定時間
#my $IAperture = 0.1; # 166.67e-6 to 200e-3
# 平均を取る回数
my $nAverage = 10;

# ZeroCheck
my $ZeroCheck = 'on';

my $Who = 'Tsukamoto';
my $Today = Utils::BuildDateString(time());

#===============================================
# Applicationオブジェクト作成
#===============================================
my $App = new MyApplication;
exit if($App->Initialize() < 0);


#===============================================
# GPIBオブジェクト作成
#===============================================
my $Interface = 'ni';
my $DeviceName = "Keithley6517A:27";
my $K6517A = new Keithley6517A($Interface, $DeviceName, GPIB->T1s, 1, 0);
if(!defined $K6517A) {
	$App->print("6517A can not be opened. [$Interface: $DeviceName]\n");
	exit;
}
print("GPIB Address for $DeviceName: $K6517A->{GPIBAddress}\n");

#my $DeviceName2 = "Keithley2000:17";
#my $K2000 = new GPIBDevice($Interface, $DeviceName2, GPIB->T1s, 1, 0);
#if(!defined $K2000) {
#	$App->print("2000 can not be opened. [$Interface: $DeviceName]\n");
#	exit;
#}
#print("GPIB Address for $DeviceName: $K2000->{GPIBAddress}\n");


$K6517A->print("*IDN?");
my $DeviceID = $K6517A->read();
print("Device ID: $DeviceID\n");



# 6517Aの初期化
$K6517A->Initialize();

#測定モードの設定 （Curr:DC)
$K6517A->SetDCCurrentMode($ZeroCheck);


#電圧源リミッターの設定
$K6517A->print(":sour:volt:limit $VSourceLimit");
$K6517A->print(":sour:volt:limit:stat on");
#print "volt:limit\n"; <>;


#電流測定リミッターの設定
#$K6517A->print(":sens:curr:rang:ulim $ISourceLimit");
$K6517A->print(":sens:curr:rang:auto on");
#$K6517A->print(":sens:curr:aper 0.01");
#$K6517A->print(":sens:curr:nplcycles 10");
#$K6517A->print(":sens:curr:nplcycles:auto on");
#print "sense:curr\n"; <>;


if(0){
#抵抗電流リミット制御
my $RLimit = 21e-3;
$K6517A->print(":sour:curr:Rlimit");
$K6517A->print(":sour:curr:limit:stat on");
#print "sour:limit\n"; <>;

}



#測定開始
my $nVStep = int( ($VEnd - $VStart) / $VStep + 1.0001 );
#print "before wait\n"; <>;

sleep($WaitBefore);
#print "after wait\n"; <>;

$K6517A->print(":sour:volt $VStart");
$K6517A->print(":outp:stat on");

#ファイル書込み
#print "file write\n"; <>;
if (-w $DataFileName) { print "Can write to [$DataFileName]\n"; }
my $out = new JFile;
if(!$out->Open($DataFileName, "w")) {
	print "Error: Can not write to [$DataFileName]\n";
	exit;
}



#Excelにデータを移す
$out->print("V,I,|I|,**********Measured by $Who, $Today,\n");
for(my $i = 0 ; $i < $nVStep ; $i++) {
	my $V = $VStart + $VStep * $i;
	$K6517A->SetSourceVoltage($V);

	my $I = $K6517A->MeasureAveraged($nAverage);

	print("V=$V  I=$I\n");
	my $absI = abs($I);
	$out->print("$V,$I,$absI\n");
}
$K6517A->Finish();

$out->Close();





exit;

