#!/usr/bin/perl

use lib 'd:/Programs/Perl/lib';
use lib 'd:/Programs/Perl/lib2.0';
use lib '.';

use strict;

use MyApplication2;
use Device::Device;

$SIG{INT} = \&ctrl_c_handler;
BEGIN {
#	$| = 1;
	open (STDERR, ">&STDOUT");
}

#===============================================
# 測定関係変数
#===============================================
#データファイルの名前
my $DataFileName = "CPM2.csv";

#測定前のWait
my $WaitBefore = 1.0;
my $WaitForMeasurement = 1.0;

#Keithley6517Aの設定電圧
my $V = 3.0;

#Keuthley6517Aゼロチェック
my $ZeroCheck = 'on';

#平均を取る回数
my $nAverage = 10;

#Cornerstone130の測定波長
my $W = 500;

#測定時間
my $tStart = 0.0;
my $tEnd = 3600.0; 
my $tStep = 1.0;



#名前＆日日時
my $Who = 'Tsukamoto';
my $Today = Utils::BuildDateString(time());



#===============================================
# Applicationオブジェクト作成
#===============================================
my $App = new MyApplication2;
$App->SetOutputMode("auto");
my $ProgramPath = $App->SpeculateProgramPath($0, "");
my $IniFile = $App->OpenIniFile($ProgramPath, 1);
$App->AddIniFileVariable("\\Preferences\\DeviceName", "DeviceName", "Keithley2000");
$App->AddIniFileVariable("\\Preferences\\Port",       "Port",       "GPIB17");
$App->ReadSetting();



#===============================================
# Deviceオブジェクト作成
#===============================================
my $K6517A         = OpenDevice("Keithley6517A",  "GPIB27");
my $Cornerstone130 = OpenDevice("Cornerstone130", "COM1");
my $Laserstar3APLS = OpenDevice("Laserstar3APLS", "COM4");


#Keithley6517Aの初期化
$K6517A->Initialize();
$K6517A->SetDCCurrentMode($ZeroCheck);
$K6517A->SetAutoRangeForCurrent("on");


#Laserstar 3AP-LS設定
$Laserstar3APLS->SetRange("auto");
$Laserstar3APLS->SetPowerMode();




#===============================================
# 測定開始
#===============================================
my $ntStep = int( ($tEnd - $tStart) / $tStep + 1.000001 );

Utils::sleep($WaitBefore);

$K6517A->SetSourceVoltage($V);
$K6517A->SetVSourceOutput("on");
$Cornerstone130->GoWave($W);
$Cornerstone130->Shutter("0");      #シャッター開


#回折格子の波長が設定されたら、人間がENTERキーを押して次の作業に進む
print "\nPress ENTER if ready>";
<>;



#ファイル書込み
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("t,P,**********Measured by $Who, $Today,\n");
for(my $i = 0 ; $i < $ntStep ; $i++) {
	my $t = $tStart + $tStep * $i;
    
	$Cornerstone130->GoWave($W);
	Utils::sleep($WaitForMeasurement);
	print("Waiting $WaitForMeasurement s...\n");

	my $P = $Laserstar3APLS->Measure();
	print("t=$t   P=$P\n");
	$out->print("$t,$P\n");
}

$out->Close();


#Cornerstone130シャッター閉じる
$Cornerstone130->Shutter("1");

exit;



#============================================================
#　一般関数
#============================================================
sub ctrl_c_handler{
	$App->print("CTRL+C pressed\n");
	$App->print("Input 'yes' if terminate this program.\n>>");
	my $line = <>;
	if($line =~ /^\s*yes\s*$/i) {
	   exit;
	}
	return;
}


sub OpenDevice
{
	my ($DeviceName, $Port) = @_;
	Utils::DelSpace($DeviceName);
	Utils::DelSpace($Port);

#RS232Cの設定
my $ConfFile        = '';
my $COMPort         = 'COM1';
my $UseModule       = 0;
my $BaudRate        = 9600;
my $Parity          = 'none';
my $DataBits        = 8;
my $StopBits        = 1;
my $HandShake       = 'none';
my $WriteBufferSize = 1024 * 20;
my $ReadBufferSize  = 1024 * 4;
my $ReadInterval    = 800;
my $ReadCharTime    = 400;
my $ErrorMessage    = 1;
my $UserMessage     = 1;


#GPIBの設定
my $GPIBInterface   = "ni";
my $GPIBTimeOut     = GPIB->T1s;
my $GPIBEOT         = 1;
my $GPIBEOS         = 0;

	my $dev = new Device(
			-Port               => $Port,
			-ModuleName         => $DeviceName,
			-DeviceName         => $DeviceName,
			-GPIBInterface      => $GPIBInterface,
			-GPIBTimeOut        => $GPIBTimeOut,
			-GPIBEOT            => $GPIBEOT,
			-GPIBEOS            => $GPIBEOS,
			-COMConfName        => $ConfFile,
			-COMBaudRate        => $BaudRate,
			-COMParity          => $Parity,
			-COMDataBits        => $DataBits,
			-COMStopBits        => $StopBits,
			-COMHandShake       => $HandShake,
			-COMWriteBufferSize => $WriteBufferSize,
			-COMReadBufferSize  => $ReadBufferSize,
			-COMReadInterval    => $ReadInterval,
			-COMReadCharTime    => $ReadCharTime,
			-COMErrorMessage    => $ErrorMessage,
			-COMUserMessage     => $UserMessage,
			-PrintError         => 1,
			);
	if(!defined $dev) {
		$App->print("Device can not be opened. [$DeviceName: $Port]\n");
		exit;
	}

	my $Id = $dev->GetId();
	$App->print("$DeviceName: [$dev->{ModuleName}:$dev->{Port}] (Object: $dev)\n");
	$App->print("Id: $Id\n");

	return $dev;
}



