Console output -------------- cmd; AT @2 x x AT_@2 Cmd; AT @2 Length; 15 Response; 0123456789AB__>. cmd; 01 00 x x PIDs01_20 Cmd; 01 00 Length; 21 Response; 41 00 BE 1F B8 11 __>. cmd; 01 20 x x PIDs21_40 Cmd; 01 20 Length; 21 Response; 41 20 80 06 00 11 __>. cmd; 01 40 x x PIDs41_60 Cmd; 01 40 Length; 21 Response; 41 40 78 D0 00 00 __>. TPA $cmd; 0101 cmd; 0101 xWin32::API::Call: parameter 2 had a buffer overflow at C:/Strawberry/perl/vendor/lib/Win32API/CommPort.pm line 217. #### Serial.pm ---------- #!usr/bin/perl use strict; use warnings; use Win32::SerialPort; my $port; package Serial; require Exporter; Exporter->import('import'); #Now declare what we permit to be visible within the module. our @EXPORT = qw( &Serial_Init &Serial_TrxRcv &Serial_Close); use Time::HiRes qw(usleep); # ############ subroutine Serial_Init ################################################### sub Serial_Init { my $port_name = 'COM5'; #my $config_file = 'setup.cfg'; # use this to configure from the file $port = new Win32::SerialPort( $port_name ) || die "Unable to open: $^E\n"; # $^E EXTENDED_OS_ERROR #$port = new Win32::SerialPort($port_name, $config_file) || die "Unable to open: $^E\n"; $port->handshake('none'); # none dtr rts xoff $port->baudrate(38400); # 19200 57600 1200 9600 115200 4800 600 2400 300 38400 $port->parity('none'); # space none odd even mark #$port->parity_enable(1); # for any parity except "none" $port->databits(8); # options 7 8 $port->stopbits(1); # options 2 1 $port->buffers(256, 256); $port->read_interval(0); #RI $port->read_const_time(20); #RC $port->write_char_time(1); #WM $port->write_const_time(100); #WC print "Write settings; "; $port->write_settings || undef $port; # A report out to the console my $baud = $port->baudrate; my $parity = $port->parity; my $data = $port->databits; my $stop = $port->stopbits; my $hshake = $port->handshake; print "B = $baud, D = $data, S = $stop, P = $parity, H = $hshake\n\n"; # use the below to save the current configuration # if ( $port ) { $port->save('setup.cfg') ; print "Serial Port OK \n" }; } # ############## subroutine Serial_TrxRcv ############### sub Serial_TrxRcv { my ($cmd) = @_; my $response = ""; print "cmd; $cmd x"; $port->write($cmd."\r"); # IT CRASHES HERE !!!!!!! print " x\n"; my $loop = 1; while( $loop ) { usleep(200000); # 0.2 of a second my $partial_resp; $partial_resp = $port->input; # chomp $partial_resp; # only removes linefeeds $response = $response.$partial_resp; # print $response; #my $responseHex = unpack ('H*', $response); #print $responseHex."\n"; my $last = substr ( $response, -1 ); # get the last character if ($last eq ">") { $loop = 0; # $response = substr( $response, 0, -1); # -1 removes the ">"; #Leave the ">" character # chomp $response; # only removes linefeeds next; } print "."; } return $response; } # ############## subroutine Serial_Close ################# sub Serial_Close { $port->close(); } # ############## Execute when module exits ############## END { undef $port; # free it before global destruction runs } 1;