Monitor a Serial Port (Win2k)

K

Kean

I have a Barcode-Scanner wich is connected to a com-port. When i start
the Hyperterminal and set the comport, i see the data that sends the
Scanner when i scan a label. This works fine.
Yet i will use the Scanner with this PerlScript:

use Win32::API;
use Win32::SerialPort;

my $Port = 'com7';
$Configuration_File_Name = 'config.sys';

$handle = new Win32::SerialPort ($Port, $quiet)
|| die "Can't open $PortName: $^E\n";

$handle->baudrate(9600);
$handle->parity('none');
$handle->databits(8);
$handle->stopbits(1);
$handle->handshake('rts');
$handle->write_settings || die "Can't initialize Handle\n";

$handle->save($Configuration_File_Name)
|| warn "Can't save $Configuration_File_Name: $^E\n";

$handle->restart($Configuration_File_Name)
|| warn "Can't reread $Configuration_File_Name: $^E\n";


open (LOG,">barcode.log") || die "Can't open 'barcode.log': $!\n";

while (1) {

my $reply = HardwareReply($handle,100);
print LOG "> Rx: $reply\n";

}
close LOG;

sub HardwareReply {
my ($Port,$timeout) = @_;

$Port->purge_all;

my $in;
my $max = 500;
my $count = 0;
my $return;
my $to_read;
my $readcount;
my $input;
my $counter=0;

$timeout ||= '100';

do {
select(undef,undef,undef, 0.1);
$to_read = $max - $count;

($readcount, $input) = $Port->read($to_read);
$in.=$input;
$count+=$readcount;

if ( $in=~/[\n\r]$/ ) {
$return=$in;
}

$counter++;
} while ((!$return) && ($counter<$timeout));

$return=~s/[\r\n]//g;
return $return;
}

The Script opens the comport, but the sub HardwareReply doesnt work.
Have anyone an idea what the problem is?
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top