package RS232C; use lib 'd:/Programs/Perl/lib'; use JFile; @ISA = qw(JFile); #公開したいサブルーチン #@EXPORT = qw(DelSpace Reduce01 MakePath RegExpQuote); use strict; use Jcode; use File::Path; use File::Basename; use File::Find; use Win32::SerialPort; use Symbol qw(gensym); use Utils; use Deps; #=============================================== # デバッグ関係変数 #=============================================== my $Debug = 0; #=============================================== # 文字コード関係変数 #=============================================== # sjis, euc, jis, noconv, utf8 my $FileSystemCharCode = Deps::FileSystemCharCode(); my $PerlCharCode = Deps::PerlCharCode(); my $MySQLCharCode = Deps::MySQLCharCode(); my $WebCharCode = Deps::WebCharCode(); my $WebCharSet = Deps::WebCharSet(); #=============================================== # スクリプト大域変数 #=============================================== my $LF = Deps::LF(); my $DirectorySeparator = Deps::DirectorySeparator(); my $OS = Deps::OS(); sub new { my ($module, $path, $mode) = @_; my $this = {}; bless $this; if(defined $path and defined $mode) { return undef unless($this->Open($path, $mode)); } return $this; } sub DESTROY { my $this = shift; $this->Close(); } sub Initialize { my ($this) = @_; $this->{HANDLE} = ''; $this->{'inHANDLE'} = ''; $this->{'outHANDLE'} = ''; $this->{'FileName'} = ''; $this->{'Mode'} = ''; $this->{'CharCode'} = ''; } sub Configure { my ($this, $ConfFile, $COMPort, $BaudRate, $Parity, $DataBits, $StopBits, $HandShake, $WriteBufferSize, $ReadBufferSize, $ReadInterval, $ReadCharTime, $ErrorMessage, $UserMessage) = @_; $this->{ConfFile} = $ConfFile; $this->{COMPort} = $COMPort; $this->{BaudRate} = $BaudRate; $this->{Parity} = $Parity; $this->{DataBits} = $DataBits; $this->{StopBits} = $StopBits; $this->{HandShake} = $HandShake; $this->{WriteBufferSize} = $WriteBufferSize; $this->{ReadBufferSize} = $ReadBufferSize; $this->{ReadInterval} = $ReadInterval; $this->{ReadCharTime} = $ReadCharTime; $this->{ErrorMessage} = $ErrorMessage; $this->{UserMessage} = $UserMessage; $this->{ConfFile} = 'SerialPort.conf' if($ConfFile eq ''); $this->{COMPort} = 'COM1' if($COMPort eq ''); $this->{BaudRate} = 9600 if($BaudRate eq ''); $this->{Parity} = 'none' if($Parity eq ''); $this->{DataBits} = 8 if($DataBits eq ''); $this->{StopBits} = 1 if($StopBits eq ''); #フロー制御方法の選択;rts(ハードウェア制御)、xoff(ソフトウェア制御)、 # dtr(rtsと同じことができるがピンが違う)がある。特に理由がない限りrts。 $this->{HandShake} = 'none' if($HandShake eq ''); #読込み&書込みバッファ $this->{WriteBufferSize} = 1024 * 20 if($WriteBufferSize eq ''); $this->{ReadBufferSize} = 1024 * 4 if($ReadBufferSize eq ''); #読込み時間間隔の設定 $this->{ReadInterval} = 800 if($ReadInterval eq ''); $this->{ReadCharTime} = 400 if($ReadCharTime eq ''); $this->{ErrorMessage} = 1 if($ErrorMessage eq ''); $this->{UserMessage} = 1 if($UserMessage eq ''); my $com1 = new Win32::SerialPort($this->{COMPort}) or return 0; $com1->baudrate($this->{BaudRate}); $com1->parity($this->{Parity}); $com1->databits($this->{DataBits}); $com1->stopbits($this->{StopBits}); $com1->handshake($this->{HandShake}); $com1->buffers($this->{WriteBufferSize}, $this->{ReadBufferSize}); $com1->read_interval($this->{ReadInterval}); $com1->read_char_time($this->{ReadCharTime}); $com1->error_msg($this->{ErrorMessage}); $com1->user_msg($this->{UserMessage}); $com1->write_settings(); $com1->save($this->{ConfFile}); undef $com1; return 1; } sub Open { my ($this, $ConfFile, $COMPort, $BaudRate, $Parity, $DataBits, $StopBits, $HandShake, $WriteBufferSize, $ReadBufferSize, $ReadInterval, $ReadCharTime, $ErrorMessage, $UserMessage, $IsPrintOut) = @_; $this->Initialize(); $this->Configure($ConfFile, $COMPort, $BaudRate, $Parity, $DataBits, $StopBits, $HandShake, $WriteBufferSize, $ReadBufferSize, $ReadInterval, $ReadCharTime, $ErrorMessage, $UserMessage); $this->{'outHANDLE'} = $this->{'inHANDLE'} = $this->{'HANDLE'} = gensym(); $COMPort = "+>$COMPort"; #print("HANDLE: $this->{'HANDLE'} F: $COMPort); my $ret = open($this->{HANDLE}, $COMPort); unless($ret) { print("Error in RS232C::Open: Can not open [$COMPort].\n") if($IsPrintOut); $this->Initialize(); return $ret; } $this->{COMPort} = $this->{FileName} = $COMPort; return $ret; } sub Close { my ($this) = @_; if($this->{'FileName'} and $this->{'FileName'} eq 'con') { $this->Initialize(); return ''; } close($this->{'HANDLE'}) if($this->{'HANDLE'}); $this->Initialize(); return ''; } sub ReadLine { my ($this) = @_; my $fh = $this->{'HANDLE'}; return undef unless($fh); my $line = <$fh>; return undef unless(defined $line); return $line; } sub print { my ($this, @args) = @_; my $fh = $this->{'HANDLE'}; print $fh @args; return @args; } sub printf { my ($this, @args) = @_; my $fh = $this->{'HANDLE'}; printf $fh @args; return @args; } 1;