Monitor a Serial Port (Win2k)

Discussion in 'Perl Misc' started by Kean, Dec 1, 2004.

  1. Kean

    Kean Guest

    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
    Kean, Dec 1, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rasha
    Replies:
    0
    Views:
    1,685
    Rasha
    Mar 17, 2005
  2. Pom
    Replies:
    2
    Views:
    1,630
    Bas-i
    Jan 31, 2007
  3. ScottZ
    Replies:
    2
    Views:
    349
    Jorgen Grahn
    Nov 2, 2008
  4. Max Kotasek
    Replies:
    4
    Views:
    969
    Max Kotasek
    Apr 9, 2010
  5. msalerno
    Replies:
    3
    Views:
    405
    Ilmari Karonen
    Jul 14, 2005
Loading...

Share This Page