#============================================================ # RS232CComm #============================================================ package RS232CComm; use JFile; @ISA = qw(JFile); #公開したいサブルーチン #@EXPORT = qw(); use strict; use Win32API::CommPort; use Win32API::CommPort qw(:RAW :PARAM :STAT 0.19 ); 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} = 'rts'; $this->{HandShake} = 'rts' if($HandShake eq ''); # $this->{HandShake} = 'none' if($HandShake eq ''); #読込み&書込みバッファ $this->{WriteBufferSize} = 1024 * 4 if($WriteBufferSize eq ''); $this->{ReadBufferSize} = 1024 * 4 if($ReadBufferSize eq ''); #読込み時間間隔の設定 $this->{ReadInterval} = 50 if($ReadInterval eq ''); # $this->{ReadInterval} = 800 if($ReadInterval eq ''); $this->{ReadCharTime} = 5 if($ReadCharTime eq ''); # $this->{ReadCharTime} = 400 if($ReadCharTime eq ''); $this->{ErrorMessage} = 1 if($ErrorMessage eq ''); $this->{UserMessage} = 1 if($UserMessage eq ''); my $quiet = 0; my $PortObj = new Win32API::CommPort($this->{COMPort}, $quiet) || die "Can't open $this->{COMPort}: $^E\n"; #print "CommPort($this->{COMPort}, $quiet)\n"; $PortObj->is_handshake($this->{HandShake}); # set parameter $PortObj->is_baudrate($this->{BaudRate}); $PortObj->is_parity($this->{Parity}); $PortObj->is_databits($this->{DataBits}); $PortObj->is_stopbits($this->{StopBits}); #print "PortObj->is_handshake( $this->{HandShake});\n"; # set parameter #print "PortObj->is_baudrate( $this->{BaudRate});\n"; #print "PortObj->is_parity($this->{Parity});\n"; #print "PortObj->is_databits($this->{DataBits});\n"; #print "PortObj->is_stopbits($this->{StopBits});\n"; $PortObj->debug_comm(0); #$PortObj->is_xon_limit(100); # bytes left in buffer #$PortObj->is_xoff_limit(100); # space left in buffer #$PortObj->is_xon_char(0x11); #$PortObj->is_xoff_char(0x13); #$PortObj->is_eof_char(0x0); #$PortObj->is_event_char(0x0); #$PortObj->is_error_char(0); # for parity errors $PortObj->is_buffers($this->{ReadBufferSize}, $this->{WriteBufferSize}); # read, write $PortObj->is_read_interval($this->{ReadInterval}); # max time between read char (millisec) $PortObj->is_read_char_time($this->{ReadCharTime}); # avg time between read char $PortObj->is_read_const_time(5); # total = (avg * bytes) + const $PortObj->is_write_char_time(100); $PortObj->is_write_const_time(1000); #$PortObj->is_binary("T"); # just say Yes (Win 3.x option) #$PortObj->is_parity_enable("F"); # faults during input my @required = qw( BAUD DATA STOP ); my $faults = $PortObj->initialize(@required); if ($faults) { die "Required parameters not set before initialize\n"; } #my $rbuf = $PortObj->is_read_buf; #print "rbuf: $rbuf\n"; #my $milliseconds = $PortObj->get_tick_count; #print "TickCount: $milliseconds\n"; #my $WL = 250; #my $output_string = "GOWAVE $WL\n"; #print "OUT: $output_string"; #$PortObj->write_bg($output_string); # background write #my ($done, $count_out) = $PortObj->write_done(0); #print "done: $done count_out: $count_out s: $output_string\n"; $this->{PortObj} = $PortObj; 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); return 1; } sub Close { my ($this) = @_; if($this->{PortObj}) { $this->{PortObj}->close(); delete $this->{PortObj}; $this->Initialize(); return ''; } $this->Initialize(); return ''; } sub eof { my ($this) = @_; return 0; } sub truncate { my ($this,$c) = @_; # $this->read(100); } sub ReadLine { return shift->read(); } sub read { my ($this, $maxread) = @_; $maxread = 1 if(!defined $maxread); my $s = ''; for(my $i = 0 ; $i < $maxread ; $i++) { $this->{PortObj}->read_bg($this->{ReadBufferSize}); my ($done, $count_in, $string_in) = $this->{PortObj}->read_done(1); #print "done: $done count_in: $count_in s: $string_in\n"; last if($count_in == 0); $s .= $string_in; } return $s; } sub print { my ($this, @args) = @_; #print "PortObj: $this->{PortObj}\n"; # $this->{PortObj}->write_bg($args[0]); # background write my ($done, $count_out) = $this->{PortObj}->write_done(0); #print "done: $done count_out: $count_out s: @args\n"; return $count_out; } sub printf { my ($this, @args) = @_; my $s = sprintf(@args); my $count_out = $this->{PortObj}->write_bg($s); # background write #print "count_out: $count_out\n"; my ($done, $count_out) = $this->{PortObj}->write_done(0); #print "done: $done count_out: $count_out s: $output_string\n"; return $count_out; } 1;