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

I was wondering if any one has had problems reading from a constant stream over the serial port using Win32::SerialPort?   I am using the following read statement after opening a com port:
(undef, $string_in) = $ob->read(65536);
This code works for finite strings, but when the data keeps coming, I think the internal buffer gets over run and it hangs. Any help would be greatly appreciated.

thanks,

kris

janitored by ybiC: Balanced <code> tags, linkifyed "Win32::SerialPort"

Replies are listed 'Best First'.
Re: win32::serialport buffer overflow
by Roger (Parson) on Nov 14, 2003 at 03:48 UTC
    You could use a tied filehandle to read the continuous incoming stream. The documentation of Win32::Serialport on CPAN has a section to cover a tied filehandle.

    Another observation is that your code does -
    (undef, $string_in) = $ob->read(65536);
    where you discarded the input size with undef. What you should do is -
    my $input_string; { my ($size, $str) = $ob->read(4096); # 4k at a time $input_string .= $str; redo if ($size == 4096); # read more? }
    So the idea is to keep reading the input, and append to the input string, until there is no more input.

      Doing many smaller reads instead of one large one keeps the internal buffer from overflowing and makes everything work beautifully. Thanks a lot for the help.
Re: win32::serialport buffer overflow
by rupesh (Hermit) on Nov 14, 2003 at 03:40 UTC
    Im not sure if this will help, but I believe one can also alter the internal buffer options using in-built attributes of the Win32::seialport module.
    However, it would be much better if you could post your code to be more specific.

    If you want to see data as and when they come and see it NOW, then have a look at Perl Idioms Explained - $|++
    SRK.