Archimedeus has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks!

I am desparately trying to connect to a serial port. However whatever I do, it simply walks through my <FH> or $PortObj->input statements. Currently I am testing between 2 PCs using perl on one end, Teraterm on the other

Just for completeness: This is perl 5, version 16, subversion 1 (v5.16.1) built for MSWin32-x64-multi-thread (strawberry perl) OS is WinXP 32bit SP3

This is my script:

use Win32::SerialPort qw( :STAT 0.19 ); use Convert::ASCIInames; use String::HexConvert ':all'; #use strict; $PortName = "COM1"; $Configuration_File_Name = "Serial_Settings.cfg"; $PortObj = new Win32::SerialPort ($PortName, $quiet) || die "Can't open $PortName: $^E\n"; # $quiet is optional #$PortObj->user_msg(1); $PortObj->databits(8); $PortObj->baudrate(115200); $PortObj->parity("none"); $PortObj->stopbits(1); $PortObj->handshake("none"); $PortObj->buffers(4096, 4096); $PortObj->are_match("\n"); # possible end strings $PortObj->read_interval(100); # max time between read char (millise +conds) $PortObj->read_char_time(5); # avg time between read char $PortObj->read_const_time(100); # total = (avg * bytes) + const $PortObj->write_char_time(5); $PortObj->write_const_time(100); $PortObj->write_settings || undef $PortObj; $PortObj->save($Configuration_File_Name); $PortObj->close || die "close failed"; undef $PortObj; #--------------------------------------------------------- $PortObj = tie (*FH, 'Win32::SerialPort', $Configuration_File_Name) || die "Can't tie: $^E\n"; $PortObj->are_match("\n","\r"); # possible end strings $PortObj->write_settings || undef $PortObj; $PortObj->lookclear; # empty buffers sleep 5; print FH "Enter string:\n"; # works well my $out = <FH>; # shouldn't this be blocking? print "$out\n"; print FH "Enter another string:\n"; # works well my $out = <FH>; # shouldn't this be blocking? print "$out\n"; close FH || warn "close failed"; ## CLOSE ## undef $PortObj; untie *FH; ## DESTROY ##

Whats the problem? I sometimes receive the right chars from serial port, but seldom all. The expected behavior is that the script waits at <FH> for an input with enter. Instead it just goes through. If I put the <FH> ito a loop then I can randomly catch my input.

I am using the native RS232, but I saw similar behavior with Win7 and USB a serial adapter. Any hints? Please save my strong believe into Perl :-)

Cheers and thanks, Archie

Replies are listed 'Best First'.
Re: Win32::SerialPort is not blocking
by BrowserUk (Patriarch) on Mar 19, 2014 at 18:22 UTC

    You've disabled handshaking, and applied timeouts to your reads:

    $PortObj->handshake("none"); ... $PortObj->read_interval(100); # max time between read char (millise +conds) $PortObj->read_char_time(5); # avg time between read char $PortObj->read_const_time(100); # total = (avg * bytes) + const

    It is doing what you've asked it to do.

    Try adding handshaking; or extending your timeout; or add your own line buffering loop.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.