#============================================================ # RS232CComm #============================================================ package RS232CComm; use JFile; @ISA = qw(JFile); #公開したいサブルーチン #@EXPORT = qw(); use strict; use Win32::SerialPort; use Symbol qw(gensym); use Utils; use Deps; 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->{COMPort} = 'COM1' if($COMPort eq ''); $this->{BaudRate} = 9600 if($BaudRate eq ''); $this->{Parity} = 'none' if(!defined $Parity); $this->{DataBits} = 8 if(!defined $DataBits); $this->{StopBits} = 1 if(!defined $StopBits); #フロー制御方法の選択;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} = 400 if($ReadInterval eq ''); $this->{ReadCharTime} = 100 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}) if($this->{ConfFile} ne ''); # 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->{COMPort}) { $this->Initialize(); return ''; } close($this->{'HANDLE'}) if($this->{'HANDLE'}); $this->Initialize(); return ''; } sub eof { my ($this) = @_; return eof($this->{'HANDLE'}); } sub truncate { my ($this,$c) = @_; $c = 0 if(!defined $c); return truncate($this->{'HANDLE'},$c); } sub ReadLine { return shift->read(); } sub read { my ($this) = @_; my $fh = $this->{'HANDLE'}; return undef unless($fh); #print "fh: $fh\n"; # my $line; # sysread($fh, $line, 1024); # return $line; my $line = <$fh>; return undef unless(defined $line); return $line; $line = ''; while(1) { last if(eof($fh)); my $c = getc($fh); last if($c == 0); print "c: $c\n"; $line = $line . getc($fh); last if(eof($fh)); } 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;