#!/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 = "CPM1.csv";

#測定前のWait
my $WaitBefore = 1.0;
my $WaitForMeasurement = 1.0;

#設定電圧
my $V = 3.0;

#測定時間
#my $IAperture = 0.1; # 166.67e-6 to 200e-3

#平均を取る回数
my $nAverage = 10;

#測定波長の範囲
my $WStart = 250;
my $WEnd   = 800;
my $WStep  = 1.0;

#ゼロチェック
my $ZeroCheck = 'on';


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;
my $Laserstar3APLS;
my $Cornerstone130 = OpenDevice("Cornerstone130", "COM1");

$Cornerstone130->Shutter("open");


#while(1) {
#	last if($Cornerstone130->eof());
#print "1\n";
#	last if(!$Cornerstone130->read());
#}

$Cornerstone130->GoWave(300);
my $a = $Cornerstone130->read();
print "a=$a\n";
my $a = $Cornerstone130->read();
print "a=$a\n";
#my $a = $Cornerstone130->read();
#print "a=$a\n";
#my $a = $Cornerstone130->read();
#print "a=$a\n";



my $Wave = $Cornerstone130->GetWave();
print "Wave: $Wave\n";
exit;



#===============================================
# 測定開始
#===============================================
my $nWStep = int( ($WEnd - $WStart) / $WStep + 1.000001 );

Utils::sleep($WaitBefore);

$K6517A->SetSourceVoltage($V);
$K6517A->SetVSourceOutput("on");
$Cornerstone130->GoWave($WStart);

#回折格子の波長が設定されたら、人間が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("W,I,L,**********Measured by $Who, $Today,\n");
for(my $i = 0 ; $i < $nWStep ; $i++) {
	my $W = $WStart + $WStep * $i;

	Utils::sleep($WaitForMeasurement);
	print("Waiting $WaitForMeasurement s...\n");

#	my $W = $Cornerstone130->Measure();
	my $I = $K6517A->MeasureAveraged($nAverage);
	my $L = $Laserstar3APLS->Measure();
	print("W=$W  I=$I  L=$L\n");
	$out->print("$W,$I,$L\n");
}

$out->Close();

$Cornerstone130->Shutter("close");


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;
}



