#============================================================ # Device #============================================================ package Device; use Common2; @ISA = qw(Common2); #公開したいサブルーチン @EXPORT = qw(); use strict; use Deps; use MyApplication; use Device::GPIBComm; use Device::RS232CComm; use Device::DummyComm; use Device::Cornerstone130; use Device::ArcLamp; use Device::Laserstar3APLS; #============================================================ # 一般関数 #============================================================ sub OpenDevice_old { my ($App, $DeviceName, $Port) = @_; Utils::DelSpace($DeviceName); Utils::DelSpace($Port); my $dev = new Device( -Port => $Port, -ModuleName => $DeviceName, -DeviceName => $DeviceName, ); if(!defined $dev) { $App->print("Device can not be opened. [$DeviceName: $Port]\n"); exit; } return $dev; } sub OpenDevice { my ($App, $DeviceName, $Port) = @_; #print "App=($App, $DeviceName, $Port)\n"; if(!defined $Port or $Port eq '') { ($DeviceName, $Port) = @_; $App = new MyApplication(); } Utils::DelSpace($DeviceName); Utils::DelSpace($Port); #print "App=($App, $DeviceName, $Port)\n"; # 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"); return undef; } my $Id = $dev->GetId(); $App->print("$DeviceName: [$dev->{ModuleName}:$dev->{Port}] (Object: $dev)\n"); $App->print("Id: $Id\n"); return $dev; } #============================================================ # コンストラクタ、デストラクタ #============================================================ sub new { my ($module, @args) = @_; my $this = {}; bless $this; $this->ReadKeyedArgs(@args); if(!defined $this->{PrintError}) { $this->{PrintError} = 1; } $this->{ModuleName} = $this->{DeviceName} if(!defined $this->{ModuleName}); $this->{DeviceName} = $this->{ModuleName} if(!defined $this->{DeviceName}); my $comm; if($this->{Port} =~ /GPIB(\d+)/i) { my $iGPIB = $1; $this->{GPIBInterface} = 'ni' if(!defined $this->{GPIBInterface}); $this->{GPIBTimeOut} = GPIB->T1s if(!defined $this->{GPIBTimeOut}); $this->{GPIBEOT} = 1 if(!defined $this->{GPIBEOT}); $this->{GPIBEOS} = 1 if(!defined $this->{GPIBEOS}); #print "GPIBDevice :$this->{GPIBInterface} $this->{DeviceName}:$iGPIB\n"; $comm = new GPIBComm($this->{GPIBInterface}, "$this->{DeviceName}:$iGPIB", $this->{GPIBTimeOut}, $this->{GPIBEOT}, $this->{GPIBEOS}); if(!defined $comm) { $this->{Error} = "GPIB can not be opened. " ."[$this->{DeviceName}: $this->{GPIBInterface}: $this->{Port}]"; print("Error in Device::new: $this->{Error}\n") if($this->{PrintError}); return undef; } if(!defined $comm->g()) { $this->{Error} = "GPIB Object could not be created. " ."[$this->{DeviceName}: $this->{GPIBInterface}: $this->{Port}]"; print("Error in Device::new: $this->{Error}\n") if($this->{PrintError}); return undef; } } elsif($this->{Port} =~ /(COM\d+)/i) { $comm = new RS232CComm(); my $ret = $comm->Open($this->{COMConfName}, $this->{Port}, $this->{COMBaudRate}, $this->{COMParity}, $this->{COMDataBits}, $this->{COMStopBits}, $this->{COMHandShake}); if(!$ret) { $this->{Error} = "COMPort can not be opened. " ."[$this->{DeviceName}: $this->{Port}]"; print("Error in Device::new: $this->{Error}\n") if($this->{PrintError}); return undef; } } elsif($this->{Port} =~ /Dummy/i) { $comm = new DummyComm(); } else { $this->{Error} = "Invalid Port [$this->{Port}]"; print("Error in Device::new: $this->{Error}\n") if($this->{PrintError}); return undef; } #print "comm: $comm\n"; #print("Load $this->{ModuleName}\n"); my $dev; no strict; if(defined $this->{pDeviceObject}) { delete $this->{pDeviceObject}; } if(defined $this->{PresentModule}) { eval "no Device::$this->{PresentModule};"; } delete $this->{PresentModule}; eval "use Device::$this->{ModuleName};"; if (!defined(&{"$this->{ModuleName}::new"})) { print("Can not find the module [$this->{ModuleName}].\n"); return undef; } $dev = &{"$this->{ModuleName}::new"}(@args); $this->{pDeviceObject} = $dev; $this->{PresentModule} = $this->{ModuleName}; use strict; #print "dev: $dev\n"; $dev->SetCommObject($comm); Common2::ReadKeyedArgs($dev, @args); return $dev; } sub DESTROY { my ($this) = @_; #print "Device::DESTROY: Finalizing processed.\n"; $this->Finish(); } sub Finish { my ($this) = @_; } sub SetCommObject { my ($this, $comm) = @_; return $this->{CommObject} = $comm; } sub CommObject { return shift->{CommObject}; } sub GetId { return "Undefined"; } sub read { my ($this, @args) = @_; #print "Device::read [$this->{CommObject}]\n"; return $this->{CommObject}->read(@args); } sub print { my ($this, @args) = @_; if(!$this->{CommObject}) { print "Device::print: Error: CommObject for [$this->{Port}] is not opened.\n"; return undef; } return $this->{CommObject}->print(@args); } sub printf { my ($this, @args) = @_; return $this->{CommObject}->printf(@args); } 1;